服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C语言实现电话订餐管理系统

C语言实现电话订餐管理系统

2022-08-08 10:35乐观的阿斯顿 C/C++

这篇文章主要为大家详细介绍了C语言实现电话订餐管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了c语言实现电话订餐管理系统的具体代码,供大家参考,具体内容如下

这是我c语言课程设计的题目。非常奇怪啊,下面的代码能在c-free中跑起来,却没办法在vc++6.0中跑起来。可能是编译器支持的标准不一样。不管他,反正老师不会把我的代码打一遍,然后放到vc中去运行。

实现了4个功能:添加、查找、修改、删除,同时会把信息写入到同一目录下的customer.dat文件中。(这个文件需要手动建立,没有建立的话程序会不运行。)。
能力有限,错误在所难免,欢迎指出。

代码:

?
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
 * 电话订餐处理系统 
 * 第八组c语言课程设计
 * 佛祖保佑,永无 bug
 */
 
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
 
void printmenu(); // 声明主界面函数 
void addcustomerinfo(); // 声明添加顾客订餐信息的函数 
void modifycustmoerinfo(); // 声明修改顾客订餐信息的函数,记得加参数 
void deletecustomerinfo(); // 声明删除顾客订餐信息的函数,记得加参数
int searchdata();// 查找顾客订餐信息并返回值 
void searchcustomerinfo();// 声明查询顾客订餐信息的函数,记得加参数
void viewallinfo(); // 声明预览全部订餐信息的函数,记得加参数 
void colorchange(int); // 声明修改背景&字体颜色的函数 
void cutline(); // 声明分界线函数 
int unix2time();// 声明时间戳转换为普通时间的函数
void time2unix(time_t,char,char []);// 声明普通时间转换为时间戳的函数  
void getallinfo();//获取所有顾客的全部信息 
void setconsolsize(int x,int y);//定义修改缓冲区大小的函数 
static int n=0;// 定义一个全局变量n用来获取总共有多少顾客信息 
 
 
// 声明一个顾客的结构体变量 
struct customer
{
    char no[15]; 
    char name[20];
    char phonenumber[20];
    char booktime[40];
    int num;
    char other[200];
    char ordertime[40];
}customer[100],custmp;
 
int main()
{
    int choice;
    setconsoletitle("电话订餐系统");
    getallinfo();
    system("mode con cols=150 lines=40");// 调用cmd命令修改窗口大小 
    setconsolsize(150,999);//修改缓冲区的大小 
    printf("欢迎使用电话订餐系统!\n");
    printf("请输入菜单前标号以执行操作\n");
    printmenu:
    printmenu();
    //colorchange(5);
    getchoice:
    fflush(stdin); 
    choice=-1;//重置choice的值 
    printf("\n请输入你的选项 >");
    scanf("%d",&choice); 
    fflush(stdin); // 清空缓冲区,防止scanf接受多余的回车导致死循环 
    switch(choice)
    {
        case 1:addcustomerinfo();break;
        case 2:modifycustmoerinfo();break;
        case 3:deletecustomerinfo();break;
        case 4:searchcustomerinfo();break;
        case 5:viewallinfo();break;
        case 6:goto printmenu;break;
        case 0:exit(0);
        default:{
            cutline();
            colorchange(4);
            printf("输入错误!请重新输入!\n"); 
            colorchange(-1);
            cutline();
        }
            
    }
    goto getchoice;
}
 
void printmenu()//打印菜单函数 
{
    printf("┏━━━━━━━━━━━━━━━━┓\n"); 
    printf("┃ 0. 退出本系统  ┃\n");
    printf("┃ 1. 录入订餐信息┃\n");
    printf("┃ 2. 修改订餐信息┃\n");
    printf("┃ 3. 删除订餐信息┃\n");
    printf("┃ 4. 查询订餐信息┃\n");
    printf("┃ 5. 预览订餐信息┃\n");
    printf("┃ 6. 显示菜单    ┃\n");
    printf("┗━━━━━━━━━━━━━━━━┛\n"); 
}
 
void colorchange(int color)//改变字体颜色函数 
{
    handle self = getstdhandle(std_output_handle);
    
    if(color==-1)
        setconsoletextattribute(self,7);
    setconsoletextattribute(self,color);
}
 
void cutline()//显示一条分割线 
{
    printf("————————————\n");
}
 
