Skip to content

Commit 688c529

Browse files
committed
All lab's code
1 parent 0b6be0b commit 688c529

20 files changed

+2380
-0
lines changed

lab1/studentManage.c

+350
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,350 @@
1+
/*
2+
Author: 17051713
3+
gcc studentManage.c -o test
4+
*/
5+
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <stdbool.h>
9+
#include <string.h>
10+
11+
typedef struct student
12+
{
13+
int sno;
14+
char *sname;
15+
int sage;
16+
int sclass;
17+
struct student* next;
18+
} stuInfo, *pStuInfo;
19+
20+
#define ADD 1
21+
#define DEL 2
22+
#define SEEA 3
23+
#define SEEO 4
24+
#define SORT 5
25+
#define GG 6
26+
27+
pStuInfo findStudent(pStuInfo tempStu, int num)
28+
{
29+
while(tempStu)
30+
{
31+
if(tempStu->sno == num)
32+
{
33+
return tempStu;
34+
}
35+
tempStu = tempStu->next;
36+
}
37+
return NULL;
38+
}
39+
40+
int cmp(const void* a, const void* b)
41+
{
42+
if( ((pStuInfo)a)->sclass != ((pStuInfo)b)->sclass )
43+
{
44+
return (((pStuInfo)a)->sclass - ((pStuInfo)b)->sclass);
45+
}
46+
if( ((pStuInfo)a)->sno != ((pStuInfo)b)->sno )
47+
{
48+
return (((pStuInfo)a)->sno - ((pStuInfo)b)->sno);
49+
}
50+
if( ((pStuInfo)a)->sage != ((pStuInfo)b)->sage )
51+
{
52+
return (((pStuInfo)a)->sage - ((pStuInfo)b)->sage);
53+
}
54+
if( ((pStuInfo)a)->sname[0] != ((pStuInfo)b)->sname[0] )
55+
{
56+
return (((pStuInfo)a)->sname[0] - ((pStuInfo)b)->sname[0]);
57+
}
58+
return 0;
59+
}
60+
61+
int getStudentNum(pStuInfo tempStu)
62+
{
63+
int num = 0;
64+
while(num++, tempStu) tempStu = tempStu->next;
65+
return (num - 2);
66+
}
67+
68+
pStuInfo initStudent(char *name, int num, int age, int class)
69+
{
70+
pStuInfo temp = (pStuInfo)malloc(sizeof(stuInfo));
71+
if(!temp)
72+
{
73+
return false;
74+
}
75+
temp->sname = name;
76+
temp->sno = num;
77+
temp->sage = age;
78+
temp->sclass = class;
79+
temp->next = NULL;
80+
return temp;
81+
}
82+
83+
bool printAllStudent(pStuInfo tempStu)
84+
{
85+
if(!tempStu)
86+
{
87+
return false;
88+
}
89+
printf("Name\t\tClass\t\tAge\t\tNumber\n");
90+
while(tempStu->next)
91+
{
92+
printf("%s\t\t%d\t\t%d\t\t%d\n", \
93+
tempStu->next->sname, \
94+
tempStu->next->sclass, \
95+
tempStu->next->sage, \
96+
tempStu->next->sno);
97+
tempStu = tempStu->next;
98+
}
99+
return true;
100+
}
101+
102+
bool printOneStudent(pStuInfo stu)
103+
{
104+
if(!stu)
105+
{
106+
return false;
107+
}
108+
printf("Name\t\tClass\t\tAge\t\tNumber\n");
109+
printf("%s\t\t%d\t\t%d\t\t%d\n", \
110+
stu->sname, \
111+
stu->sclass, \
112+
stu->sage, \
113+
stu->sno);
114+
return true;
115+
}
116+
117+
bool addStudent(pStuInfo tempStu, pStuInfo stu)
118+
{
119+
if(!stu || !tempStu)
120+
{
121+
return false;
122+
}
123+
while(tempStu->next)
124+
{
125+
tempStu = tempStu->next;
126+
}
127+
tempStu->next = stu;
128+
return true;
129+
}
130+
131+
bool delStudent(pStuInfo tempStu, int num)
132+
{
133+
if(!tempStu)
134+
{
135+
return false;
136+
}
137+
while(tempStu)
138+
{
139+
if(tempStu->next && \
140+
tempStu->next->sno == num)
141+
{
142+
pStuInfo temp = tempStu->next->next;
143+
free(tempStu->next->sname);
144+
free(tempStu->next);
145+
tempStu->next = temp;
146+
return true;
147+
}
148+
printf("%d\n", num);
149+
tempStu = tempStu->next;
150+
}
151+
return false;
152+
}
153+
154+
bool changeStudent(pStuInfo tempStu, pStuInfo stu)
155+
{
156+
if(!tempStu || !stu)
157+
{
158+
return false;
159+
}
160+
while(tempStu)
161+
{
162+
if(tempStu->next && \
163+
tempStu->next->sno == stu->sno)
164+
{
165+
pStuInfo temp = tempStu->next;
166+
tempStu->next = stu;
167+
stu->next = temp->next;
168+
free(temp);
169+
}
170+
tempStu = tempStu->next;
171+
}
172+
}
173+
174+
bool sortStudent(pStuInfo tempStu)
175+
{
176+
if(!tempStu)
177+
{
178+
return false;
179+
}
180+
int num = getStudentNum(tempStu);
181+
pStuInfo head = tempStu;
182+
pStuInfo temp = (pStuInfo)malloc(sizeof(stuInfo) * num);
183+
for(int i = 0; i < num; i++)
184+
{
185+
memcpy(temp + i, tempStu->next, sizeof(stuInfo));
186+
temp[i].next = tempStu->next;
187+
tempStu = tempStu->next;
188+
}
189+
qsort(temp, num, sizeof(stuInfo), cmp);
190+
for(int i = 0; i < num; i++)
191+
{
192+
head->next = (pStuInfo)(temp[i]).next;
193+
head = head->next;
194+
}
195+
head->next = NULL;
196+
free(temp);
197+
return true;
198+
}
199+
200+
void printMenu()
201+
{
202+
puts("It's a easy--easy student manage system");
203+
puts("Please tell me what's you want to do");
204+
puts("1. add student");
205+
puts("2. del student");
206+
puts("3. see see students");
207+
puts("4. see one student");
208+
puts("5. sort it!");
209+
puts("6. bye bye");
210+
printf(">");
211+
fflush(stdout);
212+
}
213+
214+
void print(char *str)
215+
{
216+
printf("%s", str);
217+
fflush(stdout);
218+
}
219+
220+
void getSnum(int *num)
221+
{
222+
print("Please input Sno >");
223+
scanf("%d", num);
224+
fflush(stdin);
225+
}
226+
227+
void deleteAll(pStuInfo tempStu)
228+
{
229+
while(tempStu)
230+
{
231+
pStuInfo temp = tempStu;
232+
if(temp->sname)
233+
{
234+
free(tempStu->sname);
235+
}
236+
tempStu = tempStu->next;
237+
free(temp);
238+
}
239+
}
240+
241+
void getAllMessage(int *num, int *age, char **name, int *class)
242+
{
243+
print("Please input Sno >");
244+
scanf("%d", num);
245+
print("Please input Sname >");
246+
fflush(stdin);
247+
*name = (char*)malloc(0x100);
248+
scanf("%255s", *name);
249+
fflush(stdin);
250+
print("Please input class num >");
251+
scanf("%d", class);
252+
print("Please input age >");
253+
scanf("%d", age);
254+
puts("OK, now all finish");
255+
}
256+
257+
void errHandle()
258+
{
259+
puts("Something Error!");
260+
exit(-1);
261+
}
262+
263+
int main()
264+
{
265+
pStuInfo headStu = initStudent(NULL, -1, -1, -1);
266+
while(1)
267+
{
268+
unsigned int choice = 0;
269+
int Sno = 0;
270+
int Sage = 0;
271+
char *Sname = NULL;
272+
int Sclass = 0;
273+
pStuInfo temp = NULL;
274+
printMenu();
275+
scanf("%d", &choice);
276+
while(choice > 6 || choice < 1)
277+
{
278+
puts("Bad guy, retry!");
279+
scanf("%d", &choice);
280+
}
281+
switch (choice)
282+
{
283+
case ADD:
284+
getAllMessage(&Sno, &Sage, &Sname, &Sclass);
285+
if (temp = initStudent(Sname, Sno, Sage, Sclass))
286+
{
287+
if(!addStudent(headStu, temp))
288+
{
289+
errHandle();
290+
}
291+
puts("OK!");
292+
}
293+
else
294+
{
295+
errHandle();
296+
}
297+
break;
298+
case DEL:
299+
getSnum(&Sno);
300+
if (!delStudent(headStu, Sno))
301+
{
302+
puts("No such guy");
303+
}
304+
else
305+
{
306+
puts("OK!");
307+
}
308+
break;
309+
case SEEO:
310+
getSnum(&Sno);
311+
temp = findStudent(headStu, Sno);
312+
if (temp)
313+
{
314+
if (!printOneStudent(temp))
315+
{
316+
puts("Something error!");
317+
}
318+
else
319+
{
320+
puts("Ok!");
321+
}
322+
}
323+
else
324+
{
325+
puts("No such people!");
326+
}
327+
break;
328+
case SEEA:
329+
if (!printAllStudent(headStu))
330+
{
331+
puts("Something Error!");
332+
}
333+
break;
334+
case SORT:
335+
if (!sortStudent(headStu))
336+
{
337+
puts("Something Error!");
338+
}
339+
else
340+
{
341+
puts("Sort OK!");
342+
}
343+
break;
344+
case GG:
345+
puts("Bye!");
346+
deleteAll(headStu);
347+
return 0;
348+
}
349+
}
350+
}

0 commit comments

Comments
 (0)