//writer: jiandandian245
//time 2021.07.01
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct fruit_node{
char fruitname[100]; // 水果名称
int flag; // 每种水果对应编号
float price; // 价格
float stock ; // 库存
struct fruit_node*next;
}Fruit_Node;
typedef struct transaction_node{ //水果交易信息
char fruitname[10]; //水果名称
int flag ; //标号
float Totalprice; //交易总价
struct transaction_node *next;
}transaction;
Fruit_Node* Create_fruit() ; //创建
Fruit_Node* Insert_fruit(Fruit_Node*head); //插入水果信息
Fruit_Node* Delete_fruit(Fruit_Node*head, int flag,int &flag5); // 删除
void print_fruit(Fruit_Node*head); //遍历
int calculation(int flag,float weight,Fruit_Node*head); //购买计算
int menu();
int main()
{
Fruit_Node* head;
int chioce;
int flag;
float weight;
int flag2;
int flag3=1;
float result=0;
float money=0;
int flag4;
int flag5=1;
printf("tip:输入1先建立在进行后续操作\n");
printf("******欢迎使用水果超市信息管理系统******\n");
do{
chioce=menu();
switch(chioce)
{
case 1: // 创建
head=Create_fruit();
break;
case 2: //增加水果信息
head=Insert_fruit(head);
break;
case 3: //删除水果信息
printf("输入你要删除的水果编号:") ;
scanf("%d",&flag);
head =Delete_fruit(head,flag,flag5);
if(flag5!=0)
{
printf("已删除%d号水果信息\n",flag);
printf("\n");
}
break;
case 4: //查询
print_fruit(head);
break;
case 5: //购买水果
do{
print_fruit(head);
printf("\n");
printf("输入需要购买的水果的编号和重量\n");
scanf("%d",&flag2);
scanf("%f",&weight);
if(weight<0)
{
printf("输入重量非法\n");
}
else {
money=calculation(flag2,weight,head);
printf("当前应付%.2f\n",money);
}
result=money+result;
printf ("是否继续购买1:是 0:否\n");
scanf("%d",&flag3);
}while(flag3!=0);
printf("最终总金额为:%f\n",result);
break;
}
printf("是否继续操作 1继续 0退出\n");
scanf("%d",&flag4);
printf("\n");
printf("\n");
}while(chioce!=0&&flag4!=0);
return 0;
}
int menu() //菜单
{
char chioce[20];
int flag=1;
printf("1:*******创建************\n");
printf("2:*****增加水果信息******\n");
printf("3:*****删除水果信息******\n");
printf("4:*****查询水果信息******\n");
printf("5:*********购买**********\n");
printf("0:*********退出**********\n");
scanf("%s",chioce);
while(flag)
{
if(strlen(chioce)!=1)
{
printf("输入错误,请重新输入!\n");
scanf("%s",chioce);
continue;
}
switch(chioce[0])
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
flag=0;
break;
default:
printf("输入错误,请重新输入!\n");
scanf("%s",chioce);
break;
}
}
return chioce[0]-'0';
}
Fruit_Node* Create_fruit()
{
Fruit_Node* head;
int size=sizeof(Fruit_Node);
head=(Fruit_Node*)malloc(size);
head->next=NULL;
return head;
}
Fruit_Node* Insert_fruit(Fruit_Node*head)
{
int size=sizeof(Fruit_Node);
Fruit_Node* tail;
tail=head;
while(tail->next!=NULL)
{
tail=tail->next;
}
int flag=1;
char name[10];
int bianhao;
float price;
float stock;
do{
printf("输入水果名称 编号 价格 库存\n");
scanf("%s%d%f%f",name,&bianhao,&price,&stock);
Fruit_Node* newnode;
newnode=(Fruit_Node*)malloc(size);
newnode->flag=bianhao;
strcpy(newnode->fruitname,name);
newnode->price=price;
newnode->stock=stock;
tail->next=newnode;
tail=newnode;
tail->next=NULL;
printf("是否继续加入:1;是 0:否\n");
scanf("%d",&flag);
}while(flag!=0);
return head;
}
void print_fruit(Fruit_Node*head)
{
Fruit_Node* p;
p=head->next;
printf(" 名称 编号 价格 库存\n");
while(p!=NULL)
{
printf(" %s %d %.2f$ %.2fkg \n",p->fruitname,p->flag,p->price,p->stock);
p=p->next;
}
}
Fruit_Node* Delete_fruit(Fruit_Node*head, int flag,int &flag5)
{
Fruit_Node* p,*q;
p=head;
q=p->next;
while(1)
{
if(q->flag==flag)
{
p->next=q->next;
free(q);
break;
}
if(q->next==NULL)
{
printf("未找到要删除的水果\n");
printf("\n");
flag5=0;
break;
}
p=q;
q=q->next;
}
return head;
}
int calculation(int flag,float weight,Fruit_Node*head)
{
float result;
float price;
Fruit_Node* p, *q;
p=head;
q=p->next;
if(head->next==NULL)
printf("库存中无所要水果\n");
int a=0;
while(1)
{
if(q->flag==flag)
{
price=q->price;
a=1;
break;
}
if(q==NULL)
{
printf("库存中无所要水果\n");
return 0;
}
p=q;
q=q->next;
}
if(a==1&&q->stock>=weight)
{
result=price*weight;
q->stock=q->stock-weight;
return result;
}
if(a==1&&q->stock<weight)
{
printf("库存不足\n");
return 0;
}
}