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

 更新时间:2022年01月04日 14:36:00   作者:乐观的阿斯顿  
这篇文章主要为大家详细介绍了C语言实现电话订餐管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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++List容器常用函数接口刨析

    C++List容器常用函数接口刨析

    最近我学习了C++中的STL库中的list容器,对于常用容器,我们不仅要会使用其常用的函数接口,我们还有明白这些接口在其底层是如何实现的。所以特意整理出来一篇博客供我们学习
    2022-08-08
  • Sersync+Rsync实现触发式文件同步实战过程

    Sersync+Rsync实现触发式文件同步实战过程

    sersync是使用c++编写,而且对linux系统文 件系统产生的临时文件和重复的文件操作进行过滤。下面通过本文给大家分享Sersync+Rsync实现触发式文件同步实战过程,需要的朋友参考下吧
    2017-09-09
  • C语言二维数组应用之扫雷游戏

    C语言二维数组应用之扫雷游戏

    这篇文章主要为大家详细介绍了C语言二维数组应用之扫雷游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • C++如何用数组模拟链表

    C++如何用数组模拟链表

    大家好,本篇文章主要讲的是C++如何用数组模拟链表,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-01-01
  • C++中各种可调用对象深入讲解

    C++中各种可调用对象深入讲解

    这篇文章主要给大家介绍了关于C++中各种可调用对象的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-02-02
  • C语言实现小学生考试系统

    C语言实现小学生考试系统

    这篇文章主要为大家详细介绍了C语言实现小学生考试系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • C语言动态内存管理的原理及实现方法

    C语言动态内存管理的原理及实现方法

    C语言动态内存管理的原理是通过 malloc() 函数申请一块连续的内存空间,并返回其地址,通过 free() 函数释放该内存空间。实现方法是通过在程序运行时动态地管理内存,即在需要内存时申请,不需要时释放,避免了静态内存分配的浪费和不足
    2023-04-04
  • C/C++时间库chrono的使用总结

    C/C++时间库chrono的使用总结

    std::chrono是C++标准库中的一个组件,用于表示和处理时间,其功能就像是心理学中的感知系统,它可以为我们捕捉、量化并操作抽象的时间概念,这就如同我们的大脑可以理解和感知周围环境的时间流逝一样,这种感知和理解能力是人类进行日常活动所必需的,
    2023-12-12
  • C++11 移动构造函数的使用

    C++11 移动构造函数的使用

    本文主要介绍了C++11 移动构造函数的使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01

最新评论