主题:求教这段C代码错在哪里?? ??
#include <stdio.h>
#include <string.h>
struct family
{
int year;//年份
int month;//月份
char name[15];//姓名
int money;//钱
char detail[100];//备注
struct family *next;
};
struct family *incomehead=NULL;
struct family *payouthead=NULL;
void cover()//界面函数
{
printf("*******************\n");
printf("收入管理:\n");
printf(" 1添加收入处理\n");
printf(" 3查询收入处理\n");
printf(" 5删除收入处理\n");
printf(" 7修改收入处理\n");
printf("******************\n");
printf("支出管理:\n");
printf(" 2添加支出处理\n");
printf(" 4查询支出处理\n");
printf(" 6删除支出处理\n");
printf(" 8修改支出处理\n");
printf("******************\n");
printf("统计管理:\n");
printf(" 9统计收入\n");
printf(" 10统计支出\n");
}
void initincomehead(struct family *head)
{
FILE *fp;
int count=0;
struct family *p1,*p2;
p1=(struct family *)malloc(sizeof(struct family));
memset(p1,0,sizeof(struct family));
p1->next=NULL;
fp=fopen("c.txt","rb");
if(fp==NULL) printf("文件不存在,没有已有信息存在\n");//判断是否打开文件
else
{
while(fread(p1,sizeof(struct family),1,fp)==1)
{
count++;
if(count==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct family));//开辟空间
memset(p1,0,sizeof(struct family));
p1->next=NULL;
}
p2->next=NULL;
free(p1);
fclose(fp);
printf("载入信息成功\n");
}
}
void initpayouthead(struct family *head)
{
FILE *fp;
int count=0;
struct family *p1,*p2;
p1=(struct family *)malloc(sizeof(struct family));//开辟空间
memset(p1,0,sizeof(struct family));
p1->next=NULL;
fp=fopen("d.txt","rb");
if(fp==NULL) printf("文件不存在,没有已有信息存在\n");//判断是否打开文件
else
{
while(fread(p1,sizeof(struct family),1,fp)==1)
{
count++;
if(count==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct family));
memset(p1,0,sizeof(struct family));
p1->next=NULL;
}
p2->next=NULL;
free(p1);
fclose(fp);
printf("载入信息成功\n");
}
}
add_data(struct family *head)//添加函数
{
struct family *p,*p1;
p=(struct family *)malloc(sizeof(struct family));
printf("请输入年\n");
scanf("%d",&p->year);
printf("请输入月\n");
scanf("%d",&p->month);
printf("请输入姓名\n");
scanf("%s",p->name);
printf("请输入钱\n");
scanf("%d",&p->money);
printf("请输入备注\n");
scanf("%s",p->detail);
p->next=NULL;
if(head==NULL)
head=p;
else
{
p1=head;
while(p1!=NULL&&p1->next!=NULL)
{
p1=p1->next;
}
p1->next=p;
return(head);
printf("添加成功\n");
}
}
void search_data(struct family *head)//查询函数
{
struct family *p;
int year,month;
if(head!=NULL)
{
printf("请输入年:\n");
scanf("%d",&year);
printf("请输入月:\n");
scanf("%d",&month);
p=head;
while(p->next!=NULL&&strcmp(year,p->year)!=0) //判断是否存在要查询的信息
while(p->next!=NULL&&strcmp(month,p->month)!=0)
p=p->next;
if( strcmp(year,p->year)==0)
{
printf("你要查询的人的信息为:\n");
printf("%d %d %s %d %s\n",p->year,p->month,p->name,p->money,p->detail);
}
else
printf("查无此信息\n");
}
else
printf("kong\n");
}
void del_data(struct family *head)//删除函数
{
struct family *p1,*p2;
int year,month;
if(head==NULL) printf("kong\n");
else
{
p1=head;
printf("输入要删除的年:\n");
scanf("%d",&year);
printf("输入要删除的月:\n");
scanf("%d",&month);
while(p1->next!=NULL&&strcmp(year,p1->year)!=0) //判断是否存在要删除的信息
while(p1->next!=NULL&&strcmp(month,p1->month)!=0)
{
p2=p1;
p1=p1->next;
}
if( strcmp(year,p1->year)==0)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
}
else
printf("查无此信息\n");
}
free(p1);
}
void modify_data(struct family *head)//修改函数
{
struct family *p;
int year,month;
if(head==NULL) printf("kong\n");
else
{
printf("输入要修改的人的年:\n");
scanf("%d",&year);
printf("输入要修改的人的月:\n");
scanf("%d",&month);
p=head;
while(p->next!=NULL&&strcmp(year,p->year)!=0)//判断是否存在要修改的信息
while(p->next!=NULL&&strcmp(month,p->month)!=0)
p=p->next;
if( strcmp(year,p->year)==0)
{
printf("你要修改的信息为:\n");
printf("请输入年\n");
scanf("%d",&p->year);
printf("请输入月\n");
scanf("%d",&p->month);
printf("请输入姓名\n");
scanf("%s",p->name);
printf("请输入钱\n");
scanf("%d",&p->money);
printf("请输入备注\n");
scanf("%s",p->detail);
printf("重写输入的信息:\n");
scanf("%d %d %s %d %s\n",p->year,p->month,p->name,p->money,p->detail);
}
else
printf("查无此信息\n");
}
}
void total_income(struct family *head)//统计收入函数
{
struct family *p;
int sum=0,year;
printf("输入要统计的年份:\n");
scanf("%d",&year);
if(p==NULL) printf("没有该年的信息");
else
while(p->next!=NULL&&strcmp(year,p->year)!=0)
{
sum+=p->money;
p=p->next;
}
printf("该年的收入为:%d\n",sum);
}
void total_payout(struct family *head)//统计支出函数
{
struct family *p;
int sum=0,year;
printf("输入要统计的年份:\n");
scanf("%d",&year);
if(p==NULL) printf("没有该年的信息");
else
while(p->next!=NULL&&strcmp(year,p->year)!=0)
{
sum+=p->money;
p=p->next;
}
printf("该年的支出为:%d\n",sum);
}
void save1(struct family *head)//保存收入函数
{
FILE *fp;
struct family *p;
fp=fopen("c.txt","wb");
if(fp==NULL)printf("文件打开失败\n");
else
{
p=head;
while(p!=NULL)
{
fwrite(p,sizeof(struct family),1,fp);
p=p->next;
}
printf("保存成功\n");
}
fclose(fp);
}
void save2(struct family *head)//保存支出函数
{
FILE *fp;
struct family *p;
fp=fopen("d.txt","wb");
if(fp==NULL)printf("文件打开失败\n");
else
{
p=head;
while(p!=NULL)
{
fwrite(p,sizeof(struct family),1,fp);
p=p->next;
}
printf("保存成功\n");
}
fclose(fp);
}
main()//主函数
{
int choice;
printf("家庭财务管理系统\n");
initincomehead(incomehead);
initpayouthead(payouthead);
while(1)
{
cover();
scanf("%d",&choice);
switch(choice)
{
case 1:add_data(incomehead);save1(incomehead);break;
case 2:add_data(payouthead);save2(payouthead);break;
case 3:search_data(incomehead);break;
case 4:search_data(payouthead);break;
case 5:del_data(incomehead);save1(incomehead);break;
case 6:del_data(payouthead);save2(payouthead);break;break;
case 7:modify_data(incomehead);save1(incomehead);break;
case 8:modify_data (payouthead);save2(payouthead);break;
case 9:total_income(incomehead);save1(incomehead);break;
case 10:total_payout(payouthead);save2(payouthead);break;
case 11:exit(0);break;
default :printf("输入不合法\n");exit(0);
}
}
}
#include <string.h>
struct family
{
int year;//年份
int month;//月份
char name[15];//姓名
int money;//钱
char detail[100];//备注
struct family *next;
};
struct family *incomehead=NULL;
struct family *payouthead=NULL;
void cover()//界面函数
{
printf("*******************\n");
printf("收入管理:\n");
printf(" 1添加收入处理\n");
printf(" 3查询收入处理\n");
printf(" 5删除收入处理\n");
printf(" 7修改收入处理\n");
printf("******************\n");
printf("支出管理:\n");
printf(" 2添加支出处理\n");
printf(" 4查询支出处理\n");
printf(" 6删除支出处理\n");
printf(" 8修改支出处理\n");
printf("******************\n");
printf("统计管理:\n");
printf(" 9统计收入\n");
printf(" 10统计支出\n");
}
void initincomehead(struct family *head)
{
FILE *fp;
int count=0;
struct family *p1,*p2;
p1=(struct family *)malloc(sizeof(struct family));
memset(p1,0,sizeof(struct family));
p1->next=NULL;
fp=fopen("c.txt","rb");
if(fp==NULL) printf("文件不存在,没有已有信息存在\n");//判断是否打开文件
else
{
while(fread(p1,sizeof(struct family),1,fp)==1)
{
count++;
if(count==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct family));//开辟空间
memset(p1,0,sizeof(struct family));
p1->next=NULL;
}
p2->next=NULL;
free(p1);
fclose(fp);
printf("载入信息成功\n");
}
}
void initpayouthead(struct family *head)
{
FILE *fp;
int count=0;
struct family *p1,*p2;
p1=(struct family *)malloc(sizeof(struct family));//开辟空间
memset(p1,0,sizeof(struct family));
p1->next=NULL;
fp=fopen("d.txt","rb");
if(fp==NULL) printf("文件不存在,没有已有信息存在\n");//判断是否打开文件
else
{
while(fread(p1,sizeof(struct family),1,fp)==1)
{
count++;
if(count==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct family));
memset(p1,0,sizeof(struct family));
p1->next=NULL;
}
p2->next=NULL;
free(p1);
fclose(fp);
printf("载入信息成功\n");
}
}
add_data(struct family *head)//添加函数
{
struct family *p,*p1;
p=(struct family *)malloc(sizeof(struct family));
printf("请输入年\n");
scanf("%d",&p->year);
printf("请输入月\n");
scanf("%d",&p->month);
printf("请输入姓名\n");
scanf("%s",p->name);
printf("请输入钱\n");
scanf("%d",&p->money);
printf("请输入备注\n");
scanf("%s",p->detail);
p->next=NULL;
if(head==NULL)
head=p;
else
{
p1=head;
while(p1!=NULL&&p1->next!=NULL)
{
p1=p1->next;
}
p1->next=p;
return(head);
printf("添加成功\n");
}
}
void search_data(struct family *head)//查询函数
{
struct family *p;
int year,month;
if(head!=NULL)
{
printf("请输入年:\n");
scanf("%d",&year);
printf("请输入月:\n");
scanf("%d",&month);
p=head;
while(p->next!=NULL&&strcmp(year,p->year)!=0) //判断是否存在要查询的信息
while(p->next!=NULL&&strcmp(month,p->month)!=0)
p=p->next;
if( strcmp(year,p->year)==0)
{
printf("你要查询的人的信息为:\n");
printf("%d %d %s %d %s\n",p->year,p->month,p->name,p->money,p->detail);
}
else
printf("查无此信息\n");
}
else
printf("kong\n");
}
void del_data(struct family *head)//删除函数
{
struct family *p1,*p2;
int year,month;
if(head==NULL) printf("kong\n");
else
{
p1=head;
printf("输入要删除的年:\n");
scanf("%d",&year);
printf("输入要删除的月:\n");
scanf("%d",&month);
while(p1->next!=NULL&&strcmp(year,p1->year)!=0) //判断是否存在要删除的信息
while(p1->next!=NULL&&strcmp(month,p1->month)!=0)
{
p2=p1;
p1=p1->next;
}
if( strcmp(year,p1->year)==0)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
}
else
printf("查无此信息\n");
}
free(p1);
}
void modify_data(struct family *head)//修改函数
{
struct family *p;
int year,month;
if(head==NULL) printf("kong\n");
else
{
printf("输入要修改的人的年:\n");
scanf("%d",&year);
printf("输入要修改的人的月:\n");
scanf("%d",&month);
p=head;
while(p->next!=NULL&&strcmp(year,p->year)!=0)//判断是否存在要修改的信息
while(p->next!=NULL&&strcmp(month,p->month)!=0)
p=p->next;
if( strcmp(year,p->year)==0)
{
printf("你要修改的信息为:\n");
printf("请输入年\n");
scanf("%d",&p->year);
printf("请输入月\n");
scanf("%d",&p->month);
printf("请输入姓名\n");
scanf("%s",p->name);
printf("请输入钱\n");
scanf("%d",&p->money);
printf("请输入备注\n");
scanf("%s",p->detail);
printf("重写输入的信息:\n");
scanf("%d %d %s %d %s\n",p->year,p->month,p->name,p->money,p->detail);
}
else
printf("查无此信息\n");
}
}
void total_income(struct family *head)//统计收入函数
{
struct family *p;
int sum=0,year;
printf("输入要统计的年份:\n");
scanf("%d",&year);
if(p==NULL) printf("没有该年的信息");
else
while(p->next!=NULL&&strcmp(year,p->year)!=0)
{
sum+=p->money;
p=p->next;
}
printf("该年的收入为:%d\n",sum);
}
void total_payout(struct family *head)//统计支出函数
{
struct family *p;
int sum=0,year;
printf("输入要统计的年份:\n");
scanf("%d",&year);
if(p==NULL) printf("没有该年的信息");
else
while(p->next!=NULL&&strcmp(year,p->year)!=0)
{
sum+=p->money;
p=p->next;
}
printf("该年的支出为:%d\n",sum);
}
void save1(struct family *head)//保存收入函数
{
FILE *fp;
struct family *p;
fp=fopen("c.txt","wb");
if(fp==NULL)printf("文件打开失败\n");
else
{
p=head;
while(p!=NULL)
{
fwrite(p,sizeof(struct family),1,fp);
p=p->next;
}
printf("保存成功\n");
}
fclose(fp);
}
void save2(struct family *head)//保存支出函数
{
FILE *fp;
struct family *p;
fp=fopen("d.txt","wb");
if(fp==NULL)printf("文件打开失败\n");
else
{
p=head;
while(p!=NULL)
{
fwrite(p,sizeof(struct family),1,fp);
p=p->next;
}
printf("保存成功\n");
}
fclose(fp);
}
main()//主函数
{
int choice;
printf("家庭财务管理系统\n");
initincomehead(incomehead);
initpayouthead(payouthead);
while(1)
{
cover();
scanf("%d",&choice);
switch(choice)
{
case 1:add_data(incomehead);save1(incomehead);break;
case 2:add_data(payouthead);save2(payouthead);break;
case 3:search_data(incomehead);break;
case 4:search_data(payouthead);break;
case 5:del_data(incomehead);save1(incomehead);break;
case 6:del_data(payouthead);save2(payouthead);break;break;
case 7:modify_data(incomehead);save1(incomehead);break;
case 8:modify_data (payouthead);save2(payouthead);break;
case 9:total_income(incomehead);save1(incomehead);break;
case 10:total_payout(payouthead);save2(payouthead);break;
case 11:exit(0);break;
default :printf("输入不合法\n");exit(0);
}
}
}