酒店点餐系统c语言版

来源:互联网 发布:豆瓣fm mac下载 编辑:程序博客网 时间:2024/05/01 11:29

因为使用了数据库,要安装数据库。

头文件:

ifndef __DLIST_H
#define __DLIST_H


/* This file is from Linux Kernel (include/linux/list.h)
* and modified by simply removing hardware prefetching of list items.
* Here by copyright, credits attributed to wherever they belong.
* Kulesh Shanmugasundaram (kulesh [squiggly] isis.poly.edu)
*/

/*
* Simple doubly linked list implementation.
*
* Some of the internal functions (“__xxx”) are useful when
* manipulating whole lists rather than single entries, as
* sometimes we already know the next/prev entries and we can
* generate better code by using them directly rather than
* using the generic single-entry routines.
*/
/**
 * container_of - cast a member of a structure out to the containing structure
 *
 * @ptr: the pointer to the member.
 * @type: the type of the container struct this is embedded in.
 * @member: the name of the member within the struct.
 *
 */
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)


#define container_of(ptr, type, member) ({ \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);\
        (type *)( (char *)__mptr - offsetof(type,member) );})
/*
 * These are non-NULL pointers that will result in page faults
 * under normal circumstances, used to verify that nobody uses
 * non-initialized list entries.
 */
#define LIST_POISON1  ((void *) 0x00100100)
#define LIST_POISON2  ((void *) 0x00200200)


struct list_head {
struct list_head *next, *prev;
};


#define LIST_HEAD_INIT(name) { &(name), &(name) }


#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)


#define INIT_LIST_HEAD(ptr) do { \
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)


/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}


/**
* list_add – add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}


/**
* list_add_tail – add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}


/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_del(struct list_head *prev, struct list_head *next)
{
next->prev = prev;
prev->next = next;
}


/**
* list_del – deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty on entry does not return true after this, the entry is in an undefined state.
*/
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = (void *) 0;
entry->prev = (void *) 0;
}


/**
* list_del_init – deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}


/**
* list_move – delete from one list and add as another’s head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static inline void list_move(struct list_head *list,
struct list_head *head)
{
__list_del(list->prev, list->next);
list_add(list, head);
}


/**
* list_move_tail – delete from one list and add as another’s tail
* @list: the entry to move
* @head: the head that will follow our entry
*/
static inline void list_move_tail(struct list_head *list,
struct list_head *head)
{
__list_del(list->prev, list->next);
list_add_tail(list, head);
}


/**
* list_empty – tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(struct list_head *head)
{
return head->next == head;
}


static inline void __list_splice(struct list_head *list,
struct list_head *head)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
struct list_head *at = head->next;


first->prev = head;
head->next = first;


last->next = at;
at->prev = last;
}


/**
* list_splice – join two lists
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice(struct list_head *list, struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head);
}


/**
* list_splice_init – join two lists and reinitialise the emptied list.
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* The list at @list is reinitialised
*/
static inline void list_splice_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head);
INIT_LIST_HEAD(list);
}
}


/**
* list_entry – get the struct for this entry
* @ptr:    the &struct list_head pointer.
* @type:    the type of the struct this is embedded in.
* @member:    the name of the list_struct within the struct.
*/
#define list_entry(ptr, type, member) \
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))


/**
* list_for_each    -    iterate over a list
* @pos:    the &struct list_head to use as a loop counter.
* @head:    the head for your list.
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); \
pos = pos->next)
/**
* list_for_each_prev    -    iterate over a list backwards
* @pos:    the &struct list_head to use as a loop counter.
* @head:    the head for your list.
*/
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; pos != (head); \
pos = pos->prev)


/**
* list_for_each_safe    -    iterate over a list safe against removal of list entry
* @pos:    the &struct list_head to use as a loop counter.
* @n:        another &struct list_head to use as temporary storage
* @head:    the head for your list.
*/
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)


