#include<iostream>
#include<fstream> //文件读取
#include<sstream>
#include<string>
using namespace std;
#define max 50
#define true 1
#define false 0
/*全局变量定义*/
char Id[max][max];
char Name[max][max];
int Type[max];
float InPrice[max];
float OutPrice[max];
int Store[max];
char Come[max][max];
int Count[max];
int k = 1; //商品信息条数
int i = 0;
float sum = 0.00; //统计商品总利润
/*定义商品结构体*/
typedef struct Node
{
const char* id; //商品编号
const char* name; //商品名称
int type; //(1代表食品 2代表化妆品 3代表日用品 4代表饮料)
float inPrice; //进货价格
float outPrice; //销售价格
int store; //库存数量
const char* come; //生产厂家
int count; //记录售出数量
float profit; //销售利润
Node* next; //指针域指向下一个商品
Node();
}node, * Link;
Node::Node() //结点初始化
{
id = NULL;
name = NULL;
type = 0;
inPrice = 0;
outPrice = 0;
store = 0;
count = 0;
profit = 0;
come = NULL;
next = NULL;
}
void MainMenu(); //主菜单
void Search(Link one); //查找功能
void SearchMenu(); //查找菜单
void TypeSearch(Link one, int k); //按照商品类型进行查找
void NameSearch(Link one, char* ss); //按照商品名称进行查找
void ComeSearch(Link one, char* aa); //按照商品生产厂家查询
bool Set(Link one, char* bb, int k, Link* temp); //通过商品名称和类别定位节点位置
void Create(Link one); //创建链表
void ShowAll(Link one); //展示所有信息
void Show(Link no); //输出某一个节点的信息
void Sell(Link one, char* se, int e, int c); //销售功能(商品名称 商品类型 购买数量)
void Return(Link one, char* se, int e, int c); //退货功能(商品名称 商品类型 购买数量)
void Exchange(Link n1, Link n2); //交换两个结点的信息
void Sort(Link one); //按照利润从高到低排序
void CreateNew(Link head, Link New); //头插法添加商品
void Sum(Link one); //统计售出商品总利润
void Change(Link one); //修改商品信息
void Delete(Link head); //删除商品信息
void IdSearch(Link one, char* ss) //按照编号进行查找
int main() //主函数
{
string sline; //每一行
ifstream inf;
inf.open("d://supermarket.txt");
while (getline(inf, sline))
{
i++;
istringstream sin(sline);
sin >> Id[i] >> Name[i] >> Type[i] >> InPrice[i] >> OutPrice[i] >> Store[i] >> Come[i] >> Count[i];
}
Link headnode = new Node;
Link onenode = new Node;
headnode->next = onenode;
Create(onenode);
cout << "************欢迎使用超市管理系统!!!*************" << endl;
MainMenu();
cout << "请输入相应功能前的数字进行实现:" << endl;
int choice;
cin >> choice;
for (; choice != 0;)
{
switch (choice)
{
case 1:
{
int b;
cout << "根据提示输入要购买商品类别对应的数字:(1代表食品 2代表化妆品 3代表日用品 4代表饮料)" << endl;
cin >> b;
char nam[max];
cout << "输入需要购买的商品名称:" << endl;
cin >> nam;
cout << "输入需要购买的数量:" << endl;
int f;
cin >> f;
Sell(onenode, nam, b, f);
cout << "售出成功!" << endl;
cout << "_____________________________________________________________________" << endl;
}
break;
case 2:
{
Search(onenode);
}
break;
case 3:
{
Sort(onenode);
}
break;
case 4:
{
Sum(onenode);
cout << "统计得售出商品所得总利润为:" << sum << "元" << endl;
}
break;
case 5:
{
Link New = new Node;
CreateNew(headnode, New);
onenode = New;
}
break;
case 6:
{
Delete(headnode);
}
break;
case 7:
{
ShowAll(onenode);
}
break;
case 8:
{
Change(onenode);
}
break;
case 9:
{
int b;
cout << "根据提示输入要退货商品类别对应的数字:(1代表食品 2代表化妆品 3代表日用品 4代表饮料)" << endl;
cin >> b;
char nam[max];
cout << "输入需要退货的商品名称:" << endl;
cin >> nam;
cout << "输入需要退回的数量:" << endl;
int f;
cin >> f;
Return(onenode, nam, b, f);
cout << "退货成功!" << endl;
cout << "_____________________________________________________________________" << endl;
}
break;
case 0:
break;
}
cout << endl;
cout << endl;
cout << endl;
cout << "请输入相应功能前数字进行实现!" << endl;
fflush(stdin);//清除键盘缓冲区
cin >> choice;
system("cls");
MainMenu();
}
return 0;
}
void MainMenu() //主菜单
{
cout << endl;
cout << endl;
cout << endl;
cout << "**********************************" << endl;
cout << "* 超市管理系统 *" << endl;
cout << "*________________________________*" << endl;
cout << "* 1.销售功能 *" << endl;
cout << "* 2.查找菜单 *" << endl;
cout << "* 3.排序 *" << endl;
cout << "* 4.统计 *" << endl;
cout << "* 5.添加商品 *" << endl;
cout << "* 6.删除商品 *" << endl;
cout << "* 7.输出所有 *" << endl;
cout << "* 8.修改商品信息 *" << endl;
cout << "* 9.退货功能 *" << endl;
cout << "* 0.退出程序 *" << endl;
cout << "**********************************" << endl;
cout << endl;
cout << endl;
cout << endl;
}
void SearchMenu() //查找菜单
{
cout << "* * * * * * * * * * * * * * * * * * * * * * *" << endl;
cout << "* 1.按照商品类别查询 *" << endl;
cout << "* 2.按照商品名称查询 *" << endl;
cout << "* 3.按照生产厂家查询 *" << endl;
cout << "* 4.按照编号进行查找 *" << endl;
cout << "* * * * * * * * * * * * * * * * * * * * * * *" << endl;
cout << endl;
cout << endl;
cout << endl;
}
void Search(Link one) //查找功能
{
SearchMenu();
int choice; //记录选择
cout << "输入相应功能前的数字进行实现:" << endl;
cin >> choice;
switch (choice)
{
case 1:
{
int a;
cout << "请输入想要查询的商品类别:(1代表食品 2代表化妆品 3代表日用品 4代表饮料)" << endl;
cout << "请根据提示输入想要查询类别对应的数字:" << endl;
cin >> a;
TypeSearch(one, a);
cout << "********************************************************************************************************************" << endl;
}
break;
case 2:
{
char na[max];
cout << "请输入想要查询商品的名称:" << endl;
cin >> na;
NameSearch(one, na);
cout << "*******************************************************************************************************************" << endl;
}
break;
case 3:
{
cout << "请输入想要查询商品的生产厂家:" << endl;
char co[max];
cin >> co;
ComeSearch(one, co);
cout << "******************************************************************************************************************" << endl;
}
break;
case 4:
{
cout << "想要查找的编号:" << endl;
char bianhao[max];
cin >> bianhao;
IdSearch(onenode, bianhao);
}
}
}
void Create(Link one) //创建链表
{
one->id = Id[k]; //通过数组将节点进行赋值
one->name = Name[k];
one->come = Come[k];
one->inPrice = InPrice[k];
one->count = Count[k];
one->outPrice = OutPrice[k];
one->store = Store[k];
one->type = Type[k];
one->profit = one->count * (one->outPrice - one->inPrice); //计算利息
Link nex = new Node;
one->next = nex;
k++;
if (k < i)
Create(nex); //递归创建链表
}
void ShowAll(Link one) //展示所有信息
{
Show(one);
cout << "__________________________________________________" << endl;
if (one->next->next != NULL)
ShowAll(one->next);
}
void Show(Link no) //输出某一个节点的信息
{
cout << "商品名称:" << no->name << endl;
cout << "商品编号:" << no->id << endl;
if (no->type == 1)
cout << "商品类型:食品" << endl;
else if (no->type == 2)
cout << "商品类型:化妆品" << endl;
else if (no->type == 3)
cout << "商品类型:日用品" << endl;
else if (no->type == 4)
cout << "商品类型:饮料" << endl;
else
cout << "商品类型输入异常!" << endl;
cout << "商品生产厂家:" << no->come << endl;
cout << "商品库存:" << no->store << endl;
cout << "商品进价:" << no->inPrice << "元" << endl;
cout << "商品售价:" << no->outPrice << "元" << endl;
cout << "商品已售:" << no->count << endl;
cout << "售出该商品已得利润为:" << no->profit << "元" << endl;
}
void TypeSearch(Link one, int k) //按照商品类型进行查找
{
if (one->type == k)
cout << one->name << endl;;
if (one->next->next != NULL)
TypeSearch(one->next, k);
}
void ComeSearch(Link one, char* aa) //按照商品生产厂家进行查找
{
if (strcmp(one->come, aa) == 0)
Show(one);
if (one->next->next != NULL)
ComeSearch(one->next, aa);
}
void NameSearch(Link one, char* ss) //按照商品名称进行查找
{
if (strcmp(one->name, ss) == 0)
Show(one);
if (one->next->next != NULL)
NameSearch(one->next, ss);
}
void IdSearch(Link one, char* ss) //按照编号进行查找
{
if (strcmp(one->id, ss) == 0)
Show(one);
if (one->next->next != NULL)
IdSearch(one->next, ss);
}
bool Set(Link one, char* bb, int k, Link* temp) //通过商品名称和类别定位节点位置
{
if (strcmp(one->name, bb) == 0 && one->type == k)
{
*temp = one;
return true;
}
else
{
if (one->next->next != NULL)
Set(one->next, bb, k, temp);
else
return false;
}
}
void Sell(Link one, char* se, int e, int c) //销售功能 (商品名称 商品类型 购买数量)
{
Link Temp = NULL;
bool d = Set(one, se, e, &Temp);
if (d)
{
if (Temp->store >= c)
{
Temp->store -= c;
Temp->count += c;
Temp->profit = (Temp->outPrice - Temp->inPrice) * c + Temp->profit;
cout << "购买成功" << endl;
Show(Temp);
}
else
cout << "该商品库存不足!" << endl;
}
else
cout << "该商品不存在!" << endl;
}
void Return(Link one, char* se, int e, int c) //退货功能(商品名称 商品类型 购买数量)
{
Link Temp = NULL;
bool d = Set(one, se, e, &Temp);
if (d)
{
Temp->store += c;
Temp->count -= c;
Temp->profit = Temp->profit - (Temp->outPrice - Temp->inPrice) * c;
cout << "退货成功!" << endl;
Show(Temp);
}
else
cout << "该商品不存在!" << endl;
}
void Exchange(Link n1, Link n2) //交换两个结点的信息
{
Link n3 = new Node;
n3->name = n1->name;
n3->id = n1->id;
n3->store = n1->store;
n3->count = n1->count;
n3->come = n1->come;
n3->profit = n1->profit;
n3->inPrice = n1->inPrice;
n3->outPrice = n1->outPrice;
n3->type = n1->type;
n1->name = n2->name;
n1->id = n2->id;
n1->store = n2->store;
n1->count = n2->count;
n1->come = n2->come;
n1->profit = n2->profit;
n1->inPrice = n2->inPrice;
n1->outPrice = n2->outPrice;
n1->type = n2->type;
n2->name = n3->name;
n2->id = n3->id;
n2->store = n3->store;
n2->count = n3->count;
n2->come = n3->come;
n2->profit = n3->profit;
n2->inPrice = n3->inPrice;
n2->outPrice = n3->outPrice;
n2->type = n3->type;
}
void CreateNew(Link head, Link New)
{
cout << "请输入商品的编号:" << endl;
cin >> Id[i];
cout << "请输入商品的名称:" << endl;
cin >> Name[i];
cout << "请输入商品的类型(1代表食品 2代表化妆品 3代表日用品 4代表饮料):" << endl;
cin >> Type[i];
cout << "请输入商品的进价:" << endl;
cin >> InPrice[i];
cout << "请输入商品的售价" << endl;
cin >> OutPrice[i];
cout << "请输入商品的库存量" << endl;
cin >> Store[i];
cout << "请输入商品的生产厂家:" << endl;
cin >> Come[i];
cout << "请输入商品的售出量:" << endl;
cin >> Count[i];
New->id = Id[i];
New->name = Name[i];
New->come = Come[i];
New->inPrice = InPrice[i];
New->count = Count[i];
New->outPrice = OutPrice[i];
New->store = Store[i];
New->type = Type[i];
i++;
New->profit = (New->outPrice - New->inPrice) * New->count;
New->next = head->next;
head->next = New;
}
void Sort(Link one) //按照利润从高到低排序
{
Link p = NULL;
Link q = NULL;
for (p = one; p->next != NULL; p = p->next)
for (q = p->next; q->next != NULL; q = q->next)
{
if ((p->profit) < (q->profit))
Exchange(p, q);
}
cout << "排序完成!" << endl;
}
void Sum(Link one) //统计售出商品总利润
{
sum += (one->profit);
if (one->next->next != NULL)
Sum(one->next);
}
void Change(Link one) //修改商品信息
{
cout << "请输入想要修改商品的类型:(1代表食品 2代表化妆品 3代表日用品 4代表饮料)" << endl;
int g;
cin >> g;
cout << "请输入想要修改商品的名称:" << endl;
char h[max];
cin >> h;
Link k = NULL;
bool j = Set(one, h, g, &k);
if (j)
{
cout << "将商品名称更改为:" << endl;
cin >> Name[i];
k->name = Name[i];
cout << "将商品生产厂家更改为:" << endl;
cin >> Come[i];
k->come = Come[i];
cout << "将商品编号更改为:" << endl;
cin >> Id[i];
k->id = Id[i];
cout << "将商品的进价改为:" << endl;
float p;
cin >> p;
k->inPrice = p;
cout << "将商品的售价改为:" << endl;
float q;
cin >> q;
k->outPrice = q;
cout << "将商品的库存量改为:" << endl;
int r;
cin >> r;
k->store = r;
cout << "修改成功!" << endl;
cout << "修改后的商品信息为:" << endl;
Show(k);
}
else
cout << "未找到该条信息!" << endl;
}
void Delete(Link head) //删除商品信息
{
cout << "请输入想要删除的商品类型:(1代表食品 2代表化妆品 3代表日用品 4代表饮料)" << endl;
int s;
cin >> s;
cout << "请输入想要删除的商品的名称:" << endl;
char t[max];
cin >> t;
Link o = NULL;
int flag = 0;
for (o = head; o->next->next != NULL; o = o->next)
{
if (strcmp(o->next->name, t) == 0 && o->next->type == s)
{
o->next = o->next->next;
flag = 1;
break;
}
}
if (flag == 1)
cout << "删除成功!" << endl;
else
cout << "未找到该条信息!" << endl;
}