一个简单的点餐系统

    快要期末了,期末要答辩,在DEVC++里做了一个小系统。  

    首先先创建一个项目,再在该项目里创建四个如图所示的单元

main.cpp:

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <conio.h>
#include "struch.h"
#include "function_declare.h"


/* run this program using the console pauser or add your own getch, system("pause") or input loop */

using namespace std;
int main() {
	int ch,result1,result; 
	struct SqList L;
	struct sqList Q;
	struct OrderList W;     //用户点的菜的顺序表 
	OrderInitList_Sq(W);
	FoodInitList_Sq(Q);
	Foodread(&Q);
	char name[15];
	system("cls");
	cout << "--按任意键进入主菜单--";
	label:
	system("cls");
	getch();
	while(1){
		cout << "\n1、用户登录!" << "\n";
		cout << "2、商家登录!" << "\n";
		cout << "0、退出!" << "\n";
		cin >> ch;
		switch(ch){
//用户操作--------------------------------------------------------------------------------
		getch();
	    while(1){
		case 1:{                                 
		Tips();
		CInitList_Sq(L);
		Cread(&L); 
		cin >> ch;
		 switch(ch){
		 	case 1:
		 		Register(&L);
		 		CFprintf(&L);
		 		system("pause");
		 		break;
			case 2:					//登录成功 -----------------------------------
		 	if(Login(&L)){
		 		system("cls");
				while(1){
				system("cls");
		 		printf("1.查看菜单! \n") ;
		 		printf("2.点菜 \n") ;
		 		printf("3查看点的菜单 \n") ;
		 		cout << "4、查找菜品" << "\n"; 
		 		cout<<"5、预约"<<"\n"; 
		 		cout << "6、结账!" << "\n";
		 		cout << "10、返回登陆界面!" << "\n";
				cin >> ch;
		        switch(ch){
		        case 1:
		 		PrintfFood(&Q);
		 		system("pause");
		 		break;
		        case 2:
				Order(&W);
		 		system("pause");
		 		break;
				 		
		 		case 3:
				OrderPrintfFood(&W);
		 		system("pause");
		 		break;
		 		
		 		case 4:
		 		cout<<"输入要查找的菜名"<<"\n";
		 			cin>>name;
		 			result1=FindByname(Q,name);
		 			if(result1){
		 			ShowBookByIndex(Q,result1);
		 			}else{
		 				printf("未找到\n");
					 }
			     	system("pause");
			 		break;
			 	
				case 5:
				 ROOMS rooms;
					ROOM room;
					YROOM yroom;
					YROOMS yrooms;
					USERS users;
					USER user;
					
					InitList_ROOMS_Sq(rooms);
					InitList_USERS_Sq(users);
					InitList_YROOMS_Sq(yrooms);
					read_rooms(rooms);
					ShowRooms(rooms);
//					FindRooms(rooms);
					cout<<"名字"<<"\n" ;
					cin>>rooms.rooms->user_name;
					cout<<"手机号"<<"\n";
					cin>>rooms.rooms->mobile;
					cout<<"房间号"<<"\n";
					cin>>rooms.rooms->roomID;
					cout<<"time"<<"\n";
					cin>>rooms.rooms->ytime;
					ReserveRoom(rooms,yrooms,yroom); 
					ShowYRooms(yrooms);
					ShowRooms(rooms);
			     	system("pause");
			 		break;
				case 6:
				printf("一共需要支付%.0lf元!谢谢惠顾!",Settlement(&W,&Q));
				system("pause");
			 	break;	 	
		        case 10:
		        goto label;
				}					//------------------------------------ 
			 }}
			 system("pause");
				break;
				case 3:PrintfPassword(&L); 
			 system("pause");
				break;
				
			case 0:
				system("cls");
				cout << "===已退出!===" << "\n";
				system("pause");
				return 0;
			default :
            printf("输入错误,请重新输入!\n");
            system("pause");
            break;	
		 }
} }                                               
//用户操作------------------------------------------------------------------------------------------------------------
//商家操作-----------------------------------------------------------------------------------------------------------
		case 2:
	    while(1){                                
		Tips();
		CInitList_Sq(L);
		Sread(&L); 
		cin >> ch;
		 switch(ch){
		 	case 1:
		 		Register(&L);
		 		SFprintf(&L);
		 		system("pause");
		 		break;
			case 2:					//登录成功 -----------------------------------
		 	if(Login(&L)){
		 		system("cls");
				cout << "1、添加菜品!" << "\n";
				cout << "2、显示菜品!" << "\n";
				printf("3查看用户点的菜单 \n") ;
				cout << "4、删除菜品!" << "\n";
				cout << "5、查找菜品!" << "\n";
				cout << "10、返回登陆界面!" << "\n";
				cout << "0、退出!" << "\n";
				
				getch();
				while(1){
		 		cin >> ch;
		        switch(ch){
				case 1:
				AddFood(&Q);
				system("pause");
		 		break;	
		 		case 2:
		 		PrintfFood(&Q);
		 		system("pause");
		 		break;
		 		case 3:
				OrderPrintfFood(&W);
		 		system("pause");
		 		break;
		 		case 4:
			 		cout<<"输入要删除的菜名"<<"\n";
					scanf("%s",&name); 
			 		deletefoods(Q,name);
			 		system("pause");
			 		break;
			 		
		 		case 5:
		 			cout<<"输入要查找的菜名"<<"\n";
		 			cin>>name;
		 			result=FindByname(Q,name);
		 			if(result){
		 			ShowBookByIndex(Q,result);}
		 			break;
		 		case 10:
		        goto label;
		        
		        case 0:
				system("cls");
				cout << "===已退出!===" << "\n";
				system("pause");
				return 0;
			default :
            printf("输入错误,请重新输入!\n");
            system("pause");
            break;		
				}					//------------------------------------ 
			 }
 		}
			 system("pause");
				break;
				case 3:PrintfPassword(&L); 
			 system("pause");
				break;
			case 0:
				system("cls");
				cout << "===已退出!===" << "\n";
				system("pause");
				return 0;
			default :
            printf("输入错误,请重新输入!\n");
            system("pause");
            break;	
		 }
} 
//商家操作-----------------------------------------------------------------------------------------------------------					
				case 0:
				system("cls");
				cout << "===已退出!===" << "\n";
				system("pause");
				return 0;
			default :
            printf("输入错误,请重新输入!\n");
            system("pause");
            break;
}
}
	return 0;
}

 struct.h:

