This commit is contained in:
justuser-31 2025-05-09 14:14:59 +03:00
parent 99861f5cb0
commit 879828845c
5 changed files with 128 additions and 0 deletions

6
.gitmodules vendored
View File

@ -7,3 +7,9 @@
[submodule "STEP1_Basics/6_Files/c_cross_pack"]
path = STEP1_Basics/6_Files/c_cross_pack
url = https://gitea.del.pw/justuser-31/c_cross_pack.git
[submodule "STEP1_Basics/7_JSON/cJSON"]
path = STEP1_Basics/7_JSON/cJSON
url = https://github.com/DaveGamble/cJSON.git
[submodule "STEP1_Basics/7_JSON/c_cross_pack"]
path = STEP1_Basics/7_JSON/c_cross_pack
url = https://gitea.del.pw/justuser-31/c_cross_pack.git

@ -0,0 +1 @@
Subproject commit 8f2beb57ddad1f94bed899790b00f46df893ccac

@ -0,0 +1 @@
Subproject commit 957177e9ecd9547e89129ead6fb055262c222e00

View File

@ -0,0 +1,29 @@
[
{
"name": "Иван Иванов",
"age": 21,
"grades": {
"math": 85,
"physics": 90,
"history": 78
}
},
{
"name": "Мария Петрова",
"age": 22,
"grades": {
"math": 92,
"physics": 88,
"history": 80
}
},
{
"name": "Алексей Смирнов",
"age": 23,
"grades": {
"math": 75,
"physics": 84,
"history": 79
}
}
]

View File

@ -0,0 +1,91 @@
#include <stdio.h>
#include "c_cross_pack/libs/string.h"
#include "cJSON/cJSON.c"
int main() {
FILE* fp = fopen("data.json", "r");
char buffer[2048];
fread(buffer, 1, sizeof(buffer), fp);
fclose(fp);
// Parse the JSON data
cJSON *json = cJSON_Parse(buffer);
// Variables for working with parsing json
int count = cJSON_GetArraySize(json);
cJSON *name, *age, *student, *grades, *math, *physics, *history;
// What we are looking for
int average_best, aver_best_i;
int stud_80_plus[10]; int studs_80_i = 0;
average_best = -1;
// Find some data
int average;
for (int i = 0; i < count; i++) {
student = cJSON_GetArrayItem(json, i);
grades = cJSON_GetObjectItem(student, "grades");
math = cJSON_GetObjectItem(grades, "math");
physics = cJSON_GetObjectItem(grades, "physics");
history = cJSON_GetObjectItem(grades, "history");
average = (math->valueint + physics->valueint + history->valueint) / 3;
if (average > average_best) {
average_best = average;
aver_best_i = i;
}
if (math->valueint > 80) {
stud_80_plus[studs_80_i] = i;
studs_80_i++;
}
}
// Format average best
string aver_best_str = strInit("");
// Get data
student = cJSON_GetArrayItem(json, aver_best_i);
name = cJSON_GetObjectItem(student, "name");
age = cJSON_GetObjectItem(student, "age");
grades = cJSON_GetObjectItem(student, "grades");
math = cJSON_GetObjectItem(grades, "math");
physics = cJSON_GetObjectItem(grades, "physics");
history = cJSON_GetObjectItem(grades, "history");
// Add
strAdd(&aver_best_str, name->valuestring); strAdd(&aver_best_str, " ");
strAdd(&aver_best_str, int2Str(&age->valueint)); strAdd(&aver_best_str, " ");
strAdd(&aver_best_str, int2Str(&math->valueint)); strAdd(& aver_best_str, "-");
strAdd(&aver_best_str, int2Str(&physics->valueint)); strAdd(&aver_best_str, "-");
strAdd(&aver_best_str, int2Str(&history->valueint));
// Format math 80+
string stud_80_str = strInit("");
for (int i = 0; i < studs_80_i; i++) {
// Get data
student = cJSON_GetArrayItem(json, i);
name = cJSON_GetObjectItem(student, "name");
age = cJSON_GetObjectItem(student, "age");
grades = cJSON_GetObjectItem(student, "grades");
math = cJSON_GetObjectItem(grades, "math");
physics = cJSON_GetObjectItem(grades, "physics");
history = cJSON_GetObjectItem(grades, "history");
// Add
strAdd(&stud_80_str, name->valuestring); strAdd(&stud_80_str, " ");
strAdd(&stud_80_str, int2Str(&age->valueint)); strAdd(&stud_80_str, " ");
strAdd(&stud_80_str, int2Str(&math->valueint)); strAdd(& stud_80_str, "-");
strAdd(&stud_80_str, int2Str(&physics->valueint)); strAdd(&stud_80_str, "-");
strAdd(&stud_80_str, int2Str(&history->valueint)); strAdd(&stud_80_str, "\n");
}
// Get out data
printf("Student with best average score:\n"
"%s\n\n"
"Students with 80+ math scores:\n"
"%s",
aver_best_str, stud_80_str);
cJSON_Delete(json);
free(aver_best_str); free(stud_80_str);
return 0;
}