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
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define MaxSize 20
typedef struct guest_info {
char name[8];
int sum;
char time[10];
int number;
struct guest_info * next;
} GuestLink,* Pointer;
void Insert(Pointer * Head);
void Search(Pointer Head );
void Update(Pointer Head);
void Delete(Pointer * Head);
void Show(Pointer Head);

Pointer Sort(Pointer );///排序
void Storage(Pointer ); ///储存
Pointer ReadFile();///浏览文件
int main() {
Pointer Head=NULL;
int i;
do {
printf("\n");
printf("1---插入(Insert)\n");
printf("2---查询(Search)\n");
printf("3---修改(Update)\n");
printf("4---删除-(Delete)\n");
printf("5---显示(Show)\n");
printf("6---退出(Exit)\n");
printf("7---排序(Sort)\n");
printf("8---录入文件(Storage)\n");
printf("9---读取数据(ReadFile)\n");
scanf("%d",&i);
switch(i) {
case 1:
Insert(&Head);
break;
case 2:
Search(Head);
break;
case 3:
Update(Head);
break;
case 4:
Delete(&Head);
break;
case 5:
Show(Head);
break;
case 6:
break;
case 7:
Head = Sort(Head);
break;
case 8:
Storage(Head);
break;
case 9:
Head = ReadFile();
Show(Head);
break;
default:
printf("错误选择!请重选");
break;
}
} while(i!=6);
return 0;
}


void Insert(Pointer * Head) {
int in_number;
Pointer p,q,r;
printf("请输入编号:");
scanf("%d",&in_number);
p=q=* Head;
while(p!=NULL) {
if(p->number==in_number) {
printf("已经有相同的编号:");
return;
} else {
q=p;
p=p->next;
}
}
r=(Pointer)malloc(sizeof(GuestLink));
r->next=NULL;
if(r==NULL) {
printf("分配空间失败!");
return;
}
if(q==NULL)
* Head=r;
else {
q->next=r;
}
r-> number=in_number;
printf("请输入姓名:");
scanf("%s",r->name);
printf("请输入人数:");
scanf("%d", &r->sum);
printf("请输入用餐时间:");
scanf("%s", r->time);
}



void Search(Pointer Head) {
int flag=1;
int number;
Pointer p;
printf("请输入要查询的编号:");
scanf("%d", &number);
p=Head;
while(p!=NULL&&flag) {
if(p->number==number) {
printf("姓名:%s",p->name);
printf("人数:%d",p->sum);
printf("用餐时间:%s",p->time);
flag=0;
} else
p=p->next;
}
if(flag)
printf("没有查询到!!");
}



void Update(Pointer Head) {
int flag=1;
int number;
Pointer p;
printf("请输入要修改的编号:");
scanf("%d",&number);
p=Head;
while(p!=NULL&&flag) {
if(p->number==number) {
printf("请输入人数:");
scanf("%d",p->sum);
printf("请输入用餐时间:");
scanf("%s",p->time);
flag=0;
} else
p=p->next;
}
if(flag)
printf("没有找到要修改的记录!!");
}


void Delete(Pointer *Head) {
int flag=1;
int number;
Pointer p,q;
printf("请输入要删除数据的编号");
scanf("%d",&number);
p=q=* Head;
while(p!=NULL&&flag) {
if(p->number==number) {
if(p==* Head) {
*Head=p->next;
free(p);
} else {
q->next=p->next;
free(p);
}
flag=0;
} else {
q=p;
p=p->next;
}
}
if(flag)
printf("没有找到可以删除的数据!!");
}



void Show(Pointer Head) {
Pointer p;
p=Head;
while(p!=NULL) {
printf("姓名: %-10s",p->name);
printf("人数: %-10d",p->sum);
printf("用餐时间: %-10s",p->time);
printf("编号: %-10d\n",p->number);
p=p->next;
}
}
Pointer Sort(Pointer Head) {
Pointer pfirst,ptail,pminBefore,pmin,p;
pfirst = NULL;

while(Head!=NULL) {
for (p = Head, pmin = Head; p->next != NULL; p = p->next) {
if(strcmp(p->next->time,pmin->time) < 0) {
pminBefore = p;
pmin = p->next;
}
}
if(pfirst == NULL) {
pfirst = pmin;
ptail = pmin;
} else {
ptail->next = pmin;
ptail = pmin;
}
if(pmin == Head)
Head = Head->next;
else
pminBefore->next = pmin->next;
}
if(pfirst != NULL)
ptail->next = NULL;
Head = pfirst;
return Head;
}
void Storage(Pointer Head) { ///储存
Pointer cur;
cur = Head;
FILE *fp = fp = fopen( "学生信息.txt", "w" );
while(cur != NULL){
fprintf(fp, "%s\t%d\t%s\t%d\n", cur->name,cur->sum,cur->time,cur->number);
cur = cur->next;
}
fclose(fp);
}
Pointer ReadFile() {
char name[8];
int sum;
Pointer r,s;
Pointer first;
first = (Pointer)malloc(sizeof(GuestLink));
r = first;
first->next = NULL;

char time[10];
int number;
FILE* fp;
fp = fopen("学生信息.txt", "r");
while(1) {
fscanf(fp, "%s%d%s%d",name,&sum,time,&number);
if(feof(fp))
break;
s=(Pointer)malloc(sizeof(GuestLink));
strcpy(s->name,name);
s->sum = sum;
strcpy(s->time,time);
s->number = number;
r->next = s;
r = s;
}
r->next = NULL;
return first->next;
fclose(fp);
}
///浏览文件*/
觉得文章不错的话可以请我喝一杯茶哟~
  • 本文作者: bestsort
  • 本文链接: https://bestsort.cn/2019/04/22/102/
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-SA 许可协议。转载请注明出处!并保留本声明。感谢您的阅读和支持!
-------------本文结束感谢您的阅读-------------