#ifndef __STRUCH_H__
#define __STRUCH_H__
#define CARD_NO_LEN				10
#define TYPE_LEN				10
#define USER_NAME_LEN			30
#define PWD_LEN					30
#define USER_NUM_MAX			500
#define TRANSACTION_NUM_MAX		5000
#define ROOM_MAX                100
#define USERS_MAX               100

#define DIR_LEN_MAX				256
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXBOOKS 100
typedef int Status; //Status 是函数返回值类型,其值是函数结果状态代码。
typedef int ElemType; //ElemType 为可定义的数据类型,此设为int类型
//using namespace std;

#define MAXSIZE 100			//顺序表可能达到的最大长度
struct Password{
	char account [15];
	char password [15];
};
typedef struct SqList{
	Password *elem;
	int length;
}SqList;


struct Mune{
	char FoodName[10];
	double Price;
}; 
typedef struct sqList{
	Mune *elem;
	int length;
}sqList;

struct Order{
	char FoodName[10];
};
typedef struct OrderList{
	Order *elem;
	int length;
}OrderList;


struct room{
	char RoomName [15];
	double Price;
	char Time[20];
};
typedef struct SqListRoom
{
	room *elem;
	int length;
}SqListRoom;
struct YROOM{
	char user_name[13];	// 用户名
	char mobile[12];				// 手机号
	char roomID[12];	// 房间号 
	char ytime[12];	   // 预约时间 
};
typedef struct {
	YROOM *yrooms;
	int length;
}YROOMS;


