|
超市會員信息管理系統(tǒng)
- #include <stdio.h> //編譯預(yù)處理指令,引入頭文件
- #include <stdlib.h>
- #include <string.h>
- #include <conio.h>
- #define MAX 10 //自定義符號常量
- #define MENU_MAIN_COUNT 7 //主菜單的選項(xiàng)個數(shù)
- typedef struct member //定義結(jié)構(gòu)體類型member,采用鏈?zhǔn)酱鎯Y(jié)構(gòu)
- {
- char name[MAX]; //姓名
- char id[MAX]; //卡號
- char sex[2]; //性別
- int age; //年齡
- float money; //余額
- char tel[12]; //電話
- struct member *next; //定義一個指針指向下一個會員信息
- float cost; //消費(fèi)金額
- }mem; //定義結(jié)構(gòu)體變量mem
- /*聲明函數(shù)*/
- void menu(); /*聲明函數(shù),聲明菜單函數(shù)*/
- void save(mem *p); //聲明保存函數(shù)
- mem *head=NULL; //頭指針為NULL
- mem* get_last(mem *p) //取得鏈表最后一個元素的節(jié)點(diǎn)指針并返回該指針
- {
- if(p->next == NULL)
- {
- return p;
- }
- else
- {
- get_last(p->next);
- }
- }
- void creat_member(mem *pNew) //輸入信息操作,會員登記函數(shù)
- { //創(chuàng)建一個新的會員
- char s;
- printf("卡號:"); scanf("%s",pNew->id); //運(yùn)用指針輸入卡號
- printf("請輸入姓名: "); scanf("%s",pNew->name); //運(yùn)用指針輸入姓名
- a:
- printf("請輸入性別(f--女m--男): "); getchar(); //輸入男女
- s = getchar();
- if(s=='f'||s=='m') //判斷是 男 是 女
- {
- if (s == 'f' || s == 'F') strcpy(pNew->sex,"女");
- if (s == 'M' || s == 'm') strcpy(pNew->sex,"男");
- }
- else //如果輸入錯誤
- {
- printf("輸入錯誤,請重新輸入...\n");
- goto a; //跳轉(zhuǎn)至a,重新輸入
- }
- printf("請輸入年齡: "); scanf("%d",&(pNew->age)); //運(yùn)用指針輸入年齡
- printf("請輸入繳費(fèi)金額: "); scanf("%f",&(pNew->money)); //運(yùn)用指針輸入余額
- printf("請輸入電話: "); scanf("%s",pNew->tel); //運(yùn)用指針輸入電話
- pNew->cost=0; //運(yùn)用指針初始化消費(fèi)額為0
- printf("\n創(chuàng)建完畢!\n");
- }
- void newMember() //會員登記函數(shù)
- { //添加會員信息
- char con='N';
- mem *pLast= NULL;
- mem *pNew=(mem *)malloc(sizeof(mem)); //按 mem 動態(tài)分配內(nèi)存
- pNew->next=NULL;
- creat_member(pNew); //調(diào)用會員登記函數(shù)
- if(head == NULL)
- {
- head = pNew;
- }
- else
- {
- pLast = get_last(head);
- pLast->next = pNew;
- }
- printf("繼續(xù)輸入會員信息?(Y 繼續(xù), N 返回菜單)"); //判斷是否繼續(xù)登記
- getchar();
- con = getchar();
- if (con == 'n' || con == 'N')
- {
- save(pNew);
- menu();
- }
- else{
- save(pNew);
- newMember();
- }
- }
-
- //顯示信息操作
- void table_head()
- { //定義表頭
- printf("+----------+------+------+------+--------+--------+-----------+\n");
- printf("| 卡號 | 姓名 | 性別 | 年齡 | 余額 |累計(jì)消費(fèi)| 電話 |\n");
- printf("+----------+------+------+------+--------+--------+-----------+\n");
- }
- void table_buttom()
- { //定義底線
- printf("+----------+------+------+------+--------+--------+-----------+\n");
- }
- void table_row(mem *p)
- { // 定義中間部分
- printf("|%10s|%6s|%6s|%6d|%8.2f|%8.2f|%11s|\n",
- p->id,p->name,p->sex,p->age,p->money,p->cost,p->tel);
- }
- void display(mem *p)
- { //顯示所有信息
- p = head;
- table_head(); //調(diào)用表頭
- while(p != NULL)
- {
- table_row(p); //調(diào)用中間部分
- p = p->next;
- }
- table_buttom(); //調(diào)用底線
- }
- void display_one(mem *p)
- { //只顯示一條信息
- table_head();
- table_row(p);
- table_buttom();
- }
- void table_sum()
- { //定義總計(jì)
- printf("+----------+------+------+------+--------+--------+-----------+\n");
- printf("| | | | | 總計(jì) | 總計(jì) | |\n");
- printf("+----------+------+------+------+--------+--------+-----------+\n");
- }
- void table_r(float money,float cost)
- { //定義總計(jì)2
- printf("| | | | |%8.2f|%8.2f| |\n",money,cost);
- }
- void display_statistics(mem *p,float money,float cost)
- { //顯示統(tǒng)計(jì)信息函數(shù)
- p = head;
- table_head(); //調(diào)用表頭
- while(p != NULL)
- {
- table_row(p); //定義中間部分
- p = p->next;
- }
- p=head;
- table_sum(); //定義總計(jì)
- table_r(money,cost); //定義總計(jì)2
- table_buttom(); //定義表尾
- } //文件相關(guān)操作
- void save(mem *p)
- { //保存函數(shù)
- FILE *fp; //定義一個指向文件的指針變量
- fp=fopen("member.dat", "a"); //將fopen函數(shù)的返回值賦給指針變量fp
- while(p != NULL)
- {
- fprintf(fp, "%s %s %s %d %f %s %d\n",p->id, p->name, p->sex, p->age, p->money, p->tel,p->cost);
- p = p->next;
- }
- fclose(fp); //關(guān)閉指針
- getchar();
- }
- void modify_file(mem *p)
- { //保存修改會員信息函數(shù)
- FILE *fp;
- p= head;
- if((fp=fopen("member.dat", "w"))==NULL)
- { //檢查打開文件的操作是否有錯
- printf("文件不存在");
- getchar();
- menu(); //調(diào)用菜單函數(shù)
- }
- while(p != NULL)
- {
- fprintf(fp, "%s %s %s %d %f %s %f\n",p->id, p->name, p->sex, p->age, p->money, p->tel,p->cost);
- p = p->next;
- }
- fclose(fp);
- getchar();
- }
- void load(mem *p)
- { //從文件加載信息
- FILE *fp;
- mem *pLast = NULL;
- head=NULL;
- if((fp=fopen("member.dat","r"))==NULL)
- { //檢查打開文件的操作是否有錯
- printf("沒有會員信息,請錄入!");
- getch();
- menu(); //調(diào)用菜單函數(shù)
- }
- while(!feof(fp))
- { //文件指針是否到達(dá)末尾
- mem *pNew = (mem *)malloc(sizeof(mem)); //按 mem 動態(tài)分配內(nèi)存
- pNew->next = NULL;
- fscanf(fp, "%s %s %s %d %f %s %f\n",
- pNew->id, pNew->name, pNew->sex, &(pNew->age), &(pNew->money), pNew->tel,&(pNew->cost));
- if(head == NULL)
- {
- head = pNew;
- }
- else
- {
- pLast = get_last(head);
- pLast->next = pNew;
- }
- }
- p = head;
- fclose(fp);
- }
- //通過卡號搜索信息函數(shù)
- mem* search_id(mem *p)
- { //通過卡號搜索信息
- char id[MAX];
- int i=0;
- p = head;
- scanf("%s",id);
- while(p&&(strcmp(id,"n")!=0))
- { //判斷id是否不為n
- if(strcmp(p->id,id)==0)
- { //找到
- i=1;
- break;
- }
- else
- p=p->next;
- }
- if(strcmp(id,"n")==0) //輸入n時返回菜單
- menu();
- if(p==NULL)
- { //重新搜索卡號
- printf("您查找的卡號不存在,請重新輸入:\n");
- p=search_id(p); //調(diào)用尋找函數(shù)
- }
- else
- return p; //返回p
- }
- /*修改會員信息操作*/
- void modify_m()
- { //修改會員的信息
- char s,fun='y';
- char pro[] =
- "|\n"
- "| 1 姓名\n"
- "| 2 性別\n"
- "| 3 年齡\n"
- "| 4 電話\n"
- "| 5 返回\n"
- "|\n";
- mem *p;
- load(p);
- display(p); //調(diào)用顯示函數(shù)
- printf("請輸入需要修改信息的會員卡號(n返回菜單):");
- p=search_id(p); //調(diào)用按號尋找函數(shù)
- while(fun=='y'||fun=='Y')
- { //當(dāng)繼續(xù)尋找時
- system("cls"); //清屏
- display_one(p); //調(diào)用顯示一條函數(shù)
- printf("請選擇修改的項(xiàng)目:\n");
- printf(pro); //輸出pro
- getchar();
- scanf("%c",&fun);
- switch(fun)
- { //用switch語句選擇修改內(nèi)容
- case '1': printf("請輸入姓名: "); scanf("%s",p->name);break;
- case '2': a:
- printf("請輸入性別(f--女m--男): "); getchar();
- s = getchar();
- if(s=='f'||s=='m')
- {
- if (s == 'f' || s == 'F') strcpy(p->sex,"女");
- if (s == 'M' || s == 'm') strcpy(p->sex,"男");
- }
- else
- {
- printf(">輸入錯誤,請重新輸入...\n");
- goto a;
- }
- break;
- case '3': printf("請輸入年齡: "); scanf("%d",&(p->age));break;
- case '4': printf("請輸入電話: "); scanf("%s",p->tel); break;
- default : break;
- }
- printf("是否繼續(xù)修改Y/N?"); //判斷是否繼續(xù)尋找
- getchar();
- fun=getchar();
- }
- modify_file(p); //調(diào)用保存修改會員信息函數(shù)
- system("cls"); //清屏
- display_one(p); //調(diào)用顯示一條函數(shù)
- printf("\n修改成功,按任意鍵繼續(xù)!");
- getchar();
- menu(); //調(diào)用菜單函數(shù)
- }
- //會員續(xù)費(fèi)函數(shù)
- void add()
- { //會員續(xù)費(fèi)
- float money;
- mem *p;
- load(p);
- display(p); //調(diào)用瀏覽函數(shù)
- printf("\n");
- printf("請輸入需要續(xù)費(fèi)的會員卡號(n返回菜單):");
- p=search_id(p); //調(diào)用按號尋找函數(shù)
- system("cls"); //清屏
- display_one(p); //調(diào)用顯示一條函數(shù)
- printf("請輸入續(xù)費(fèi)金額:");
- scanf("%f",&money);
- p->money+=money; //續(xù)費(fèi)
- modify_file(p); //調(diào)用保存修改會員信息函數(shù)
- system("cls"); //清屏
- display_one(p); //調(diào)用顯示一條函數(shù)
- printf("續(xù)費(fèi)成功,任意鍵繼續(xù)!");
- getchar();
- menu(); //調(diào)用菜單函數(shù)
- }
- //會員結(jié)算函數(shù)
- void consume()
- { //會員結(jié)算
- mem *p;
- float cost;
- load(p);
- display(p); /* 調(diào)用瀏覽函數(shù)*/
- printf("\n");
- printf("請輸入需要結(jié)算的會員卡號(n返回菜單):");
- p=search_id(p); //調(diào)用按號尋找函數(shù)
- system("cls"); //清屏
- display_one(p); //調(diào)用顯示一條函數(shù)
- printf("請輸入花費(fèi)金額:");
- scanf("%f",&cost);
- if(p->cost>=1000)
- { //判斷是否升級為vip
- printf("恭喜您已經(jīng)升級為VIP,本次消費(fèi)9折優(yōu)惠。\n");
- printf("本次實(shí)際消費(fèi)%.2f元!",cost*0.9); //升級為vip,消費(fèi)打9折
- cost*=0.9;
- if((p->money-cost)<0)
- { //判斷余額是否充足
- printf("對不起,余額不足,請及時充值!");
- getchar();
- menu();
- }
- p->cost+=cost;
- p->money-=cost; //消費(fèi)
- getchar();
- }
- else
- {
- if((p->money-cost)<0)
- { //同上
- printf("對不起,余額不足,請及時充值!");
- getchar();
- menu();
- }
- p->cost+=cost;
- p->money-=cost;
- }
- modify_file(p); //調(diào)用保存修改會員信息函數(shù)
- system("cls"); //清屏
- display_one(p); //調(diào)用顯示一條函數(shù)
- printf("結(jié)算成功,任意鍵繼續(xù)!");
- getch();
- menu(); //調(diào)用菜單函數(shù)
- }
- /*會員退出函數(shù)*/
- void delete_m()
- { //刪除會員信息
- mem *p,*q=head,*thelast;
- float money;
- load(p);
- display(p); /* 調(diào)用瀏覽函數(shù)*/
- printf("\n");
- printf("請輸入需要刪除的會員卡號(n返回菜單):");
- p=search_id(p); //調(diào)用按號尋找函數(shù)
- thelast=head;
- if(p==head)
- { //判斷指針
- head=p->next;
- money=p->money;
- free(p) ;
- }
- else
- {
- while(thelast)
- {
- if(thelast->next==p)
- break;
- else
- thelast=thelast->next;
- }
- thelast->next=p->next;
- money=p->money;
- free(p);
- }
- modify_file(q); //調(diào)用保存函數(shù)
- printf("退回余額%.2f元!\n",money);
- printf("刪除成功,按任意鍵繼續(xù)!"); //顯示退回的余額
- getch();
- menu(); //調(diào)用菜單函數(shù)
- } /*統(tǒng)計(jì)函數(shù)*/
- mem *sort_m()
- { //按繳費(fèi)總額排序
- mem *q,*tail,*p=(mem*)malloc(sizeof(mem));
- p->next=head;
- head=p;
- tail=NULL; //定義 tail
- while(tail!=head->next)
- {
- p=head;
- q=p->next;
- while(q->next!=tail)
- { //判斷 q是否為空
- if(p->next->money>q->next->money)
- {
- p->next=q->next; //比較大小,排序
- q->next=q->next->next;
- p->next->next=q;
- }
- p=p->next;
- q=p->next;
- }
- tail=q;
- }
- head=head->next;
- free(p);
- return head; //返回值
- }
- mem* sort_c()
- { //按消費(fèi)金額排序
- mem *q,*tail,*p=(mem*)malloc(sizeof(mem));
- p->next=head;
- head=p;
- tail=NULL; //定義 tail
- while(tail!=head->next)
- {
- p=head;
- q=p->next;
- while(q->next!=tail)
- {
- if(p->next->cost>q->next->cost)
- { //比較大小,排序
- p->next=q->next;
- q->next=q->next->next;
- p->next->next=q;
- }
- p=p->next;
- q=p->next;
- }
- tail=q;
- }
- head=head->next;
- free(p);
- return head; //返回值
- }
- float summ(mem *p)
- { //計(jì)算繳費(fèi)總額
- float summ=0;
- p=head;
- while(p)
- { //循環(huán),實(shí)現(xiàn)求總額
- summ+=p->money;
- p=p->next;
- }
- return summ; //返回總額
- }
- float sumc(mem *p)
- { //計(jì)算消費(fèi)總額
- float sumc=0;
- p=head;
- while(p)
- { //循環(huán),實(shí)現(xiàn)求總額
- sumc+=p->cost;
- p=p->next;
- }
- return sumc; //返回總額
- }
- void statistics()
- { //統(tǒng)計(jì)會員信息函數(shù)
- int f=0;
- mem *p;
- load(p);
-
- system("cls");
- printf("\t\t=======================================\n"
- "\t\t|\n"
- "\t\t|1 按繳費(fèi)總額排序\n"
- "\t\t|2 按消費(fèi)總額排序\n"
- "\t\t|3 返回菜單\n"
- "\t\t|\n"
- "\t\t=======================================\n"
- ) ;
- printf("請選擇功能:");
- scanf("%d",&f);
- switch(f)
- { //用switch語句選擇功能
- case 1: p=sort_m(); //調(diào)用按繳費(fèi)總額排序
- display_statistics(p,summ(p),sumc(p)); //調(diào)用函數(shù)
- getch();
- menu();
- break;
- case 2: p=sort_c(); //調(diào)用按消費(fèi)總額排序
- display_statistics(p,summ(p),sumc(p)); //調(diào)用函數(shù)
- getch();
- menu();break;
- default : menu();break;
- }
- }
- /*退出系統(tǒng)操作*/
- void clear(mem *p)
- { //釋放鏈表空間
- if(p==NULL)
- {
- return;
- }
- if(p->next ==NULL)
- {
- free(p);
- }
- else
- {
- clear(p->next);
- free(p);
- p = NULL;
- }
- }
- void quit()
- { //退出函數(shù)
- clear(head);
- exit(0);
- }
- /*菜單信息*/
- void menu_tile(char *title)
- { //菜單表頭
- printf("\t\t=======================================\n");
- printf("\t\t| %s\n", title);
- printf("\t\t---------------------------------------\n");
- }
- void (* menu_main_func[])()= /*函數(shù)指針數(shù)組 menu_main_func[] 存儲的是主菜單項(xiàng)中7個功能函數(shù)的地址,
- 分別對應(yīng)1-7菜單項(xiàng)。例如用戶選擇1時,調(diào)用數(shù)組的第0個元素,即調(diào)用
- newMember()函數(shù),以此類推。*/
- {
- newMember,
- modify_m,
- add,
- consume,
- delete_m,
- statistics,
- quit,
- };
- char menu_main[] = //菜單主函數(shù)
- "\t\t\n"
- "\t\t| 1 新會員登記\n"
- "\t\t| 2 會員信息修改\n"
- "\t\t| 3 會員續(xù)費(fèi)\n"
- "\t\t| 4 會員消費(fèi)結(jié)算\n"
- "\t\t| 5 會員退卡\n"
- "\t\t| 6 統(tǒng)計(jì)功能\n"
- "\t\t| 7 退出系統(tǒng)\n"
- "\t\t|\n";
- void menu()
- { //菜單函數(shù)
- int selected = 0; //初始化變量
- system("cls"); //清屏
-
- printf(" 會員卡計(jì)費(fèi)系統(tǒng)");
- printf(menu_main); //輸出菜單主函數(shù)
- printf("\t\t=======================================\n");
-
- while(!(selected >= 1 && selected <= MENU_MAIN_COUNT))
- { //判斷初始值
- printf("請選擇:");
- scanf("%d",&selected);
- if(selected >= 1 && selected <= MENU_MAIN_COUNT)
- { //判斷輸入值是否有效
- break;
- }
- printf("\n>輸入錯誤。ㄗⅲ赫堖x擇 1 - %d)\n", MENU_MAIN_COUNT);
- }
- menu_main_func[selected-1](); //調(diào)用相應(yīng)的函數(shù)指針數(shù)組中的函數(shù),執(zhí)行操作
-
- }
- int main() /*主函數(shù)*/
- {
- menu(); //調(diào)用菜單函數(shù)
- return 0; //返回值
- }
復(fù)制代碼
|
|