个人帐簿管理系统记录某人每月的全部收入及各项开支情况,包括食品消费,房租,子女教育费用,水电费,医疗费,储蓄等。进入系统后可以输入和修改某月的收支情况,可以对每月的开支从小到大进行排序,可以根据输入的月份查询每月的收支情况。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*账户开支信息*/
struct Count{
int month; //月份
float income; //总收入
float outcom; //总支出
float food; //食品消费
float rent; //房租
float education; //子女教育费
float water; //水电费
float hospital; //医疗费
float save; //储蓄
float other; //其他
};
/*个人账户每月详细开支信息*/
struct Node;
typedef struct Node *pNode;
struct Node {
struct Count acount; //账户各项数据
pNode nodelink; //账户按月开支的节点
};
typedef struct Node *LinkList;
/*已经存在的账户*/
struct CountList;
typedef struct CountList* PCountList;
struct CountList {
char name[20]; //账户名
LinkList llink; //指向账户每月账单信息的头节点
PCountList pclink; //指向下一个账户
};
void Menu();
void NewCount();
void SearchCount();
int IsCountExist(char* name);
void AddMonthData(PCountList pcount);
LinkList creatNullLinkList();
PCountList CreatNullCountList();
FILE *fp;
int i,mon;
PCountList CreatNullCountList() {
PCountList pclist;
pclist = (PCountList) malloc (sizeof (struct CountList));
if (NULL == pclist) {
printf ("新建账户内存出错!\n");
return NULL;
}
pclist->pclink = NULL;
pclist->llink = creatNullLinkList();
return pclist;
}
void NewCount(){
PCountList pcount;
pcount = CreatNullCountList();
printf("输入账户名:");
scanf("%s",pcount->name);
if (IsCountExist(pcount->name)){
printf("账户已经存在!请进行其他操作!\n");
return ;
}
else {
fp = fopen(pcount->name,"w");
fputs( "月份 食品消费 房 租 子女教育 水电费 医 疗 储 蓄 其 他\n",fp);
fclose(fp);
printf("输入月份数目:");
scanf("%d",&mon);
for(i=0;i<mon;i++) {
AddMonthData(pcount);
}
/**/
}
}
void Menu() {
printf("\n\t\t\t\t主功能菜单 \t\t\t\t");
printf("\n\t\t\t\t1.新建账户\t\t\t\t");
printf("\n\t\t\t\t2.查询账户\t\t\t\t");
printf("\n\t\t\t\t3. \t\t\t\t");
printf("\n\t\t\t\t4. \t\t\t\t");
printf("\n\t\t\t\t5. \t\t\t\t");
printf("\n\t\t\t\t6. \t\t\t\t");
printf("\n\t\t\t\t0.退出程序 \t\t\t\t");
}
FILE* fp;
char ch;
int temp;
/*查看账户菜单*/
void LookCountMenu() {
printf("\n\t\t\t\t 查询菜单 \t\t\t\t");
printf("\n\t\t\t\t1.查询全部开支 \t\t\t\t\t");
printf("\n\t\t\t\t2.添加某月开支 \t\t\t\t\t");
printf("\n\t\t\t\t3.修改某月开支 \t\t\t\t\t");
printf("\n\t\t\t\t4.对月总支出排序\t\t\t\t\t");
printf("\n\t\t\t\t5.查询某月开支 \t\t\t\t\t");
printf("\n\t\t\t\t0.返回 \t\t\t\t\t");
}
/*按月份查询收支情况*/
void LookMonthData() {
}
/*按月份重置收支情况*/
void ResetMonthData() {
}
/*按月支出排序而从小到大*/
void SortOutcomeData() {
}
/*查看全部的开支情况*/
void LookCount(char *name) {
fp = fopen(name,"r");
while((ch=getc(fp)) != EOF)
printf("%c",ch);
fclose(fp);
}
void SearchCount() {
//char name[20];
PCountList pcount;
pcount = CreatNullCountList();
while(1){
printf("\n输入要查找的账户:");
scanf("%s",pcount->name);
if((fp = fopen(pcount->name,"r")) == NULL)
printf("\n账户不存在,请核对后重先输入!");
else {
fclose(fp);
break;
}
}
while(1) {
LookCountMenu();
printf("\n选择菜单:");
scanf("%d",&temp);
switch (temp) {
case 0:
return;
case 1:
LookCount(pcount->name);
break;
case 2:
AddMonthData(pcount);
break;
case 3:
ResetMonthData();
break;
case 4:
SortOutcomeData();
break;
case 5:
LookMonthData();
break;
}
}
}
/*判断账户是否存在*/
int IsCountExist(char* name) {
FILE *fp;
if((fp = fopen(name,"r")) != NULL)
return 1;
else
return 0;
}
FILE *fp;
/*为账户信息头结点们配空间*/
LinkList creatNullLinkList(){
LinkList llist;
llist = (LinkList) malloc (sizeof (struct Node));
if (NULL == llist) {
printf ("memory allocation error!\n");
return NULL;
}
llist->nodelink = NULL;
return llist;
}
/*按月添加数据*/
pNode InsertCountData(char *name) {
pNode p = (pNode) malloc (sizeof (struct Node));
if (p == NULL){
printf ("内存出错!返回\n");
return NULL;
}
/*输入各项开支*/
printf("%s","输入月份:");
scanf("%d",&p->acount.month); /*需在这儿增加判断功能,如果输入的月份已经存在则返回*/
//if ();
printf("%20s","输入食品消费:");
scanf("%f",&p->acount.food);
printf("%20s","输入房租:");
scanf("%f",&p->acount.rent);
printf("%20s","输入子女教育费:");
scanf("%f",&p->acount.education);
printf("%20s","输入水电费:");
scanf("%f",&p->acount.water);
printf("%20s","输入医疗费:");
scanf("%f",&p->acount.hospital);
printf("%20s","输入储蓄费:");
scanf("%f",&p->acount.save);
printf("%20s","输入其他费用:");
scanf("%f",&p->acount.other);
/*把刚添加的数据写入到文件*/
fp = fopen(name,"a");
fprintf(fp,"%-4d%9.2f%9.2f%9.2f%9.2f%9.2f%9.2f%9.2f\n",
p->acount.month,p->acount.food,p->acount.rent,
p->acount.education,p->acount.water,p->acount.hospital,
p->acount.save,p->acount.other);
fclose(fp);
return p;
}
/*按月份添加收支情况*/
void AddMonthData(PCountList pcount) {
pNode mlink; //按月链表
mlink = creatNullLinkList();
mlink = InsertCountData(pcount->name);
if (mlink == NULL){
return ;
}
while(pcount->llink->nodelink != NULL ) {
pcount->llink->nodelink = pcount->llink->nodelink->nodelink;
}
pcount->llink->nodelink = mlink;
pcount->llink->nodelink->nodelink = NULL;
free(mlink);
}
int main() {
int n;
while(1){
Menu();
printf("\n选择菜单:");
scanf("%d",&n);
//n = atoi(ch);
switch(n) {
case 1:
NewCount();
break;
case 2:
SearchCount();
break;
case 0:
exit(0);
default:
printf("\n输入错误!");
break;
}
}
return 0;
}
第二天写得代码