struct USER{
	char mobile[12];				// 手机号 
	char password[PWD_LEN];			// 密码 
	char user_name[USER_NAME_LEN];	// 用户名 
	int roleid;						// 角色ID:0 卡用户, 1 卡业务员, 2 系统管理员 
	bool has_card;					// 是否开卡 
	char card_no[CARD_NO_LEN];		// 卡号 
};

typedef struct {
	USER *users;
	int length;
}USERS;

struct ROOM{
	char roomID[12];	// 房间号 
	int  status;		// 房间状态,0-空房,1-入住,2-预约 
	char ytime[12];	   // 预约时间 
	char user_name[USER_NAME_LEN];	// 用户名
	char mobile[12];
};

typedef struct {
	ROOM *rooms;
	int length;
}ROOMS;
#endif

books_operation.cpp :

#include<string.h>
#include<iostream>
#include "struch.h"
using namespace std;
//初始化
Status CInitList_Sq(SqList &L) { 

	L.elem = new Password[MAXSIZE]; 
	if (!L.elem){
		cout << "顺序表建立失败" <<"\n"; 
		exit(OVERFLOW); 
	}
	L.length = 0; 
//	cout << "顺序表建立成功" <<"\n"; 
	return OK;
}
Status FoodInitList_Sq(sqList &L) { 

	L.elem = new Mune[MAXSIZE]; 
	if (!L.elem){
		cout << "顺序表建立失败" <<"\n"; 
		exit(OVERFLOW); 
	}
	L.length = 0; 
	cout << "顺序表建立成功" <<"\n"; 
	return OK;
}
Status OrderInitList_Sq(OrderList &L) { 

	L.elem = new Order[MAXSIZE]; 
	if (!L.elem){
		cout << "顺序表建立失败" <<"\n"; 
		exit(OVERFLOW); 
	}
	L.length = 0; 
	cout << "顺序表建立成功" <<"\n"; 
	return OK;
}
//刷新账号表 
void CFprintf(SqList *L){
	int i = 0,p=L->length;
	FILE *fp;
	if((fp = fopen("CustomerPassword.txt","w"))==NULL)
	printf("打开失败了!");
	while(p){
	p--;
	fprintf(fp,"%s %s \n",L->elem[i].account,L->elem[i].password);
	i++;
	}
	fclose(fp);
}
//查看口令
void PrintfPassword(SqList *L){
	int i = 0,p=L->length;
	while(p){
		p--;
		printf("账号:%s||密码:%s  \n",L->elem[i].account,L->elem[i].password);
		i++;
	}
}
//注册
void Register(SqList *L){
	printf("-----注册-----\n"); 
	printf("账号:");
	cin>>L->elem[L->length].account;
	printf("密码:");
	cin>>L->elem[L->length].password;
	L->length++;
}
//登录
int Login(SqList *L) {
	int i=0,p=L->length;
	char a[15],b[15];
	printf("-----登录-----\n");
	printf("账号:");
	cin>>a;
	printf("密码:");
	cin>>b; 
	while (p){
		p--;
//		printf("账号:%s||密码:%s  \n",L->elem[i].account,L->elem[i].password);
		if((strcmp(L->elem[i].account,a)==0)&&(strcmp(L->elem[i].password,b)==0)){
			printf("口令正确,登录成功!"); 
			return 1;
		}
		i++;
	}
	printf("账号或密码错误,登录失败!");
	return 0;
}
//提示 
void Tips(){
	system("cls");
		cout << "============主菜单=========" << "\n";
		cout << "1、注册账号" << "\n";
		cout << "2、登录账号!" << "\n";
		cout << "请输入需要的功能序号:" << "\n";
}
//刷新账号表 
void SFprintf(SqList *L){
	int i = 0,p=L->length;
	FILE *fp;
	if((fp = fopen("BusinessPassword.txt","w"))==NULL)
	printf("打开失败了!");
	while(p){
	p--;
	fprintf(fp,"%s %s \n",L->elem[i].account,L->elem[i].password);
	i++;
	}
	fclose(fp);
}
void Order(OrderList *Q){                         //用户点菜的菜单 
	int i=Q->length;
	char a[10];
    //while(1){
    //i=i+1;
	printf("菜名:");
	cin >>Q->elem[i].FoodName;
	
	Q->length++;
    printf("what's up");
    //break;
	//}
}
void OrderPrintfFood(OrderList *Q){						 //用户查看点的菜 
	int p = Q->length;
	int i = 0;
	printf("  菜名\n"); 
	while(p){
		printf("%s\n",Q->elem[i].FoodName);
		i++;
		p--;
	}
}


