用C语言编个程序,用数据结构体和函数!
发布网友
发布时间:2022-04-29 22:54
我来回答
共1个回答
热心网友
时间:2022-06-25 03:23
#include<iostream>
#include<string>
using namespace std;
typedef struct{
string code;
string name;
int price;
int quantity;
int left;
}Information;
Information infor[100],output[100],temp;
int index=0;
string s;
void input_infor()
{
cout<<"请输入商品编号:";
cin>>infor[index].code;
cout<<"请输入商品名称:";
cin>>infor[index].name;
cout<<"请输入商品售价:";
cin>>infor[index].price;
cout<<"请输入商品销售量:";
cin>>infor[index].quantity;
cout<<"请输入商品剩余数:";
cin>>infor[index].left;
index++;
}
void update_infor()
{
cout<<"请输入要修改的商品编号:";
cin>>s;
cout<<endl;
for(int i=0;i<index;i++)
{
if(infor[i].code==s)
{
cout<<"该商品的售价改为:";
cin>>infor[i].price;
cout<<"该商品的销售量改为:";
cin>>infor[i].quantity;
cout<<"该商品的剩余数改为:";
cin>>infor[i].left;
}
}
}
void delete_infor()
{
cout<<"请输入要删除的商品编号:";
cin>>s;
for(int i=0;i<index;i++)
{
if(infor[i].code==s)
{
for(int j=i;j<index-1;j++)
infor[j]=infor[j+1];
index--;
}
}
}
void show_infor()
{
cout<<"商品信息如下(按销售额从高到低):"<<endl;
for(int i=0;i<index;i++)
{
for(int j=i+1;j<index;j++)
{
if(infor[i].quantity<infor[j].quantity)
{
temp=infor[i];
infor[i]=infor[j];
infor[j]=temp;
}
}
}
cout<<"编号"<<" "<<"名称"<<" "<<"售价"<<" "<<"销售量"<<" "<<"剩余数"<<endl;
for(int i=0;i<index;i++)
{
cout<<infor[i].code<<" "<<infor[i].name<<" "<<infor[i].price<<" "<<infor[i].quantity<<" "<<infor[i].left<<endl;
}
}
int main()
{
cout<<"主菜单:"<<endl;
cout<<"1--创建商品档案"<<endl<<"2--修改商品信息"<<endl<<"3--删除商品信息"<<endl<<"4--显示商品信息"<<endl<<"0--退出"<<endl;
int input;
cout<<"请选择操作:"<<endl;
while(cin>>input&&input!=0)
{
switch(input)
{
case 1:
input_infor();
break;
case 2:
update_infor();
break;
case 3:
delete_infor();
break;
case 4:
show_infor();
break;
}
cout<<"请选择操作:"<<endl;
}
system("pause");
return 0;
}