c语言电话订餐管理系统

这是我C语言课程设计的题目。非常奇怪啊,下面的代码能在C-Free中跑起来,却没办法在vc++6.0中跑起来。可能是编译器支持的标准不一样。不管他,反正老师不会把我的代码打一遍,然后放到vc中去运行。
实现了4个功能:添加、查找、修改、删除,同时会把信息写入到同一目录下的customer.dat文件中。(这个文件需要手动建立,没有建立的话程序会不运行。)。
能力有限,错误在所难免,欢迎指出。
代码很长,有很多代码是用来使得这个界面更好看的或者是忘记删除了的,比如修改控制台缓冲区大小的函数,修改字体颜色的函数(用来将查询的结果用不同的颜色标出来,更醒目一些,还有删除的时候用红色提示),好像还有功能写在里面但是没完全写在里面哈哈哈哈哈哈。
要在编译链接好的程序同一目录下手动建立一个 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、 预览全部信息

预览选项
报告可真难写。
挑战10天学完高等数学上
后记:高数上期末75分,感谢我亲爱的高数老师!哈哈哈哈哈哈哈

  • 6
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
以下是一个简单的C语言链表订餐管理系统的示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义菜品结构体 typedef struct Dish { int id; char name[100]; float price; struct Dish* next; } Dish; // 全局变量,指向链表头节点 Dish* head = NULL; // 创建新菜品节点 Dish* createDish(int id, char name[], float price) { Dish* newDish = (Dish*)malloc(sizeof(Dish)); newDish->id = id; strcpy(newDish->name, name); newDish->price = price; newDish->next = NULL; return newDish; } // 添加菜品到链表 void addDish(int id, char name[], float price) { Dish* newDish = createDish(id, name, price); if (head == NULL) { head = newDish; } else { Dish* current = head; while (current->next != NULL) { current = current->next; } current->next = newDish; } } // 打印菜单 void printMenu() { Dish* current = head; printf("菜单:\n"); while (current != NULL) { printf("ID:%d,名称:%s,价格:%.2f\n", current->id, current->name, current->price); current = current->next; } } // 查找菜品 Dish* findDish(int id) { Dish* current = head; while (current != NULL) { if (current->id == id) { return current; } current = current->next; } return NULL; } // 删除菜品 void deleteDish(int id) { Dish* current = head; Dish* previous = NULL; while (current != NULL) { if (current->id == id) { if (previous == NULL) { head = current->next; } else { previous->next = current->next; } free(current); return; } previous = current; current = current->next; } } int main() { // 添加菜品到链表 addDish(1, "鱼香肉丝", 18.0); addDish(2, "宫保鸡丁", 20.0); addDish(3, "红烧肉", 25.0); // 打印菜单 printMenu(); // 查找菜品 int dishId = 2; Dish* foundDish = findDish(dishId); if (foundDish != NULL) { printf("找到了ID为%d的菜品:%s,价格:%.2f\n", dishId, foundDish->name, foundDish->price); } else { printf("未找到ID为%d的菜品\n", dishId); } // 删除菜品 int deleteId = 3; deleteDish(deleteId); printf("删除ID为%d的菜品后的菜单:\n", deleteId); printMenu(); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值