//===============================================================================
//显示菜单 
void PrintfFood(sqList *Q){
	int p = Q->length;
	int i = 0;
	printf("  菜名    价格\n"); 
	while(p){
		printf("%s   %.0lf \n",Q->elem[i].FoodName,Q->elem[i].Price);
		i++;
		p--;
	}
}
//商家添加菜品 
void AddFood(sqList *Q){
	int p,i = 0;
	printf("菜名:");
	cin >> Q->elem[Q->length].FoodName;
	printf("价格:");
	cin >> Q->elem[Q->length].Price;
	Q->length++;
	p=Q->length;
	FILE *fp;
	if((fp = fopen("Food.txt","w"))==NULL)
	printf("打开失败啊!");
	while(p){
		fprintf(fp,"%s %.0lf \n",Q->elem[i].FoodName,Q->elem[i].Price);
		i++;
		p--;
	}
	fclose(fp);
}

Status FindByname(sqList Q,char* name){
	int i;
	if(Q.length<0)
	return ERROR;
	for(i=1;i<Q.length;i++){
		if(strcmp(Q.elem[i].FoodName,name)==0){
			return i+1;
		}
	}
	return ERROR;
}

Status deletefoods(sqList &Q,char *name){
	int index = FindByname(Q,name);
	int i;
	if((index<1)||(index>Q.length))  return ERROR;
	if(index){
		for(i=index;i<=Q.length-1;i++){
			Q.elem[i-1]=Q.elem[i];
		}
		--Q.length;
		return OK;
	}
	return ERROR;
}

void ShowBookByIndex(sqList Q,int i){
	Mune mune;
	mune=Q.elem[i-1];
	cout<<"菜名"<<"\t"<<"价格";
	cout<<"\n";
	cout<<mune.FoodName<<"\t"<<mune.Price<<"\n";
}

//房间===============================================================================
//int SqListRoomInitList_Sq(SqListRoom &L)      //创建房间的顺序表 
//{
//	L.elem=new room[10];
//	if(!L.elem)
//	{
//	printf("创建失败了啊!");
//	exit(OVERFLOW);
//	}
//	L.length=0;
//	return 1;
//}

//初始化
Status InitList_YROOMS_Sq(YROOMS &L) { //算法2.1 顺序表的初始化
	//构造一个空的顺序表L
	L.yrooms = new YROOM[ROOM_MAX]; //为顺序表分配一个大小为MAXSIZE的数组空间
	if (!L.yrooms)
		exit(OVERFLOW); //存储分配失败退出
	L.length = 0; //空表长度为0
	return OK;
}

Status InitList_USERS_Sq(USERS &L) { //算法2.1 顺序表的初始化
	//构造一个空的顺序表L
	L.users = new USER[USERS_MAX]; //为顺序表分配一个大小为MAXSIZE的数组空间
	if (!L.users)
		exit(OVERFLOW); //存储分配失败退出
	L.length = 0; //空表长度为0
	return OK;
}

