薇可馨
楼主
发布于 2023-6-4 22:53:31
阅读 1280
查看全部
//===头文件的引用
#include
#include
#include
#include
//程序状态码(宏定义常量用大写)
#define MAXSIZE 100//线性表最大长度
#define OK 1
#define ERROR0 -3
#define OVERFLOW -2
using namespace std;
typedef int status;//返回值状态
//订单信息构造体
struct Order{
string id;//编号
string company_name;//公司名称
string product_name;//产品名称
double product_price;//产品价格
double order_num;//订购数量
double order_price;//订购金额
};
//顺序表构造体
typedef struct{
Order *elem;
int length;
} SqlList;
//顺序表的初始化
status InitListSq(SqlList &L) {
L.elem = new Order[MAXSIZE]; //申请100个book数组
if(!L.elem) {
// 内存申请失败,直接退出程序
exit(OVERFLOW);
}
L.length = 0; //初始
return OK;
}
//添加功能
status addOrder(SqlList &L, Order o) {
string id, company_name, product_name;
double product_price;
double order_num;
int x=0;
cout << "请输入订单编号:";
cin >> id;
//判断订单编号是否反复
if (L.length!=0) {
while(x == 0) {
for (int i = 0; i < L.length; i++) {
if (L.elem.id == id) {
cout << "您输入的订单编号反复,请重新输入:";
cin >> id;
break;
}
x = 1;
}
}
}
printf("请输入公司名称:");
cin >> company_name;
printf("请输入产品名称:");
cin >> product_name;
printf("请输入产品定价:");
cin >> product_price;
printf("请输入订单数量:");
cin >> order_num;
//赋值
o.id = id;
o.company_name = company_name;
o.product_name = product_name;
o.product_price = product_price;
o.order_num = order_num;
o.order_price = product_price * order_num;
//先增加线性表长度,再给与数据
++L.length;
L.elem[L.length-1]=o;
cout<<"添加胜利"< return OK;
}
//阅读功能
void showOreder(SqlList &L) {
int i=0;
for(int k=0; k < L.length; k++) {
cout <<"订单编号:"< <<"\t公司名称:"< <<"\t产品名称:"<< L.elem[k].product_name
<<"\t产品定价:"< <<"\t订购数量:"<< L.elem[k].order_num
<<"\t订单金额:"<< L.elem[k].order_price< i=1;
}
//假设刚开端文件数据为空
if (i==0){
cout<<"订单为空!!!\n";
}
}
//查询功能(根据订单编号,公司名称,产品名称查询)
void locate_id(SqlList &L) {
string name;
cout<<"请输入订单编号或公司名或产品名:";
cin>>name;
//对用户输入的数据停止合法判断
if(name.empty()){
cout<<"您输入的信息为空";
}
int cc=0;
for (int k = 0; k < L.length; ++k) {
//根据产品名称,订单编号,公司名称来查询订单信息
if (L.elem[k].product_name == name) {
cout <<"订单编号:"< <<"\t公司名称:"< <<"\t产品名称:"<< L.elem[k].product_name
<<"\t产品定价:"< <<"\t订购数量:"<< L.elem[k].order_num
<<"\t订单金额:"<< L.elem[k].order_price< cc = 1;
}
if (L.elem[k].company_name == name) {
cout <<"订单编号:"< <<"\t公司名称:"< <<"\t产品名称:"<< L.elem[k].product_name
<<"\t产品定价:"< <<"\t订购数量:"<< L.elem[k].order_num
<<"\t订单金额:"<< L.elem[k].order_price< cc = 1;
}
if (L.elem[k].id == name) {
cout <<"订单编号:"< <<"\t公司名称:"< <<"\t产品名称:"<< L.elem[k].product_name
<<"\t产品定价:"< <<"\t订购数量:"<< L.elem[k].order_num
<<"\t订单金额:"<< L.elem[k].order_price< cc = 1;
}
}
//根据CC的值来判断是否查询胜利
if(cc==0){
cout<<"您输入的信息查询不到。";
}
}
//删除订单
void deleteO(SqlList &L) {
string cc;
printf("请输入要删除的订单号:");
cin>>cc;
int x;
//根据订单号来判断订单信息的位置
for (int j = 0; j < L.length; j++) {
if (L.elem[j].id == cc) {
x = j;
}
}
//根据订单信息的位置来删除订单信息并挪动线性表中的数据
for (int j = x; j < L.length - 1; ++j) {
L.elem[j]=L.elem[j + 1];
}
--L.length;
cout<<"删除胜利,删除后的订单列表为:"< showOreder(L);
}
//统计功能(根据公司名称,统计并输出其所有订单的总额)
void price_sum(SqlList &L) {
string company_name;
cout<<"请输入公司名:";
cin>>company_name;
double sum=0;
for(int i=0;i if(L.elem.company_name==company_name){
sum=sum+L.elem.order_price;
}
}
if (sum==0){
cout<<"该公司无订单信息";
} else{
cout<<"订单总额为:"< }
}
//保管磁盘
void Save_Disk(SqlList &L) {
fstream Fileout(".\\order.txt",ios::out);
if (!Fileout) {
cout << "文件写入失败!" << endl;
exit(0);
}
//先写入头文件再把线性表中的数据写入文件
Fileout<<"id " <<"company_name "<<"product_name "<<"product_price "<<"order_num "<<"order_price"< for(int i=0; i Fileout< < < < < < }
Fileout.close();
}
//读取
void read_Disk(SqlList &L){
int v = 0;
string hand_1, hand_2, hand_3,hand_4,hand_5,hand_6;
L.elem = new Order[MAXSIZE];
if (!L.elem) {
exit(OVERFLOW);
}
L.length = 0;
//读文件
fstream FilePtr;
FilePtr.open(".\\order.txt");
if (!FilePtr) { //容错判断!!!
cout << "文件读取失败!" << endl;
exit(ERROR0);
}
//跳过文件中第一行表头
FilePtr >> hand_1 >> hand_2 >> hand_3 >> hand_4 >> hand_5 >> hand_6;
while (!FilePtr.eof()) {
FilePtr >> L.elem[v].id >> L.elem[v].company_name
>>L.elem[v].product_name >> L.elem[v].product_price
>> L.elem[v].order_num >> L.elem[v].order_price;
//防止读出最后一行回车符
FilePtr.get();
if (FilePtr.peek()=='\n'){
break;
}
v++;
}
cout << "订单信息导入胜利!"< L.length = v;
FilePtr.close();
}
int main() {
SqlList L;
string c;
int choose = -1;
Order e;
cout << "******************************************" << endl;
cout << "* 1.添加订单 *" << endl;
cout << "* 2.阅读订单 *" << endl;
cout << "* 3.查询订单 *" << endl;
cout << "* 4.统计订单金额 *" << endl;
cout << "* 5.删除订单 *" << endl;
cout << "* 6.退出 *" << endl;
cout << "******************************************" << endl;
//建立线性表
InitListSq(L);
//读取数据
read_Disk(L);
while (choose != 0) {
cout << "请输入操作项【0-5】:";
cin >> choose;
switch (choose) {
case 1: {
addOrder(L, e);
break;
}
case 2: {
showOreder(L);
break;
}
case 3: { //查找
locate_id(L);
break;
}
case 4: {
// 统计
price_sum(L);
break;
}
case 5: {
deleteO(L);
break;
}
case 6: {
Save_Disk(L);
printf("感激使用订单管理系统!\n");
return 0;
}
default:
cout << "输入指令错误请重新输入";
}
}
return 0;
}
txt文本信息:
程序运行结果:
|
|