-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentInfo.c
More file actions
340 lines (311 loc) · 8.47 KB
/
StudentInfo.c
File metadata and controls
340 lines (311 loc) · 8.47 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
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
337
338
339
#include "stdio.h"
#include "string.h"
#include "malloc.h"
#include "stdlib.h"
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OVERFlow -2
#define LENGTH_SNO 11
#define LENGTH_NAME 25
#define LENGTH_BIRTHDAY 8
#define LENGTH_TELEPHONE 11
/*
不合理之处:
1.查询时只能查到符合条件的第一条记录
2.不能进行多条件查询(只能进行但条件查询)
3.每次都将数据库全部导入到内存中 ,如果记录多,这样会造成内存空间不够用
源码应该优化之处:
1.查询函数中的代码有冗余
2.数据输出到控制端和外部文件用了不同的输入输出函数
*/
//-----------------------------------------------------------------结构体数据定义
typedef struct student{
char sno[LENGTH_SNO];
char name[LENGTH_NAME];
int sex;
char birthday[LENGTH_BIRTHDAY];
char telephone[LENGTH_TELEPHONE];
}studentInfo;
typedef struct studentLinkList{
studentInfo stu;
struct studentLinkList *next;
}studentLink;
//-----------------------------------------------------------------函数声明
//创建数据库
studentLink *newDataBase();
studentLink *newDataBaseFromFile(FILE **f); //通过导入文件建立数据库
int newDataBase2(studentLink **head);//这样也可以实现正确的头结点申请
//选择,添加,删除,清空
int addRecord(studentLink **head);
studentLink *selectRecord(studentLink **head);
int deleteRecord(studentLink **head,studentLink *selectedInfo);//以head为头结点的链表中删除p结点
int clear(studentLink **head);
//输出链表中的所有元素
int printList(studentLink **head);
//快捷对结构体进行赋值与输出
int inputStudentInfo(studentInfo *elem);
int printStudentInfo(studentInfo *elem);
//数据输出到文件
int outputToFile(studentLink **head,FILE **f);
//-----------------------------------------------------------------主函数
int main(){
char ch='^';
studentLink *dataBase=NULL;//数据链表头结点,可以当做数据库的名称
FILE *dataBaseFile=NULL;
while(ch!='$'){
char c=0;
printf ( "=============================\n" );
if(dataBase==NULL){
printf ( "a.新建数据档案\n" );
printf ( "z.从数据库文件提取信息\n" );
}else{
printf ( "b.添加数据\n" );
printf ( "c.删除数据\n" );
printf ( "d.数据查询\n" );
printf ( "e.清除数据库\n" );
printf ( "f.遍历数据库\n" );
printf ( "o.导出至数据库文件\n" );
printf ( "q.退出程序\n" );
}
printf ( "\n=============================\n" );
printf ("\n请输入选项:");
scanf("%c",&ch);
switch(ch){
case 'a':
dataBase=newDataBase();break;//显示输入示例
case 'z':
dataBase=newDataBaseFromFile(&dataBaseFile);break;
case 'b':
addRecord(&dataBase);break;
case 'c':
deleteRecord(&dataBase,selectRecord(&dataBase));break;
case 'd':
printStudentInfo(&(selectRecord(&dataBase))->stu);system("pause");break;
case 'e':
clear(&dataBase);break;
case 'f':
printList(&dataBase);system("pause");break;
case 'o':
outputToFile(&dataBase,&dataBaseFile);break;
case 'q':
clear(&dataBase);exit(0);break;
default :
printf("Invalid Input\n");break;
}
ch=getchar();
}
return 0;
}
//-----------------------------------------------------------------自定义函数的实现
studentLink *newDataBase(){
studentLink *p,*head;
head=(studentLink *)malloc(sizeof(studentLink));
p=head;
p->next=NULL;
printf("succeed created!\n");
return head;
}
int newDataBase2(studentLink **head){
studentLink *p;
*head=(studentLink *)malloc(sizeof(studentLink));
p=*head;
p->next=NULL;
printf("succeed created!\n");
return TRUE;
}
int addRecord(studentLink **head){
studentLink *newStudentInfo;
if((*head)!=NULL){
newStudentInfo=(studentLink *)malloc(sizeof(studentLink));
if(newStudentInfo!=NULL){
printf("input student infomation:\n");
inputStudentInfo(&(newStudentInfo->stu));
newStudentInfo->next=(*head)->next;
(*head)->next=newStudentInfo;
return TRUE;
}
else{
return OVERFlow;
}
}
else{
return FALSE;
}
}
int deleteRecord(studentLink **head,studentLink *selectedInfo){
studentLink *p,*q;
if((p=(*head))!=NULL && (q=selectedInfo)!=NULL)
while(p->next!=q) p=p->next;
p->next=q->next;
free(q);
selectedInfo=NULL;
}
//数据查询,并返回该记录的指针
studentLink *selectRecord(studentLink **head){
char sno_name[15]="sno";
char name_name[15]="name";
char sex_name[15]="sex";
char birthday_name[15]="birthday";
char telephone_name[15]="telephone";
char tempSelectName[15]="";
printf ( "=============================\n" );
printf ( "1.sno\n" );
printf ( "2.name\n" );
printf ( "3.sex\n" );
printf ( "4.birthday\n" );
printf ( "5.telephone\n" );
printf ( "\n=============================\n" );
printf ( "(example:sno 001)\n" );
scanf("%s",tempSelectName);
if(strcmp(tempSelectName,sno_name)==0){
studentLink *p=NULL;
char input_sno[LENGTH_SNO];
scanf("%s",input_sno);
if((p=*head)!=NULL)
while(p->next!=NULL){
p=p->next;
if(strcmp(p->stu.sno,input_sno)==0){
return p;
}
}
return NULL;
}
else if(strcmp(tempSelectName,name_name)==0){
studentLink *p=NULL;
char input_name[LENGTH_NAME];
scanf("%s",input_name);
if((p=*head)!=NULL)
while(p->next!=NULL){
p=p->next;
if(strcmp(p->stu.name,input_name)==0){
return p;
}
}
return NULL;
}
else if(strcmp(tempSelectName,sex_name)==0){
studentLink *p=NULL;
int input_sex;
scanf("%d",&input_sex);
if((p=*head)!=NULL)
while(p->next!=NULL){
p=p->next;
if(p->stu.sex==input_sex){
return p;
}
}
return NULL;
}
else if(strcmp(tempSelectName,birthday_name)==0){
studentLink *p=NULL;
char input_birthday[LENGTH_BIRTHDAY];
scanf("%s",input_birthday);
if((p=*head)!=NULL)
while(p->next!=NULL){
p=p->next;
if(strcmp(p->stu.birthday,input_birthday)==0){
return p;
}
}
return NULL;
}
else if(strcmp(tempSelectName,telephone_name)==0){
studentLink *p=NULL;
char input_telephone[LENGTH_TELEPHONE];
scanf("%s",input_telephone);
if((p=*head)!=NULL)
while(p->next!=NULL){
p=p->next;
if(strcmp(p->stu.telephone,input_telephone)==0){
return p;
}
}
return NULL;
}
}
int clear(studentLink **head){
studentLink *p,*q;
if((p=q=(*head))!=NULL)
while(p){
q=p;
p=p->next;
free(q);
}
*head=NULL;
}
int printList(studentLink **head){
studentLink *p=NULL;
if((p=*head)!=NULL)
while(p->next!=NULL){
p=p->next;
printStudentInfo(&(p->stu));
}
return TRUE;
}
int printStudentInfo(studentInfo *elem){
if(elem){
printf("sno:%s\n",(*elem).sno);
printf("name:%s\n",(*elem).name);
printf("sex:%d\n",(*elem).sex);
printf("birthday:%s\n",(*elem).birthday);
printf("telephone:%s\n",(*elem).telephone);
printf("\n");
}
}
int inputStudentInfo(studentInfo *elem){
printf("sno:");
scanf("%s",elem->sno);
printf("name:");
scanf("%s",elem->name);
printf("sex:");
scanf("%d",&(elem->sex));
printf("birthday:");
scanf("%s",elem->birthday);
printf("telephone:");
scanf("%s",elem->telephone);
printf("\n");
}
studentLink *newDataBaseFromFile(FILE **f){
studentLink *head;
studentLink temp;
head=newDataBase();
if(*f=fopen("DataBase.dat","rb"))
while(!feof(*f))
{
studentLink *newStudentInfo;
if((head)!=NULL){
newStudentInfo=(studentLink *)malloc(sizeof(studentLink));
if(newStudentInfo!=NULL){
fscanf(*f,"%s",newStudentInfo->stu.sno);
fscanf(*f,"%s",newStudentInfo->stu.name);
fscanf(*f,"%d",&(newStudentInfo->stu.sex));
fscanf(*f,"%s",newStudentInfo->stu.birthday);
fscanf(*f,"%s",newStudentInfo->stu.telephone);
newStudentInfo->next=(head)->next;
(head)->next=newStudentInfo;
}
}
}
return head;
}
int outputToFile(studentLink **head,FILE **f){
//此函数暂时不可判断数据的重复性
studentLink *p=NULL;
//控制文本文件中没有多余的空格符
int i=0;
if((p=*head)!=NULL)
//全部导入,因为在引入时 就是全部引入
if(*f=fopen("DataBase.dat","w")){
while(p->next!=NULL && (i=i+1)){
p=p->next;
if(i>1)fprintf(*f,"\n");
fprintf(*f,"%s ",p->stu.sno);
fprintf(*f,"%s ",p->stu.name);
fprintf(*f,"%d ",p->stu.sex);
fprintf(*f,"%s ",p->stu.birthday);
fprintf(*f,"%s",p->stu.telephone);
}
fclose(*f);
}
}