#include<stdio.h>
#include<string.h>
#define N 2
struct waterfee
{
char name[20];
int cardid;
int totalvolume;
int monthvolume[12];
int checked;
int fee;//欠费
int residual;//余额
} ;//1.
struct waterfee wfs[N];
int initialize = 0;
char usename[]="yt",password[]="123";
void printfhelp();
void initializeinfo(struct waterfee x[],int n);
double calcfee(int volume);//5.
void sorttotal(struct waterfee x[],int n);//6.
void printrecord(struct waterfee x[],int n);//3.1)
void printarecord(struct waterfee x[],int idx);//3.2)
int find(struct waterfee x[],int n,int cardid);
void payfee(struct waterfee x[],int idx);//4.
int main(void)
{
int choice,idx;
int cardid;
char user[100],ps[100];
printf("请输入用户名:");
gets(user);
printf("请输入密码:");
gets(ps);
if(strcmp(user,usename) || strcmp(ps,password))
{
printf("用户名或密码错误,退出系统!\n");
return 0;
}
printfhelp();
while(1)
{
printf("请输入一个0-7的整数:");
scanf("%d",&choice);
while(choice<0 || choice>7)
{
printf("请输入一个0-7的整数:");
scanf("%d",&choice);
}
switch(choice)
{
case 0:
initializeinfo(wfs,N);//初始化数据
initialize = 1;
break;
case 1:
printf("查询时请输入你的卡号:");
scanf("%d",&cardid);
idx=find(wfs,N,cardid);
if(idx==-1)
printf("卡号不存在!\n");
else
printarecord(wfs,idx);//输出一条记录
break;
case 2:
printf("缴费前请输入你的卡号:");
scanf("%d",&cardid);
idx=find(wfs,N,cardid);
if(idx==-1)
printf("卡号不存在!\n");
else
{
payfee(wfs,idx);//个人缴费
printarecord(wfs,idx);
}
break;
case 4:
sorttotal(wfs,N);
break;
case 5:
printfhelp();
break;
case 6:
printrecord(wfs,N);
break;
}
if(choice == 7)
break;
}
return 0;
}//2.
void printfhelp()
{
printf("**********使用帮助*********\n");
printf("[0]数据初始化\n");
printf("[1]查询个人账户\n");
printf("[2]个人缴费\n");
printf("[3]欠费信息汇总\n");
printf("[4]数据排序\n");
printf("[5]调出菜单\n");
printf("[6]输出所有用户用水详情\n");
printf("[7]退出系统\n");
printf("***************************\n");
}
void initializeinfo(struct waterfee x[],int n)
{
int i,j;
int totalvolume;
for(i=0;i<n;i++)
{
totalvolume=0;
printf("输入用户姓名:");
fflush(stdin);
gets(x[i].name);
printf("输入用户水卡编号:");
scanf("%d",&x[i].cardid);
x[i].checked=0;
fflush(stdin);
for(j=0;j<12;j++)
{
printf("第%d个月的用水量:",j+1);
scanf("%d",&x[i].monthvolume[j]);//月份的合法性
while(x[i].monthvolume[j]<0)
scanf("%d",&x[i].monthvolume[j]);
totalvolume+=x[i].monthvolume[j];
}
x[i].totalvolume=totalvolume;
x[i].residual=0;
x[i].fee=(int)calcfee(x[i].totalvolume);
}
}
double calcfee(int volume)/7olume隐式保持非负
{
double pay=0;
double n1,n2;
n1=1500;
n2=n1+2200;
if(volume<100)
{
pay=volume*1.5;
}
else if(volume<200)
{
pay=n1+(volume-100)*2.2;
}
else
pay=n2+(volume-200)*3;
return pay;
}
void sorttotal(struct waterfee x[],int n)
{
int i,j;
struct waterfee t;
if(!initialize)
{
printf("数据未进行初始化,排序失败!\n");
return;
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(x[j].totalvolume>x[j+1].totalvolume)
{
t=x[j];x[j]=x[j+1];x[j+1]=t;
}
}
}
}
void printarecord(struct waterfee x[],int idx)
{
int j;
if(!initialize)
{
printf("数据未进行初始化!\n");
return;
}
printf("第%d个用户的信息\n",idx+1);
puts(x[idx].name);
printf("水卡编号:%d\n",x[idx].cardid);
printf("总用水量:%d吨\n",x[idx].totalvolume);
printf("每月用水详情:\n");
for(j=0;j<12;j++)
{
printf("第%d个月的用水量:%d\n",j+1,x[idx].monthvolume[j]);
}
if(x[idx].checked)
printf("当前费用已缴清!\n");
else
printf("用户欠费:%d\n",x[idx].fee);
printf("用户当前余额:%d\n",x[idx].residual);
}//5.
void printrecord(struct waterfee x[],int n)
{
int i;
if(!initialize)
{
printf("数据未进行初始化!\n");
return;
}
for(i=0;i<n;i++)
printarecord(x,i);
}
//根据卡号找出索引
int find(struct waterfee x[],int n,int cardid)
{
int i,res=-1;
for(i=0;i<n;i++)
{
if(x[i].cardid==cardid)
{
res=i;
break;
}
}
return res;
}
//缴费或充值
void payfee(struct waterfee x[],int idx)
{
int money;
if(!initialize)
{
printf("数据未进行初始化!\n");
return;
}
scanf("%d",&money);
while(money<=0)
scanf("%d",&money);
if(x[idx].fee>0)
{
if(money<x[idx].fee)
x[idx].fee-=money;
else
{
x[idx].fee=0;
x[idx].checked=1;
x[idx].residual=money-x[idx].fee;
}
}
else
{
x[idx].residual+=money;
}
}