/**
* list_for_each_entry    -    iterate over list of given type
* @pos:    the type * to use as a loop counter.
* @head:    the head for your list.
* @member:    the name of the list_struct within the struct.
*/
#define list_for_each_entry(pos, head, member)                \
for (pos = list_entry((head)->next, typeof(*pos), member);    \
&pos->member != (head);                     \
pos = list_entry(pos->member.next, typeof(*pos), member))


/**
* list_for_each_entry_safe – iterate over list of given type safe against removal of list entry
* @pos:    the type * to use as a loop counter.
* @n:        another type * to use as temporary storage
* @head:    the head for your list.
* @member:    the name of the list_struct within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member)            \
for (pos = list_entry((head)->next, typeof(*pos), member),    \
n = list_entry(pos->member.next, typeof(*pos), member);    \
&pos->member != (head);                     \
pos = n, n = list_entry(n->member.next, typeof(*n), member))


#endif

服务端代码:

#include <stdio.h>

#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <unistd.h>
#include <sqlite3.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>
#include <error.h>
#include "dlist.h"

struct node_data{
int site;
int sign;
int sock_fd;
int id[100];
int total;
};


typedef struct node{
struct node_data data;
struct list_head list;
}server, *pserver;


#define SQL_SIZE 128
char sql[SQL_SIZE] = {'\0'};
sqlite3 *db_menu;//菜单数据库
sqlite3 *db_bd;//运营数据库
char *errmsg;


void menu_add(void);
void store_mod(void);
void menu_query(void);
void menu_del(void);
void quer_bd(void);
void *sock_write(void *arg);


void *sock_write(void *arg)
{
int i;


while(1){
printf("******************************\n");
printf("\t1:—>\t\t添加新菜品\n");
printf("\t2:—>\t\t删除旧菜品\n");
printf("\t3:—>\t\t查询菜品\n");
printf("\t4:—>\t\t修改菜品信息\n");
printf("\t5:—>\t\t统计运营额\n");
printf("******************************\n");
scanf("%d",&i);
switch(i){
case 1:
menu_add();
break;
case 2:
menu_del();
break;
case 3:
menu_query();
break;
case 4:
store_mod();
break;
case 5:
quer_bd();
break;
}
}
return;
}


void quer_bd(void)
{
int ret;
int row;
int i;
int colum;
char **result;


sprintf(sql, "select * from bdo");
ret = sqlite3_get_table(db_menu, sql, &result, &row, &colum, &errmsg);
if(SQLITE_OK != ret){
printf("insert:%s\n", errmsg);
exit(1);
}
for(i=0 ; i<(row+1)*colum; i++){
if(0 == i%3){
printf("\n");
}
printf("%s\t",result[i]);
}
printf("\n");
}


void store_mod(void)
{
int i;
int ret;
int p;
int s;
char n[20];


printf("请输入你要更新菜名的id\n");
scanf("%d", &i);
printf("请输入你要更改的信息price, stock\n");
scanf("%d%d", &p, &s);
while(getchar() != '\n');
printf("请输入要改的名字\n");
gets(n);
sprintf(sql, "update menu set name = '%s', price = %d, stock = %d where id = %d", n, p, s, i);
ret = sqlite3_exec(db_menu, sql, NULL, NULL, &errmsg);
if(SQLITE_OK != ret){
printf("insert:%s\n", errmsg);
exit(1);
}
printf("更改成功\n");
}


void menu_query(void)
{
int ret;
int row;
int colum;
int i;
char **result;


sprintf(sql, "select * from menu");
ret = sqlite3_get_table(db_menu, sql, &result, &row, &colum, &errmsg);
if(SQLITE_OK != ret){
printf("insert:%s\n", errmsg);
exit(1);
}
for(i=0 ; i<(row+1)*colum; i++){
if(0 == i%4){
printf("\n");
}
printf("%s\t",result[i]);
}
printf("\n");
}


void menu_add(void)
{
int ret;
int i;
char n[20];
int p;
int s = 99;
printf("请给菜的id,price输入\n");
scanf("%d%d", &i, &p);
while(getchar() != '\n');
printf("请给菜取名\n");
gets(n);


sprintf(sql, "insert into menu(id, name, price, stock) values(%d, '%s', %d, %d)", i, n, p, s);
ret = sqlite3_exec(db_menu, sql, NULL, NULL, &errmsg);
if(SQLITE_OK != ret){
printf("insert:%s\n", errmsg);
exit(1);
}
printf("保存成功\n");
}


void menu_del(void)
{
int id;
int ret;
int i;


printf("请输入要删除菜名的id\n");
scanf("%d",&i);
sprintf(sql, "delete from menu where id = %d", i);
ret = sqlite3_exec(db_menu, sql, NULL, NULL, &errmsg);
if(SQLITE_OK != ret){
printf("insert:%s\n", errmsg);
exit(1);
}
printf("删除成功\n");
}


int main(void)
{
int tem;
int ret;
int fd;
int i;
int j;
int k;
int w = 0;
int d = 0;
int h = 0;
int obj = 0;


fd_set read_fd;
char buf[BUFSIZ];


int row;
int colum;
char **result = NULL;


pserver head;
pserver p;
pserver new;
pserver q;


int bind_fd;
struct sockaddr_in sock_add;
int maxfd;
tem = sizeof(sock_add);


pthread_t tid_write;
pthread_attr_t attr;


/*建立2个数据库,分别为menu与bdo*/
sqlite3_open("./menu.db", &db_menu);
sprintf(sql, "create table menu(id int, name varchar[20], price int, stock int)");
ret = sqlite3_exec(db_menu, sql, NULL, NULL, &errmsg);
if(SQLITE_OK != ret){
printf("insert:%s\n", errmsg);
}
sprintf(sql, "create table bdo(id varchar[4],name varchar[20], price varchar[4])");
ret = sqlite3_exec(db_menu, sql, NULL, NULL, &errmsg);
if(SQLITE_OK != ret){
printf("insert:%s\n", errmsg);
}


