满意答案
tg3656dfd
.04.03
采纳率:55%等级:9
已帮助:317人
#include
#include
#include
#pragmapack(1)
constchar*DataFilename="d:\\classfee.data";
typedefstructclassfeeClassFee;
structclassfee{
intid;
chario;
charcbr[10];
charreason[256];
floatfee;
charmemo[256];
floatrestfee;
ClassFee*next;
};
voidShowMenu(){
constchar*menu={
"1.添加班费收支信息\n"
"2.浏览所有班费收支信息\n"
"3.查询班费收支信息\n"
"4.修改班费收支信息\n"
"5.退出\n请选择:\n"
};
printf("%s",menu);
}
ClassFee*fee;
intindex=0;
floatlastfee=0.0f;
intInitDataFile(){
FILE*fp=fopen(DataFilename,"rb");
size_tsizefee=sizeof(ClassFee);
fee=(ClassFee*)malloc(sizefee);
fee->next=NULL;
if(fp==NULL){
return1;
}
ClassFee*p=fee;
size_tlen;
while(!feof(fp)){
ClassFee*q=(ClassFee*)malloc(sizefee);
len=fread((char*)q,sizefee,1,fp);
if(len==1){
index++;
lastfee=q->restfee;//最后一次取得数据为最后余额
q->next=NULL;
p->next=q;
p=q;
}
}
fclose(fp);
return0;
}
intWriteDataFile(){
FILE*fp=fopen(DataFilename,"wb");
if(fp==NULL){
printf("无法打开数据文件:%s,请检查。\n",DataFilename);
return1;
}
size_tsizefee=sizeof(ClassFee);
ClassFee*p=fee->next;
while(p){
fwrite((char*)p,sizefee,1,fp);
p=p->next;
}
fclose(fp);
return0;
}
voidFreeResource(){
ClassFee*p=fee,*q=NULL;
while(p){
q=p->next;
free(p);
p=q;
}
}
voidAppendFee(ClassFee*f){
ClassFee*p=fee,*q;
q=p->next;
while(q){
p=q;
q=p->next;
}
p->next=f;
}
voidInputFee(){
ClassFee*p=(ClassFee*)malloc(sizeof(ClassFee));
p->id=++index;
fflush(stdin);
printf("输入费用信息:\n");
printf("支出(o)?收入(i)?:");
scanf("%c",&p->io);
if(p->io!='i'&&p->io!='o'){
printf("输入错误,按收入处理,此处应该做成循环输入,直到输入成功为止,这里省略\n");
p->io='i';
}
printf("经办人:");scanf("%s",p->cbr);
printf("原因:");scanf("%s",p->reason);
printf("金额:");scanf("%f",&p->fee);
printf("备注:");scanf("%s",p->memo);
p->restfee=p->io=='i'?(lastfee+p->fee):(lastfee-p->fee);
lastfee=p->restfee;
p->next=NULL;
AppendFee(p);
printf("-------------------------------------------------\n");
}
voidDisplayFee(ClassFee*p){
printf("收支编号:%d\n",p->id);
printf("收入/支出:%s\n",p->io=='i'?"收入":"支出");
printf("经办人:%s\n",p->cbr);
printf("原因:%s\n",p->reason);
printf("金额:%.2f\n",p->fee);
printf("备注:%.2f\n",p->memo);
printf("余额:%.2f\n",p->restfee);
printf("-------------------------------------------------\n");
}
voidSearch(){
ints;
constchar*msg[]={"输入收支编号:","输入原因:"};
printf("输入查询条件:\n1.按收支编号查询\n2.按原因查询:");
fflush(stdin);
scanf("%d",&s);
ClassFee*p=fee->next;
if(s==1){
size_ts0;
printf("%s",msg[s-1]);
scanf("%d",&s0);
while(p){
if(p->id==s0){
DisplayFee(p);
break;
}
p=p->next;
}
}elseif(s==2){
charr[256];
printf("%s",msg[s-1]);
scanf("%s",r);
while(p){
if(strstr(p->reason,r)!=NULL){
DisplayFee(p);
}
p=p->next;
}
}
}
voidModifyFee(){
size_ts;
printf("输入收支编号:");
scanf("%d",&s);
ClassFee*p=fee->next;
while(p){
if(p->id==s){
DisplayFee(p);
printf("输入修改信息:\n-------------------------------\n");
//这里对p进行修改,这个比较麻烦,不单单是修改本节点的信息
//如果对fee和收入/支出做了修改,则需要修改后续所有节点的restfee的信息
//有点麻烦,不想写了,你自己补上这块即可。
break;
}
p=p->next;
}
}
intmain(){
intchoice;
InitDataFile();
printf("index=%d,lastfee=%.2f\n",index,lastfee);
while(1){
ShowMenu();
scanf("%d",&choice);
if(choice<1||choice>5){
system("cls");
printf("你的输入错误,请重新输入\n------------------------\n");
continue;
}else{
if(choice==5){
WriteDataFile();
break;
}elseif(choice==1){
InputFee();
}elseif(choice==2){
system("cls");
ClassFee*p=fee->next;
while(p){
DisplayFee(p);
p=p->next;
}
}elseif(choice==3){
Search();
}elseif(choice==4){
ModifyFee();
}
}
}
return0;
}
00分享举报