worldjfq0 2021-12-17 01:03 采纳率: 0%
浏览 16

帮我看一下代码吧(刚大一,所以只会C),为什么账单.txt里的数据总是会变成0啊(每次执行命令1,输入超过一单后,txt里只有最后一单被保存了下来,其他全变成了0)(O∆O)可以的话帮我改改吧

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 200

typedef struct date
{
int year;
int month;
int day;
}DATE;

typedef struct bill
{
char billName[100];
double money;
DATE time;
long long int billID;
char billSort[20];
char note[100];
}BILL;

void web(void);
void PrintMessage(BILL stu[],int i);
void Exchange(BILL temp[],BILL stu[],int i,int j);
void WritetoFile(BILL stu[],int z);
int ReadfromFile(BILL stu[]);

int main()
{
BILL temp[200];
int i=0;int z=0;int n,j;
int count=0;

DATE timech;
int x=0;

double Sum[13]={0,0,0,0,0,0,0,0,0,0,0,0,0}; 
double S=0;
float P[100]={};

double sum1=0;
double sum2=0;
double sum3=0;
double sum=0;
float p1,p2,p3;
 
long int max,min;

BILL stu [N];

WEB: web();
int SLN;
scanf("%d",&SLN);

switch(SLN)
{
    case 1:
z=0;
printf("请开始输入账单。\n"); 
char a;
do 
{
    printf("\n 账单摘要:"); 
    getchar();
    gets(stu[i].billName);
    printf(" 金额:");
    scanf("%lf",&stu[i].money);
    printf(" 日期:");
    scanf("%d",&stu[i].time.year);
    scanf("%d",&stu[i].time.month);
    scanf("%d",&stu[i].time.day);
    printf(" 订单号:");
    scanf("%lld",&stu[i].billID);
    printf(" ***请从“消费”“借还款”“人情往来”三者中选择。***\n");
    printf(" 账单分类:");
    scanf("%s",stu[i].billSort);
    printf(" 备注:");
    scanf("%s",stu[i].note);
    z++;
    printf("\n是否录入下一项(Y/N)\n");
    scanf(" %c",&a);
    while(a!='Y'&&a!='N')//防止非法输入 
    {
    printf("非法输入,请输入正确指示\n");
    getchar();
    scanf("%c",&a);
    }

}while(a=='Y');//拒绝进入下一项
WritetoFile(stu,z);
goto WEB;

    case 2:
 n=ReadfromFile(stu);
 count=n;
//按时间先后排序数组
 for(j=0;j<count-1;j++)//年排序 
 {
     for(i=j+1;i<count;i++)
     {
     if(stu[j].time.year>stu[i].time.year)
     {
         Exchange(temp,stu,i,j);
     }
    }
 }
 for(j=0;j<count-1;j++)//月排序 
 {
     for(i=j+1;i<count;i++)
     {
     if(stu[j].time.month>stu[i].time.month)
      {
         Exchange(temp,stu,i,j);
     }
    }
 }
 for(j=0;j<count-1;j++)//日排序 
 {
     for(i=j+1;i<count;i++)
     {
     if(stu[j].time.year>stu[i].time.year)
      {
        Exchange(temp,stu,i,j);
     }
    }
 }
 for(i=0;i<count;i++)
 {
     printf("\n----------------------------------- \n");
    PrintMessage(stu,i);
    printf("\n");     
 }
 goto WEB;
        
    case 3:
    n=ReadfromFile(stu);
    //按日期查询物资信息
    printf("请输入您想查找的日期:\n");
    scanf("%d%d%d",&timech.year,&timech.month,&timech.day); 
    for(i=0;i<n;i++)
{
     if(timech.year==stu[i].time.year&&timech.month==stu[i].time.month&&timech.day==stu[i].time.day)
     {
    printf("\n----------------------------------- \n");
    PrintMessage(stu,i);
    printf("\n"); 
    x++; 
   }

  }
    if(x==0)
    printf("无对应此日期的账单");
    x=0;//清空x值以便再次查找 
      goto WEB;         
     
     
    case 4:
    n=ReadfromFile(stu);    
    
        for(i=0;i<n;i++)
   {
           for(j=1;j<=12;j++)
           {
         if(stu[i].time.month==j)
        {
             Sum[j]=Sum[j]+stu[i].money;
        }
         } 
         S=S+stu[i].money;

   }
         for(i=1;i<=12;i++)
        {
             P[i]=Sum[i]*1.0/S;
             printf("%d月消费占比为:%0.1f%%\n",i,P[i]*100);
        }
    goto WEB;    

    case 5:
        n=ReadfromFile(stu);
        for(i=0;i<n;i++)
   {
         if(strcmp(stu[i].billSort,"消费\0")==0)
         {
             sum1=sum1+stu[i].money;
         }
        if(strcmp(stu[i].billSort,"借还款\0")==0)
         {
             sum2=sum2+stu[i].money;
         } 
        if(strcmp(stu[i].billSort,"人情往来\0")==0)
        {
             sum3=sum3+stu[i].money;
         } 
         
        sum=sum+stu[i].money;
   }
        p1=sum1*1.0/sum;
        p2=sum2*1.0/sum;
        p3=sum3*1.0/sum;
        printf("消费占比为:%0.1f%%\n",p1*100);
        printf("借还款占比为:%0.1f%%\n",p2*100);
        printf("人情往来占比为:%0.1f%%\n",p3*100);
   
   goto WEB;    
    case 6:
 n=ReadfromFile(stu);
 
 max=stu[0].money;
 min=stu[0].money;
 for(i=1;i<n;i++)
 {
     if(stu[i].money>max)
     max=stu[i].money;
     if(stu[i].money<min)
     min=stu[i].money;
 }
 for(i=0;i<n;i++)
 {
     if(stu[i].money==max)
     {
     printf("\n------------------------------\n");
     printf("最高纪录为:\n");
    PrintMessage(stu,i);
    }
    if(stu[i].money==min)
    {
     printf("\n------------------------------\n");
     printf("最低纪录为:\n");
    PrintMessage(stu,i);    
    }
}
 goto WEB;
    
    default :
        
    printf("您输入错误!\n");
 goto WEB;
    
    case 0:
        exit(0);
        
}
return 0;     

}

