From 879828845c175b53e06394be84ebd9db1d0b96bb Mon Sep 17 00:00:00 2001 From: justuser-31 Date: Fri, 9 May 2025 14:14:59 +0300 Subject: [PATCH] up --- .gitmodules | 6 +++ STEP1_Basics/7_JSON/cJSON | 1 + STEP1_Basics/7_JSON/c_cross_pack | 1 + STEP1_Basics/7_JSON/data.json | 29 ++++++++++ STEP1_Basics/7_JSON/main.c | 91 ++++++++++++++++++++++++++++++++ 5 files changed, 128 insertions(+) create mode 160000 STEP1_Basics/7_JSON/cJSON create mode 160000 STEP1_Basics/7_JSON/c_cross_pack create mode 100644 STEP1_Basics/7_JSON/data.json create mode 100644 STEP1_Basics/7_JSON/main.c diff --git a/.gitmodules b/.gitmodules index 3314ec9..02144aa 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/STEP1_Basics/7_JSON/cJSON b/STEP1_Basics/7_JSON/cJSON new file mode 160000 index 0000000..8f2beb5 --- /dev/null +++ b/STEP1_Basics/7_JSON/cJSON @@ -0,0 +1 @@ +Subproject commit 8f2beb57ddad1f94bed899790b00f46df893ccac diff --git a/STEP1_Basics/7_JSON/c_cross_pack b/STEP1_Basics/7_JSON/c_cross_pack new file mode 160000 index 0000000..957177e --- /dev/null +++ b/STEP1_Basics/7_JSON/c_cross_pack @@ -0,0 +1 @@ +Subproject commit 957177e9ecd9547e89129ead6fb055262c222e00 diff --git a/STEP1_Basics/7_JSON/data.json b/STEP1_Basics/7_JSON/data.json new file mode 100644 index 0000000..0e7cbf9 --- /dev/null +++ b/STEP1_Basics/7_JSON/data.json @@ -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 + } + } +] diff --git a/STEP1_Basics/7_JSON/main.c b/STEP1_Basics/7_JSON/main.c new file mode 100644 index 0000000..27b8760 --- /dev/null +++ b/STEP1_Basics/7_JSON/main.c @@ -0,0 +1,91 @@ +#include + +#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; +}