forked from akshatnavlani/C-Case-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.c
336 lines (272 loc) · 11.6 KB
/
functions.c
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <stdio.h>
#include <string.h>
#include "header.h"
#include<stdlib.h>
int get_student_details(struct Student *student) {
printf("Enter the SRN of the student:");
scanf("%13s",student->SRN);
printf("Enter student name: ");
scanf("%s", student->name);
for (int j = 0; j < MAX_SUBJECTS; j++) {
printf("Enter marks for Subject %d - ISA1: ", j + 1);
scanf("%d", &(student->s[j].exams.ISA1[j]));
if(student->s[j].exams.ISA1[j]<=20)
;
else{
printf("Please enter correct details.");
return 1;}
printf("Enter marks for Subject %d - ISA2: ", j + 1);
scanf("%d", &(student->s[j].exams.ISA2[j]));
if(student->s[j].exams.ISA2[j]<=20)
;
else{
printf("Please enter correct details.");
return 1;}
printf("Enter marks for Subject %d - ESA: ", j + 1);
scanf("%d", &(student->s[j].exams.ESA[j]));
if(student->s[j].exams.ESA[j]<=50)
;
else{
printf("Please enter correct details.");
return 1;}
printf("Enter internal marks for Subject %d: ", j + 1);
scanf("%d", &(student->s[j].exams.Assignment[j]));
if(student->s[j].exams.Assignment[j]<=10)
;
else{
printf("Please enter correct details.");
return 1;}
printf("Enter credits for Subject %d: ", j + 1);
scanf("%d", &(student->s[j].credits[j]));
if(student->s[j].credits[j]<=5)
;
else{
printf("Please enter correct details.");
return 1;}
}
}
float calculate_sgpa(struct Student *student) {
float total_credit_weighted_points = 0.0f;
float total_credits = 0.0f;
float total_points = 0.0f;
for (int j = 0; j < MAX_SUBJECTS; j++) {
int total_marks = student->s[j].exams.ISA1[j] + student->s[j].exams.ISA2[j] + student->s[j].exams.ESA[j] + student->s[j].exams.Assignment[j];
int grade_points = 0;
if (total_marks >= 91 && total_marks <= 100) {
grade_points = 10;
} else if (total_marks >= 81 && total_marks <= 90) {
grade_points = 9;
} else if (total_marks >= 71 && total_marks <= 80) {
grade_points = 8;
} else if (total_marks >= 61 && total_marks <= 70) {
grade_points = 7;
} else if (total_marks >= 51 && total_marks <= 60) {
grade_points = 6;
} else if (total_marks >= 41 && total_marks <= 50) {
grade_points = 5;
} else {
grade_points = 0;
}
total_points += grade_points * student->s[j].credits[j];
}
total_credit_weighted_points += total_points;
for (int j = 0; j < MAX_SUBJECTS; j++) {
total_credits += student->s[j].credits[j];
}
if (total_credits == 0.0f) {
return -1.0f; // Indicate error
}
student->sgpa=total_credit_weighted_points / (total_credits);
return student->sgpa;
}
void display_grade_card(struct Student *student) {
// writing in file:
FILE *file;
file=fopen("details.csv","a");
printf("\nGrade Card for Student: %s\t%s\n", student->name,student->SRN);
fprintf(file,"%s,%s,",
student->SRN,
student->name);
printf("Subject\tCredits\tISA1\tISA2\tESA\tInternal\tTotal\tGrade Points\tGrade\n");
printf("----------------------------------------------------------------------------------------------\n");
for (int j = 0; j < MAX_SUBJECTS; j++) {
int total_marks = student->s[j].exams.ISA1[j] + student->s[j].exams.Assignment[j] + student->s[j].exams.ISA2[j] + student->s[j].exams.ESA[j];
int grade_points = 0;
char grade;
if (total_marks >= 91 && total_marks <= 100) {
grade_points = 10;
grade='S';
} else if (total_marks >= 81 && total_marks <= 90) {
grade_points = 9;
grade='A';
} else if (total_marks >= 71 && total_marks <= 80) {
grade_points = 8;
grade='B';
} else if (total_marks >= 61 && total_marks <= 70) {
grade_points = 7;
grade='C';
} else if (total_marks >= 51 && total_marks <= 60) {
grade_points = 6;
grade='D';
} else if (total_marks >= 41 && total_marks <= 50) {
grade_points = 5;
grade='E';
} else {
grade_points = 0;
grade='F';
}
printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%c\n\n\n", j + 1, student->s[j].credits[j], student->s[j].exams.ISA1[j], student->s[j].exams.ISA2[j], student->s[j].exams.ESA[j], student->s[j].exams.Assignment[j], total_marks, grade_points,grade);
fprintf(file,
"%d,%d,%d,%d,%d,%d,%d,%d,%c,",
j + 1, student->s[j].credits[j], student->s[j].exams.ISA1[j], student->s[j].exams.ISA2[j], student->s[j].exams.ESA[j], student->s[j].exams.Assignment[j], total_marks, grade_points,grade);
}
fprintf(file,"%f,\n",student->sgpa);
fclose(file);
}
void print_data(void) {
int total_marks[MAX_SUBJECTS];
int grade_points[MAX_SUBJECTS];
char grade[MAX_SUBJECTS];
FILE *file = fopen("details.csv", "r");
if (file == NULL) {
printf("Could not open file.\n");
return;
}
struct Student student[100];
int records = 0;
int sub_no[MAX_SUBJECTS];
char line[1024];
while (fgets(line, sizeof(line), file)) {
// Read the SRN and name
char *token = strtok(line, ",");
if (token == NULL) continue;
strncpy(student[records].SRN, token, sizeof(student[records].SRN));
token = strtok(NULL, ",");
if (token == NULL) continue;
strncpy(student[records].name, token, sizeof(student[records].name));
// Read subjects data
for (int j = 0; j < MAX_SUBJECTS; j++) {
token = strtok(NULL, ",");
if (token == NULL) break;
sub_no[j] = atoi(token);
token = strtok(NULL, ",");
if (token == NULL) break;
student[records].s[j].credits[j] = atoi(token);
token = strtok(NULL, ",");
if (token == NULL) break;
student[records].s[j].exams.ISA1[j] = atoi(token);
token = strtok(NULL, ",");
if (token == NULL) break;
student[records].s[j].exams.ISA2[j] = atoi(token);
token = strtok(NULL, ",");
if (token == NULL) break;
student[records].s[j].exams.ESA[j] = atoi(token);
token = strtok(NULL, ",");
if (token == NULL) break;
student[records].s[j].exams.Assignment[j] = atoi(token);
token = strtok(NULL, ",");
if (token == NULL) break;
total_marks[j] = atoi(token);
token = strtok(NULL, ",");
if (token == NULL) break;
grade_points[j] = atoi(token);
token = strtok(NULL, ",");
if (token == NULL) break;
grade[j] = token[0];
}
token = strtok(NULL, ",");
if (token != NULL) {
student[records].sgpa = atof(token);
}
// Printing student data
printf("\nGrade Card for Student: %s\t%s\n", student[records].name, student[records].SRN);
printf("Subject\tCredits\tISA1\tISA2\tESA\tInternal\tTotal\tGrade Points\tGrade\n");
printf("----------------------------------------------------------------------------------------------\n");
for (int j = 0; j < MAX_SUBJECTS; j++) {
total_marks[j] = student[records].s[j].exams.ISA1[j] + student[records].s[j].exams.Assignment[j] + student[records].s[j].exams.ISA2[j] + student[records].s[j].exams.ESA[j];
printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%c\n",
sub_no[j],
student[records].s[j].credits[j],
student[records].s[j].exams.ISA1[j],
student[records].s[j].exams.ISA2[j],
student[records].s[j].exams.ESA[j],
student[records].s[j].exams.Assignment[j],
total_marks[j],
grade_points[j],
grade[j]);
}
printf("%.2f\n", student[records].sgpa);
records++;
}
fclose(file);
printf("%d record(s) read\n\n\n", records);
}
void print_student_result(const char *srn) {
FILE *file = fopen("details.csv", "r");
if (file == NULL) {
printf("Could not open file.\n");
return;
}
struct Student student[MAX_STUDENTS];
int records = 0;
char line[1024];
int total_marks[MAX_SUBJECTS];
int grade_points[MAX_SUBJECTS];
char grade[MAX_SUBJECTS];
int sub_no[MAX_SUBJECTS];
while (fgets(line, sizeof(line), file)) {
// Split the line into tokens using strtok
char *token = strtok(line, ",");
if (token != NULL && strcmp(token, srn) == 0) // Found the student with the matching SRN
{
strncpy(student[records].SRN, token, sizeof(student[records].SRN));
token = strtok(NULL, ",");
if (token != NULL) {
strncpy(student[records].name, token, sizeof(student[records].name));
}
// Read subjects data
for (int j = 0; j < MAX_SUBJECTS; j++) {
for (int k = 0; k < 9; k++) {
token = strtok(NULL, ",");
if (token == NULL) break;
if(k==0)sub_no[j] = atoi(token);
else if (k == 1) student[records].s[j].credits[j] = atoi(token);
else if (k == 2) student[records].s[j].exams.ISA1[j] = atoi(token);
else if (k == 3) student[records].s[j].exams.ISA2[j] = atoi(token);
else if (k == 4) student[records].s[j].exams.ESA[j] = atoi(token);
else if (k == 5) student[records].s[j].exams.Assignment[j] = atoi(token);
else if (k == 6) total_marks[j]= atoi(token); // total marks
else if (k == 7) grade_points[j]= atoi(token); // grade points
else if (k == 8) grade[j]= token[0]; // grade
}
}
token = strtok(NULL, ",");
if (token != NULL) {
student[records].sgpa = atof(token);
}
// Printing student data
printf("\nGrade Card for Student: %s\t%s\n", student[records].name, student[records].SRN);
printf("Subject\tCredits\tISA1\tISA2\tESA\tAssignment\tTotal\tGrade Points\tGrade\n");
printf("-----------------------------------------------------------------------------------\n");
for (int j = 0; j < MAX_SUBJECTS; j++) {
printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%c\n",
sub_no[j],
student[records].s[j].credits[j],
student[records].s[j].exams.ISA1[j],
student[records].s[j].exams.ISA2[j],
student[records].s[j].exams.ESA[j],
student[records].s[j].exams.Assignment[j],
total_marks[j],
grade_points[j],
grade[j]);
}
printf("\nSGPA: %.2f\n\n\n", student[records].sgpa);
records++;
break; // Exit the loop once the student is found
}
}
fclose(file);
if (records == 0) {
printf("Student with SRN %s not found.\n", srn);
}
}