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

2022-07-19,,,

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

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

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

代码:

/*
 * 电话订餐处理系统 
 * 第八组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、录入选项

2、删除选项

3、查询选项

4、修改选项

5、 预览全部信息

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

《C语言实现电话订餐管理系统.doc》

下载本文的Word格式文档,以方便收藏与打印。