课程设计账户管理系统(1)

 

个人帐簿管理系统记录某人每月的全部收入及各项开支情况,包括食品消费,房租,子女教育费用,水电费,医疗费,储蓄等。进入系统后可以输入和修改某月的收支情况,可以对每月的开支从小到大进行排序,可以根据输入的月份查询每月的收支情况。

 

#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;
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章