一、需求分析
1、商品上架:添加商品信息;
2、商品浏览:输出商品信息;
3、商品销售:输入商品编号和购买数量,通过商品标号获得商品名称和单价;通过输入的购买数量,修改该商品的库存量,计算该商品应付金额。一个销售单可包含多个商品,操作结束时打印整个销售单应付的金额;
4、商品查询;
二、数据结构
1)逻辑结构
商品及销售单使用线性结构表示(顺序表)
2)物理结构(物理结构要给出所用到的类型定义,如结点类型、线性表类型等)
//商品结构体
struct Goods {
string id; //编号
string name; //名称
double price; //单价
string producer; //生产商
int store; //库存
};
//销售单项
struct OrderItem {
string id; //编号
string name; //名称
double price; //单价
int count; //数量
};
typedef struct {
Goods *elem;//顺序表首地址
int length;
} SqlList; //商品表
typedef struct {
OrderItem *elem;
int length;
} OrderSqlList; //订单表
三、函数调用关系(所有函数间的调用关系,包括main函数)
main函数根据用户输入数字选项调用以下函数
//初始化
status InitListSq(SqlList &L);
status InitOListSq(OrderSqlList &L);
//根据id,name查询商品
int locateElem_Sq(SqlList &L, string id);
int Find_Name(SqlList &L, string name) ;
//商品上架
status ListInsert(SqlList &L, Goods g);
//添加订单
status ListOInsert(OrderSqlList &L, OrderItem o);
//商品的销售 修改goods库存
int ListUpdate(SqlList &L, string id, int count);
//打印
void PrintAllGoods(SqlList &L);
void PrintOrder(OrderSqlList &L);
//保存
void Save_Disk(SqlList &L);
void Save_Order_Disk(OrderSqlList &L);
【代码】
shop.h
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
#include<stdio.h>
// 程序状态码
#define MAXSIZE 100
#define OK 1
#define ERROR0 -3
#define ERROR1 0
#define ERROR2 -1
#define OVERFLOW -2
using namespace std;
typedef int status;//返回值状态
typedef int ElemType;//返回值的类型
//商品结构体
struct Goods {
string id; //编号
string name; //名称
double price; //单价
string producer; //生产商
int store; //库存
};
//销售项
struct OrderItem {
string id; //编号
string name; //名称
double price; //单价
int count; //数量
};
//顺序表结构体
typedef struct {
Goods *elem;//顺序表首地址
int length;
} SqlList; //商品表
typedef struct {
OrderItem *elem;
int length;
} OrderSqlList; //订单表
//顺序表的初始化
status InitListSq(SqlList &L);
status InitOListSq(OrderSqlList &L);
//根据id,name查询商品
int locateElem_Sq(SqlList &L, string id);
int Find_Name(SqlList &L, string name) ;
//商品上架
status ListInsert(SqlList &L, Goods g);
//添加订单
status ListOInsert(OrderSqlList &L, OrderItem o);
//商品的销售 修改goods库存
int ListUpdate(SqlList &L, string id, int count);
//打印
void PrintAllGoods(SqlList &L);
void PrintOrder(OrderSqlList &L);
//保存
void Save_Disk(SqlList &L);
void Save_Order_Disk(OrderSqlList &L);
main.cpp
#include<iostream>
#include "shop.h"
int main() {
SqlList L;
OrderSqlList O;
int i = 0;
int temp, a, c, choose = -1;
double price;
Goods e;
OrderItem o;
double total = 0; //订单总应付价格
string hand_1, hand_2, hand_3, hand_4, hand_5;
cout << "********************************************************" << endl;
cout << "* 商品销售管理系统菜单 *" << endl;
cout << "********************************************************" << endl;
cout << "* *" << endl;
cout << "* 1、初始化 2、录入商品信息 3、商品查询 *" << endl;
cout << "* *" << endl;
cout << "* 4、商品上架 5、商品销售 6、浏览所有商品 7、退出 *" << endl;
cout << "* *" << endl;
cout << "********************************************************" << endl;
while (choose!=0) {
cout << "请输入操作项【0-7】:";
cin >> choose;
switch (choose) {
case 1: {
//创建顺序表
if (InitListSq(L) && InitOListSq(O)) {
cout << "初始化成功!" << endl;
} else {
cout << "初始化失败!" << endl;
}
break;
}
case 2: { // 顺序表信息录入
int i = 0;
L.elem = new Goods[MAXSIZE];
if (!L.elem) {
exit(OVERFLOW);
}
L.length = 0;
fstream FilePtr;
FilePtr.open("e:/goods.txt");
if (!FilePtr) { //容错判断!!!
cout << "文件读取失败!" << endl;
exit(ERROR0);
}
//跳过文件中 第一行表头
FilePtr >> hand_1 >> hand_2 >> hand_3>> hand_4 >> hand_5;
while (!FilePtr.eof()) {
FilePtr >> L.elem[i].id >> L.elem[i].name >> L.elem[i].price >> L.elem[i].producer >> L.elem[i].store;
i++;
}
cout << "信息导入成功!" << endl;
L.length = i;
FilePtr.close();
break;
}
case 3: { //查找
string name;
printf("请输入查找名称:");
cin>>name;
int index = Find_Name(L,name);
if(index>=0) {
//cout<<"您要查找的书籍"<<name<<"价格为:"<<L.elem[index].price<<endl<<endl;
cout <<"查找到商品编号:"<<L.elem[index].id<<",名称:"<<L.elem[index].name<<",价格:"<<L.elem[index].price<<",生产商:"<<L.elem[index].producer<<",库存数:"<<L.elem[index].store<<endl;
} else {
printf("查找不存在!\n");
}
break;
}
case 4: {
// 插入图书
string id,name,pr;
double p;
int index,s;
printf("请输入编码:");
cin>>id;
printf("请输入名称:");
cin>>name;
printf("请输入单价:");
cin>>p;
printf("请输入生产商:");
cin>>pr;
printf("请输入库存:");
cin>>s;
e.id = id;
e.name = name;
e.price = p;
e.producer = pr;
e.store = s;
int state = ListInsert(L, e);
if(state == ERROR2) {
printf("存储位置已满!上架商品失败\n");
} else {
printf("成功存入!\n");
}
break;
}
case 5: {
string buy;
while(buy!="n") {
string id; //id
int c; //count
cout << "请输入购买编号" << endl;
cin>>id;
cout << "请输入购买数量" << endl;
cin>>c;
//scanf("%s %d",&id,&c);
if(ListUpdate(L, id, c)) {
cout << "购买成功!" << endl;
}
int index = locateElem_Sq(L, id);
if(index>=0) {
cout <<"商品编号:"<<L.elem[index].id<<",名称:"<<L.elem[index].name<<",价格:"<<L.elem[index].price<<",生产商:"<<L.elem[index].producer<<",库存数:"<<L.elem[index].store<<endl;
} else {
printf("查找不存在!\n");
}
o.id = id;
o.name = L.elem[index].name;
o.price = L.elem[index].price;
o.count = c;
int state = ListOInsert(O, o);
if(state == ERROR2) {
printf("存储位置已满!添加订单失败\n");
} else {
cout << "成功加入订单!" << endl;
total+= o.price*o.count;
cout << "********************************************************" << endl;
cout << "总价:"<< total << endl;
PrintOrder(O);
cout << "********************************************************" << endl;
cout << "是否继续购买?y/n" << endl;
cin >> buy;
}
}
break;
}
case 6: {
PrintAllGoods(L);
cout << "打印完成!" << endl;
break;
}
case 7: {
cout << "************生成的销售单****************" << endl;
PrintOrder(O);
cout << "需付总额为"<< total << endl;
cout << "****************************************" << endl;
Save_Disk(L);
Save_Order_Disk(O);
printf("感谢使用!\n");
return 0;
}
}
}
return 0;
}
shop_manager.cpp
#include "shop.h"
//顺序表的初始化 商品
status InitListSq(SqlList &L) {
L.elem = new Goods[MAXSIZE]; //申请100个数组
if(!L.elem) {
// 内存申请失败,直接退出程序
exit(OVERFLOW);
}
L.length = 0; //初始
return OK;
};
// 初始化 order
status InitOListSq(OrderSqlList &L) {
L.elem = new OrderItem[MAXSIZE]; //申请100个数组
if(!L.elem) {
// 内存申请失败,直接退出程序
exit(OVERFLOW);
}
L.length = 0; //初始
return OK;
};
//查询 下标
int locateElem_Sq(SqlList &L, string e) { // 根据id查询
for(int i = 0; i < L.length; i++) {
if(L.elem[i].id == e) {
return i ;
}
}
return 0;
}
int Find_Name(SqlList &L, string name) { // 根据name查询
for(int i = 0; i < L.length; i++) {
if(L.elem[i].name == name) {
return i ;
}
}
return -1;
}
status ListInsert(SqlList &L, Goods g) { // 商品上架
if(L.length == MAXSIZE) return ERROR2;
L.elem[L.length] = g;
++L.length;
return OK;
}
status ListOInsert(OrderSqlList &L, OrderItem o) { // 添加至订单
if(L.length == MAXSIZE) return ERROR2;
L.elem[L.length] = o;
++L.length;
return OK;
}
int ListUpdate(SqlList &L, string id, int count) { //修改商品库存
for(int i = 0; i < L.length; i++) {
if(L.elem[i].id == id) {
L.elem[i].store -= count;
}
}
return 1;
}
//打印
void PrintAllGoods(SqlList &L) { //打印商品
for(int i=0;i<L.length;i++) {
cout <<"商品编号:"<<L.elem[i].id<<",名称:"<<L.elem[i].name<<",价格:"<<L.elem[i].price<<",生产商:"<<L.elem[i].producer<<",库存数:"<<L.elem[i].store<<endl;
}
}
void PrintOrder(OrderSqlList &L) { //打印订单
for(int i=0;i<L.length;i++) {
cout <<"商品编号:"<<L.elem[i].id<<",名称:"<<L.elem[i].name<<",单价"<<L.elem[i].price<<",购买数量:"<<L.elem[i].count<<endl;
}
}
void Save_Disk(SqlList &L) { //保存goods文件
fstream Fileout("e:/goods.txt",ios::out);
if (!Fileout) {
cout << "文件写入失败!" << endl;
exit(ERROR0);
}
Fileout<<"id " <<"name "<<"price "<<"producer "<<"store"<<endl;
for(int i = 0; i < L.length; ++i) {
Fileout<<L.elem[i].id<<' '<<L.elem[i].name<<' '<<L.elem[i].price<<' '<<L.elem[i].producer<<' '<<L.elem[i].store<<endl;
}
Fileout.close();
return;
}
void Save_Order_Disk(OrderSqlList &L) { //保存订单至磁盘
fstream Fileout("e:/order.txt",ios::out);
if (!Fileout) {
cout << "订单文件写入失败!" << endl;
exit(ERROR0);
}
Fileout<<"id " <<"name "<<"count "<<"price"<<endl;
for(int i = 0; i < L.length; ++i) {
Fileout<<L.elem[i].id<<' '<<L.elem[i].name<<' '<<L.elem[i].count<<' '<<L.elem[i].price<<endl;
}
Fileout.close();
return;
}
【运行结果】