/
GradeBook.c
219 lines (175 loc) · 3.79 KB
/
GradeBook.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
// Jared Adamson
// Program to hold and compute student grades
#include <stdio.h>
#include <string.h>
#include <stdlib.h> // used by malloc
#include <ctype.h>
#pragma warning(disable: 4996)
typedef enum{Final=0, Midterm, Homework, Quiz} Category;
struct Entry { // a node to hold personal details
Category type;
char name[1024];
float points;
float possible_points;
struct contact* next;
} *head = NULL;
void flush(); // forward declaration of functions
void branching(char c);
int remove_entry();
int insert_entry();
int average();
char* category_name(Category type);
void delete_list(struct Entry *temp);
int main() { // print a menu for selection
char ch = 'i';
ungetc('\n', stdin); // inject input buffer with a return character
do {
printf("Enter your selection\n");
printf("\ta: insert a new entry\n");
printf("\td: delete an entry\n");
printf("\tp: compute average\n");
printf("\tq: quit \n");
flush(); // flush input buffer
ch = tolower(getchar()); // see tutorial
branching(ch);
} while (ch != 113);
delete_list(head);
return 0;
}
void flush() {
int c;
do {
c = getchar();
} while (c != '\n' && c != EOF);
}
void branching(char c) { // branch to different tasks
char sname[30];
switch (c) {
case 'a':
insert_entry();
break;
case 'd':
remove_entry();
break;
case 'p':
average();
break;
default:
printf("Invalid input\n");
}
}
int insert_entry()
{
struct Entry *new_entry = (struct Entry *) malloc(sizeof(struct Entry));
if (new_entry != NULL)
{
printf("Please enter name: ");
scanf("%s", new_entry->name);
printf("Please enter grade category: (Final=0, Midterm=1, Homework=2, Quiz=3)\n");
scanf("%d", &new_entry->type);
printf("Please enter possible points for this assignment: ");
scanf("%f", &new_entry->possible_points);
printf("Please enter points earned for this assignment: ");
scanf("%f", &new_entry->points);
new_entry->next = head;
head = new_entry;
printf("Entry was added successfully!\n");
return 0;
}
else
{
printf("Insufficient Memory\n");
return -1;
}
}
int remove_entry()
{
struct Entry *navi, *prev, *temp;
char item[1024];
printf("Please enter the name of the entry you wanted to delete: ");
scanf("%s", item);
if (head == NULL) {
printf("No records found.\n");
return 0;
}
else if (head != NULL && strcmp(head->name, item) == 0)
{
temp = head;
head = head->next;
free(head);
return 0;
}
else if (head != NULL)
{
navi = prev = head;
while (navi->next != NULL) {
if (strcmp(navi->name, item) == 0) {
temp = navi;
prev->next = navi->next;
free(temp);
return 0;
}
else {
prev = navi;
navi = navi->next;
}
}
if (strcmp(navi->name, item) == 0)
{
temp = prev;
prev->next = NULL;
free(navi);
}
}
return 0;
}
int average()
{
struct Entry *temp;
if (head == NULL)
{
printf("No records found.\n");
return -1;
}
else
{
float possible_points = 0.0;
float earned_points = 0.0;
temp = head;
while (temp != NULL)
{
printf("\n\nName: %s\n", temp->name);
printf("Category: %s\n", category_name(temp->type));
printf("Possible Points: %f\n", temp->possible_points);
printf("Points Earned: %f\n", temp->points);
possible_points += temp->possible_points;
earned_points += temp->points;
temp = temp->next;
}
if (possible_points != 0)
printf("\n\nAverage Grade: %0.2f%%\n\n", (earned_points / possible_points) * 100);
return 0;
}
}
void delete_list(struct Entry *temp)
{
if (temp != NULL) {
delete_list(temp->next);
free(temp);
}
}
char* category_name(Category type) {
switch (type)
{
case Midterm:
return "Midterm";
case Final:
return "Final Exam";
case Homework:
return "Homework";
case Quiz:
return "Quiz";
default:
return "Invalid Selection";
}
}