//Write a simple fruit management system
//1. You can view all fruits
//2. You can add new fruits (check whether the fruit has the same name when adding)
//3. You can sort all fruits, price descending, quantity descending
//4. Delete the fruit with the specified name
//5. Exit the system
#include
#include
#include
typedef struct fruit{
char name[30]; //Fruit name
int count; //Fruit quantity
double price; //Fruit unit price
}fruit_t;
//Menu function
void log_menu();
//View function
void check(fruit_t *p,int *count1);
//Add function
int add(fruit_t *p,int *count1);
//Function control function
void log_ctrl(fruit_t *p,int *count1);
//Delete function
int del(fruit_t *p,int *count1);
//Sorting menu function
void cmp_menu();
//Sorting control function
void cmp_ctrl(fruit_t *p,int *count1);
//Sorting function by quantity
int cmp_count_desc(const void *px,const void *py);
//Sorting function by price
int cmp_price_desc(const void *px,const void *py);
//System initialization function
void log_start(fruit_t *p,int *count1);
//Save function
void write(fruit_t *p,int *count1);
int main(){
fruit_t stu[100]={""}; //Save all fruit names, quantities, unit prices
int count1=0; //count1 represents the number of fruit types
printf("Welcome to the fruit management system");
log_start(stu,&count1); //System initialization
printf("Initialization successful");
log_ctrl(stu,&count1); // Function control function
return 0;
}
//System initialization function
void log_start(fruit_t *p,int *count1){
FILE *fp=NULL;
int i=0;
fp=fopen("data","a+");
if(NULL==fp)
exit(1);
while(fread(&p[*count1],sizeof(fruit_t),1,fp))
*count1+=1;
fclose(fp);
}
//Function control function
void log_ctrl(fruit_t *p,int *count1){
int chose=0;
while(1){
chose=0;
log_menu();
printf("Please select:");
scanf("%d",&chose);
switch(chose){
case 1://View
check(p,count1);
break;
case 2://Add
if(add(p,count1)==1)
printf("Add failed, this fruit already exists");
else
printf("Congratulations, added successfully");
break;
case 3://Sort
cmp_ctrl(p,count1);
break;
case 4://Delete
if(del(p,count1)==1)
printf("Delete failed, there is no such fruit");
else
printf("Congratulations, deleted successfully");
break;
case 5://Exit
write(p,count1); //Save
printf("Information saved successfully");
exit(0);
break;
default:
printf("Input error, please re-enter!");
}
}
}
//Save function
void write(fruit_t *p,int *count1){
FILE *fp=NULL;
int i=0;
fp=fopen("data","w");
if(NULL==fp)
exit(2);
fwrite(p,sizeof(fruit_t),*count1,fp);
fclose;
}
//Sorting control function
void cmp_ctrl(fruit_t *p,int *count1){
int chose=0;
if(*count1==0){
printf("Fruit type is 0, sorting failed");
return;
}
while(1){
cmp_menu();
printf("Please select:");
scanf("%d",&chose);
switch(chose){
case 1: //Quantity
qsort(p,*count1,sizeof(fruit_t),cmp_count_desc);
printf("Sorting successful");
break;
case 2: //Price
qsort(p,*count1,sizeof(fruit_t),cmp_price_desc);
printf("Sorted successfully");
break;
case 3: //Return
return;
break;
default:
printf("Input error, please re-enter");
}
}
}
//Sort by price sorting function
int cmp_price_desc(const void *px,const void *py){
const fruit_t *p1=px;
const fruit_t *p2=py;
if(p1->price>p2->price)
return -1;
else if(p1->priceprice)
return 1;
else
return 0;
}
//Sort by quantity function
int cmp_count_desc(const void *px,const void *py){
const fruit_t *p1=px;
const fruit_t *p2=py;
if(p1->count>p2->count)
return -1;
else if(p1->countcount)
return 1;
else
return 0;
}
//Sorting menu function
void cmp_menu(){
printf("1. Sort by quantity 2. Sort by price 3. Return");
}
//Delete function 0-success 1-failure
int del(fruit_t *p,int *count1){
int i=0,j=0; //j saves the subscript of the fruit to be deleted
char str[30]="";
int flag=0;
printf("Please enter the name of the fruit to be deleted:");
scanf("%s",str);
flag=0;
for(i=0;i<=*count1;i++){
if(strcmp(p[i].name,str)==0){
flag=1;
j=i;
break;
}
}
if(flag==0)
return 1;
for(i=j;i<*count1-1;i++){
strcpy(p[i].name,p[i+1].name);
p[i].count=p[i+1].count;
p[i].price=p[i+1].price;
}
*count1-=1;
return 0;
}
//Add function, 1-failure 0-success
int add(fruit_t *p,int *count1){
int i=0;
printf("Fruit name: ");
scanf("%s",p[*count1].name);
for(i=0;i<*count1;i++)
if(strcmp(p[*count1].name,p[i].name)==0)
return 1;
printf("Quantity:");
scanf("%d",&p[*count1].count);
printf("Price:");
scanf("%lf",&p[*count1].price);
*count1+=1;
return 0;
}
//View function
void check(fruit_t *p,int *count1){
int i;
if(*count1==0){
printf("Fruit type is 0 ");
return;
}
printf("Fruit quantity unit price ");
for(i=0;i<*count1;i++)
printf("%s %d %.2lf ",p[i].name,p[i].count,p[i].price);
}
//Menu function
void log_menu(){
printf("1. View 2. Add 3. Sort 4. Delete 5. Exit");
}
Reference address:Simple fruit management system written in C language
//1. You can view all fruits
//2. You can add new fruits (check whether the fruit has the same name when adding)
//3. You can sort all fruits, price descending, quantity descending
//4. Delete the fruit with the specified name
//5. Exit the system
#include
#include
#include
typedef struct fruit{
char name[30]; //Fruit name
int count; //Fruit quantity
double price; //Fruit unit price
}fruit_t;
//Menu function
void log_menu();
//View function
void check(fruit_t *p,int *count1);
//Add function
int add(fruit_t *p,int *count1);
//Function control function
void log_ctrl(fruit_t *p,int *count1);
//Delete function
int del(fruit_t *p,int *count1);
//Sorting menu function
void cmp_menu();
//Sorting control function
void cmp_ctrl(fruit_t *p,int *count1);
//Sorting function by quantity
int cmp_count_desc(const void *px,const void *py);
//Sorting function by price
int cmp_price_desc(const void *px,const void *py);
//System initialization function
void log_start(fruit_t *p,int *count1);
//Save function
void write(fruit_t *p,int *count1);
int main(){
fruit_t stu[100]={""}; //Save all fruit names, quantities, unit prices
int count1=0; //count1 represents the number of fruit types
printf("Welcome to the fruit management system");
log_start(stu,&count1); //System initialization
printf("Initialization successful");
log_ctrl(stu,&count1); // Function control function
return 0;
}
//System initialization function
void log_start(fruit_t *p,int *count1){
FILE *fp=NULL;
int i=0;
fp=fopen("data","a+");
if(NULL==fp)
exit(1);
while(fread(&p[*count1],sizeof(fruit_t),1,fp))
*count1+=1;
fclose(fp);
}
//Function control function
void log_ctrl(fruit_t *p,int *count1){
int chose=0;
while(1){
chose=0;
log_menu();
printf("Please select:");
scanf("%d",&chose);
switch(chose){
case 1://View
check(p,count1);
break;
case 2://Add
if(add(p,count1)==1)
printf("Add failed, this fruit already exists");
else
printf("Congratulations, added successfully");
break;
case 3://Sort
cmp_ctrl(p,count1);
break;
case 4://Delete
if(del(p,count1)==1)
printf("Delete failed, there is no such fruit");
else
printf("Congratulations, deleted successfully");
break;
case 5://Exit
write(p,count1); //Save
printf("Information saved successfully");
exit(0);
break;
default:
printf("Input error, please re-enter!");
}
}
}
//Save function
void write(fruit_t *p,int *count1){
FILE *fp=NULL;
int i=0;
fp=fopen("data","w");
if(NULL==fp)
exit(2);
fwrite(p,sizeof(fruit_t),*count1,fp);
fclose;
}
//Sorting control function
void cmp_ctrl(fruit_t *p,int *count1){
int chose=0;
if(*count1==0){
printf("Fruit type is 0, sorting failed");
return;
}
while(1){
cmp_menu();
printf("Please select:");
scanf("%d",&chose);
switch(chose){
case 1: //Quantity
qsort(p,*count1,sizeof(fruit_t),cmp_count_desc);
printf("Sorting successful");
break;
case 2: //Price
qsort(p,*count1,sizeof(fruit_t),cmp_price_desc);
printf("Sorted successfully");
break;
case 3: //Return
return;
break;
default:
printf("Input error, please re-enter");
}
}
}
//Sort by price sorting function
int cmp_price_desc(const void *px,const void *py){
const fruit_t *p1=px;
const fruit_t *p2=py;
if(p1->price>p2->price)
return -1;
else if(p1->price
return 1;
else
return 0;
}
//Sort by quantity function
int cmp_count_desc(const void *px,const void *py){
const fruit_t *p1=px;
const fruit_t *p2=py;
if(p1->count>p2->count)
return -1;
else if(p1->count
return 1;
else
return 0;
}
//Sorting menu function
void cmp_menu(){
printf("1. Sort by quantity 2. Sort by price 3. Return");
}
//Delete function 0-success 1-failure
int del(fruit_t *p,int *count1){
int i=0,j=0; //j saves the subscript of the fruit to be deleted
char str[30]="";
int flag=0;
printf("Please enter the name of the fruit to be deleted:");
scanf("%s",str);
flag=0;
for(i=0;i<=*count1;i++){
if(strcmp(p[i].name,str)==0){
flag=1;
j=i;
break;
}
}
if(flag==0)
return 1;
for(i=j;i<*count1-1;i++){
strcpy(p[i].name,p[i+1].name);
p[i].count=p[i+1].count;
p[i].price=p[i+1].price;
}
*count1-=1;
return 0;
}
//Add function, 1-failure 0-success
int add(fruit_t *p,int *count1){
int i=0;
printf("Fruit name: ");
scanf("%s",p[*count1].name);
for(i=0;i<*count1;i++)
if(strcmp(p[*count1].name,p[i].name)==0)
return 1;
printf("Quantity:");
scanf("%d",&p[*count1].count);
printf("Price:");
scanf("%lf",&p[*count1].price);
*count1+=1;
return 0;
}
//View function
void check(fruit_t *p,int *count1){
int i;
if(*count1==0){
printf("Fruit type is 0 ");
return;
}
printf("Fruit quantity unit price ");
for(i=0;i<*count1;i++)
printf("%s %d %.2lf ",p[i].name,p[i].count,p[i].price);
}
//Menu function
void log_menu(){
printf("1. View 2. Add 3. Sort 4. Delete 5. Exit");
}
Previous article:Keil c optimization options
Next article:C language random function
- Popular Resources
- Popular amplifiers
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
Guess you like
- Do you know the two technologies that help the Internet of Things during the COVID-19 pandemic?
- Play board + simple TMS320F28035 development board
- Principle of the hardware debounce circuit for buttons
- Prize-winning live broadcast: TI low-power MCU products and Zigbee wireless solutions video playback summary!
- About CAN communication
- How to add Fourier algorithm to measure body temperature and pulse in stc89c52? How to use algorithm to analyze the results after getting it?
- Bluetooth 5 CC2640R2F
- ESP-C3 series module products
- Support the e-sports competition and get gifts for grabbing the building~~
- 【MicroPython】ESP32 starts to support hardware I2C method