居民水费管理系统
#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;
?? ? } ?? ?
?}
?
總結
- 上一篇: C语言中生成随机数
- 下一篇: C语言-随机数的生成