/*创建一个分离线程*/
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
ret = pthread_create(&tid_write, &attr, (void *)sock_write, NULL);
if(ret < 0){
perror("pthread_create");
exit(1);
}


/*链表的建立与给对应的位赋值*/
head = (pserver )malloc(sizeof(server));
if(NULL == head){
list_for_each_entry(p, &head->list, list);
perror("malloc");
exit(1);
}
INIT_LIST_HEAD(&head->list);


for(i=0; i<50; i++){//链表建立
new = (pserver )malloc(sizeof(server));
if(NULL == new){
perror("malloc");
exit(1);
}
new->data.site = i+1;
new->data.sign = 0;
new->data.sock_fd = 0;
list_add(&new->list, &head->list);
}
#if 0 
struct list_head *pos, *n;
pserver tmp;
list_for_each_safe(pos, n, &(head->list)){
tmp = list_entry(pos, server, list);
printf("data.site:%d\t", tmp->data.sign);
}
printf("\n");
#endif
/*网络初始化与绑定ip、端口*/
bzero(&sock_add, tem);
sock_add.sin_family = AF_INET;
sock_add.sin_port = htons(10000);
sock_add.sin_addr.s_addr = inet_addr("192.168.0.245");


bind_fd = socket(PF_INET, SOCK_STREAM, 0);
if(bind_fd < 0){
perror("socket");
exit(1);
}


int on = 1;
setsockopt(bind_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));


ret = bind(bind_fd, (struct sockaddr *)&sock_add, tem);
if(ret < 0){
perror("bind");
exit(1);
}


maxfd = bind_fd;
ret = listen(bind_fd, 5);
if(ret < 0){
perror("listen");
exit(1);
}