//预约(往预约表里面添加一条数据)
Status Inser(YROOMS &yrooms, YROOM yroom){
	if (yrooms.length == ROOM_MAX)
		return ERROR; //当前存储空间已满
	yrooms.yrooms[yrooms.length] = yroom;
	yrooms.length++;
	return OK;
} 

Status InitList_ROOMS_Sq(ROOMS &L) { //算法2.1 顺序表的初始化
	//构造一个空的顺序表L
	L.rooms = new ROOM[ROOM_MAX]; //为顺序表分配一个大小为MAXSIZE的数组空间
	if (!L.rooms)
		exit(OVERFLOW); //存储分配失败退出
	L.length = 0; //空表长度为0
	return OK;
}

//显示 
void ShowYRooms(YROOMS yrooms){
	int i;
	cout << "用户名" << "\t" << "手机号" << "\t" << "房间号" << "\t" << "预约时间";
	cout << "\n"; 
	for (i = 0; i < yrooms.length; i++){
		cout << yrooms.yrooms[i].user_name << "\t" << yrooms.yrooms[i].mobile << "\t" << yrooms.yrooms[i].roomID << "\t" << yrooms.yrooms[i].ytime << "\n";
	}
}

Status FindRoomByRoomId(ROOMS rooms, char *roomID){
	int i;
	for(i = 0; i < rooms.length; i++){
		if(strcmp(rooms.rooms[i].roomID,roomID) == 0)
			return i;
	}
	return ERROR;
}

Status ReserveRoom(ROOMS rooms, YROOMS &yrooms, YROOM yroom){
	int index = FindRoomByRoomId(rooms,yroom.roomID);
	if(index && rooms.rooms[index].status == 0){
		Inser(yrooms,yroom);
		return OK;
	}
	return ERROR;
}

Status UpDateRoomStatus(ROOMS &rooms, char *roomID, int status, char *ytime, char *username, char *mobile){
	int index = FindRoomByRoomId(rooms,roomID);
	if(index){
		rooms.rooms[index].status = status;
		strcpy(rooms.rooms[index].ytime, ytime);
		strcpy(rooms.rooms[index].user_name, username);
		strcpy(rooms.rooms[index].mobile, mobile);
		return OK;
	}
	return ERROR;
	
}

void ShowRooms(ROOMS rooms){
	int i;
	cout << "房间号" << "\t" << "房间状态" << "\t" << "预约时间" << "\t" << "用户" << "\t" << "号码" << "\n";
	cout << "\n"; 
	for (i = 0; i < rooms.length; i++){
		cout << rooms.rooms[i].roomID<< "\t" << rooms.rooms[i].status << "\t" << rooms.rooms[i].ytime << "\t" << rooms.rooms[i].user_name << "\t" << rooms.rooms[i].mobile << "\n";
	}
}

double Settlement(OrderList *Q,sqList *L){                  //结账 
	double Price=0;
	int i=0,q=0,qq=0;
	while(qq<Q->length){
	while(q<L->length){
	if(strcmp(Q->elem[qq].FoodName,L->elem[q].FoodName)==0){
	Price=Price+L->elem[q].Price;
	}
	q++;
}
qq++;
q=0;
}
return Price;
} 




file__operation.cpp :

using namespace std;
#include<stdio.h>
#include<string>
#include "struch.h"