void addcustomerinfo()//追加一条顾客的信息 
{
    file *fp;
    cutline();
    
    //尝试打开顾客数据文件 customer.dat 
    if((fp=fopen(".\\customer.dat","rb"))==null)
    
        colorchange(4);
        printf("打开顾客数据文件失败!\n");
        //printf("写入顾客信息失败!");
        colorchange(7);
        cutline();
        return;
    }
    
    //输入顾客的订餐信息 
    printf("请输入顾客姓名 >");
    scanf("%[^\n]s",custmp.name);
    fflush(stdin); //清空缓冲区 
    printf("请输入顾客电话 >");
    scanf("%s",custmp.phonenumber);
    fflush(stdin); 
    printf("请输入顾客的预定时间 >");
    scanf("%[^\n]s",custmp.booktime);
    fflush(stdin); 
    printf("请输入用餐的人数 >");
    scanf("%d",&custmp.num);
    fflush(stdin); 
    printf("请输入顾客的备注 >");
    scanf("%[^\n]s",custmp.other);
    fflush(stdin); 
     
    // 生成以时间为编号的顾客编号 
    time_t rawtime;
    time(&rawtime); 
    time2unix(rawtime,'t',custmp.ordertime);
    time2unix(rawtime,'n',custmp.no);
        
    fclose(fp);
    
    //将顾客的数据文件写入到 customer.dat中去 
    fp=fopen(".\\customer.dat","ab");
    fwrite(&custmp,sizeof(struct customer),1,fp);
    fclose(fp);
    cutline();
}
 
