Orders.h:
#ifndef __ORDERS_H__
#define __ORDERS_H__
#include <cstdio>
#include "MyTools.h"
struct Order
{
int num;
int quantity;
char (*books)[105];
char address[105];
char date[11];
bool ok;
Order *nextOrder;
Order(int x)
{
quantity = x;
books = new char[x][105];
ok = false;
nextOrder = nullptr;
}
};
class Orders
{
private:
int tot;
Order *incomplete;
Order *complete;
void GenerateInfo(Order &x);
public:
Orders();
virtual ~Orders() { Clear(); }
void AddNode1(Order &x);
void AddNode2(Order &x);
bool GetNode(int x,Order **y);
Order *GetNode1(int x);
Order *GetNode2(int x);
bool Delete(int x);
bool SetOK(int x);
void Traverse1() const;
void Traverse2() const;
void ShowInfo(Order *x) const;
void Clear();
};
Orders::Orders()
{
tot = 0;
incomplete = nullptr;
complete = nullptr;
}
void Orders::GenerateInfo(Order &x)
{
x.num = ++tot;
GetTimeStr(x.date);
x.ok = false;
}
void Orders::AddNode1(Order &x)
{
GenerateInfo(x);
x.nextOrder = incomplete;
incomplete = &x;
char date[50], word[50]="建立了订单No.", a[10];
GetTimeStr(date);
strcat(date,"---");
itoa(x.num,a,10);
strcat(word,a);
strcat(date,word);
Write(date);
}
void Orders::AddNode2(Order &x)
{
x.nextOrder = complete;
complete = &x;
}
bool Orders::GetNode(int x,Order **y)
{
if(incomplete!=nullptr&&incomplete->num==x)
{
*y = incomplete;
incomplete = incomplete->nextOrder;
return true;
}
Order *p=incomplete;
while(p!=nullptr)
{
if(p->nextOrder->num==x)
{
*y = p->nextOrder;
p->nextOrder = p->nextOrder->nextOrder;
return true;
}
p = p->nextOrder;
}
return false;
}
Order* Orders::GetNode1(int x)
{
Order *i=incomplete;
while(i!=nullptr)
{
if(i->num==x) return i;
i = i->nextOrder;
}
return nullptr;
}
Order* Orders::GetNode2(int x)
{
Order *i=complete;
while(i!=nullptr)
{
if(i->num==x) return i;
i = i->nextOrder;
}
return nullptr;
}
bool Orders::Delete(int x)
{
Order *p=nullptr;
bool flag=GetNode(x,&p);
delete p;
return flag;
}
bool Orders::SetOK(int x)
{
Order *p=nullptr;
bool flag=GetNode(x,&p);
AddNode2(*p);
p->ok = true;
return flag;
}
void Orders::Traverse1() const
{
if(incomplete==nullptr)
{
printf("没有未完成的订单。\n");
return ;
}
Order *p=incomplete;
while(p!=nullptr)
{
ShowInfo(p);
p = p->nextOrder;
}
}
void Orders::Traverse2() const
{
if(incomplete==nullptr)
{
printf("没有已完成的订单。\n");
return ;
}
Order *p=complete;
while(p!=nullptr)
{
ShowInfo(p);
p = p->nextOrder;
}
}
void Orders::ShowInfo(Order *x) const
{
printf("*-----------------------\n");
printf("| 订单编号: No.%d\n",x->num);
printf("| 书籍总数: %d\n",x->quantity);
printf("| 书籍列表:\n");
for(int i=0; i<x->quantity; i++)
printf("| %d) 《%s》\n",i+1,x->books[i]);
printf("| 收货地址: %s\n",x->address);
printf("| 订购日期: %s\n",x->date);
printf("| 订单状态: %s\n\n",x->ok?"已完成":"未完成");
}
void Orders::Clear()
{
Order *i=incomplete, *tmp=nullptr;
incomplete = nullptr;
while(i!=nullptr)
{
tmp = i;
i = i->nextOrder;
delete tmp;
}
i = complete;
complete = nullptr;
while(i!=nullptr)
{
tmp = i;
i = i->nextOrder;
delete tmp;
}
tot = 0;
}
#endif
UserSystem.h:
#ifndef __USERSYSTEM_H__
#define __USERSYSTEM_H__
#include "Orders.h"
#include "MyTools.h"
#include <cstdio>
#include <cstring>
class UserSystem
{
private:
Orders *data;
void record();
public:
UserSystem(Orders *x): data(x) {}
virtual ~UserSystem() { data=nullptr; }
void Menu() const;
void MakeOrder();
void FindOrder() const;
void ModifyData();
void Cancel();
void ConfirmReceipt();
};
void UserSystem::MakeOrder()
{
printf("请输入想要订购的书籍数量:");
int n;
scanf("%d",&n);
getchar();
Order *p=new Order(n);
char s[105];
printf("请输入这%d本书的名称:\n",n);
for(int i=0; i<n; i++)
{
gets(s);
strcpy(p->books[i],s);
//printf("#check books:%s,%s\n",s,p->books[i]);
}
printf("请输入收货地址:\n");
gets(s);
strcpy(p->address,s);
data->AddNode1(*p);
printf("订购成功。\n");
data->ShowInfo(p);
}
void UserSystem::ModifyData()
{
printf("请输入想要修改的订单的编号:\n");
int n;
scanf("%d",&n);
Order *p=data->GetNode1(n);
printf("check3\n");
if(p==nullptr) printf("订单不存在。\n");
else
{
printf("请输入新地址:\n");
char s[105];
scanf(" %s",s);
strcpy(p->address,s);
printf("修改成功。\n");
char date[50], word[50]="修改了订单No.", a[10];
GetTimeStr(date);
strcat(date,"---");
itoa(n,a,10);
strcat(word,a);
strcat(date,word);
Write(date);
}
}
void UserSystem::Cancel()
{
printf("请输入想要取消的订单的编号:\n");
int n;
scanf("%d",&n);
if(data->Delete(n))
{
printf("订单已取消。\n");
char date[50], word[50]="取消了订单No.", a[10];
GetTimeStr(date);
strcat(date,"---");
itoa(n,a,10);
strcat(word,a);
strcat(date,word);
Write(date);
}
else printf("订单不存在。\n");
}
void UserSystem::ConfirmReceipt()
{
printf("请输入想要确认签收的订单的编号:\n");
int n;
scanf("%d",&n);
if(data->SetOK(n))
{
printf("已确认签收。\n");
char date[50], word[50]="完成了订单No.", a[10];
GetTimeStr(date);
strcat(date,"---");
itoa(n,a,10);
strcat(word,a);
strcat(date,word);
Write(date);
}
else printf("订单不存在。\n");
}
void UserSystem::Menu() const
{
printf("-----用户操作界面-----\n");
printf(" 1.订购书籍\n");
printf(" 2.查找订单\n");
printf(" 3.修改订单\n");
printf(" 4.退订\n");
printf(" 5.确认签收\n");
printf(" 6.退出界面\n");
printf("请输入数字 1~5 以继续操作\n");
}
void UserSystem::FindOrder() const
{
printf("请输入想要查找的订单的编号:\n");
int n;
scanf("%d",&n);
Order *i=data->GetNode1(n);
Order *j=data->GetNode2(n);
if(i==nullptr&&j==nullptr) printf("订单不存在。\n");
else if(i!=nullptr) data->ShowInfo(i);
else data->ShowInfo(j);
}
#endif
?AdminstratorSystem.h:
#ifndef __ADMINISTRATORSYSTEM_H__
#define __ADMINISTRATORSYSTEM_H__
#include "Orders.h"
#include "MyTools.h"
#include <cstring>
#include <cstdio>
class AdministratorSystem
{
private:
const char password[14]="pky2014020218";
Orders *data;
public:
AdministratorSystem(Orders *t): data(t) {}
virtual ~AdministratorSystem() { data=nullptr; }
void Menu() const;
void ViewOpenOrders() const { data->Traverse1(); }
void ViewCompletedOrders() const { data->Traverse2(); }
void ViewRecord() const { Read(); }
bool CheckPassword(char s[]) const { return !strcmp(password,s); }
};
void AdministratorSystem::Menu() const
{
printf("------管理员操作界面------\n");
printf(" 1.查看未完成订单\n");
printf(" 2.查看已完成订单\n");
printf(" 3.查看后台记录\n");
printf(" 4.退出界面\n");
printf("请输入数字 1~4 以继续操作\n");
}
#endif
MyTools.h:
#ifndef __MYTOOLS_H__
#define __MYTOOLS_H__
#include <ctime>
#include <fstream>
#include <cstring>
#include <iostream>
using namespace std;
void GetTimeStr(char s[])
{
time_t now;
tm *tm_now;
time(&now);
tm_now = localtime(&now);
int y,m,d;
y = tm_now->tm_year+1900;
m = tm_now->tm_mon+1;
d = tm_now->tm_mday;
//printf("#check: %d-%d-%d\n",y,m,d);
int i=3;
while(y)
{
s[i--] = '0'+y%10;
y /= 10;
}
s[4] = '-';
s[5] = '0'+m/10;
s[6] = '0'+m%10;
s[7] = '-';
s[8] = '0'+d/10;
s[9] = '0'+d%10;
s[10] = '\0';
}
void Read()
{
ifstream inf("Record.txt");
string s;
while(inf>>s)
cout << s << endl;
inf.close();
}
void Write(char s[])
{
ofstream outf("Record.txt",ios::app);
string a(s);
a += '\n';
outf << a;
outf.close();
}
#endif
Main.cpp:
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include "UserSystem.h"
#include "AdministratorSystem.h"
#include "Orders.h"
int main()
{
Orders t;
UserSystem user(&t);
AdministratorSystem ad(&t);
char c;
while(1)
{
printf("若要订购书籍请输入'U'\n");
printf("若要进入管理员界面请输入'A'\n");
printf("输入其他键彻底退出: \n");
fflush(stdin);
c = getchar();
if(c=='U')
{
system("cls");
while(1)
{
user.Menu();
fflush(stdin);
c = getchar();
system("cls");
//printf("#check: c=%c\n",c);
if(c=='1') user.MakeOrder();
else if(c=='2') user.FindOrder();
else if(c=='3') user.ModifyData();
else if(c=='4') user.Cancel();
else if(c=='5') user.ConfirmReceipt();
else if(c=='6') break;
else printf("无效命令,请重试。\n");
system("pause");
system("cls");
}
system("cls");
printf("已退出订购系统。\n");
}
else if(c=='A')
{
char s[20], c;
printf("请输入密码:\n");
scanf(" %s",s);
bool flag=ad.CheckPassword(s);
if(!flag) printf("密码错误。\n");
else printf("登陆成功。\n");
while(flag)
{
system("pause");
system("cls");
ad.Menu();
fflush(stdin);
c = getchar();
system("cls");
//printf("#check: c=%c\n",c);
if(c=='1') ad.ViewOpenOrders();
else if(c=='2') ad.ViewCompletedOrders();
else if(c=='3') ad.ViewRecord();
else if(c=='4') break;
else printf("无效命令,请重试。\n");
}
printf("已退出管理员系统。\n");
}
else break;
}
system("cls");
printf("程序已关闭。\n");
return 0;
}
|