C语言电话订餐管理系统

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C语言电话订餐管理系统

乐观的阿斯顿   2022-05-23 我要评论

这是我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、 预览全部信息

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们