C language Supermarket Management System design
- 2020-06-03 07:34:32
- OfStack
This article shares the specific code of C language supermarket management system design for your reference. The specific content is as follows
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define NUM 5
struct item{
char brand[20];
char id[10];
float in_price;
float out_price;
int storage;
};
struct item_node{
struct item wanted;
int amount;
struct item_node *next;
};
int menu();
void establish();
void dis_all();
void shop_cart();
int cart_menu();
void add();
void display();
void calculate();
struct item goods[NUM];
struct item_node *cart;
void main()
{
printf("***********************************\n");
printf(" Welcome to the supermarket management system \n");
printf("***********************************\n");
while(1)
{
switch(menu())
{
case 1:
establish();break;
case 2:
dis_all();break;
case 3:
shop_cart();break;
case 4:
calculate();break;
case 5:
printf(" Thanks for using it. Bye !\n");
exit(0);
}
}
}
int menu()
{
char str[5];
int select;
printf("\n\n Please select the number to operate \n");
printf("1. Establish inventory information \n");
printf("2. Display all information \n");
printf("3. The shopping cart \n");
printf("4. settlement \n");
printf("5. exit \n");
printf(" Please select the corresponding number 1--5");
while(1)
{
fflush(stdin);
gets(str);
select=atoi(str);
if(select<1||select>5)
printf(" Input error, please retype :");
else
break;
}
return select;
}
void dis_all()
{
int i;
FILE *fp;
fp=fopen("goods","r");
for(i=0;(fread(goods+i,sizeof(struct item),1,fp))!=0;i++)
{
printf("---------------------------------\n");
printf(" goods Of the goods The unit price inventory \n");
printf("%s%7s%7.2f%8d\n",goods[i].id,goods[i].brand,goods[i].out_price,goods[i].storage);
}
fclose(fp);
}
void shop_cart()
{
while(1)
{
switch(cart_menu())
{
case 1:
display();break;
case 2:
add();break;
case 3:
return;
}
}
}
int cart_menu()
{
char str[5];
int select;
printf("\n Please select Operation \n");
printf("-----------------------\n");
printf("1. Displays the current shopping list \n");
printf("2. Add the goods \n");
printf("3. exit \n");
printf("-----------------------\n\n");
while(1)
{
fflush(stdin);
gets(str);
select=atoi(str);
if(select<1||select>3)
printf(" Input error, please retype :");
else
break;
}
return select;
}
void display()
{
struct item_node *p=cart;
if(p==NULL){
printf(" Empty shopping cart \n");
return ;
}
while(p!=NULL){
printf("----------------------------------\n");
printf(" The article number Of the goods The unit price The number of \n");
printf("%10s%20s%7.2f%8d\n",p->wanted.id,p->wanted.brand,p->wanted.out_price,p->amount);
p=p->next;
}
}
void add()
{
FILE *fp;
int i,n;
char str[20];
char choice1,choice2;
struct item_node *p,*p1;
do
{
printf(" Enter the name or article number of the item you want : ");
fflush(stdin);
gets(str);
if((fp=fopen("goods","r"))==NULL){
printf(" Failed to open file \n");
continue;
}
for(i=0;fread(goods+i,sizeof(struct item),1,fp)!=0;i++){
if((strcmp(goods[i].brand,str)==0||strcmp(goods[i].id,str)==0)&&goods[i].storage!=0){
printf(" The necessary items have been found : \n");
printf("---------------------\n");
printf(" The article number Of the goods The unit price inventory \n");
printf("%s%6s%3.2f%4d\n",goods[i].id,goods[i].brand,goods[i].out_price,goods[i].storage);
printf(" Please enter the required quantity : ");
scanf("%d",&n);
if(n>goods[i].storage){
printf(" Insufficient inventory \n");
break;
}
printf("\n Whether or not to buy ?(Y/N)");
fflush(stdin);
choice1=getchar();
if(choice1=='Y'||choice1=='y'){
p1=(struct item_node*)malloc(sizeof(struct item_node));
if(p1==NULL){
printf(" Memory request failed !\n");
exit(1);
}
p1->amount=n;
p1->wanted=goods[i];
p1->next=NULL;
p=cart;
if(cart==NULL)
cart=p1;
else{
while(p->next!=NULL)
p=p->next;
p1->next=p->next;
p->next=p1;
}
}
break;
}
}
if(i==NUM)
printf(" No required items were found \n");
fclose(fp);
printf(" Whether to continue shopping ?(Y/N)");
fflush(stdin);
choice2=getchar();
}while(choice2=='Y'||choice2=='y');
}
void establish(){
FILE *fp;
int i;
printf(" Please enter the cargo information in turn :\n");
printf("----------------------------\n");
for(i=0;i<NUM;i++)
{
printf(" Of the goods : ");
fflush(stdin);
gets(goods[i].brand);
printf(" The article number : ");
fflush(stdin);
gets(goods[i].id);
printf(" Purchase price : ");
fflush(stdin);
scanf("%f",&goods[i].in_price);
printf(" Whistle price : ");
fflush(stdin);
scanf("%f",&goods[i].out_price);
printf(" The number of : ");
fflush(stdin);
scanf("%d",&goods[i].storage);
printf("\n");
}
if((fp=fopen("goods","w"))==NULL){
printf(" File creation failed .\n");
return;
}
fwrite(goods,sizeof(struct item),NUM,fp);
fclose(fp);
}
void calculate()
{
float total=0,pay;
struct item_node *p;
int i;
FILE *fp;
printf(" Here is the shopping list : \n");
display();
if((fp=fopen("goods","r"))==NULL){
printf(" Failed to open file : \n");
return;
}
for(i=0;(fread(goods+i,sizeof(struct item),1,fp))!=0;i++);
fclose(fp);
p=cart;
while(p!=NULL){
total+=p->wanted.out_price*p->amount;
for(i=0;strcmp(goods[i].id,p->wanted.id)!=0;i++);
goods[i].storage-=p->amount;
p=p->next;
}
printf(" A total of %7.2f",total);
printf("\n Enter the actual amount paid : ");
scanf("%f",&pay);
printf(" Real pay : %7.2f The change : %7.2f",pay,pay-total);
if((fp=fopen("goods","w"))==NULL){
printf(" Failed to open file .\n");
return;
}
fwrite(goods,sizeof(struct item),NUM,fp);
fclose(fp);
}