int Cread(SqList *L){
	int i = 0;
	FILE* fp;
	if((fp = fopen("CustomerPassword.txt","r"))==NULL){
		printf("打开失败了!");
		return 0;
	}
	while (!feof(fp)){
		fscanf(fp,"%s%s",L->elem[i].account,L->elem[i].password);
		i++;
		L->length++; 
	}
	fclose(fp);
	return 1;
}
int Sread(SqList *L){
	int i = 0;
	FILE* fp;
	if((fp = fopen("BusinessPassword.txt","r"))==NULL){
		printf("打开失败了!");
		return 0;
	}
	while (!feof(fp)){
		fscanf(fp,"%s%s",L->elem[i].account,L->elem[i].password);
		i++;
		L->length++; 
	}
	fclose(fp);
	return 1; 
}
int Foodread(sqList *Q){
	int i = 0;
	FILE *fp;
	if((fp = fopen("Food.txt","r"))==NULL){
	printf("读取失败啊!");
	return 0;}
	
	while(!feof(fp)){
		fscanf(fp,"%s%lf",Q->elem[i].FoodName,&Q->elem[i].Price);
		i++;
		Q->length++;
	}
	fclose(fp);
	return 1;
}
//int SqListRoomread(SqListRoom *L){
//	FILE *fp;
//	if((fp = fopen("Room.txt","r"))==NULL)
//}

Status read_rooms(ROOMS &L){
	
	int i=0;
	FILE* fp;
	if((fp = fopen("room.txt","r")) == NULL){
		puts("Fail to open file");
		return ERROR;
	}
	
	while(!feof(fp)){
		fscanf(fp, "%s%d%s%s%s\n", L.rooms[i].roomID,&L.rooms[i].status,L.rooms[i].ytime,L.rooms[i].user_name,L.rooms[i].mobile);
		//printf("%s %s %lf\n",L->elem[i].id,L->elem[i].name,L->elem[i].price);
		i++;
		L.length++;
	}

	
	fclose(fp);
	return OK;
}

//写数据 
Status write_rooms(ROOMS &L){
	
	int i=0;
	FILE* fp;
	if((fp = fopen("room.txt","wb")) == NULL){
		puts("Fail to open file");
		return ERROR;
	}
	
	for(; i < L.length; i++){
		fprintf(fp,"%s %d %s %s %s\n", L.rooms[i].roomID,L.rooms[i].status,L.rooms[i].ytime,L.rooms[i].user_name,L.rooms[i].mobile);
	}

	
	fclose(fp);
	return OK;
}

function_declare.h :

#ifndef __FUNCTION_H__
#define __FUNCTION_H__

Status CInitList_Sq(SqList &L);
int Cread(SqList *L);
//刷新账号表 
void CFprintf(SqList *L);
//查看口令
void PrintfPassword(SqList *L);
//注册
void Register(SqList *L);
//登录
int Login(SqList *L);
//提示 
void Tips(); 
int Sread(SqList *L);
//刷新账号表 
int Foodread(sqList *Q);
void SFprintf(SqList *L);
//显示菜单 
void PrintfFood(sqList *Q);
//商家添加菜品 
void AddFood(sqList *Q); 
Status FoodInitList_Sq(sqList &L);
void OrderPrintfFood(OrderList *Q);					 //用户查看点的菜 
void Order(OrderList *Q);                         //用户点菜的菜单
Status OrderInitList_Sq(OrderList &L);
Status FindByname(sqList Q,char* name);
Status deletefoods(sqList &Q,char *name);
void ShowBookByIndex(sqList Q,int i);
void ShowRooms(ROOMS rooms);
Status UpDateRoomStatus(ROOMS &rooms, char *roomID, int status, char *ytime, char *username, char *mobile);
Status ReserveRoom(ROOMS rooms, YROOMS &yrooms, YROOM yroom);
Status FindRoomByRoomId(ROOMS rooms, char *roomID);
void ShowYRooms(YROOMS yrooms);
Status Inser(YROOMS &yrooms, YROOM yroom);
Status InitList_YROOMS_Sq(YROOMS &L) ;
Status InitList_USERS_Sq(USERS &L);
Status read_rooms(ROOMS &L);
Status write_rooms(ROOMS &L);
Status InitList_ROOMS_Sq(ROOMS &L);
double Settlement(OrderList *Q,sqList *L);  //计算点菜的总价格 
#endif

由于该系统涉及到文件的操作,因此还需要在跟项目在同一目录下的几个文件:CustomerPassword.txt      

BusinessPassword.txt

Food.txt

room.txt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值