void web(void)
{
printf("\n支付宝账单信息管理系统\n");
printf(" \n 1.账单信息录入\n");
printf(" 2.按日期排序输出账单信息\n");
printf(" 3.按日期查询账单信息\n");
printf(" 4.按月份统计消费金额\n");
printf(" 5.按账单类别统计分析\n");
printf(" 6.查询金额最少和最多的账单\n");
printf(" 0.退出系统\n");
printf(" \n提示:请输入数字序号选择对应的操作 ");
}

void PrintMessage(BILL stu[],int i)
{
printf("账单摘要:%s\n",stu[i].billName);
printf("金额:%.1f\n",stu[i].money);
printf("日期:%3d-%02d-%02d\n",stu[i].time.year,stu[i].time.month,stu[i].time.day);
printf("订单号:%I64d\n",stu[i].billID);
printf("账单分类:%s\n",stu[i].billSort);
printf("备注:%s\n",stu[i].note);
}

void Exchange(BILL temp[],BILL stu[],int i,int j)
{
temp[i]=stu[i];
stu[i]=stu[j];
stu[j]=temp[i];

}

void WritetoFile(BILL stu[],int z)
{
FILE*fp;
if((fp=fopen("bill.txt","a"))==NULL)
{
printf("没有找到该文件!\n");
exit(0);
}
fwrite(stu,sizeof(BILL),z,fp);
fclose(fp);
}

int ReadfromFile(BILL stu[])
{
FILE*fp;
int i;
if((fp=fopen("bill.txt","r"))==NULL)
{
printf("没有找到该文件!\n");
exit(0);
}

for(i=0;!feof(fp);i++)
{
    fread(&stu[i],sizeof(BILL),1,fp);
 }
 fclose(fp);
 return i-1; 

}

  • 写回答

1条回答 默认 最新

  • CSDN专家-link 2021-12-17 05:45
    关注

    你在do while循环中用stu[i]记录每个人信息,但这个i在循环过程中一直是0啊,你自增的是z不是i,所以等于一直在修改同一个人的信息。把z++改成i++,并且放到所有数据输入完之后
    或者把所有i改成z,因为你写入文件传入的是z

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 12月17日

悬赏问题

  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真
  • ¥15 关于#c语言#的问题,请各位专家解答!