Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it
Article catalog
- preface I spent four days, more than 460 lines of code,
- During this period, many things learned before were consolidated, such as linked lists, structures, etc.
- And learned a lot of new things, such as file input and output -- fopen and so on
- It's a lie to say it's not difficult for me, but fortunately I persevered and succeeded. It's good
- It can also be regarded as a complete end to my C language learning in the last semester of my freshman year.
- In addition, I think some of the code in this article is related to the bug s encountered in the process of coding
- Maybe it can help some confused students
-
I Code function
- 1. Password login (limit times)
- 2. Bill storage (linked list)
- 3. Bill search (enter the number and no error is reported)
- 4. Bill deletion (enter the number to delete, and no error is reported)
- 5. Bill display (show all)
- 6. Set the budget (remind the user when it exceeds 80% and 100% of the budget)
- 7. Save and read files (including the bills saved by the user and the pre settings set by the user)
- 8. In the program, you can constantly return to the main menu and secondary menu
-
II Step by step explanation
- 1. Password login
- Set a char array initialization password, and use the strcmp function to compare whether the user input is the same as the password
-
int Validation(const char p[]) //Verify password function { int ret=0,count=0; char password[12]; printf("Enter your password:\n"); gets(password); while(strcmp(password,p)!=0) { count++; if(count==6) { ret=1;break; } puts("Wrong password!"); gets(password); } if(count==6) { system("cls"); printf("Password locked!\n Auto exit!\n"); return 0; } else system("cls"); return 1; }
- 2. Bill storage
- Define a bill structure as follows:
- struct Bill{
int B_number; / / deposit bill No
char rate[11]; / / bill deposit date
char B_detal[24]; / / used to deposit bills
int money; / / amount of bill deposit
struct Bill *next;
}; - If the password is correct, the user enters the storage function after selecting bill storage (but the file is not saved at this time, and the saved file is placed at the end of the main function):
/************************Insert or generate linked list*************/ struct Bill*Insert(struct Bill *head,int biu)//Insert linked list { struct Bill *p,*p1; int sum=0; int B_number; char rate[11]; char B_detal[20]; int money; if(biu==1) printf("Please enter your income bill,They are the number (no more than 9 integers at most), date details (no more than 10 words) and amount (yuan no more than 9 digits),Separate with spaces\n"); else printf("Please enter your expense bill,They are the number (no more than 9 integers at most), date details (no more than 10 words) and amount (yuan no more than 9 digits),Separate with spaces\n"); scanf("%6d%13s%22s%12d",&B_number,rate,B_detal,&money);getchar(); if(head!=NULL) { p1=head; while(p1->B_number!=B_number&&p1->next!=NULL) { p1=p1->next; } if(p1->B_number==B_number) { printf("This number already exists! Archive failed!\n"); return head; } } p=(struct Bill*)malloc(sizeof(struct Bill)); p->B_number=B_number; strcpy(p->rate,rate); strcpy(p->B_detal,B_detal); if(biu==2) p->money=-money; else p->money=money; p->next=head; return p; }
- The teacher laughed when he saw the way I linked the linked list. Generally, I link forward. I insert it backwards in front of the head, and then pay attention to me
- Direct return p; Then put the head of the main function = p; Just pass in the head next time you deposit the bill.
- In addition, I have a heavy surplus in this code, which can simplify reading the entry form directly into the linked list. I'm afraid I don't dare to do so because I don't have a good control.
- 3. Bill search
- It will save the bill, and it is certainly not difficult to find the bill. It is mainly used to compare the codes entered by the user with those in the linked list. The key is to control the cycle conditions - see the code
char Finds(struct Bill*head)//Find multiple times { int x=999999; char a; struct Bill *p1,*p2; if(head==NULL) { printf("The bill is empty and does not exist! input R Return to the main menu. If not, exit:\n"); scanf("%c",&a); system("cls"); return a; } else { p1=head; printf("Please enter the account number you want to find:"); scanf("%d",&x); //There will be a bug here. If you enter a symbol and return x, the value after the last call will be maintained, and then the last result will be printed while(getchar()!='\n') continue; //getchar one symbol and one carriage return are directly eaten by a, resulting in the end of the program //Solution: initialize the maximum value of x and clear the screen system("cls"); No method 2: loop getchar ok! while(p1->B_number!=x&&p1->next!=NULL) p1=p1->next; if(p1->B_number==x) printf("%d %s %s %d\n",p1->B_number,p1->rate,p1->B_detal,p1->money); else printf("This bill does not exist!\n"); printf("input S Indicates to continue searching,input R Indicates return to the main menu,If not, exit!\n"); scanf("%c",&a); system("cls"); } return a; }
It is necessary to explain why it is a char pointer, because I let the user enter characters to determine the next operation after the user looks up the bill
-
In addition, the bugs and solutions of the code have been marked in the code. Please note that each bug is my blood experience
- 4. Bill deletion
- Deleting a linked list is a little more complicated than searching, but it's just a little. After the while loop finds the p to be deleted, connect the point before p to the point after P, and then {free(p) is ok. See the code:
struct Bill*Delete(struct Bill*head)//Delete a point { int x; struct Bill *p1,*p2; printf("Please enter the number of points to delete:"); scanf("%d",&x); while(getchar()!='\n') continue; if(head->B_number==x) { p1=head; head=head->next; free(p1); printf("Deleted a bill!\n"); } else { if(head->next==NULL) { printf("This bill does not exist!\n"); } else { p1=head; p2=head->next; //If there is no if else here, there will be a bug. If there is only one header node, p2=NULL, and P2 - > next does not exist //At this time, if there is only one node, the value of input x is not equal to head - > B_ Number, the program will make an error while(p2->B_number!=x&&p2->next!=NULL) { p1=p2; p2=p2->next; } if(p2->B_number==x) { p1->next=p2->next; free(p2); printf("Deleted a bill\n"); } else { printf("This bill does not exist!\n"); } } } return head; }
The bug here is very interesting. I found it by chance. Imagine what the program will do when only one head comes in and the number entered by the user is not equal to the number in the head
- 5. Display of bills
- The display and query of linked list bills are not very difficult logically. The main thing is to make no mistake about the details,
-
struct Bill*Show(struct Bill *head) { struct Bill *p1; p1=head; if(p1==NULL) { printf("You haven't deposited any bills yet\n"); } else { while(1) { printf("%6d%13s%22s%12d\n",p1->B_number,p1->rate,p1->B_detal,p1->money); if(p1->next==NULL) break; p1=p1->next; } } return head; }
6. Set budget
-
I use global variables for budget, so I don't have to worry that the main function budget (budget) can't be used. After all, I need to set the budget in the budget function
char Budget() { char a='?'; if(budget!=0) { printf("What is your budget%d,input S Reset your budget, enter R Return to the main menu, otherwise exit the program\n",budget); scanf("%c",&a); system("cls"); while(a=='S') { printf("Please enter your budget:"); scanf("%d",&budget);getchar(); printf("You reset your budget to%d,input S Reset your budget, enter R Return to the main menu, otherwise exit the program\n",budget); scanf("%c",&a); system("cls"); } } if(budget==0) printf("You have not set your budget yet. The default budget is 0. Please enter S Set your budget, enter R Return to main menu,If not, exit the program\n"); scanf("%c",&a); system("cls"); while(a=='S') { printf("Please enter your budget:"); scanf("%d",&budget);getchar(); printf("What is your budget%d,input S Reset your budget, enter R Return to the main menu, otherwise exit the program\n",budget); scanf("%c",&a); system("cls"); } return a; }
Like the lookup function, I use the return value of char type to judge the user's instruction, so that the user can return to the main menu or reset the budget and exit the program
-
7. Saving and reading files
- This part of knowledge is new to me. It took me two days. Of course, I didn't fully master it, but it can also realize simple reading or saving, which requires accumulated experience. Therefore, I can't explain this knowledge in detail, but I can give some suggestions.
- Add: this part of knowledge is not difficult logically. For beginners, what is difficult is the specific code details
- For example, at the beginning, I had no idea. I thought that the archive needed to manually create a file and then format it for import. After asking the teacher, I knew that the code would help you build it
- First: ask the teacher
- Second: turn to the C Primer Plus book, but this part of the knowledge is difficult to read through, but the above code can imitate the practice
-
void Archive(struct Bill *head) //file { FILE *fp,*fp1; if((fp1=fopen("budget","w"))==NULL) { printf("Archive failed!\n"); Sleep(3000); exit(0); } fprintf(fp1,"%d\n",budget); if((fp=fopen("660","w"))==NULL) { printf("Archive failed!\n"); } else { struct Bill *p; p=head; if(p==NULL) { ; } else { while(1) { fprintf(fp,"%6d%13s%22s%12d\n",p->B_number,p->rate,p->B_detal,p->money); if(p->next==NULL) break; p=p->next; } } } fclose(fp); }
Archiving is not difficult. Take a look at the reading. The linked list reading is really a little around!
-
Don't panic: listen to me. First, the budget and bill exist in two different files.struct Bill *Rade_file() //File reading { FILE *fp1; if((fp1=fopen("budget","a+"))==NULL) { printf("File reading failed!\n"); Sleep(3000); exit(0); } fscanf(fp1,"%d",&budget); //As for fscanf, I'm not sure whether it should be \ n in the file, but it doesn't matter whether it should be or not, struct Bill *p=(struct Bill*)malloc(sizeof(struct Bill)); struct Bill *head,*p1; head=NULL; FILE *fp; if((fp=fopen("660","a+"))==NULL) { printf("File reading failed!\n"); Sleep(3000); exit(0); } if(fscanf(fp,"%6d%13s%22s%12d\n",&p->B_number,p->rate,p->B_detal,&p->money)==EOF) { return head; } //In fact, now I don't know where I was wrong head=p; //I don't know how it's getting better now do { p1=p; p->next=(struct Bill*)malloc(sizeof(struct Bill)); p=p->next; }while(fscanf(fp,"%6d%13s%22s%12d\n",&p->B_number,p->rate,p->B_detal,&p->money)!=EOF); p1->next=NULL; //If the tail is not sealed, the next of p should be sealed instead of p return head; //Add p1 to point to the previous one of p, and then close p1 - > next }
- Then every time you pour a value into the pointer, you need to open up space for the pointer first
- If the file is empty, return NULL and assign it to the head in main
- In addition, the bug is the problem of tail sealing. At first, I let the last p=NULL and found that there is a problem. Later, I thought that p1 - > next = null in front of p should be solved. Is it difficult to understand? Hahaha, think about it
3, All codes
#include <stdio.h> #include <windows.h> int budget=0; struct Bill{ int B_number; //Deposit bill No char rate[11]; //Date of bill deposit char B_detal[24]; //For saving bills int money; //Amount of deposit bill struct Bill *next; }; struct Bill *Rade_file(); //File reading void Archive(struct Bill *head); //file struct Bill*Show(struct Bill *head); //Show bill struct Bill*Insert(struct Bill *head,int biu);//Insert linked list struct Bill*Delete(struct Bill*head);//You can delete a bill multiple times char Finds( struct Bill *head);//You can find a bill multiple times int Validation(const char p[]);//Verify password char Budget(); //Set budget int Menu(); //Show main menu int Menu1(); //Display secondary menu void Deletechain(struct Bill *head); int main() { int sum=0; //Used to calculate total expenditure char a; //Operation selection for readers and users int x,x1; //x is used to record the selection of the user's main menu, and x1 is used to record the selection of the secondary menu struct Bill *p,*head=NULL; head=Rade_file(); char password[12]="my bill";//password if(Validation(password)) //If the password is correct { rr: x=Menu(); //Print main menu } /********************Add bill**********************/ if(x==1) { r: x1=Menu1(); //Print secondary menu if(x1==3) { system("cls");goto rr;//Return to main menu } do { head=Insert(head,x1);//Start saving linked list /***********Insert the procedure to determine whether the budget is exceeded***********************/ p=head; if(x1==2) { printf("The budget is%d\n",budget); while(1) { if(p->money<0) { sum+=-(p->money); } if(p->next==NULL) break; p=p->next; } if(sum>budget) printf("Please note: your expenditure has exceeded your budget\n"); else if(sum>budget*0.8) printf("Please note that your budget has exceeded 80% of your budget\n"); sum=0; } /******************Dividing line*****************************/ printf("input S For continuation, enter R The representative returns to the previous level. If not, exit:"); scanf("%c",&a); system("cls"); }while(a=='S'); if(a=='R') { system("cls");goto r; //Return to main menu } system("cls"); } /**********************Query bill************************/ else if(x==2) { s: a=Finds(head); if(a=='R') goto rr; if(a=='S') goto s; } /*********************Delete bill************************/ else if(x==3) { if(head==NULL) { printf("The bill is empty, no bill can be deleted!\n\n"); printf("input R Return to the main menu, otherwise exit!"); scanf("%c",&a); system("cls"); if(a=='R') goto rr; } else { t: head=Delete(head); if(head==NULL) { printf("Bill is empty,input R Return to the main menu, otherwise exit!"); scanf("%c",&a); system("cls"); if(a=='R') goto rr; } else { printf("input S Continue to delete, enter R Return to the main menu, otherwise exit!"); scanf("%c",&a); system("cls"); if(a=='S') goto t; if(a=='R') goto rr; } } } /***********************Show bill***************************/ else if(x==4) { Show(head); printf("input R Return, otherwise exit the program\n"); scanf("%c",&a);getchar(); system("cls"); if(a=='R') goto rr; } /*********************Set budget***************************/ else if(x==5) { a=Budget(); if(a=='R') goto rr; } Archive(head); Deletechain(head); //Release linked list printf("bye"); return 0; } /******************************File reading function**************************/ struct Bill *Rade_file() //File reading { FILE *fp1; if((fp1=fopen("budget","a+"))==NULL) { printf("File reading failed!\n"); Sleep(3000); exit(0); } fscanf(fp1,"%d",&budget); //As for fscanf, I'm not sure whether it should be \ n in the file, but it doesn't matter whether it should be or not, struct Bill *p=(struct Bill*)malloc(sizeof(struct Bill)); struct Bill *head,*p1; head=NULL; FILE *fp; if((fp=fopen("660","a+"))==NULL) { printf("File reading failed!\n"); Sleep(3000); exit(0); } if(fscanf(fp,"%6d%13s%22s%12d\n",&p->B_number,p->rate,p->B_detal,&p->money)==EOF) { return head; } //In fact, now I don't know where I was wrong head=p; //I don't know how it's getting better now do { p1=p; p->next=(struct Bill*)malloc(sizeof(struct Bill)); p=p->next; }while(fscanf(fp,"%6d%13s%22s%12d\n",&p->B_number,p->rate,p->B_detal,&p->money)!=EOF); p1->next=NULL; //If the tail is not sealed, the next of p should be sealed instead of p return head; //Add p1 to point to the previous one of p, and then close p1 - > next } /**********************************Archive function***************************/ void Archive(struct Bill *head) //file { FILE *fp,*fp1; if((fp1=fopen("budget","w"))==NULL) { printf("Archive failed!\n"); Sleep(3000); exit(0); } fprintf(fp1,"%d\n",budget); if((fp=fopen("660","w"))==NULL) { printf("Archive failed!\n"); } else { struct Bill *p; p=head; if(p==NULL) { ; } else { while(1) { fprintf(fp,"%6d%13s%22s%12d\n",p->B_number,p->rate,p->B_detal,p->money); if(p->next==NULL) break; p=p->next; } } } fclose(fp); } /*********************************Display function******************************/ struct Bill*Show(struct Bill *head) { struct Bill *p1; p1=head; if(p1==NULL) { printf("You haven't deposited any bills yet\n"); } else { while(1) { printf("%6d%13s%22s%12d\n",p1->B_number,p1->rate,p1->B_detal,p1->money); if(p1->next==NULL) break; p1=p1->next; } } return head; } /**************************************Budget function************************/ char Budget() { char a='?'; if(budget!=0) { printf("What is your budget%d,input S Reset your budget, enter R Return to the main menu, otherwise exit the program\n",budget); scanf("%c",&a); system("cls"); while(a=='S') { printf("Please enter your budget:"); scanf("%d",&budget);getchar(); printf("You reset your budget to%d,input S Reset your budget, enter R Return to the main menu, otherwise exit the program\n",budget); scanf("%c",&a); system("cls"); } } if(budget==0) printf("You have not set your budget yet. The default budget is 0. Please enter S Set your budget, enter R Return to main menu,If not, exit the program\n"); scanf("%c",&a); system("cls"); while(a=='S') { printf("Please enter your budget:"); scanf("%d",&budget);getchar(); printf("What is your budget%d,input S Reset your budget, enter R Return to the main menu, otherwise exit the program\n",budget); scanf("%c",&a); system("cls"); } return a; } /****************************Delete bill***************************/ struct Bill*Delete(struct Bill*head)//Delete a point { int x; struct Bill *p1,*p2; printf("Please enter the number of points to delete:"); scanf("%d",&x); while(getchar()!='\n') continue; if(head->B_number==x) { p1=head; head=head->next; free(p1); printf("Deleted a bill!\n"); } else { if(head->next==NULL) { printf("This bill does not exist!\n"); } else { p1=head; p2=head->next; //If there is no if else here, there will be a bug. If there is only one header node, p2=NULL, and P2 - > next does not exist //At this time, if there is only one node, the value of input x is not equal to head - > B_ Number, the program will make an error while(p2->B_number!=x&&p2->next!=NULL) { p1=p2; p2=p2->next; } if(p2->B_number==x) { p1->next=p2->next; free(p2); printf("Deleted a bill\n"); } else { printf("This bill does not exist!\n"); } } } return head; } /******************************Find function**********************/ char Finds(struct Bill*head)//Find multiple times { int x=999999; char a; struct Bill *p1,*p2; if(head==NULL) { printf("The bill is empty and does not exist! input R Return to the main menu. If not, exit:\n"); scanf("%c",&a); system("cls"); return a; } else { p1=head; printf("Please enter the account number you want to find:"); scanf("%d",&x); //There will be a bug here. If you enter a symbol and return x, the value after the last call will be maintained, and then the last result will be printed while(getchar()!='\n') continue; //getchar one symbol and one carriage return are directly eaten by a, resulting in the end of the program //Solution: initialize the maximum value of x and clear the screen system("cls"); No method 2: loop getchar ok! while(p1->B_number!=x&&p1->next!=NULL) p1=p1->next; if(p1->B_number==x) printf("%d %s %s %d\n",p1->B_number,p1->rate,p1->B_detal,p1->money); else printf("This bill does not exist!\n"); printf("input S Indicates to continue searching,input R Indicates return to the main menu,If not, exit!\n"); scanf("%c",&a); system("cls"); } return a; } /****************************Release linked list******************/ void Deletechain(struct Bill *head) { struct Bill *p1; while(head) { p1=head; head=head->next; free(p1); } } /************************Insert or generate linked list*************/ struct Bill*Insert(struct Bill *head,int biu)//Insert linked list { struct Bill *p,*p1; int sum=0; int B_number; char rate[11]; char B_detal[20]; int money; if(biu==1) printf("Please enter your income bill,They are the number (no more than 9 integers at most), date details (no more than 10 words) and amount (yuan no more than 9 digits),Separate with spaces\n"); else printf("Please enter your expense bill,They are the number (no more than 9 integers at most), date details (no more than 10 words) and amount (yuan no more than 9 digits),Separate with spaces\n"); scanf("%6d%13s%22s%12d",&B_number,rate,B_detal,&money);getchar(); if(head!=NULL) { p1=head; while(p1->B_number!=B_number&&p1->next!=NULL) { p1=p1->next; } if(p1->B_number==B_number) { printf("This number already exists! Archive failed!\n"); return head; } } p=(struct Bill*)malloc(sizeof(struct Bill)); p->B_number=B_number; strcpy(p->rate,rate); strcpy(p->B_detal,B_detal); if(biu==2) p->money=-money; else p->money=money; p->next=head; return p; } /*******************Secondary menu******************/ int Menu1() { int x=10; //If no initial value is assigned here, there will be a bug. This variable will default to the value of the previous int variable, that is, x=1; //If you enter a symbol next, the int type X cannot be read, and the default x=1 will enter the next procedure printf("1,Revenue bill\n2,Expenditure bill\n3,Return to main menu\n"); printf("Please enter your choice:\n"); scanf("%d",&x);getchar(); while(x!=1&&x!=2&&x!=3) { printf("i 'm sorry! This function is not available!\n"); scanf("%d",&x);getchar(); } system("cls"); return x; } /*******************Main menu**************/ int Menu() { int x; printf("1,Add bill\n2,Find bill\n3,Delete bill\n4,Show bill\n5,Set budget\n"); printf("Please enter your choice:\n"); scanf("%d",&x);getchar(); while(x!=1&&x!=2&&x!=3&&x!=4&&x!=5) { printf("i 'm sorry! This function is not available!\n"); scanf("%d",&x);getchar(); } system("cls"); return x; } /************************Verify password******************/ int Validation(const char p[]) //Verify password { int ret=0,count=0; char password[12]; printf("Enter your password:\n"); gets(password); while(strcmp(password,p)!=0) { count++; if(count==6) { ret=1;break; } puts("Wrong password!"); gets(password); } if(count==6) { system("cls"); printf("Password locked!\n Auto exit!\n"); return 0; } else system("cls"); return 1; }
IV summary
The level is limited, it is difficult to explain everything clearly, and my code is not the best. The obvious problem is that the main function is too complex and the hidden code details are not implemented.
However, the learning progress of each of me is different. There will certainly be some people who are not as good as me. Just ask questions and I will try to make it clear
No more sleep, sudden death cao bb
Tip: the following is the main content of this article. The following cases can be used for reference
1, What is pandas?
Example: pandas is a NumPy based tool created to solve data analysis tasks.
2, Use steps
1. Import and storage
The code is as follows (example):
import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import warnings warnings.filterwarnings('ignore') import ssl ssl._create_default_https_context = ssl._create_unverified_context
2. Read in data
The code is as follows (example):
data = pd.read_csv( 'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv') print(data.head())
The url used here is the data requested by the network.
summary
Tip: here is a summary of the article:
For example, the above is what we want to talk about today. This paper only briefly introduces the use of pandas, which provides a large number of functions and methods that enable us to process data quickly and conveniently.