-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.c
More file actions
231 lines (205 loc) · 3.7 KB
/
db.c
File metadata and controls
231 lines (205 loc) · 3.7 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
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
#include <stdio.h>
#include <stdlib.h>
#include "db.h"
#include <string.h>
attend_rec db[100],ar;
int db_size = 0;
/* reset the database for testing */
int clear_db()
{
db_size = 0;
return TRUE;
}
/*
Story 1: As a teacher, I want a user interface for my database.
*/
int is_whitespace(char c) {
return (' ' == c || '\t' == c || '\n' == c);
}
char get_first_non_white(char *s) {
for (int i = 0; '\0' != s[i] && i < 80; i++) {
if (!is_whitespace(s[i])) {
return(s[i]);
}
}
return('\0');
}
char get_command()
{
char buffer[80];
printf("Enter operator> ");
scanf("%80s", buffer);
return get_first_non_white(buffer);
}
// Returns FALSE when 'x' enter; TRUE otherwise
int execute_command(char c)
{
int sno;
attend_rec ar;
switch (c) {
case 'a':
scanf_attend(&ar);
add(ar);
printf_attend(ar);
return TRUE;
case 'l':
list();
return TRUE;
case 'i':
initialize_db();
return TRUE;
case 'f':
sno = get_sno();
ar = find(sno);
if (ar.sno == -1) {
printf("Student %d not found\n", sno);
} else {
printf_attend(ar);
}
return TRUE;
case 'm':
scanf_attend(&ar);
modify(ar);
return TRUE;
case 'd':
sno = get_sno();
delete(sno);
return TRUE;
case 'x':
return FALSE;
default:
printf("Invalid command\n");
return TRUE;
}
}
/*
Story 2: As a teacher, I want to add a student to my database, so I
can track his or her record.
*/
void scanf_attend(attend_rec *r)
{
printf("Enter S.No> ");
scanf("%d",&r->sno);
printf("Enter Name >");
scanf("%s",r->name);
printf("Enter Attendance >");
scanf("%d",&r->present);
}
void printf_attend(attend_rec r)
{
char *y;
y=(r.present==1?"present":"absent");
printf("Sno\tName\tAttendance\t\n");
printf("%d\t",r.sno);
printf("%s\t %s\t",r.name,y);
printf("\n");
}
int add(attend_rec r)
{
db[db_size++]=r;
return TRUE;
}
/*
Story 3: As a teacher, I want to be see all of the students in my
class, so I can see who is present and absent.
*/
void list()
{
printf("Sno.\tName\tAttendance\n");
for(int o=0;o<db_size;o++)
{
printf("%d\t",db[o].sno);
printf("%s\t",db[o].name);
if(db[o].present==1)
printf("present\t\n");
else
printf("absent\t\n");
}
printf("\n");
}
/*
Story 4: As a programmer, I want to add a set of records to my
database for testing purposes.
*/
void initialize_db()
{
attend_rec ar;
int i;
db_size=0;
for(i=0;i<10;i++)
{
ar.sno = i;
strcpy(ar.name,"student");
ar.present=1;
add(ar);
}
}
/*
Story 5: As a teacher, I want to be find a student's record so I can
see whether a student was present or absent for a class.
*/
// We need a special record to return when the one we are looking for
// is not found
attend_rec not_found = {.sno = -1,
.name = "none",
.present = -1};
// Prompt the user for a serial number and read from keyboard
int get_sno()
{
int a;
printf("Enter an integer>\n");
scanf("%d",&a);
return a;
}
attend_rec find(int sno)
{
int i;
for(i=0;i<db_size;i++)
{
if(sno==db[i].sno)
return db[i];
}
return not_found;
}
/*
Story 6: As a teacher, I want to modify a student's record, so I can
correct mistakes.
*/
int modify(attend_rec r)
{
int i;
for(i=0;i<db_size;i++)
{
if(r.sno==db[i].sno)
{
strcpy(db[i].name,r.name);
db[i].present=r.present;
return TRUE;
}
}
return FALSE;
}
/*
Story 7: As a teacher, I want to delete a student's record, so I can
drop a student from the class.
*/
int find_index(int sno)
{
for(int i=0;i<db_size;i++)
{
if(sno==db[i].sno)
return i;
}
return -1;
}
int delete(int sno)
{
int i=0;
i=find_index(sno);
if(i<0)
return FALSE;
for(;i<db_size-1;i++)
db[i]=db[i+1];
db_size--;
return TRUE;
}