current position:Home>Help me look at the code (I'm just a freshman, so I only know C). Why the bill The data in txt will always become 0 (after each command 1 is executed and more than one order is entered, only the last order in txt is saved, and all the others become 0) (o
Help me look at the code (I'm just a freshman, so I only know C). Why the bill The data in txt will always become 0 (after each command 1 is executed and more than one order is entered, only the last order in txt is saved, and all the others become 0) (o
2022-02-02 19:20:17 【CSDN Q & A】
#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(" Please start entering the bill .\n"); char a;do { printf("\n Bill summary :"); getchar(); gets(stu[i].billName); printf(" amount of money :"); scanf("%lf",&stu[i].money); printf(" date :"); scanf("%d",&stu[i].time.year); scanf("%d",&stu[i].time.month); scanf("%d",&stu[i].time.day); printf(" The order number :"); scanf("%lld",&stu[i].billID); printf(" *** Please from “ consumption ”“ Loan repayment ”“ Human relations ” Choose among the three .***\n"); printf(" Bill classification :"); scanf("%s",stu[i].billSort); printf(" remarks :"); scanf("%s",stu[i].note); z++; printf("\n Enter next item (Y/N)\n"); scanf(" %c",&a); while(a!='Y'&&a!='N')// Prevent illegal import { printf(" illegal input , Please enter the correct instruction \n"); getchar(); scanf("%c",&a); }
}while(a=='Y');// Refuse to enter the next item
WritetoFile(stu,z);
goto WEB;
case 2: n=ReadfromFile(stu); count=n;// Chronological array for(j=0;j<count-1;j++)// Annual ranking { 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++)// Monthly sorting { 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++)// Daily sorting { 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); // Query material information by date printf(" Please enter the date you want to find :\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(" There is no bill corresponding to this date "); x=0;// Empty x Value to find again 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 The proportion of monthly consumption is :%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," consumption \0")==0) { sum1=sum1+stu[i].money; } if(strcmp(stu[i].billSort," Loan repayment \0")==0) { sum2=sum2+stu[i].money; } if(strcmp(stu[i].billSort," Human relations \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(" The proportion of consumption is :%0.1f%%\n",p1*100); printf(" The proportion of loan and repayment is :%0.1f%%\n",p2*100); printf(" The proportion of human relations is :%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(" The highest record is :\n"); PrintMessage(stu,i); } if(stu[i].money==min) { printf("\n------------------------------\n"); printf(" The lowest record is :\n"); PrintMessage(stu,i); }} goto WEB; default : printf(" Your input is wrong !\n"); goto WEB; case 0: exit(0); }return 0;
}
void web(void)
{
printf("\n Alipay billing information management system \n");
printf(" \n 1. Bill information entry \n");
printf(" 2. Sort and output bill information by date \n");
printf(" 3. Query bill information by date \n");
printf(" 4. Statistics of consumption amount by month \n");
printf(" 5. Statistical analysis by bill category \n");
printf(" 6. Query the bills with the least amount and the most amount \n");
printf(" 0. Exit the system \n");
printf(" \n Tips : Please enter the numerical sequence number and select the corresponding operation ");
}
void PrintMessage(BILL stu[],int i)
{
printf(" Bill summary :%s\n",stu[i].billName);
printf(" amount of money :%.1f\n",stu[i].money);
printf(" date :%3d-%02d-%02d\n",stu[i].time.year,stu[i].time.month,stu[i].time.day);
printf(" The order number :%I64d\n",stu[i].billID);
printf(" Bill classification :%s\n",stu[i].billSort);
printf(" remarks :%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(" The file was not found !\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(" The file was not found !\n");
exit(0);
}
for(i=0;!feof(fp);i++){ fread(&stu[i],sizeof(BILL),1,fp); } fclose(fp); return i-1;
}
Refer to the answer 1:
you are here do while Use... In circulation stu[i] Record everyone's information , But this i Always during the cycle 0 ah , What you add is z No i, So it's equal to always modifying the information of the same person . hold z++ Change to i++, And put it after all the data is input
Or put all i Change to z, Because what you write to the file is z
Refer to the answer 2:
copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202021920007721.html
The sidebar is recommended
- C language file encryption applet problem
- Three types of MPEG images
- How to write this program code
- Questions about data structure: data structure sequence table (language-c language)
- A problem of energy economics
- Vscode why does Python have two problems when the external window is running? Press any key to continue?
- Using 7474n to build JK Trigger and D trigger.
- C language two-dimensional array, for interpretation
- When simulating with botu and factory IO, factory IO I2 The contacts behind 0 are not working
- How to solve termux qqbot error?
guess what you like
-
C + + getline cannot correctly read the console input Chinese
-
Android development bookkeeping
-
About the problem of C language: input 12 + 3 with DEVC + + to get 12.000000. I don't know what's wrong, include & lt; stdio. h&gt;
-
Prompt the user to enter an integer n and N real numbers, calculate and output the sum of the negative numbers in the input n real numbers.
-
Using C52 and LCD1602 to simulate an electronic clock, how to program four modes and one LED flashing for four keys
-
Do any friends know how to do this? Please
-
For problems occurred during GCN reproduction, it is suggested that the openposedemo file cannot be found
-
TypeError: string indices must be integers
-
I really can't find answers to the exercises after freshman class.
-
How is the MSE value compared with the vector?
Random recommended
- How to set a draw when making tic tac toe chess in Python
- Solving Python language C language
- How does Maple set a variable to be any integer from a certain interval
- How to realize that the opening of a website computer is one interface and the opening of a mobile phone is another interface, similar to self adaptation?
- After entering two indefinite length arrays and sorting them, merge and sort the two arrays again
- The computer has a web, but it can't access other web pages except the navigation page display
- C + + how to do this program
- Want to know how to find the parameters of this function? How should the matlab code knock
- Multi module project cannot inject mapper java
- MapReduce average score how to find the average of each line
- Wechat applet rendering layer error
- What does this mean? Why can't I open my QQ
- CMD run Py file encountered a problem
- Help me see what's wrong
- Why not 13bingo
- Decipher the password and there is no result after running?
- Ask you why this code can't run (◞‸◟)
- Hello, can you extract the of our applet
- Why not show folders when attaching a database
- How can the last line end with 0 without spaces
- Generate images from HTML pages and download them into PDF files
- Two Python experiment questions
- Hidden display problems in HTML
- Is there any good analysis process for push box problem
- What's going on here?.,!
- Cyclic input and output. If the input is a number, it will print. If the input is not a number, it will print No
- C language freshman PTA input an English sentence, change the first letter of each word into capital letters.
- Question e: snake walking, please help me
- Why is the attenuation of wave height in numerical simulation serious?
- Difference between digital image processing and image signal processing (ISP)
- Oracle a column of multi value update problem.
- Write a program in Python.
- How can Python write here to become a variable
- Which rstudio is installed in win7 64 bit? Dear friends, the previous connection can install w10,8,7, but now it can only install W10. Cry.
- Which is better, CCD camera tracking or gray sensor tracking
- Virtual reality combined with AR space
- How to click two label controls in QT to subtract the customized numbers in the control
- Ask you why there is a period of garbled code in front of the output?
- QR code generation of shopping scanning payment and third-party payment
- The Internet is a large network composed of many small networks. Can a local area network also be such a small network? What's more, how large is the coverage of a small network in a large network?