餐厅预定系统

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

typedef struct _RECORD
{
	int date[4];//年月日小时
	char name[20];//预订人
	int count;//预定人数
	char tel[20];//联系方式
}REC;

struct List
{
	REC data;//数据域
	struct List* next;//指针域
};

int FLAG=1;//记录是否要显示

void Add_Book_Rec(List& node)
{
	List* p;
	char temp[30];
	int i,j;
	if(node.next==NULL)//如果是空链表
	{
		node.next=new List;
		p=node.next;
		p->next=NULL;
	}
	else//不是空链表 让p指向尾节点
	{
		p=node.next;
		while(p->next!=NULL)
		{
			p=p->next;
		}
		p->next=new List;
		p=p->next;
		p->next=NULL;
	}
	system("cls");
	cout<<"请输入预订人:"<<endl;
	cin>>p->data.name;
	cout<<"请输入预定时间(year-mon-day-hour):"<<endl;
	cin>>temp;
	p->data.date[0]=0;
	p->data.date[1]=0;
	p->data.date[2]=0;
	p->data.date[3]=0;//初始化日期
	for(j=0,i=0;j<4;j++)
	{
		while(temp[i]!='-'&&temp[i]!='\0')
		{
			
			p->data.date[j]=p->data.date[j]*10+temp[i]-'0';
			i++;
		}
		i++;
	}//拆分字符串 变成4个整形变量 记录时间

	cout<<"请输入预订人数:"<<endl;
	cin>>p->data.count;
	cout<<"请输入联系方式:"<<endl;
	cin>>p->data.tel;
	cout<<"添加成功!"<<endl;
	system("pause");
}

//输出首节点   以及输出数量  
void Dis_Book_Rec(List* node,int num)
{
	if(node==NULL)
	{
		cout<<"无预定信息!"<<endl;
		return ;
	}
	if(FLAG==1)//根据FLAG判断是否要多次输出
	{
		cout<<"预定日期";cout.width(20);
		cout<<"预定人";cout.width(20);
		cout<<"预定人数";cout.width(20);
		cout<<"联系方式"<<endl;
	}
	for(;num>0&&node!=NULL;num--,node=node->next)//如果链表不为空 并且没有输出指定数量
	{
		cout<<node->data.date[0]<<"-";
		cout<<node->data.date[1]<<"-";
		cout<<node->data.date[2]<<"   ";
		cout<<node->data.date[3];cout.width(12);
		cout<<node->data.name;cout.width(15);
		cout<<node->data.count;cout.width(30);
		cout<<node->data.tel<<endl;
	}
}

void Serach_Date(List& node)
{
	int i,j,flag=1;
	int time[3]={0};
	List* p=node.next;
	char temp[30];
	system("cls");
	if(node.next==NULL)
	{
		cout<<"无预定信息!"<<endl;
		system("pause");
		return ;
	}
	cout<<"请输入要查询的日期(year-mon-day)"<<endl;
	cin>>temp;
	for(j=0,i=0;j<3;j++)
	{
		while(temp[i]!='-'&&temp[i]!='\0')
		{
			
			time[j]=time[j]*10+temp[i]-'0';
			i++;
		}
		i++;
	}

	do
	{
		if(p->data.date[0]==time[0]&&p->data.date[1]==time[1]&&p->data.date[2]==time[2])//根据输入的日期查找
		{
			Dis_Book_Rec(p,1);
			flag=0;//记录是否找到
			FLAG=0;//设置下次不显示中文
		}
		p=p->next;
	}while(p!=NULL);
	FLAG=1;//再次设置显示
	if(flag)//flag为1则没有找到
	{
		cout<<"该日期无预定信息!"<<endl;
	}
	system("pause");
}

void Serach_Name(List& node)
{
	int flag=1;
	List* p=node.next;
	char temp[30];
	system("cls");
	if(node.next==NULL)
	{
		cout<<"无预定信息!"<<endl;
		system("pause");
		return ;
	}
	cout<<"请输入要查询的预订人"<<endl;
	cin>>temp;
	do
	{
		if(!strcmp(p->data.name,temp))
		{
			Dis_Book_Rec(p,1);
			flag=0;
			FLAG=0;
		}
		p=p->next;
	}while(p!=NULL);
	FLAG=1;
	if(flag)
	{
		cout<<"无该预定人信息!"<<endl;
	}
	system("pause");
}


void Serach_Menu(List& node)
{
	int chioce;
	do
	{
		system("cls");
		cout<<"1:按日期查找"<<endl;
		cout<<"2:按预订人查找"<<endl;
		cout<<"0:返回"<<endl;
		cout<<"请输入选择:";
		cin>>chioce;
		while(chioce<0||chioce>2)
		{
			cout<<"输入范围错误,请重新输入:";
			cin.clear();
			cin>>chioce;
		}
		switch(chioce)
		{
		case 1:
			Serach_Date(node);
			break;
		case 2:
			Serach_Name(node);
			break;
		}
	}while(chioce!=0);

}