void modifycustmoerinfo()//修改顾客订餐信息 
{
    char target[40];
    int no,choice;
    long int movesize;
    no=searchdata();
    printmenu:
    printf("\n查询到下列顾客信息:\n");
    printf("\n编号\t\t姓名\t\t电话\t\t用餐人数   预定日期\t\t\t下单日期\t\t\t备注\n");
    colorchange(240);
    printf("%-16s",customer[no].no);
    printf("%-16s",customer[no].name);
    printf("%-16s",customer[no].phonenumber);
    printf("%-11d",customer[no].num);
    printf("%-30s",customer[no].booktime);
    printf("%-31s",customer[no].ordertime);
    printf("%-39s\n",customer[no].other);
    colorchange(-1);
    printf("┏━━━━━━━━━━━━━━┓\n");
    printf("┃0.结束修改    ┃\n"); 
    printf("┃1.姓名        ┃\n");
    printf("┃2.电话        ┃\n");
    printf("┃3.用餐人数    ┃\n");
    printf("┃4.预定日期    ┃\n");
    printf("┃5.备注        ┃\n");
    printf("┃6.重新选择顾客┃\n");
    printf("┗━━━━━━━━━━━━━━┛\n");
    getmodifiedinfo:
    cutline();
    printf("\n请选择你要修改的项目 >");
    scanf("%d",&choice);
    //菜单分支 
    switch(choice)
    {
        
        case 1:{
            printf("请输入更正后的内容 >");
            scanf("%s",customer[no].name);
            goto writecustdata; 
        }break;
        
        case 2:{
            printf("请输入更正后的内容 >");
            scanf("%s",customer[no].phonenumber); 
            goto writecustdata;
        }break;
        
        case 3:{
            printf("请输入更正后的内容 >");
            scanf("%d",&customer[no].num); 
            goto writecustdata;
        }break;
        
        case 4:{
            printf("请输入更正后的内容 >");
            scanf("%s",customer[no].booktime); 
            goto writecustdata;
        }break;
        
        case 5:{
            printf("请输入更正后的内容 >");
            scanf("%s",customer[no].other); 
            goto writecustdata;
        }break;
        
        case 6:{ 
            no=searchdata();
            goto printmenu;
        }
        
        case 0:return;
        
        default:{
            colorchange(4);
            printf("输入错误!");
            colorchange(-1);
            goto getmodifiedinfo;
        }break;
    
    
    //将要修改的顾客信息定点在customer.dat文件中覆盖修改 
    writecustdata:
    movesize=no*sizeof(struct customer);
    printf("movesize is %d\n",movesize);
    file *fp;
    fp=fopen(".\\customer.dat","r+");
    rewind(fp);
    fseek(fp,1l*(movesize),0);
    fwrite(&customer[no],sizeof(struct customer),1,fp);
    fclose(fp);
    goto getmodifiedinfo;
}
 
void deletecustomerinfo()
{
    int i,no;
    no=searchdata();
    char choice;
    file *fp;
    
    printf("\n查询到下列顾客信息:\n");
    printf("\n编号\t\t姓名\t\t电话\t\t用餐人数   预定日期\t\t\t下单日期\t\t\t备注\n");
    colorchange(240);
    printf("%-16s",customer[no].no);
    printf("%-16s",customer[no].name);
    printf("%-16s",customer[no].phonenumber);
    printf("%-11d",customer[no].num);
    printf("%-30s",customer[no].booktime);
    printf("%-31s",customer[no].ordertime);
    printf("%-39s\n",customer[no].other);
    colorchange(-1);
    colorchange(4);
    
    printf("\n是否删除这个用户的数据?(y/n) >");
    fflush(stdin);
    scanf("%c",&choice); 
    colorchange(7);
    if(choice=='n'||choice=='n')
    {
        printf("\n返回主菜单...\n");
        return;
    }
    
    if(choice=='y'||choice=='y')
    {
        getallinfo();
        fp=fopen(".\\customer.dat","wb");
        fclose(fp);
        fp=fopen(".\\customer.dat","ab");
        printf("%d,%d",n,no);
        for(i=0;i<=(n-1);i++)
        {
            if(i==no)
                continue;
                
            fwrite(&customer[i],sizeof(struct customer),1,fp);
            
        }
        fclose(fp);
    }
    
}
 
int searchdata()//根据所给的条件寻找对应的顾客i 
{
    getallinfo();
    char target[100];
    printf("\n请输入用户任意单项信息 >");
    scanf("%s",target);
    int i,res1,res2,res3;
    for(i=0;i<=(n-1);i++)
    {
        res1=memcmp(target,customer[i].no,strlen(customer[i].no));
        res2=memcmp(target,customer[i].name,strlen(customer[i].name));
        res3=memcmp(target,customer[i].phonenumber,strlen(customer[i].phonenumber));
        if(!(res1&&res2&&res3))
            return i; 
    }
    return -1;
}
 
void searchcustomerinfo()
{
    int no=searchdata();
    printf("\n查询到下列顾客信息:\n");
    printf("\n编号\t\t姓名\t\t电话\t\t用餐人数   预定日期\t\t\t下单日期\t\t\t备注\n");
    colorchange(240);
    printf("%-16s",customer[no].no);
    printf("%-16s",customer[no].name);
    printf("%-16s",customer[no].phonenumber);
    printf("%-11d",customer[no].num);
    printf("%-30s",customer[no].booktime);
    printf("%-31s",customer[no].ordertime);
    printf("%-100s\n",customer[no].other);
    colorchange(-1);
}
 
void getallinfo()//获取所有顾客的全部信息函数 
{
    n=0;
    file *fp;
    fp=fopen(".\\customer.dat","rb");
    do
    {
        fread(&customer[n],sizeof(struct customer),1,fp);
        //if(customer[n].no[0]=='\0')
        //    break;
        n++;
    }while(feof(fp)==0);
    n=n-1;
    fclose(fp);
}
 
void viewallinfo()
{
    getallinfo(); 
    printf("n is %d",n);
    int i=0,flag=1;
    printf("\n编号\t\t姓名\t\t电话\t\t用餐人数   预定日期\t\t\t下单日期\t\t\t备注\n"); 
    while(i<=(n-1))
    {
        if(flag)
        {
            colorchange(240);
            flag=0;
        }else{
            colorchange(7);
            flag=1;
        }
        printf("%-16s",customer[i].no);
        printf("%-16s",customer[i].name);
        printf("%-16s",customer[i].phonenumber);
        printf("%-11d",customer[i].num);
        printf("%-30s",customer[i].booktime);
        printf("%-31s",customer[i].ordertime);
        printf("%-100s\n",customer[i].other);
        ++i;
    }
    colorchange(-1);
    putchar('\n');
}
 
/* 将时间戳转换为原时间的函数 */
void time2unix(time_t timestamp,char transfer_type,char transfer_time[])
{
    char time1[40];//声明原时间
    struct tm* timeinfo;
    
    if(transfer_type=='t')//如果 transfer_type 为 x,则返回的时间格式为易阅读的 
    {
        timeinfo=localtime(&timestamp);
        strftime(time1,sizeof(time1),"%y年%m月%d日%h时%m分%s秒",timeinfo);
    }
    
    if(transfer_type=='n')// //如果 transfer_type 为 n,则返回的时间格式为纯数字 
    {
        timeinfo=localtime(&timestamp);
        strftime(time1,sizeof(time1),"%y%m%d%h%m%s",timeinfo);
    }
    strcpy(transfer_time,time1);//将转换后的时间格式复制到传递过来的数组当中去 
}
 
void setconsolsize(int x,int y)// 设置缓冲区的大小 
{
    small_rect winpon={0,0,25,10};
    handle con=getstdhandle(std_output_handle);
    coord buf={x,y};// 缓冲区长10000,高25 
    setconsolescreenbuffersize(con,buf);
}

每个功能的测试:

1、录入选项

C语言实现电话订餐管理系统

2、删除选项

C语言实现电话订餐管理系统

3、查询选项

C语言实现电话订餐管理系统

4、修改选项

C语言实现电话订餐管理系统

5、 预览全部信息

C语言实现电话订餐管理系统

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_36770091/article/details/122287696

延伸 · 阅读

精彩推荐