#include #include #include #include #define OK 1 #define ERROR 0 #define NAME_INIT_MAXSIZE 20 #define CREDITS_INIT 100 #define LINK_MAXSIZE 1000 typedef int Status; int NUM=1; //顾客信息 typedef struct { int num; int age,number; int credits; double discount; char name[NAME_INIT_MAXSIZE]; }ElemType; //链表存储 typedef struct LNode { ElemType information; LNode *next; }LNode,*LinkList; Status AddInformation(LinkList &L,char Name[],int Age); Status Init(LinkList &L); Status Calculate_discount(LinkList &L); //链表长度 int LinkLength(LinkList L) { int l=0; while(L->next!=NULL) { l++; L=L->next; } return l; } //初始化 Status InitList(LinkList &L) { L=(LinkList)malloc(sizeof(LNode)); L->next=NULL; L->information.credits=0; return OK; } //插入到第n个结点后面 Status InsertNode(LinkList &L,int n,ElemType e) { if(n>LinkLength(L)) { printf("Insert Error!\n"); return ERROR; } LinkList p =L; int i=0; while(p->next!=NULL && inext; } LinkList s=(LinkList)malloc(sizeof(LNode)); e.num=NUM++; s->information=e; s->next=p->next; p->next=s; return OK; } //追加 Status SupperaddNode(LinkList &L,ElemType e) { LinkList p=L,s=(LinkList)malloc(sizeof(LNode)); while(p->next!=NULL) p=p->next; e.num=NUM++; s->information=e; s->next=NULL; p->next=s; return OK; } //输出一个 Status PrintSingle(ElemType e) { printf("Name:%s\tAge:%d\t积分:%d\t编号:%.5d\t折扣:%.2f\n",e.name,e.age,e.credits,e.number,e.discount); return OK; } //删除 Status DeleteNode(LinkList &L,int n,ElemType &e) { if(n>LinkLength(L)) { printf("Delete Error!\n"); return ERROR; } LinkList p=L; int i=1; while(p->next!=NULL && i!=n) { i++; p=p->next; } LinkList s=p->next; PrintSingle(s->information); p->next=s->next; free(s); return OK; } //添加初始用户 Status AddInformation(LinkList &L,char Name[],int Age) { ElemType e; strcpy(e.name,Name); e.age=Age; e.credits=CREDITS_INIT; e.number=rand()%100000; e.discount=1.0; SupperaddNode(L,e); return 0; } //输出所有 Status PrintAll(LinkList L) { printf("所有客户信息.\n"); printf("_____________________________________________________\n"); int i=0; while(L->next!=NULL) { i++; L=L->next; PrintSingle(L->information); } printf("_____________________ 共有%d名客户 __________________\n",i); return OK; } //查找_Name Status FindNode_name(LinkList L) { printf("Input Name:"); char Name[NAME_INIT_MAXSIZE]; gets(Name); bool flag=0; while(L->next!=NULL) { L=L->next; if(strcmp(L->information.name,Name)==0) { flag=1; PrintSingle(L->information); } } if(!flag) printf("Not Find!\n"); return OK; } //查找_number Status FindNode_number(LinkList L) { printf("Input Number:"); int Num; bool flag=0; scanf("%d",&Num); while(L->next!=NULL) { L=L->next; if(Num == L->information.number) { flag=1; PrintSingle(L->information); } } if(!flag) printf("Not Find!\n"); return OK; } //查找_age Status FindNode_age(LinkList L) { printf("Input Age:"); int Age; bool flag=0; scanf("%d",&Age); while(L->next!=NULL) { L=L->next; if(L->information.age==Age) { PrintSingle(L->information); flag=1; } } if(!flag) printf("Not Find!\n"); return OK; } //查找_credits Status FindNode_credits(LinkList L) { printf("Input Credits:"); int Credits; scanf("%d",&Credits); bool flag=0; while(L->next!=NULL) { L=L->next; if(Credits == L->information.credits) { flag=1; PrintSingle(L->information); } } if(!flag) printf("Not Find!\n"); return OK; } //模拟_购物 积分=购物价格*2 Status Shopping(LinkList &Ll) { int length=LinkLength(Ll),range=100,i=0; LinkList L=Ll; printf("\t\t**** 共有%d名顾客消费 ****\n",length); printf("此次最大消费额度为:"); scanf("%d",&range); printf("此次购物额为:\n\n"); printf("\t理论购物\t折扣后价钱\t折扣\n"); while(L->next!=NULL && i++<=length) { L=L->next; double sum=rand()%range; double price=(1-L->information.discount)*sum; printf("%d.\t%8.2f 元\t%8.2f 元\t%2.2f\n",i,sum,price,L->information.discount); L->information.credits+=(int) price*2; } printf("\n购物结束 !\n重新计算折扣...\n"); Calculate_discount(Ll); return OK; } /*计算折扣 (0) 100-200 1% (1) 200-500 2% (2) 500-1000 6% (3) 100-2000 9% (4) 2000-5000 12% (5) 5000-9000 15% (6) 9000-36000 18% (7) >36000 24% */ Status Calculate_discount(LinkList &L) { LinkList p=L; while(p->next!=NULL) { p=p->next; int cd=p->information.credits; if(100<=cd && cd<200) p->information.discount=0.01; else if(200<=cd && cd<500) p->information.discount=0.02; else if(cd<1000) p->information.discount=0.06; else if(cd<2000) p->information.discount=0.09; else if(cd<4000) p->information.discount=0.12; else if(cd<6000) p->information.discount=0.15; else if(cd<360000) p->information.discount=0.18; else p->information.discount=0.24; } return OK; } //查找菜单 int Find_menu() { printf("\t 查找 \n"); printf("\t___________________________\n"); printf("\t 1.按姓名查找 \n"); printf("\t___________________________\n"); printf("\t 2.按年龄查找 \n"); printf("\t___________________________\n"); printf("\t 3.按编号查找 \n"); printf("\t___________________________\n"); printf("\t 4.按积分数查找 \n"); printf("\t___________________________\n"); printf("\t 5.返回 \n"); printf("\t___________________________\n"); printf("--------请选择:"); char c[5]; scanf("%s",c); getchar(); return atoi(c); } //查找 Status Find(LinkList L) { switch(Find_menu()) { case 1:FindNode_name(L);break; case 2:FindNode_age(L);break; case 3:FindNode_number(L);break; case 4:FindNode_credits(L);break; case 5:break; default:printf("Input Error!\n");break; } return OK; } //添加菜单 int Add_menu() { printf("\t\t_______ 添加 _______\n\n"); printf("\t\t 1.往后追加. \n\n"); printf("\t\t 2.直接插入. \n\n"); printf("\t\t 3.返回. \n\n"); printf("\t\t____________________\n"); printf("------请输入:"); char s[5]; scanf("%s",s); getchar(); return atoi(s); } //添加 Status Add(LinkList &L) { int select=Add_menu(); if(select==3) return OK; if(select!=1 && select!=2) { printf("输入错误!\n"); return ERROR; } ElemType e; char Name[NAME_INIT_MAXSIZE]; int Age; //姓名 printf("Input Name:"); gets(Name); // getchar(); //年龄 printf("Input Age:"); scanf("%d",&Age); //存入 strcpy(e.name,Name); e.age=Age; e.credits=CREDITS_INIT; e.number=rand()%1000000; //插入或追加 if(select==1) SupperaddNode(L,e); else if(select==2) { int p; printf("请输入你想插入的位置:"); scanf("%d",&p); InsertNode(L,p,e); } Calculate_discount(L); printf("添加成功!\n"); return OK; } //修改客户信息 Status AlterInformation(LinkList &L) { printf("请输入客户编号:"); int num; bool flag=0; LinkList p=L; scanf("%d",&num); getchar(); while(p->next!=NULL) { p=p->next; if(p->information.number == num) { flag=1; break; } } if(!flag) printf("Not Find!\n"); else { PrintSingle(p->information); char Name[NAME_INIT_MAXSIZE]; int Age; printf("Name:"); gets(Name); printf("Age:"); scanf("%d",&Age); getchar(); num=rand()%1000000; printf("随机生成编号为:%.6d\n",num); char c; printf("是否确认 y/n :"); scanf("%c",&c); if(c!='y' && c!='Y') { printf("操作失败!\n"); return ERROR; } p->information.age=Age; strcpy(p->information.name,Name); p->information.number=num; printf("操作成功!\n"); return OK; } return OK; } //删除 Status Delete(LinkList &L) { int num; ElemType e; printf("请输入要删除的序号:"); scanf("%d",&num); LinkList p =L; DeleteNode(L,num,e); printf("已删除!\n"); return OK; } //主菜单 int Main_menu() { printf("\n\t\t\t 主菜单 \n"); printf("\t\t\t_____________________________\n"); printf("\t\t\t 1.模拟消费 \n"); printf("\t\t\t_____________________________\n"); printf("\t\t\t 2.显示所有客户信息. \n"); printf("\t\t\t_____________________________\n"); printf("\t\t\t 3.查找客户. \n"); printf("\t\t\t_____________________________\n"); printf("\t\t\t 4.添加客户. \n"); printf("\t\t\t_____________________________\n"); printf("\t\t\t 5.修改客户信息. \n"); printf("\t\t\t_____________________________\n"); printf("\t\t\t 6.删除客户记录. \n"); printf("\t\t\t_____________________________\n"); printf("\t\t\t 7.退出. \n"); printf("\t\t\t_____________________________\n"); printf("--------请选择:"); char chio[4]; scanf("%s",chio); return atoi(chio); } //主函数 int main() { LinkList L; Init(L); while(true) { switch(Main_menu()) { case 1:Shopping(L);break; case 2:PrintAll(L);break; case 3:Find(L);break; case 4:Add(L);break; case 5:AlterInformation(L);break; case 6:Delete(L);break; case 7:printf("谢谢使用,再见!\n");return 0; default:printf("Error!\n"); } system("pause"); } return 0; } // Status Init(LinkList &L) { printf("\t\t******** 欢迎来到客户消费积分管理系统 ********\n"); srand(time(0)); InitList(L); AddInformation(L,"Elen",19); AddInformation(L,"Amy",21); AddInformation(L,"Sunny",19); AddInformation(L,"Jeniffer",20); AddInformation(L,"章鱼",6); AddInformation(L,"光头强",12); AddInformation(L,"鞋类",99); Calculate_discount(L); // PrintAll(L); return OK; }