void Modfiy_Book_Rec(List& node)
{
	List* p=node.next;
	char temp[30];
	char name[30];
	int time[3]={0};
	int i,j,flag=1;
	if(node.next==NULL)//如果是空链表
	{
		cout<<"无预定信息!"<<endl;
		system("pause");
		return ;
	}
	system("cls");
	cout<<"请输入预订人:"<<endl;
	cin>>name;
	cout<<"请输入预定时间(year-mon-day):"<<endl;
	cin>>temp;
	for(j=0,i=0;j<3;j++)
	{
		while(temp[i]!='-'&&temp[i]!='\0')
		{
			
			time[j]=time[j]*10+temp[i]-'0';
			i++;
		}
		i++;
	}
	do
	{
		if(p->data.date[0]==time[0]&&p->data.date[1]==time[1]&&p->data.date[2]==time[2]&&(!strcmp(name,p->data.name)))
		{
			Dis_Book_Rec(p,1);
			flag=0;
			break;
		}
		p=p->next;
	}while(p!=NULL);
	if(flag)
	{
		cout<<"无该预定信息!"<<endl;
		system("pause");
		return ;
	}

	cout<<"请修改预定时间(year-mon-day-hour):"<<endl;
	cin>>temp;
	p->data.date[0]=0;
	p->data.date[1]=0;
	p->data.date[2]=0;
	p->data.date[3]=0;//初始化日期
	for(j=0,i=0;j<4;j++)
	{
		while(temp[i]!='-'&&temp[i]!='\0')
		{
			
			p->data.date[j]=p->data.date[j]*10+temp[i]-'0';
			i++;
		}
		i++;
	}

	cout<<"请修改预订人数:"<<endl;
	cin>>p->data.count;
	cout<<"请修改联系方式:"<<endl;
	cin>>p->data.tel;
	cout<<"修改成功!"<<endl;
	system("pause");
}


void Del_Book_Rec(List& node)
{
	List* p=node.next;
	List* q=node.next;
	char temp[30];
	char name[30];
	int time[3]={0};
	int i,j,flag=1;
	if(node.next==NULL)//如果是空链表
	{
		cout<<"无预定信息!"<<endl;
		system("pause");
		return ;
	}

	system("cls");
	cout<<"请输入预订人:"<<endl;
	cin>>name;
	cout<<"请输入预定时间(year-mon-day):"<<endl;
	cin>>temp;
	for(j=0,i=0;j<3;j++)
	{
		while(temp[i]!='-'&&temp[i]!='\0')
		{
			
			time[j]=time[j]*10+temp[i]-'0';
			i++;
		}
		i++;
	}//将输入字符串拆分
	do
	{
		if(p->data.date[0]==time[0]&&p->data.date[1]==time[1]&&p->data.date[2]==time[2]&&(!strcmp(name,p->data.name)))//如果是要查找的信息
		{
			Dis_Book_Rec(p,1);//显示
			flag=0;
			break;
		}
		q=p;
		p=p->next;
	}while(p!=NULL);
	if(flag)
	{
		cout<<"无该预定信息!"<<endl;
		system("pause");
		return ;
	}

	cout<<"确认删除?(Y/N)"<<endl;
	cin>>temp;
	if(!strcmp("Y",temp)||!strcmp("y",temp))
	{
		if(p==q)
		{
			node.next=q->next;
			delete q;
		}
		else
		{
			q->next=p->next;
			delete p;
		}
		cout<<"删除成功!"<<endl;
	}
	else
	{
		cout<<"已取消删除!"<<endl;
	}
	system("pause");
}

int menu()
{
	int chioce;
	system("cls");
	cout<<"1:添加预定信息"<<endl;
	cout<<"2:修改预定信息"<<endl;
	cout<<"3:查找预定信息"<<endl;
	cout<<"4:删除预定信息"<<endl;
	cout<<"5:显示预定信息"<<endl;
	cout<<"0:退出"<<endl;
	cout<<"请输入选择:";
	cin.clear();
	cin>>chioce;//记录选择
	while(chioce<0||chioce>5)//范围判断 避免错误
	{
		cout<<"输入范围错误,请重新输入:";
		cin.clear();//清空输入流
		cin>>chioce;
	}
	return chioce;//返回选择结果
}


/*文件保存读取*/
void Save_Book_Rec(List& node)
{
	List* p=node.next;
	ofstream fp;
	fp.open("Book_Rec.dat");//打开文件
	if(fp==NULL)//代开失败返回
	{return ;}

	while(p!=NULL)//遍历链表 写入文件
	{
		fp.write((char*)&p->data,sizeof(REC));
		p=p->next;
	}
	fp.close();//关闭文件
}

void Load_Book_Rec(List& node)//加载文件信息
{
	int i=0;
	List* p;
	ifstream fp;
	REC temp;
	fp.open("Book_Rec.dat");//打开文件
	if(fp==NULL)//打开失败返回
	{return ;}

	while(fp.read((char*)&temp,sizeof(REC)))//读取文件到结尾
	{
		if(node.next==NULL)
		{
			node.next=new List;
			p=node.next;
			p->next=NULL;
		}
		else
		{
			p->next=new List;
			p=p->next;
			p->next=NULL;
		}
		p->data=temp;//信息存放到链表
	}
	fp.close();//关闭文件
}


void Destory_List(List& node)//用户选择退出 释放内存链表数据
{
	List* p=node.next;
	List* q;
	while(p)
	{
		q=p;
		p=p->next;
		delete q;
	}
}

int main()
{
	List node;
	node.next=NULL;//头结点初始化
	Load_Book_Rec(node);//加载文件到链表
	int chioce;
	do
	{
		chioce=menu();//获取用户选项
		switch(chioce)
		{
		case 1:
			Add_Book_Rec(node);//添加
			Save_Book_Rec(node);//保存
			break;
		case 2:
			Modfiy_Book_Rec(node);//修改
			Save_Book_Rec(node);//保存
			break;
		case 3:
			Serach_Menu(node);//查找菜单
			break;
		case 4:
			Del_Book_Rec(node);//删除记录
			Save_Book_Rec(node);//保存
			break;
		case 5:
			system("cls");
			Dis_Book_Rec(node.next,100);//显示全部记录
			system("pause");
			break;
		case 0:
			Destory_List(node);//销毁链表
			break;
		}
	}while(chioce!=0);
	return 0;
}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值