数据结构课程设计_商品销售管理源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
/*******************************************/
/* 数据结构课程设计 */
/* 商品销售管理 */
/* */
/* */
/* */
/*******************************************/
/****** 商品销售管理系统 *****/
#include
#include
using namespace std;
struct thing
{
char name[100], producer[100]; //name商品名称,producer生产商
int num; //num商品标号
int kucun; //kucun库存量
int shuliang; //shuliang卖出去的数量
float danjia; //danjia商品价格
int buy,flag; //buy购买的商品
struct thing *next;
};
struct thing *product(struct thing *p); //商品信息
struct thing *del(struct thing *p); //删除商品信息
struct thing *sell(struct thing *head); //商品出售
struct thing *insert(struct thing *head); //商品插入
struct thing *review(struct thing *head); //商品修改
struct thing *chaxun(struct thing *head); //商品查询
struct thing *duqu(); //文件读取
void baocun(struct thing *head); //文件保存
/********* 主函数 ***********/
void main()
{
system("color 3a"); //设置系统颜色
int n = 0,start1; //start1开始标志
thing *head = duqu(); //读取
int choose; //功能选择标志
do{
/*********************** 菜单栏 *******************************/
system("cls");
cout << "\t\t◤*****************************************************◥";
cout << "\t\t\t*\t\t ☆欢迎使用商店管理系统☆ *\n";
cout << "\t\t* *\n";
cout << "\t\t* 【 1 】: 商品列表 *\n";
cout << "\t\t* 【 2 】: 购买商品 *\n";
cout << "\t\t* 【 3 】: 增新商品 *\n";
cout << "\t\t* 【 4 】: 删除商品 *\n";
cout << "\t\t* 【 5 】: 修改商品信息 *\n";
cout << "\t\t* 【 6 】: 查询商品信息 *\n";
cout << "\t\t* 【 0 】: 退出管理系统 *\n";
cout << "\t\t◣*****************************************************◢\n\n";
cout << "☆ 请输入要选择的功能: ";
cin >> choose;
if (choose >= 0 && choose <= 6)
{
switch (choose)
{
case 1:head = product(head);break; //1、显示所有商品信息
case 2:head = product(head); head = sell(head); baocun(head); break; //2、购买商品
case 3:head = insert(head);baocun(head); break; //3、插入功能
case 4:head = del(head), n--;baocun(head); break; //4、删除功能
case 5:head = review(head);baocun(head); break; //5、修改商品信息
case 6:head = chaxun(head); break; //6、查询商品信息
case 0: cout<<"\t\t\t** 欢迎使用商店管理系统! **\n";exit(0); //0、退出系统并保存
}
}
else cout << " ** 输入错误! **\n";
cout << "\n\t\t\t【1】请选择: 1 菜单"; //输入start1=1,继续进入菜单
cout << "\n\t\t\t【2】请选择: 其他任意键结束\n";
cin >> start1;
if (start1 != 1) //输入其他任意键退出
{ cout<<"\t\t\t** 欢迎使用商店管理系统! **\n";exit(0); }
} while (1);
}
/*******商品信息********/
thing *product(thing *head)
{
system("color 2c");
thing * p = head;
if (p == NULL)
return NULL;
cout <<"\t\t\t☆☆☆☆☆☆☆☆☆☆";
cout << "\n\t\t\t☆ 商品详细信息 ☆\n"; //显示所有商品信息详情
cout <<"\t\t\t☆☆☆☆☆☆☆☆☆☆\n\n";
cout << "\t产品编号\t产品名字\t单价\t库存\t生产商\n";
do
{
cout << "\t" << p->num << "\t\t" << p->name << "\t\t"<< p->danjia << "\t" << p->kucun << "\t" << p->producer << "\n";
p = p->next;
} while (p != NULL);
return head;
}
/*******商品出售********/
struct thing *sell(struct thing *head)
{
int num, n, start2; //num购买编号,n购买数量,start2购买标志
float sum = 0.0;
struct thing *p;
p = head;
start2=0;
while (start2 == 0)
{
cout << "☆ 请输入购买商品的编号: ";
cin >> num;
cout << "☆ 请输入购买的数量: ";
cin >> n;
while (p->num != num&&p->next != NULL)
{
p = p->next;
}
if (p->kucun < n) //购买数量大于库存,显示商品库存不足
{
cout << "** 商品库存不足! **\n\n\n\n";
goto end;
}
if (p->num == num)
{
p->shuliang=n;
p->buy=p->buy+n; //p->buy一次性购买商品总量
p->flag=1; //p->flag记录p->num是否购买,便于购买清单打印
p->kucun = p->kucun - n; //购买n个商品,库存就会减少n
}
else
{
cout << "** 对不起,没有此商品! **\n\n\n"; //输入购买的商品编号,库存没有时,输出“对不起,没有此商品!”
goto end;
}
sum = p->danjia*n + sum; //计算购买金额总
和
cout << "☆ 按 0 继续购买,其他任意键退出: "; //输入start2=0继续购买,其他退出,可以进行多次购买
cin >> start2;
}
cout<<"** 购买成功!**";
cout<<"\n\n ----------------------------\n";
cout<<" ----------------------------\n";
cout << "\n\t\t 【购买清单】\n\n"; //购买完毕,打印购买清单
p=head;
while (p->next !=NULL)
{
if (p->flag==1)
{
cout << "\t【商品编号】:\t\t" << p->num << "\n";
cout << "\t【商品名称】:\t\t" << p->name << "\n";
cout << "\t【商品单价】:\t\t" << p->danjia << "\n";
cout << "\t【商品数量】:\t\t" << p->shuliang << "\n";
cout<<"\n";
p->buy=0;
}
p=p->next;
}
cout << "\t【总金额 】:\t\t" << sum << "\n\n";
cout<<" ----------------------------\n";
cout<<" ----------------------------\n\n\n";
end:
return head;
}
/*********删除商品信息*******/
struct thing *del(struct thing *p)
{
thing *head = NULL, *q = NULL;
int num;
if (p == NULL)
{
cout << "** 无库存! **\n";
goto end;
}
head = p;
q = p;
cout << "☆ 请输入商品的编号: ";
cin >> num;
while (p->num != num&&p->next != NULL)
{
q = p; p = p->next;
}
if (p->num == num)
{
if (p == head)
head = p->next;
else q->next = p->next;
cout<<"** " << num << "号商品已删除! **\n";
// n=n-1;
}
else
cout << "** 无此商品! **\n";
end:
return head;
}
/******增加商品***********/
struct thing *insert(struct thing *head)
{
thing *goods =new thing;
thing * q=head;
cout << "☆ 新增商品的编号: ";
cin >> goods->num;
while(q->num!=goods->num&&q->next!=NULL)
{
q=q->next;
}
if(q->num!=goods->num)
{
cout << "【1】新增商品的名称: ";
cin >> goods->name;
cout << "【2】新增商品的单价: ";
cin >> goods->danjia;
cout << "【3】新增商品的库存: ";
cin >> goods->kucun;
cout << "【4】新增商品的的生产商: ";
cin >> goods->producer;
goods->next = NULL;
cout<<"** 增加商品成功!**";
}
else
{ cout<<"** 该编号已存在,请重新输入! **";}
thing *p=head;
if (head == NULL)
{
return NULL;
}
else
{
while (p->next != NULL)
{
p = p->next;
}
p->next = goods;
}
return(head);
}
/**********商品修改************/
struct thing *review(struct thing *head)
{
thing * p=head;
int num, kucun;
float danjia;
cout << "☆ 请输入要修改的编号: ";
cin >> num;
while(p->num!=num&&p->next!=NULL)
{
p=p->next;
}
if(p->num==num) //只有当输入的编码存在时,才会跳出体下界面,否则输出“找不到该编号”
{
cout<<"【1】该商品原来的名称: "<
cout<<"【2】该商品原来的价格: "<
cout<<"【3】该商品原来的库存量:"<
cout << "【1】请输入修改后的单价: ";
cin >> danjia;
cout << "【2】请输入修改后的库存: ";
cin >> kucun;
cout<<"** 修改商品信息成功!**";
}
struct thing *p1;
if (head == NULL)
{
cout << "\nthe list is NULL!\n";
goto end;
}
p1 = head;
while (p1->num != num&&p1->next != NULL)
{
p1 = p1->next;
}
if (p1->num == num)
{
p1->danjia = danjia;
p1->kucun = kucun;
}
else cout << "** 找不到该编号! **\n";
end:
return(head);
}
/*********查询信息*************/
struct thing *chaxun(struct thing *head)
{
thing * p =head;
int num;
cout << "☆ 请输入要查询的编号: "; //输入要查询的商品编号num
cin >> num;
while(p->num!=num&&p->next!=NULL)
{
p=p->next;
}
if(num==p->num) //只有当输入的编码存在才会继续进行
{
cout<<"【1】该商品的名称: "<
cout<<"【2】该商品的价格: "<
cout<<"【3】该商品的库存量: "<
cout<<"【4】该商品的生产商: "<
}
struct thing *p1;
if (head == NULL)
{
cout << "\nthe list is NULL!\n";
goto end;
}
p1 = head;
while (p1->num != num&&p1->next != NULL)
{
p1 = p1->next;
}
if (p1->num != num) //该编号不存在输出“找不到该编号”
cout << "** 找不到该编号! **\n";
end:
return(head);
}
/*********文件保存********/
void baocun(thing *head)
{
thing* p = head;
if (!p)
{
return;
}
ofstream outfile("data.txt");
if (!outfile)
{
cout << "** 不能打开文件! **\n";
return;
}
while (p != NULL)
{
outfile << p->num << " "<< p->name << " "<< p->danjia << " "<
p = p->next;
}
outfile.close();
}
/*******文件读取*******/
thing *duqu()
{
int n = 0;
thing *head = NULL, *p = NULL, *q = NULL;
ifstream infile("data.txt");
if (!infile)
{
cout<<"** 不能打开文件! **";
return head;
}
p = (struct thing *)malloc(sizeof(thing));
infile >> p->num >> p->name >> p->danjia>> p->kucun >> p->producer;
p->next = NULL;
n++;
do
{
if (n == 1)
{
head = p;
q = p;
}
else
{
q->next = p;
q = p;
}
p = (struct thing *)malloc(sizeof(struct thing));
infile >> p->num >> p->name >> p->danjia>> p->kucun >> p->producer;
p->next = NULL;
n++;
} while (!infile.eof());
p->next = NULL;
infile.close();
return head;
}