-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord_ops.c
More file actions
35 lines (31 loc) · 1.37 KB
/
record_ops.c
File metadata and controls
35 lines (31 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "record_ops.h"
#include <stdlib.h>
cJSON *new_student_with_score(const char *id_num, const char *class_name,
const char *score_num) {
cJSON *new_student = cJSON_CreateObject();
cJSON *id = cJSON_CreateString(id_num);
cJSON_AddItemToObject(new_student, "id", id);
cJSON *score = cJSON_CreateObject();
int score_int = strtol(score_num, NULL, 10);
cJSON_AddItemToObject(score, class_name, cJSON_CreateNumber(score_int));
cJSON_AddItemToObject(new_student, "score", score);
return new_student;
}
bool record_add_student(cJSON *record, const char *id_num,
const char *class_name, const char *score_num) {
cJSON *student = new_student_with_score(id_num, class_name, score_num);
return cJSON_AddItemToArray(record, student);
}
bool student_add_score(cJSON *student, const char *class_name,
const char *score_num) {
cJSON *score = cJSON_GetObjectItem(student, "score");
int score_int = strtol(score_num, NULL, 10);
return cJSON_AddItemToObject(score, class_name,
cJSON_CreateNumber(score_int));
}
bool score_add_class(cJSON *score, const char *class_name,
const char *score_num) {
int score_num_int = strtol(score_num, NULL, 10);
cJSON *score_ = cJSON_CreateNumber(score_num_int);
return cJSON_AddItemToObject(score, class_name, score_);
}