前段时间我们也是迎来了课程设计,我们分配到是“会员卡计费管理系统”。秉承着有问题找度娘的职业精神,我也是百度了不少的相关资料,但是发现可以搜到了其实只有两个源码,并且使用链表的只有一个。看来最终还是得靠自己呀....
话不多说,源码呈上:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#define MAX 20
struct user
{
char name[MAX]; //姓名
char id[MAX]; //卡号
char sex[2]; //性别
int age; //年龄
double balance; //余额
int phone[MAX]; //电话
double buy; //消费金额
};
struct node
{
struct user data;
struct node *next;
};
struct node *list = NULL; //全局链表
char password[MAX]={'1','2','3'}; //初始密码
//创建表头
struct node *creathead();
//创建节点
struct node *creatnode(struct user data);
//打印
void printlist(struct node *headnode);
//插入:表头法
void insertnode(struct node *headnode,struct user data);
//指定位置删除
void delete(struct node *headnode,char *user_id);
//查找
struct node *findnode(struct node *headnode,char *user_id);
//排序
void bubblesortlist(struct node *headnode);
//文件存储
void save(const char *filename,struct node *headnode);
//文件读取
void read(const char *filename,struct node *headnode);
//密码登录
void pass();
//储存密码
void save_password(const char *filename,char *pasword);
//密码读取
void read_password(const char *filename,char *pasword);
//修改密码
void newpassword(const char *filename,char *pasword);
//菜单
void menu();
//交互
void keydown();
//表头
void table_head();
//表尾
void table_foot();
//新会员登记
void newuser();
//会员信息修改
void revise(struct node *headnode);
//会员退卡
void deleteuser(struct node *headnode);
//会员续费
void Renewal(struct node *headnode);
//消费结算
void consume(struct node *headnode);
//VIP结算函数
void vip(struct node *headnode);
//会员卡挂失
void Loss(struct node *headnode);
//统计
void statistics(struct node *headnode);
int main()
{
read_password("password.txt",password);
pass();
list = creathead();
read("user.txt",list);
while(1)
{
menu();
keydown();
system("pause"); //防止闪屏
}
return 0;
}
void menu()
{
system("cls");
printf("☆☆☆☆☆☆☆☆信息科学与技术学院☆☆☆☆☆☆☆☆\n");
printf("\n");
printf(" ~写上自己的信息就可以~\n");
printf("\n");
printf("************欢迎使用会员卡计费系统***************\n");
printf(" \n\t\t1--新会员登记\n");
printf(" \t\t2--会员信息修改\n");
printf(" \t\t3--会员消费结算\n");
printf(" \t\t4--会员退卡\n");
printf(" \t\t5--会员卡续费\n");
printf(" \t\t6--统计功能\n");
printf(" \t\t7--会员卡挂失\n");
printf(" \t\t8--信息浏览\n");
printf(" \t\t9--退出\n");
printf(" \t\t0--修改密码\n");
printf(" \n************欢迎使用会员卡计费系统***************\n");
printf("\n");
printf("\n");
printf("请选择(0~9):\n");
}
void keydown()
{
int n;
scanf("%d",&n);
switch (n)
{
case 1:
printf("【新会员登记】\n");
newuser(); //新会员等级
Sleep(1000);
break;
case 2:
revise(list); //会员信息修改
break;
case 3:
consume(list); //会员消费结算
break;
case 4: //会员退卡
deleteuser(list);
break;
case 5:
Renewal(list);
break;
case 6:
printf("【统计功能】\n");
bubblesortlist(list);
printlist(list);
statistics(list);
break;
case 7:
Loss(list); //会员卡挂失
break;
case 8:
printf("【信息浏览】\n");
printlist(list);
break;
case 9:
printf("【感谢使用!!】\n");
Sleep(1500);
exit(1);
break;
case 0:
newpassword("password.txt",password); //修改密码
break;
}
}
//创建表头
struct node *creathead()
{
//动态内存申请
struct node *headnode = (struct node*)malloc(sizeof(struct node));
//初始化
headnode->next = NULL;
return headnode;
}
//创建节点
struct node *creatnode(struct user data)
{
struct node *newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = data;
newnode->next = NULL;
return newnode;
}
//打印
void printlist(struct node *headnode)
{
struct node *pmove = headnode->next;
table_head(); //表头
while(pmove != NULL)
{
printf("|%10s|%6s|%6s|%6d|%8.2f|%8.2f|%11s|\n",
pmove->data.id,pmove->data.name,pmove->data.sex,pmove->data.age,pmove->data.balance,pmove->data.buy,pmove->data.phone);
table_foot(); //表尾
pmove = pmove->next;
}
}
//插入
void insertnode(struct node *headnode,struct user data)
{
struct node *newnode = creatnode(data);
newnode->next = headnode->next;
headnode->next = newnode;
}
//删除节点
void delete(struct node *headnode,char *user_id)
{
struct node *posleftnode = headnode; //删除节点的左侧节点
struct node *posnode = headnode->next;
//字符串比较函数
while(posnode!=NULL&&strcmp(posnode->data.id,user_id))
{
posleftnode = posnode;
posnode = posnode->next;
}
//讨论查找结果
if(posnode==NULL)
{
printf("没有此会员信息,请重新输入!!\n");
Sleep(1500);
deleteuser(list);
}
else
{
posleftnode->next = posnode->next;
free(posnode);
posnode=NULL; //滞空删除节点
printf("退卡成功!!\n");
}
}
//查找
struct node *findnode(struct node *headnode,char user_id[])
{
char id[MAX];
struct node *finnode = headnode->next;
while(finnode!=NULL&&strcmp(finnode->data.id,user_id))
{
finnode = finnode->next;
}
if(finnode==NULL)
{
printf("没有此会员信息,请重新输入:");
scanf("%s",id);
finnode = findnode(list,id);
}
else
{
return finnode;
}
}
//排序
void bubblesortlist(struct node *headnode)
{
struct node *p=headnode->next;
struct node *q=headnode->next;
for(p;p!=NULL;p=p->next)
{
for(q;q->next!=NULL;q=q->next)
{
//交换
if(q->data.buy > q->next->data.buy)
{
struct user pmove=q->data;
q->data=q->next->data;
q->next->data=pmove;
}
}
}
save("user.txt",list);
}
//文件存储
void save(const char *filename,struct node *headnode)
{
FILE *fp = fopen(filename,"w"); //打开文件
struct node *pmove = headnode->next;
while(pmove != NULL)
{
fprintf(fp,"%s %s %s %d %f %f %s\n"
,pmove->data.id,pmove->data.name,pmove->data.sex,pmove->data.age,pmove->data.balance,pmove->data.buy,pmove->data.phone);
pmove = pmove->next;
}
fclose(fp); //关闭文件
}
//文件读取
void read(const char *filename,struct node *headnode)
{
FILE *fp = fopen(filename,"r"); //打开文件
if(fp == NULL)
{ //打一次打开,文件不存在
fp = fopen(filename,"w+"); //创建出来
}
struct user tempdata; //定义临时变量
while(fscanf(fp,"%s%s%s%d%lf%lf%s"
,tempdata.id,tempdata.name,tempdata.sex,&tempdata.age,&tempdata.balance,&tempdata.buy,tempdata.phone) != EOF)
{
insertnode(list,tempdata);
}
fclose(fp); //关闭文件
}
//储存密码
void save_password(const char *filename,char *pasword)
{
FILE *fp = fopen(filename,"w"); //打开文件
char saveword[MAX];
strcpy(saveword,pasword);
fprintf(fp,"%s",saveword);
fclose(fp);
}
//密码读取
void read_password(const char *filename,char *pasword)
{
FILE *fp = fopen(filename,"r"); //打开文件
if(fp == NULL)
{ //打一次打开,文件不存在
fp = fopen(filename,"w+"); //创建出来
}
fscanf(fp,"%s",pasword);
fclose(fp);
}
//密码登录
void pass()
{
char s1[MAX];
int b=0;
int flg=0;
int j;
printf("请输入密码(共有3次机会):"); //密码重复输入三次
for(b;b<3;b++)
{ //定义密码输入的次数
gets(s1); //接收密码
printf("已经使用%d次机会\n",b+1);
read_password("password.txt",password);
j=strcmp(s1,password); //与原有密码进行比较
if(j==0)
{
printf("登陆成功!!!!");
Sleep(1000);
flg=1;
break; //正确则标志变量置1 否则继续循环
}
}
if(flg==0)
{ //根据标志变量的值决定是否继续执行
printf("密码错误!!");
Sleep(1000);
exit(0);
}
}
//修改密码
void newpassword(const char *filename,char *pasword)
{
system("cls");
printf("-------------------------【修改密码】----------------------\n");
char new_password[MAX];
new_password==pasword;
printf("请输入新的密码:");
scanf("%s",new_password);
save_password("password.txt",new_password);
read_password("password.txt",new_password);
printf("修改成功!!!\n");
Sleep(1500);
system("cls");
main();
}
//表头
void table_head()
{
printf("+----------+------+------+------+--------+--------+-----------+\n");
printf("| 卡号 | 姓名 | 性别 | 年龄 | 余额 |累计消费| 电话 |\n");
printf("+----------+------+------+------+--------+--------+-----------+\n");
}
//表尾
void table_foot()
{
printf("+----------+------+------+------+--------+--------+-----------+\n");
}
//新会员登记
void newuser()
{
struct user tempuser;
printf("请输入用户信息:\n");
printf("姓名:");
scanf("%s",tempuser.name);
printf("卡号:");
scanf("%s",tempuser.id);
printf("性别:");
scanf("%s",tempuser.sex);
printf("年龄:");
scanf("%d",&tempuser.age);
printf("缴费金额:");
scanf("%lf",&tempuser.balance);
printf("电话:");
scanf("%s",tempuser.phone);
insertnode(list,tempuser);
tempuser.buy=0;
save("user.txt",list); //保存至文件
printf("录入完成!!\n");
}
//会员信息修改
void revise(struct node *headnode)
{
system("cls");
printf("-------------------------【会员信息修改】----------------------\n");
int n;
char m;
char user_id[MAX];
struct node *pmove=headnode;
struct node *pfind;
printlist(list);
printf("\n请输入该会员的卡号(“N”返回菜单):");
scanf("%s",user_id);
if(strcmp("n",user_id)==0||strcmp("N",user_id)==0)
{
save("user.txt",list);
menu();
}
else
{
pfind = findnode(pmove,user_id);
printf("该会员的基本信息如下\n");
table_head(); //打印出要修改的信息
printf("|%10s|%6s|%6s|%6d|%8.2f|%8.2f|%11s|\n",
pfind->data.id,pfind->data.name,pfind->data.sex,pfind->data.age,pfind->data.balance,pfind->data.buy,pfind->data.phone);
table_foot();
printf("1.姓名\t2.卡号\t3.性别\t4.年龄\t5.电话\n");
printf("请输入要修改项目的序号:");
scanf("%d",&n);
switch(n)
{
case 1:
printf("请输入新的姓名:");
scanf("%s",pfind->data.name);
getchar();
printf("修改完成!!\n");
break;
case 2:
printf("请输入新的卡号:");
scanf("%s",pfind->data.id);
printf("修改完成!!\n");
break;
case 3:
printf("请输入新的性别:");
scanf("%s",pfind->data.sex);
printf("修改完成!!\n");
break;
case 4:
printf("请输入新的年龄:");
scanf("%d",&pfind->data.age);
printf("修改完成!!\n");
break;
case 5:
printf("请输入新的电话:");
scanf("%s",pfind->data.phone);
printf("修改完成!!\n");
break;
}
save("user.txt",list); //保存修改后的数据
printlist(list); //打印修改后的数据
printf("\n------------------------------------------------------\n");
printf("是否继续修改(Y / N):\n");
getchar();
m=getchar();
if(m=='y'||m=='Y')
{
revise(list);
}
else
{
menu();
}
}
}
//会员退卡
void deleteuser(struct node *headnode)
{
system("cls");
struct node *pmove=headnode;
printf("---------------------------【会员退卡】------------------------\n");
char user_id[MAX];
printlist(list);
printf("请输入会员卡号(“N”返回菜单):\n");
scanf("%s",user_id);
if(strcmp("n",user_id)==0||strcmp("N",user_id)==0)
{
save("user.txt",list);
menu();
}
else
{
delete(pmove,user_id); //调用删除函数
save("user.txt",list); //再次保存函数
}
}
//会员续费
void Renewal(struct node *headnode)
{
system("cls");
char user_id[MAX];
char down1;
double user_balance;
struct node *pmove=headnode;
struct node *pfind;
printf("---------------------------【会员续费】------------------------\n");
printlist(list);
printf("请输入会员卡号(“N”返回主菜单):\n");
scanf("%s",user_id);
if(strcmp("n",user_id)==0||strcmp("N",user_id)==0)
{
save("user.txt",list);
menu();
}
else
{
pfind = findnode(pmove,user_id);
printf("该会员的基本信息如下\n");
table_head(); //打印出要修改的信息
printf("|%10s|%6s|%6s|%6d|%8.2f|%8.2f|%11s|\n",
pfind->data.id,pfind->data.name,pfind->data.sex,pfind->data.age,pfind->data.balance,pfind->data.buy,pfind->data.phone);
table_foot();
printf("请输入要续费的金额:\n");
scanf("%lf",&user_balance);
pfind->data.balance=pfind->data.balance+user_balance; //续费操作
save("user.txt",list); //保存修改后的数据
printf("续费成功!!\n");
printlist(list); //打印修改后的数据
printf("\n------------------------------------------------------\n");
printf("是否继续修改(Y / N):\n");
getchar();
down1=getchar();
if(down1=='y'||down1=='Y')
{
Renewal(list);
}
else
{
menu();
}
}
}
//会员消费
void consume(struct node *headnode)
{
system("cls");
struct node *pmove=headnode;
struct node *pfind;
char user_id[MAX];
char down1;
double user_buy;
printf("--------------------【消费结算】------------------\n");
printlist(list);
printf("请输入会员卡号(“N”返回主菜单):\n");
scanf("%s",user_id);
if(strcmp("n",user_id)==0||strcmp("N",user_id)==0)
{
save("user.txt",list);
menu();
}
else
{
pfind = findnode(pmove,user_id);
printf("该会员的基本信息如下\n");
table_head(); //打印出要修改的信息
printf("|%10s|%6s|%6s|%6d|%8.2f|%8.2f|%11s|\n",
pfind->data.id,pfind->data.name,pfind->data.sex,pfind->data.age,pfind->data.balance,pfind->data.buy,pfind->data.phone);
table_foot();
printf("请输入花费金额:\n");
if(pfind->data.buy>=1000)
{
vip(pfind);
}
else
{
scanf("%lf",&user_buy);
if(user_buy>pfind->data.balance) //判断余额是否充足
{
printf("余额不足!!请缴费!\n");
}
else
{
pfind->data.balance=pfind->data.balance-user_buy;
pfind->data.buy=pfind->data.buy+user_buy;
if(pfind->data.buy>=1000)
{
printf("恭喜成为VIP会员,之后的您的消费将享有9折优惠\n");
}
}
save("user.txt",list); //保存修改后的数据
table_head(); //打印修改后的信息
printf("|%10s|%6s|%6s|%6d|%8.2f|%8.2f|%11s|\n",
pfind->data.id,pfind->data.name,pfind->data.sex,pfind->data.age,pfind->data.balance,pfind->data.buy,pfind->data.phone);
table_foot();
Sleep(1500);
menu();
}
}
}
//VIP消费
void vip(struct node *headnode)
{
struct node* pmove=headnode;
double user_buy;
printf("尊贵的VIP会员,请输入花费金额:\n");
scanf("%lf",&user_buy);
if(user_buy>pmove->data.balance)
{ //判断余额是否充足
printf("余额不足!!请缴费!");
}
else
{
pmove->data.balance=pmove->data.balance-user_buy*0.9;
pmove->data.buy=pmove->data.buy+user_buy*0.9;
}
save("user.txt",list); //保存修改后的数据
table_head(); //打印修改后的信息
printf("|%10s|%6s|%6s|%6d|%8.2f|%8.2f|%11s|\n",
pmove->data.id,pmove->data.name,pmove->data.sex,pmove->data.age,pmove->data.balance,pmove->data.buy,pmove->data.phone);
table_foot();
Sleep(1500);
menu();
}
//会员挂失
void Loss(struct node *headnode)
{
system("cls");
struct node *pmove=headnode;
struct node *pfind;
char user_id[MAX];
char new_id[MAX];
char down1;
printf("--------------------【会员卡挂失】------------------\n");
printlist(list);
printf("请输入会员卡号(“N”返回主菜单):\n");
if(strcmp("n",user_id)==0||strcmp("N",user_id)==0)
{
save("user.txt",list);
menu();
}
else
{
pfind = findnode(pmove,user_id);
printf("该会员的基本信息如下\n");
table_head(); //打印出要修改的信息
printf("|%10s|%6s|%6s|%6d|%8.2f|%8.2f|%11s|\n",
pfind->data.id,pfind->data.name,pfind->data.sex,pfind->data.age,pfind->data.balance,pfind->data.buy,pfind->data.phone);
table_foot();
printf("请输入新的卡号:");
scanf("%s",new_id);
strcpy(pfind->data.id,new_id);
save("user.txt",list); //保存修改后的数据
printf("补办成功!!\n");
table_head(); //打印修改后的信息
printf("|%10s|%6s|%6s|%6d|%8.2f|%8.2f|%11s|\n",
pfind->data.id,pfind->data.name,pfind->data.sex,pfind->data.age,pfind->data.balance,pfind->data.buy,pfind->data.phone);
table_foot();
Sleep(1000);
menu();
}
}
//统计
void statistics(struct node *headnode)
{
double sum_payment=0; //缴费
double sum_buy=0; //消费
struct node *user_payment=headnode->next;
struct node *user_buy=headnode->next;
for(user_payment;user_payment!=NULL;user_payment=user_payment->next)
{
sum_payment=sum_payment+user_payment->data.balance+user_payment->data.buy; //缴费总额=余额+消费额
}
for(user_buy;user_buy!=NULL;user_buy=user_buy->next)
{
sum_buy=sum_buy+user_buy->data.buy;
}
printf("+-------------------------------+--------+--------+-----------+\n");
printf("| |缴费总额|消费总额| |\n");
printf("+-------------------------------+--------+--------+-----------+\n");
printf("| |%8.2f|%8.2f| |\n",
sum_payment,sum_buy);
printf("+-------------------------------+--------+--------+-----------+\n");
}