/*多路复用*/
while(1){
FD_ZERO(&read_fd);
FD_SET(bind_fd, &read_fd);
bzero(buf, BUFSIZ);
p = head;
list_for_each_entry(p, &head->list, list){//设置select
if(p->data.sign != 0){
FD_SET(p->data.sock_fd, &read_fd);
if(maxfd < p->data.sock_fd){
maxfd = p->data.sock_fd;
}
}
}
ret = select(maxfd+1, &read_fd, NULL, NULL, NULL);
if(ret < 0){
perror("select");
exit(1);
}
else if(FD_ISSET(bind_fd, &read_fd)){
fd = accept(bind_fd, NULL, NULL);
if(p->data.sock_fd < 0){
perror("accept");
exit(1);
}
p = head;
list_for_each_entry(p, &head->list, list){
if(0 == p->data.sign){
p->data.sock_fd = fd;
p->data.sign = 1;
break;
}
}
}


p = head;
list_for_each_entry(p, &head->list, list){//找对应的fd
if(FD_ISSET(p->data.sock_fd, &read_fd)){
bzero(buf, BUFSIZ);
ret = read(p->data.sock_fd, buf, BUFSIZ);
if(ret < 0){
perror("read");
exit(1);
}else if(0 == ret){//处理客户端关闭
i = p->data.site;
close(p->data.sock_fd);
bzero(&p->data, sizeof(p->data));
p->data.site = i;
}else{
if(0 == strncmp(buf, "order", 5)){//点菜
bzero(buf, BUFSIZ);
obj = 0;
sprintf(sql, "select * from menu where stock > 0");
ret = sqlite3_get_table(db_menu, sql, &result, &row, &colum, &errmsg);
if(SQLITE_OK != ret){
printf("insert:%s\n", errmsg);
exit(1);
}


for(i=0 ; i<(row+1)*colum; i++){//处理菜单发送到客户端
if((i%4 == 0)&& (i != 0)){
sprintf((buf+obj), "%s", "\n");
obj += 1;
sprintf((buf + obj), "%s", result[i]);
obj += strlen(result[i]);
}else{
if(0 == i){
sprintf((buf + obj), "%s", result[i]);
obj += strlen(result[i]);
}else{
sprintf((buf+obj), "%s","\t");
obj += 1;
sprintf((buf + obj), "%s", result[i]);
obj += strlen(result[i]);
}
}
}
write(p->data.sock_fd, buf, strlen(buf));
}else if(0 == strncmp(buf, "shout", 5)){//呼叫服务员
printf("\t第%d桌:呼叫服务员\n", p->data.site);
strcpy(buf, "请稍等");
write(p->data.sock_fd, buf, strlen(buf));
}else if(0 == strncmp(buf, "check", 5)){//买单
bzero(buf, BUFSIZ);
strcpy(buf, (char *)&p->data.total);
write(p->data.sock_fd, buf, strlen(buf));
p->data.sign = 0;
p->data.total = 0;
}else if(0 == strncmp(buf, "place", 5)){//下单
bzero(buf, BUFSIZ);
read(p->data.sock_fd, buf, BUFSIZ);
for(j=0; j<BUFSIZ; j++){//一次打印一个菜的信息
p->data.id[j] = (int )*(buf + j);
if(p->data.id[j] != 0){
sprintf(sql, "select * from menu where id = %d and stock > 0", p->data.id[j]);
result = NULL;
ret = sqlite3_get_table(db_menu, sql, &result, &row, &colum, &errmsg);


if(SQLITE_OK != ret){
printf("insert1:%s\n", errmsg);
exit(1);
}
if((0 == row)){//没有该id的菜
bzero(buf, BUFSIZ);
strcpy(buf, "没有您要点的菜的id=");
sprintf(buf+strlen(buf), "%d\n", p->data.id[j]);
write(p->data.sock_fd, buf, strlen(buf));
break;
}
for(k=0 ; k<(row+1)*colum; k++){//打印菜单给后厨
if(k%colum == 0){
printf("\n");
}
printf("%s\t", result[k]);
}
printf("\n");
k = atoi(result[7]);//转化菜的库存
sprintf(sql, "update menu set stock = %d where id = %d", --k, p->data.id[j]);
ret = sqlite3_exec(db_menu, sql, NULL, NULL, &errmsg);//把所点的菜的库存减1
if(SQLITE_OK != ret){
printf("insert2:%s\n", errmsg);
exit(1);
}
sprintf(sql, "insert into bdo(id, name, price) values('%s', '%s', '%s')", result[4], result[5], result[6]);
ret = sqlite3_exec(db_menu, sql, NULL, NULL, &errmsg);//把所点的菜的信息放到营运数据库中
if(SQLITE_OK != ret){
printf("insert3:%s\n", errmsg);
exit(1);
}
h = atoi(result[6]);//转化菜的价格
w += h;//把菜的价格相加
}else{
break;//当菜的id为0时,返回
}
}//for()
p->data.total += w;//把菜的价格放入该桌信息链表total中
printf("第%d号桌\t合计:%d\n", p->data.site, p->data.total);
w = 0;
if(0 == strncmp(buf, "没有", 2)){//没有改菜的id时返回
break;
}else{//找到菜的信息时,表示下单成功
bzero(buf, BUFSIZ);
strcat(buf, "订单成功!");
write(p->data.sock_fd, buf, strlen(buf));
}
}//elseif
}//if
}//else
}//list_for
}//while
sqlite3_close(db_menu);
return 0;

}

