#include<stdio.h>
#include<string.h>
#include"mysql.h"
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<string>
using namespace std;
#define _HOST_ "127.0.0.1"
#define _DB_ "db1"
char _USER_[30];
char _PASSWD_[30];
MYSQL *mysql=NULL;
bool Q_break();
void Welcome();
void welcome_boss();
void Show_lin(unsigned long *len,unsigned int num);
void Query_all(char *ptr);
void Quary_pt(char *ptr);
void Quary_menu(char *ptr);
void Order_food(char *ptr);
void Show_order(char *ptr);
void Delete_ord(char *ptr);
void Pay_money(char *ptr);
void Deboss_order(char *ptr);
void Add_order(char *ptr);
void Login();
void Use_database();
void Wait_order(char *ptr);
bool Q_break()
{
char ans=getc(stdin);
while(ans=='\n'||ans==' ') ans=getc(stdin);
ungetc(ans,stdin);
if(ans=='q'||ans=='Q') getchar();
return ans=='q'||ans=='Q';
}
void Welcome()
{
system("clear");
for(int i=1;i<=8;i++) puts("");
for(int i=1;i<=25;i++) printf(" ");
printf("Welcome to my restaurant\n");
for(int i=1;i<=25;i++) printf(" ");
printf("0------------------->菜单\n");
for(int i=1;i<=25;i++) printf(" ");
printf("1------------------->订餐\n");
for(int i=1;i<=25;i++) printf(" ");
printf("2------------------->订单\n");
for(int i=1;i<=25;i++) printf(" ");
printf("3------------------->取消\n");
for(int i=1;i<=25;i++) printf(" ");
printf("4------------------->支付\n");
for(int i=1;i<=25;i++) printf(" ");
printf("5------------------->未出\n");
for(int i=1;i<=25;i++) printf(" ");
printf("q------------------->退出\n");
for(int i=1;i<=25;i++) printf(" ");
}
void welcome_boss()
{
system("clear");
for(int i=1;i<=9;i++) puts("");
for(int i=1;i<=25;i++) printf(" ");
printf("0------------------->查看订单\n");
for(int i=1;i<=25;i++) printf(" ");
printf("1------------------->配送订单\n");
for(int i=1;i<=25;i++) printf(" ");
printf("2------------------->加入菜品\n");
for(int i=1;i<=25;i++) printf(" ");
printf("3------------------->查看菜单\n");
for(int i=1;i<=25;i++) printf(" ");
printf("q------------------->退出查询\n");
for(int i=1;i<=25;i++) printf(" ");
}
void Show_lin(unsigned long *len,unsigned int num)
{
printf("+");
for(int i=0;i<num;i++)
{
for(int j=1;j<=len[i];j++) printf("-");
printf("+");
}
puts("");
}
void Query_all(char *ptr)
{
if(mysql_query(mysql,ptr))
{
printf("mysql quary err\n");
mysql_close(mysql);
exit(0);
}
}
void Quary_pt(char *ptr)
{
for(int i=1;i<=9;i++) puts("");
Query_all(ptr);
MYSQL_RES *result = mysql_store_result(mysql);
MYSQL_FIELD *fileds;
MYSQL_ROW row;
unsigned int num_fields;
unsigned long lengths[512];
unsigned long * nowlen;
if(result!=NULL)
{
num_fields=mysql_num_fields(result);
fileds=mysql_fetch_fields(result);
for(int i=0;i<num_fields;i++)
lengths[i]=fmax(fileds[i].name_length,fileds[i].max_length);
for(int i=1;i<=25;i++) printf(" ");
Show_lin(lengths,num_fields);
for(int i=1;i<=25;i++) printf(" ");
for(int i=0;i<num_fields;i++)
{
printf("|");
printf("%s",fileds[i].name);
int con=0;
for(int j=0;j<fileds[i].name_length;j++)
if(((long long)fileds[i].name[j])<=0) con++;
con/=3;
for(int j=1;j<=lengths[i]-fileds[i].name_length+con;j++) printf(" ");
}puts("|");
for(int i=1;i<=25;i++) printf(" ");
Show_lin(lengths,num_fields);
bool flag=0;
while(row=mysql_fetch_row(result))
{
for(int i=1;i<=25;i++) printf(" ");
flag=1;
nowlen=mysql_fetch_lengths(result);
for(int i=0;i<num_fields;i++)
{
printf("|");
printf("%s",row[i]);
int con=0;
for(int j=0;j<nowlen[i];j++)
if(((long long)row[i][j])<=0) con++;
con/=3;
for(int j=1;j<=lengths[i]-nowlen[i]+con;j++) printf(" ");
}puts("|");
}
if(flag)
{
for(int i=1;i<=25;i++) printf(" ");
Show_lin(lengths,num_fields);
}
for(int i=1;i<=25;i++) printf(" ");
mysql_free_result(result);
getchar();
getchar();
}
else
{
if(mysql_field_count(mysql))
{
printf("mysql qurry err\n");
exit(0);
}
}
return ;
}
void Quary_menu(char *ptr)
{
strcpy(ptr,"select id as 编号,name as 菜品,price as 价格 from menu;");
system("clear");
Quary_pt(ptr);
}
void Order_food(char *ptr)
{
Quary_menu(ptr);
int con=0;
while(1)
{
con++;
int id;
for(int i=1;i<=25;i++) printf(" ");
cout<<con<<"、"<<"编号:";
if(Q_break()) break;
cin>>id;
puts("");
sprintf(ptr,"insert into Orders(id,username,pay) values(%d,'%s',0);",id,_USER_);
Query_all(ptr);
}
}
void Show_order(char *ptr)
{
system("clear");
sprintf(ptr,"select name as 菜品,price as 单价,count(pay) as 数量 from menu,Orders where username='%s' and pay=0 and Orders.id=menu.id group by menu.id;",_USER_);
Quary_pt(ptr);
}
void Wait_order(char *ptr)
{
system("clear");
sprintf(ptr,"select name as 菜品,count(pay) as 数量 from menu,Orders where username='%s' and pay=1 and Orders.id=menu.id group by name;",_USER_);
Quary_pt(ptr);
}
void Delete_ord(char *ptr)
{
system("clear");
sprintf(ptr,"delete from Orders where username='%s' and pay=0;",_USER_);
Query_all(ptr);
}
void Pay_money(char *ptr)
{
system("clear");
sprintf(ptr,"select sum(price) as 总价 from menu,Orders where username='%s' and pay=0 and Orders.id=menu.id;",_USER_);
Quary_pt(ptr);
for(int i=1;i<=25;i++) printf(" ");
cout<<"确认:";
char y;
y=getchar();
if(y!='y'&&y!='Y') return ;
puts("");
sprintf(ptr,"update Orders set pay=1 where username='%s';",_USER_);
Query_all(ptr);
}
void Shboss_order(char *ptr)
{
system("clear");
strcpy(ptr,"select menu.id as 编号,username as 用户 ,name as 菜品,count(pay) as 数量 from Orders,menu where Orders.id=menu.id and pay=1 group by username,menu.id;");
Quary_pt(ptr);
}
void Deboss_order(char *ptr)
{
system("clear");
Shboss_order(ptr);
int id;
char user_name[30];
while(1)
{
for(int i=1;i<=25;i++) printf(" ");
cout<<"编号:";
if(Q_break()) break;
cin>>id;
puts("");
for(int i=1;i<=25;i++) printf(" ");
cout<<"用户:";
cin>>user_name;
puts("");
sprintf(ptr,"DELETE FROM Orders where Orders.username='%s' and Orders.id=%d",user_name,id);
Query_all(ptr);
}
}
void Add_order(char *ptr)
{
int hv=0;
system("clear");
strcpy(ptr,"select count(1) from menu;");
Query_all(ptr);
MYSQL_RES *result = mysql_store_result(mysql);
MYSQL_ROW row;
unsigned int num_fields;
if(result!=NULL)
{
row=mysql_fetch_row(result);
int len=strlen(row[0]);
for(int i=len-1;i>=0;i--) hv=hv*10+row[0][i]-'0';
hv++;
mysql_free_result(result);
}
else
{
if(mysql_field_count(mysql))
{
printf("mysql qurry err\n");
exit(0);
}
}
char order_name[30];
int order_price;
for(int i=1;i<=11;i++) puts("");
for(int i=1;i<=32;i++) printf(" ");
cout<<"菜名:";
if(Q_break()) return ;
cin>>order_name;
puts("");
for(int i=1;i<=32;i++) printf(" ");
cout<<"价格:";
cin>>order_price;
sprintf(ptr,"insert into menu(id,name,price) values(%d,'%s',%d);",hv,order_name,order_price);
Query_all(ptr);
}
void Login()
{
system("clear");
for(int i=1;i<=11;i++) puts("");
for(int i=1;i<=32;i++) printf(" ");
printf("id: ");
scanf("%s",_USER_);
puts("");
for(int i=1;i<=28;i++) printf(" ");
printf("passwd: ");
system("stty -echo");
scanf("%s",_PASSWD_);
system("stty echo");
system("clear");
mysql=mysql_init(NULL);
if(mysql==NULL)
{
printf("mysql init err\n");
exit(0);
}
mysql=mysql_real_connect(mysql,_HOST_,_USER_,_PASSWD_,_DB_,0,NULL,0);
if(mysql==NULL)
{
printf("mysql connect err\n");
exit(0);
}
mysql_set_character_set(mysql,"utf8");
}
void Use_database()
{
system("clear");
char ptr[512];
if(strcmp(_USER_,"root")==0)
{
while(1)
{
welcome_boss();
int ci;
if(Q_break()) break;
scanf("%d",&ci);
if(ci==0) Shboss_order(ptr);
else if(ci==1) Deboss_order(ptr);
else if(ci==2) Add_order(ptr);
else if(ci==3) Quary_menu(ptr);
}
}
else while(1)
{
Welcome();
memset(ptr,0,sizeof(ptr));
int ci;
if(Q_break()) break;
scanf("%d",&ci);
if(ci==0) Quary_menu(ptr);
else if(ci==1) Order_food(ptr);
else if(ci==2) Show_order(ptr);
else if(ci==3) Delete_ord(ptr);
else if(ci==4) Pay_money(ptr);
else if(ci==5) Wait_order(ptr);
}
}
int main()
{
system("stty echo");
Login();
Use_database();
mysql_close(mysql);
return 0;
}
界面如下,管理员可以加如菜品,不支持删除菜品哈哈,有些操作需要看代码部分(比如输入y确认)。。。
|