課程設計賬戶管理系統(1)
阿新 • • 發佈:2019-01-26
個人帳簿管理系統記錄某人每月的全部收入及各項開支情況,包括食品消費,房租,子女教育費用,水電費,醫療費,儲蓄等。進入系統後可以輸入和修改某月的收支情況,可以對每月的開支從小到大進行排序,可以根據輸入的月份查詢每月的收支情況。
#include <stdio.h> #include <stdlib.h> #include <string.h> struct Count{ int month; //月份 char *name; //賬戶名 float food; //食品消費 float rent; //房租 float education; //子女教育費 float water; //水電費 float hospital; //醫療費 float save; //儲蓄 float other; //其他 }; void Menu(); void NewCount(); void SearchCount();
int main() {
char* ch;
int n;
while(1){
Menu();
printf("\n選擇選單:");
scanf("%s",ch);
n = atoi(ch);
switch(n) {
case 1:
NewCount();
break;
case 2:
SearchCount();
break;
case 0:
exit(0);
default:
printf("\n輸入錯誤!");
break;
}
}
return 0;
}
void NewCount(){ struct Count tempCount; FILE *fp; int i,mon; printf("輸入賬戶名:"); scanf("%s",tempCount.name); if ((fp = fopen(tempCount.name,"r"))!= NULL){ printf("賬戶已經存在!請進行其他操作!\n"); return ; } else { fp = fopen(tempCount.name,"w"); fputs( "月份 食品消費 房 租 子女教育 水電費 醫 療 儲 蓄 其 他\n",fp); printf("月份 食品消費 房 租 子女教育 水電費 醫 療 儲 蓄 其 他\n"); fclose(fp); printf("輸入月份數目:"); scanf("%d",&mon); for(i=0;i<mon;i++){ fp = fopen(tempCount.name,"a"); /*輸入各項*/ printf("%s","輸入月份:"); scanf("%d",&tempCount.month); printf("%20s","輸入食品消費:"); scanf("%f",&tempCount.food); printf("%20s","輸入房租:"); scanf("%f",&tempCount.rent); printf("%20s","輸入子女教育費:"); scanf("%f",&tempCount.education); printf("%20s","輸入水電費:"); scanf("%f",&tempCount.water); printf("%20s","輸入醫療費:"); scanf("%f",&tempCount.hospital); printf("%20s","輸入儲蓄費:"); scanf("%f",&tempCount.save); printf("%20s","輸入其他費用:"); scanf("%f",&tempCount.other); /*把各項寫入到檔案*/ fprintf(fp,"%-4d%9.2f%9.2f%9.2f%9.2f%9.2f%9.2f%9.2f\n", tempCount.month,tempCount.food,tempCount.rent, tempCount.education,tempCount.water,tempCount.hospital, tempCount.save,tempCount.other); fclose(fp); } } }
void Menu() { printf("\n-----------------------------------"); printf("\n- 1.新建賬戶 -"); printf("\n- 2.查詢賬戶 -"); printf("\n- 3. -"); printf("\n- 4. -"); printf("\n- 5. -"); printf("\n- 6. -"); printf("\n- 0.退出程式 -"); printf("\n-----------------------------------"); }
void SearchCount(){
FILE* fp;
char name[20],ch;
while(1){
printf("\n輸入要查詢的賬戶:");
scanf("%s",name);
if((fp = fopen(name,"r")) == NULL){
printf("\n賬戶不存在,請核對後重先輸入或者輸入“n”結束");
scanf("%c",&ch);
if(ch =='n'){
//fclose(fp);
return ;
}
else
continue;
}
while((ch=getc(fp)) != EOF)
printf("%c",ch);
fclose(fp);
break;
}
}