欢迎大佬指点!!
亲测可用!
改了 几个bug 更新 之后的代码
会员信息管理系统文本说明
功能:
-
消费
①从vipinf.txt的文本文件中LOAD()建立一个链表,之后的操作全部在链表中进行。在链表中修改消费信息,并将更新后的链表信息同步到文件中。
②添加消费记录:直接在以序号命名的文本中append消费记录信息,本消费记录信息由:标识符flag,系统时间,消费金额,账户余额组成 -
添加会员
① 输入会员信息添加到链表中,将更新后的链表同步到文本中。会员信息包括:序号num,姓名name,电话phone,性别sex,余额money,累计消费paysum。添加会员时候paysum默认为0。
② 添加会员的同时创建以序号命名的个人消费、充值记录文件,文本文件以“a“方式打开。
③ 在添加会员之前还要查看当前链表中是否已经出现该会员,如果同姓名,则在新添加的姓名后标注s(拓展功能,有时间再搞)。 -
充值
① 从vipinf.txt的文本文件中LOAD()建立一个链表,之后的操作全部在链表中进行。在链表中修改充值信息,并将更新后的充值信息同步到文件中。
② 添加充值记录:直接在以序号命名的文本中append消费记录信息,本消费记录信息由:标识符flag,系统时间,消费金额,账户余额组成 -
删除
① 启动结算,从链表中删除该会员信息,同步到文本。
② 保留个人记录文件 -
查询
① 单个查询
② 所有会员信息 -
信息修改
① 将修改后的链表同步到文本中 -
数据分析
一、 按累计消费排序
① 排序函数,排序后,重新load。
二、 按余额大小排序
① 排序函数,排序后,重新load。
三、 显示今日消费记录
① 以“a”的方式打开以年-月-日命名的文本,将消费记录信息写入文本中,包括:序号printoneinf,消费时间、消费金额。
四、 统计今日营业额
① 以“a”打开文件名为daypayinf的文本文件,将日期 总营业额信息写入文本中,建立链表,总营业额信息由每次调用pay函数更新。由链表最后一个元素的时间判断需不需要建立新的元素。
五、 统计指定日期范围内的营业额(拓展功能)
① 由此,实现此功能会很easy。 -
退出系统
直接exit(0) -
每次调用一个重要函数的时候都要进行清屏。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<time.h>
#define NAME 8
#define PHONE 15typedef struct member{int num;char name[NAME];char sex[3];float money;float paysum;char phone[PHONE];struct member *next;
}vip;
typedef struct record {int flag; //1消费 2充值char time[20];float money;float blance;struct record *next;
}recording;
typedef struct gettime { int year;int month;int day;int hour;int min;
}Mytime;
struct daypay {char systime[11];float money;struct daypay *next;
};
/*函数声明区*/
void save_vip();
vip *searchmem();
void Pay();
void Recharge();
vip *get_last(vip *p);
vip *LOAD();
int choosenum();
void Search();
void add_tolistandfile();
void add_recordinginf(int num, int flag, float money, vip *member);
void add_Payrecording(int num, int money, vip *member);
void add_Rechargerecording(int num, int money, vip *member);
void add_tofile(int num, char name[], char sex[], char phone[], float money, float paysum);
void Add_vip();
void Delte();
char *tochartime();
void Menu();
void Modify();
void printallinf();
void printoneinf();
vip *findlast(vip *member);
struct daypay *load_daypay();
void save_daypay(struct daypay *head);
void daypay(float money);
char *systemtime();
void Analysis();
void Sort_money(vip *SL);
void Sort_paysum(vip *SL);
void search_recording();
void Free_list(vip *Head);
void Free_daypay(struct daypay *head);
float onedaymoney();
float daymoney();
/*函数声明区*//*全局变量定义区*/
vip *head=NULL;
vip *temp;
struct daypay *head_daypay=NULL;
char strtime[20] = { 0 };
/*全局变量定义区*/int main()
{system("color 0A");head = LOAD();//优先级高于一切Menu();system("PAUSE");
}/*重要函数区*/
void Menu()
{ int t;printf("WELCOME TO HAORAN VIP ADMINSTRATTION APPLICATION\n");printf("请选择功能选项\n");printf("1.消费\n");printf("2.添加会员\n");printf("3.充值\n");printf("4.修改信息\n");printf("5.删除会员\n");printf("6.查询信息\n");printf("7.数据分析\n");printf("0.退出系统\n\n");scanf("%d", &t);switch (t){case 1:Pay();Menu();break;case 2:system("CLS");Add_vip();Menu();break;case 3:system("CLS");Recharge();Menu();break;case 4:system("CLS");Modify();Menu();break;case 5:system("CLS");Delte();Menu();break;case 6:system("CLS");Search();Menu();break;case 7:system("CLS");Analysis();Menu();case 0:exit(0);default:printf("输入格式错误,请重新输入\n");system("CLS");Menu();}
}
vip *LOAD()
{FILE *fp;vip *pNew, *pLast;head = NULL;if ((fp = fopen("vipinf.txt", "a")) == NULL){printf("打开vipinf.txt失败\n");exit(0);}fclose(fp);if ((fp = fopen("vipinf.txt", "r")) == NULL){printf("打开vipinf.txt失败\n");exit(0);}while (!feof(fp)) //feof文件结束:返回非0值,文件未结束,返回0值{pNew = (vip *)malloc(sizeof(vip));pNew->next = NULL;fscanf(fp, "%d %s %s %s %f %f\n", &(pNew->num), pNew->name, pNew->sex, pNew->phone, &(pNew->money), &(pNew->paysum));if (head == NULL){head = pNew;}else{pLast = get_last(head);pLast->next = pNew;}//接到最后头}//p = head;这项应该是多余的fclose(fp);return head;
}
void Add_vip()
{add_tolistandfile();printf("新会员添加成功!\n\n");
}
void Recharge()
{vip *member = searchmem();float tmoney;char t;int f=0;if (member){printf("请输入充值金额:");scanf("%f", &tmoney);getchar();while (1){printf("确认给%s充值%.1f元吗?(Y/N)\n", member->name, tmoney);t = getchar();if (f == 1)getchar();if (t == 'Y' || t == 'y'){member->money += tmoney;break;}else if (t == 'N' || t == 'n'){printf("1.重新输入\n2.返回主菜单\n");scanf("%d", &f);if (f == 1){printf("请重新输入充值金额:");scanf("%f", &tmoney);}else if (f == 2){system("CLS");Menu();}}}add_Rechargerecording(member->num, tmoney, member);save_vip();printf("充值成功!\n");printf("当前余额%.1f\n\n", member->money);}}
void Pay()
{float temp;system("CLS");vip *member = searchmem();if (member){printf("请输入消费金额:");scanf("%f", &temp);if (member->money < temp){printf("您当前余额为%.1f,余额不足,请先充值\n", member->money);}else{member->money -= temp;member->paysum += temp;add_Payrecording(member->num, temp, member);save_vip();daypay(temp);printf("您已消费成功!\n当前余额为:%.1f\n\n\n", member->money);}}
}
void Delte()
{char t;vip *member = searchmem();vip *p1 = NULL, *p2 = NULL;if (member){while (1){printf("确定要删除 %s %s %s 吗?(Y/N)\n", member->name, member->sex, member->phone);getchar();t = getchar();if (t == 'y' || t == 'Y'){p1 = findlast(member);if (p1 == member)p2 = p1;p1->next = member->next;if (p2)head = p2->next;free(member);save_vip();printf("已删除成功!\n");break;}else if (t == 'N' || t == 'n'){Menu();}else{printf("输入有误请重新输入\n");}}}
}
void Search()
{int t = 0;while (1){printf("请输入查询方式\n");printf("1.查询会员信息\n");printf("2.查询会员的消费记录\n");printf("3.查询今日营业额\n");printf("4.查询指定日期范围内的总营业额\n");printf("0.返回主菜单\n");scanf("%d", &t);system("CLS");if (t == 1){printoneinf();break;}else if (t == 2){search_recording();break;}else if (t == 3){printf("今日营业额为:%.1f\n\n", onedaymoney());break;}else if (t == 4){printf("今日营业额为:%.1f\n\n", daymoney());break;}else if (t == 0){getchar();Menu();}elsereturn;}}
void Modify()
{int t;vip *member = searchmem();if (member){while (1){printf("请输入您需要修改的项目:\n");printf("1.姓名\n2.性别\n3.电话号码\n4.返回主菜单\n");scanf("%d", &t);getchar();if (t == 1){printf("请输入新的姓名:");scanf("%s", member->name);save_vip();printf("姓名修改成功\n");break;}else if (t == 2){printf("请输入新的性别:");scanf("%s", member->sex);save_vip();printf("性别修改成功\n");break;}else if (t == 3){printf("请输入新的电话号码:");while (1){scanf("%s", member->phone);if (strlen(member->phone) == 11)break;elseprintf("您输入的格式有误,请重新输入:");}save_vip();printf("电话号码修改成功\n");break;}else if (t == 4){Menu();}else{printf("输入格式错误请重新输入!\n");}}}}
void Analysis()
{int t = 0;while (1){printf("请输入数据统计模式\n");printf("1.按序号显示全部会员信息\n");printf("2.按累计消费显示所有信息\n");printf("3.按余额大小显示所有信息\n");printf("0.返回主菜单\n");scanf("%d", &t);if (t == 1){printallinf();}else if (t == 2){Sort_paysum(head);}else if (t == 3){Sort_money(head);}else if (t == 0){system("CLS");Menu();}else{printf("输入格式有误,请重新输入\n");}}}
/*重要函数区*//*辅助函数区*/
vip *get_last(vip *p) //取得链表最后一个元素的节点指针并返回该指针
{if (p->next == NULL){return p;}else{get_last(p->next);}
}
struct daypay *get_last2(struct daypay *p) //取得链表最后一个元素的节点指针并返回该指针
{if (p->next == NULL){return p;}else{get_last2(p->next);}
}
int choosenum()
{vip *temp;vip *last;last=get_last(head);if (temp == last)return 1;elsereturn (last->num+1)
}
void add_tolistandfile()
{FILE *fp;char s[9];vip *p,*last;p = (vip *)malloc(sizeof(vip));p->num = choosenum();printf("姓名:");scanf("%s", p->name);printf("性别:"); scanf("%s", p->sex);printf("电话:");while(1){scanf("%s", p->phone);if (strlen(p->phone) == 11)break;elseprintf("您输入的格式有误,请重新输入!\n");} printf("首充金额:");scanf("%f", &(p->money));p->paysum = 0;last = get_last(head);last->next = p;p->next = NULL;add_tofile(p->num, p->name, p->sex, p->phone, p->money,0);//将新会员信息添加到文件_itoa(p->num, s, 10);//将int转换为char类型strcat(s, ".txt");if ((fp = fopen(s, "a")) == NULL){printf("文件打开失败!\n");return;}
}
void add_tofile(int num,char name[],char sex[],char phone[],float money,float paysum)
{FILE *fp;if ((fp = fopen("vipinf.txt", "a")) == NULL){printf("打开vipinf.txt失败\n");}fprintf(fp, "%d %s %s %s %.1f %.1f\r\n", num, name, sex, phone, money,paysum);fclose(fp);
}
void printoneinf()
{vip *member = searchmem();printf("序号 姓名 性别 电话 余额\n");if (member){printf("%s %s %s %.1f %.1f\n\n", member->name, member->sex, member->phone, member->money,member->paysum);}elseprintf("该会员不存在\n");
}
void printallinf()
{vip *member=NULL;int count = 1;for (member = head; member != NULL; member = member->next){printf("%d %s %s %s %.1f %.1f\n", count++, member->name, member->sex, member->phone, member->money,member->paysum);}printf("\n\n");
}
void save_vip()
{FILE *fp;vip *temp=head;fp = fopen("vipinf.txt", "w");while (1){fprintf(fp, "%d %s %s %s %.1f %.1f\r\n", temp->num, temp->name, temp->sex, temp->phone, temp->money,temp->paysum);temp = temp->next;if (temp == NULL)break;}fclose(fp);LOAD();
}
vip *searchmem()
{char temp[PHONE];int len;int flag = 0;vip *p1 = NULL;printf("请输入电话或姓名:");scanf("%s", temp);len = strlen(temp);if (len == 11)flag = 1;elseflag = 2;if (flag == 1){for (p1 = head; p1->next != NULL; p1 = p1->next){if (strcmp(p1->phone, temp) == 0)break;}if (strcmp(p1->phone, temp) != 0){printf("文本中没有该会员,请先创建新的会员!\n");Menu();}return p1;}else if (flag == 2){for (p1 = head; p1->next != NULL; p1 = p1->next){if (strcmp(p1->name, temp) == 0)break;}if (strcmp(p1->name, temp) != 0){printf("文本中没有该会员,请先创建新的会员!\n");Menu();}return p1;}
}
vip *findlast(vip *member)
{ vip *p1, *p2;for (p1 = head, p2 = head; p1 != member; p2 = p1, p1 = p1->next){if (p1 == NULL){printf("程序出错!!!\n");}}return p2;
} //找到上一个元素
void add_recordinginf(int num,int flag,float money, vip *member)
{FILE *fp;char s[9];_itoa(num, s, 10);//将int转换为char类型strcat(s, ".txt");if ((fp = fopen(s, "a")) == NULL){printf("文件打开失败!\n");return;}fprintf(fp, "%d %s %f %f\r\n", flag, tochartime(), money, member->money);//标识 //时间 //支出或充值 //余额fclose(fp);
}
void add_Payrecording(int num,int money,vip *member)
{money = -money;add_recordinginf(num, 1, money, member);
}
void add_Rechargerecording(int num, int money, vip *member)
{add_recordinginf(num, 2, money, member);
}
char *tochartime()
{memset(strtime, '\0', sizeof(strtime));Mytime TIME;time_t timep;struct tm *p;char year[5],month[3],day[3],hour[3],min[3];time(&timep);p = gmtime(&timep);TIME.year = (1900 + p->tm_year);TIME.month = 1 + p->tm_mon;TIME.day = p->tm_mday;TIME.hour = 8 + p->tm_hour;TIME.min = p->tm_min;_itoa(TIME.year, year, 10);_itoa(TIME.month, month, 10);_itoa(TIME.day, day, 10);_itoa(TIME.hour, hour, 10);_itoa(TIME.min, min, 10);strcat(strtime, year);strcat(strtime, "-"); strcat(strtime, month);strcat(strtime, "-");strcat(strtime, day);strcat(strtime, " ");strcat(strtime, hour);strcat(strtime, ":");strcat(strtime, min);return strtime;
}
char *systemtime()
{memset(strtime, '\0', sizeof(strtime));Mytime TIME;time_t timep;struct tm *p;char year[5], month[3], day[3];time(&timep);p = gmtime(&timep);TIME.year = (1900 + p->tm_year);TIME.month = 1 + p->tm_mon;TIME.day = p->tm_mday;TIME.hour = 8 + p->tm_hour;TIME.min = p->tm_min;_itoa(TIME.year, year, 10);_itoa(TIME.month, month, 10);_itoa(TIME.day, day, 10);strcat(strtime, year);strcat(strtime, "-");strcat(strtime, month);strcat(strtime, "-");strcat(strtime, day);printf("%s", strtime);return strtime;
}
void daypay(float money)
{char time[11];strcpy(time, systemtime());struct daypay *head_p= load_daypay(),*p=NULL,*q=NULL;p = get_last2(head_p);if ((strcmp(p->systime,time ))==0){p->money += money;save_daypay(head_p);}else{q = (struct daypay *)malloc(sizeof(struct daypay));strcpy(q->systime, time);q->money = money;q->next = NULL;p->next = q;save_daypay(head_p);}Free_daypay(head_p);
}
void save_daypay(struct daypay *head_pay)
{FILE *fp;struct daypay *temp = head_pay;if ((fp = fopen("daypayinf.txt", "w")) == NULL){printf("文件打开失败!\n");}while (1){fprintf(fp, "%s %.1f\r\n", temp->systime,temp->money);temp = temp->next;if (temp == NULL)break;}fclose(fp);
}
struct daypay *load_daypay()
{FILE *fp;struct daypay *p = NULL, *q = NULL;if ((fp = fopen("daypayinf.txt", "r")) == NULL){printf("文件打开失败!\n");}head_daypay = NULL;while (!feof(fp)){p = (struct daypay *)malloc(sizeof(struct daypay));p->next = NULL;fscanf(fp, "%s %f\n", p->systime, &(p->money));if (head_daypay == NULL){head_daypay = p;}else{q->next = p;}q = p;}return head_daypay;
}
void Sort_money(vip *SL)/*递增排序函数:入口参数:链表的头指针,此为链表中的排序函数*/
{vip *p, *q;int temp;float money, paysum;char phone[12], name[NAME], sex[3];//为什么改成3之后就行了 for (p = SL; p != NULL; p = p->next){for (q = p->next; q != NULL; q = q->next){if (p->money < q->money){p->num ^= q->num;q->num ^= p->num;p->num ^= q->num;money = q->money;q->money = p->money;p->money = money;paysum = q->paysum;q->paysum = p->paysum;p->paysum = paysum;strcpy(name, q->name);strcpy(q->name, p->name);strcpy(p->name, name);strcpy(phone, q->phone);strcpy(q->phone, p->phone);strcpy(p->phone, phone);strcpy(sex, q->sex);strcpy(q->sex, p->sex);strcpy(p->sex, sex);}}}printf("\n序号 姓名 性别 电话 余额 累计消费\n");for (p = SL,q=NULL,temp=1; p != NULL; q=p,p = p->next){printf("%d %s %s %s %.1f %.1f\n", temp++, p->name, p->sex,p->phone, p->money, p->paysum);free(q);}printf("\n\n");free(p);LOAD();
}
void Sort_paysum(vip *SL)
{vip *p, *q;int temp;float money, paysum;char phone[12], name[NAME], sex[3];for (p = SL; p != NULL; p = p->next){for (q = p->next; q != NULL; q = q->next){if (p->paysum < q->paysum){p->num ^= q->num;q->num ^= p->num;p->num ^= q->num;money = q->money;q->money = p->money;p->money = money;paysum = q->paysum;q->paysum = p->paysum;p->paysum = paysum;strcpy(name, q->name);strcpy(q->name, p->name);strcpy(p->name, name);strcpy(phone, q->phone);strcpy(q->phone, p->phone);strcpy(p->phone, phone);strcpy(sex, q->sex);strcpy(q->sex, p->sex);strcpy(p->sex, sex);}}}printf("\n序号 姓名 性别 电话 余额 累计消费\n");for (p = SL,q=NULL, temp = 1; p != NULL; q=p,p = p->next){printf("%d %s %s %s %.1f %.1f\n", temp++, p->name, p->sex,p->phone, p->money, p->paysum);free(q);}printf("\n\n");free(p);LOAD();
}
void search_recording()
{FILE *fp;recording t;int temp=0;vip *member = searchmem();char s[8];char time[6];_itoa(member->num, s, 10);strcat(s, ".txt");system("CLS");printf("姓名 性别 电话 余额 累计消费\n");if (member){printf("%s %s %s %.1f %.1f\n", member->name, member->sex, member->phone, member->money, member->paysum);}elseprintf("该会员不存在\n");if ((fp=fopen(s, "r")) == NULL){printf("文件打开失败\n");return;}printf("请输入您要查看的记录类型\n1.消费记录\n2.充值记录\n");while (1){scanf("%d", &temp);system("CLS");if (temp == 1 || temp == 2){while (!feof(fp)){fscanf(fp, "%d %s %s %f %f\n", &t.flag, t.time,time, &t.money, &t.blance);strcat(t.time, " ");strcat(t.time, time);if (t.flag == temp){if (t.money < 0){t.money = -t.money;}printf("%s %.1f %.1f\n", t.time, t.money, t.blance);}}printf("\n\n");break;}elseprintf("输入格式有误,请重新输入\n");}
}
void Free_list(vip *Head)
{vip *Pointer;while(NULL!= Head){Pointer = Head;Head= Head->next;free(Pointer);}return;
}
float daymoney()
{struct daypay *head = load_daypay(),*p1,*p2;char ttime1[11];char ttime2[11];float sum=0;printf("请输入第一个时间(例如2018-3-21 返回主菜单请输入0):");scanf("%s", ttime1);if (ttime1[0] == '0')Menu();printf("请输入第二个时间例如(2018-3-21返回主菜单请输入0):");scanf("%s", ttime2);if (ttime1[0] == '0')Menu();for (p1 = head; p1->next != NULL; p1 = p1->next){if (strcmp(ttime1, p1->systime) == 0){while (strcmp(ttime2, p1->systime) != 0||p1->next!=NULL){sum += p1->money;p1 = p1->next;}sum += p1->money;}break;}Free_daypay(head);return sum;
}
float onedaymoney()
{float money;struct daypay *head = load_daypay();while (head->next != NULL)head = head->next;money = head->money;Free_daypay(head);return money;
}
void Free_daypay(struct daypay *Head)
{struct daypay *Pointer;while (NULL != Head){Pointer = Head;Head = Head->next;free(Pointer);}return;
}
/*辅助函数区*/