Simple fruit management system written in C language

Publisher:EnchantedMelodyLatest update time:2015-05-11 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//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

Previous article:Keil c optimization options
Next article:C language random function

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号