客户端代码:

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>


int sock_fd;
char buf[BUFSIZ];


void call_waiter(void);
void check_out(void);
void place_orader(void);
void order_dishes(void);


int main(void)
{
int i;
int ret;
struct sockaddr_in sock_add;


bzero(&sock_add, sizeof(sock_add));
sock_add.sin_family = AF_INET;
sock_add.sin_port = htons(10000);
sock_add.sin_addr.s_addr = inet_addr("192.168.0.245");


sock_fd = socket(PF_INET, SOCK_STREAM, 0);
if(sock_fd < 0){
perror("socket");
exit(1);
}


ret = connect(sock_fd, (struct sockaddr *)&sock_add, sizeof(sock_add));
if(ret < 0){
perror("connect");
exit(1);
}


while(1){
printf("***********************************\n");
printf("\t1:->\t点菜\n");
printf("\t2:->\t下单\n");
printf("\t3:->\t结账\n");
printf("\t4:->\t呼叫服务员\n");
printf("***********************************\n");
do{
ret = scanf("%d", &i);
while(getchar() != '\n');
}while(ret != 1);
switch(i){
case 1:
order_dishes();//点菜
break;
case 2:
place_orader();//下单
break;
case 3:
check_out();//结账
exit(0);
case 4:
call_waiter();//呼叫服务员
break;
}
}
return; 
}


void order_dishes(void)//点菜
{
int ret=0;
bzero(buf, BUFSIZ);
strcpy(buf, "order");
write(sock_fd, buf, strlen(buf));
bzero(buf, BUFSIZ);
ret = read(sock_fd, buf, BUFSIZ);
printf("%s\n", buf);
}


void place_orader(void)//下单
{
char bufl[BUFSIZ];
int id[100];
int i = 0;
int j;
printf("请在点完菜输入1001\n");
do{
scanf("%d",&id[i]);
i++;
}while(id[i-1] != 1001);

bzero(bufl, BUFSIZ);
strcpy(bufl, "place");
write(sock_fd, bufl, strlen(bufl));
sleep(1);
bzero(buf, BUFSIZ);
for(j=0; j<i-1; j++ ){
printf("%d\n", id[j]);
strcat(buf, (char *)&id[j]);
}
write(sock_fd, buf, strlen(buf));
bzero(buf, BUFSIZ);
read(sock_fd, buf, BUFSIZ);
printf("%s\n", buf);
}


void check_out(void)//结账
{
bzero(buf, BUFSIZ);
strcpy(buf, "check");
write(sock_fd, buf, strlen(buf));
bzero(buf, BUFSIZ);
read(sock_fd, buf, BUFSIZ);
printf("\t\t总计:%d\n", *(int *)buf);
}


void call_waiter(void)//呼叫服务员
{
bzero(buf, BUFSIZ);
strcpy(buf, "shout");
write(sock_fd, buf, strlen(buf));
bzero(buf, BUFSIZ);
read(sock_fd, buf, BUFSIZ);
printf("\t%s\n", buf);
}

0 0
原创粉丝点击