程序设计,实现物流管理的基本功能
这是主页面,进入后还会有很多二级页面
主要涉及到图,链表等结构
把所有的文件放在一起既可以运行
有迪杰斯特拉算法使用和深度优先遍历等等
里面的city.txt文件,employee.txt文件,order_from.txt,route.txt,工人.txt文件自己手动创建!都需要 自己手动创建,不然会提示你找不到文件
主文件
#define _CRT_SECURE_NO_WARNINGS //多次排查后发现必须放在第一行,不然没有用
#include <iostream>
#include <fstream>
#include <string.h>
#include <iomanip> //精度设置必须包括的头文件setw函数用到
#include <string>
#include "order.h" //导入菜单系统
#include "city.cpp"
#include "employee.h"
using namespace std;
//作用:将文件的内容提取出来放进变量中
//nodeName 顶点数组 edge 两点及权值
//nodenum 顶点数量 edgenum 边的数量
void getfile(string*& nodeName, dat*& edge, int& nodenum, int& edgenum)
{
ifstream fcity, froute;
char f[25];
//读取city文件
strcpy(f, "city.txt");
fcity.open(f); //将文件地址读入,如果是空,则报错
if (!fcity)
{
cout << "文件无法打开,请检查文件再试" << endl;
}
fcity >> nodenum;
nodeName = new string[MaxSize];
for (int i = 0; i < nodenum; i++)
{
fcity >> nodeName[i]; //读入顶点信息
}
fcity.close();
//读取route文件
strcpy(f, "route.txt");
froute.open(f);
if (!froute)
{
cout << "文件无法打开,请检查文件再试" << endl;
}
froute >> edgenum;
edge = new dat[MaxSize];
for (int i = 0; i < edgenum; i++)
{
froute >> edge[i].start;
froute >> edge[i].end;
froute >> edge[i].distance;
}
froute.close();
}
//实现订单查询,
int ordermain()
{
order* head = new order;
head->readFromFile(head);
int t = 1;
while (t != 0)
{
system("cls");
head->menu();
cout << "请选择菜单(0~5):" << endl;
cin >> t;
while (!(t >= 0 && t <= 5))
{
cout << "输入错误,请重新输入:" << endl;
cin >> t;
}
switch (t)
{
case 1:
head->show(head);
break;
case 2:
head->add(head);
head->writeToFile(head);
break;
case 3:
head->Delete(head);
head->writeToFile(head);
break;
case 4:
head->revise(head);
head->writeToFile(head);
break;
case 5:
head->Find(head);
break;
}
}
return 0;
}
//作用:显示运输员工的界面
int showEmployee()
{
employee* head = new employee;
head->readFromFile(head);
int choice = 1;
while (choice != 0)
{
system("cls");
head->menu();
cout << "请选择菜单(0~5):" << endl;
cin >> choice;
while (!(choice >= 0 && choice <= 5))
{
cout << "输入错误,请重新输入:" << endl;
cin >> choice;
}
switch (choice)
{
case 1:
head->show(head);
head->writeToFile(head);
break;
case 2:
head->add(head);
head->writeToFile(head);
break;
case 3:
head->Delete(head);
head->writeToFile(head);
break;
case 4:
head->revise(head);
head->writeToFile(head);
break;
case 5:
head->Find(head);
head->writeToFile(head);
break;
}
}
return 0;
}
//删除路线
void delLine(dat*& edge, int& edgenum, Map<string>& log, int nodnum)
{
int start00, end00, i;
cout << "请输入起点:";
cin >> start00;
if (start00 <= 0 || start00 > nodnum) throw "超出范围";
cout << "请输入终点:";
cin >> end00;
if (end00 <= 0 || end00 > nodnum) throw "超出范围";
log.EdgeTransform(start00, end00, 0x3f3f3f3f);// 改类 改main 改文件
for (i = 0; i < edgenum; i++) //插入边
{
if (start00 == edge[i].start && end00 == edge[i].end) break;
if (end00 == edge[i].start && start00 == edge[i].end) break;
}
for (int j = i; j < edgenum; j++)
{
edge[j].start = edge[j + 1].start;
edge[j].end = edge[j + 1].end;
edge[j].distance = edge[j + 1].distance;
}
edgenum--;
log.writeroute(edge, edgenum);
}
int main()
{
//初始化
string* nodeName; //顶点名称
dat* edge; //边的三元组
//编译过程中编译器显示不明确,解决方案有两种,要么在所有data出现的地方前加域标识符::,要么给data改名,笔者问题在把结构体和数组名重复了
int cnodeNum = 0, edgenum = 0; //顶点数,边数
getfile(nodeName, edge, cnodeNum, edgenum);
Map<string> mapmap(nodeName, cnodeNum);//创建类
for (int i = 0; i < edgenum; i++) //插入边
{
mapmap.EdgeTransform(edge[i].start, edge[i].end, edge[i].distance);
}
int syschoose = 0, choose = 0, get = 0, choose1 = 0;
while (syschoose != 1)
{
cout << "城市数量:" << cnodeNum << endl;
cout << "城市名称:" << endl;//cout << " ";
for (int i = 1; i <= cnodeNum; i++)
{
cout << setw(8) << i << "." << nodeName[i - 1] << setiosflags(ios::right) ; //setw()一般放在输出内容前面,表示输出占几个格子,不足用空格补足,setiosflags(ios::right)控制左右对齐
if (i % 5 == 0) cout << endl;
}
cout << endl << "-------物流管理系统-------" << endl;
cout << "所有功能:" << endl;
cout << "0.进入订单管理系统" << 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;//采用Dijkstra,参考书数据结构殷人昆版第376页
//cout << "10.运送货物的装载最优方案" << endl;
cout << "9.运送货物的费用计算" << endl;
cout << endl << endl;
cout << "请选择:";
cin >> choose;
switch (choose)
{
case 0://进入订单管理系统
ordermain();
break;
case 1://进入物流员工系统
showEmployee();
break;
case 2://添加运输城市,本案例以河北为例,包括城市有石家庄,唐山,秦皇岛,邯郸,邢台,保定,张家口,承德,沧州,廊坊,衡水
{
cout << "请输入管理员密码" << endl;
string mima;
cin >> mima;
if (mima != "123456")
goto there;
string cityadd;
int att = 1; //用以判断是空还是重复还是新,三种情况无法用bool
cout << "请输入城市名字:";
cin >> cityadd;
att = mapmap.addcity(cityadd);
if (att == 0) break;
else if (att == 2)
{
cout << "输入城市重复!!" << endl;
break;
}
else
{
cout << "添加城市成功!!" << endl;
nodeName[cnodeNum].append(cityadd);
cnodeNum++;
break;
}
}
case 3://删除运输城市
{
cout << "请输入管理员密码" << endl;
string mima;
cin >> mima;
if (mima != "123456")
goto there;
int att = 1, citydel;
cout << "请输入删除城市的序号:";
cin >> citydel;
att = mapmap.deletecity(citydel);
if (att == 0)
{
cout << "文件读取失败!!" << endl;
break;
}
for (int i = citydel - 1; i < cnodeNum; i++)
{
nodeName[i] = nodeName[i + 1];
}
cnodeNum--;
cout << "删除城市成功!!" << endl;
break;
}
case 4://城市路线展示
cout << "-------------------------------------" << endl;
cout << "|序号 |城市1 到 城市2|距离(km)|" << endl;
cout << "-------------------------------------" << endl;
for (int i = 0; i < edgenum; i++)
{
//setw()设置宽度
cout << "|" << setiosflags(ios::right) << setw(4) << i << " |" << setw(8) << nodeName[edge[i].start - 1] << " 到" <<setw(8)<< nodeName[edge[i].end - 1] << "| ";
cout << left << setw(5) << edge[i].distance << endl;
}
cout << "-------------------------------------" << endl;
cout << endl << endl;
break;
case 5://添加运输路线
{
cout << "请输入起点:";
cin >> edge[edgenum].start;
if (edge[edgenum].start <= 0 || edge[edgenum].start > cnodeNum) throw "超出范围";
cout << "请输入终点:";
cin >> edge[edgenum].end;
if (edge[edgenum].end <= 0 || edge[edgenum].end > cnodeNum) throw "超出范围";
cout << "请输入路程:";
cin >> edge[edgenum].distance;
if (edge[edgenum].distance <= 0 || edge[edgenum].distance > 10000) throw "超出范围";
mapmap.EdgeTransform(edge[edgenum].start, edge[edgenum].end, edge[edgenum].distance);
edgenum++;
mapmap.writeroute(edge, edgenum);
break;
}
case 6://删除运输路线
{
delLine(edge, edgenum, mapmap, cnodeNum);
break;
}
case 7://遍历城市
system("cls");
int v, s[20];
for (int i = 0; i < cnodeNum; i++)
{
s[i] = 0;
cout << i << "." << nodeName[i] << " ";
if ((i + 1) % 5 == 0) cout << endl;
}
cout << endl<<"输入遍历起始点" << endl;
cin >> v;
mapmap.DepthSearch(v, s);
break;
case 8://运送货物的路径最优方案
int start, end;
cout << "请输入起点(数字):";
cin >> start;
cout << "请输入终点(数字):";
cin >> end;
mapmap.Dijkstra(start - 1, end - 1);
break;
case 9://"9.运送货物的成本最优方案"
int start1, end1, routelong;
double sum;//成本
cout << "请输入起点:";
cin >> start1;
cout << "请输入终点:";
cin >> end1;
routelong = mapmap.Dijkstra(start1 - 1, end1 - 1);//路径长度
cout << "请输入运载货物的重量,如果是特殊物品请输入负数" << endl;
double weight;
cin >> weight;
if (0<weight&& weight < 1)//采用哈夫曼算法
{
if (routelong < 200)
{
sum = routelong * 0.0065 * weight;
cout << "重量小于一千克,运输距离小于200公里,车每百公里花费0.65元" << endl;
cout << "经计算,总成本为: " << sum << "元" << endl << endl;
}
else
{
sum = weight* (200 * 0.0062 + ((double)routelong - 200) * 0.0045);
cout << "重量小于一千克,运输距离大于200公里,200公里内车每百公里每千克花费0.6元,大于200部分车每百公里每千克花费0.45元" << endl;
cout << "经计算,总成本为: " << sum << "元" << endl << endl;
}
}
else if (weight > 1)
{
if (routelong < 200)
{
sum = routelong * 0.006 * weight;
cout << "重量大于一千克,运输距离小于200公里,车每百公里花费0.6元" << endl;
cout << "经计算,总成本为: " << sum << "元" << endl << endl;
}
else
{
sum =- weight * (200 * 0.005 + ((double)routelong - 200) * 0.004);
cout << "重量大于一千克,运输距离大于200公里,200公里内车每百公里每千克花费0.5元,大于200部分车每百公里每千克花费0.4元" << endl;
cout << "经计算,总成本为: " << sum << "元" << endl << endl;
}
}
else
{
cout << "该物品是特殊物品,需要特殊运输,请重新输入物品重量" << endl;
cin >> weight;;
sum = -weight* routelong * 0.008;
cout << "经计算,每百公里每千克需要运费0.8元,则总成本为: " << sum << "元" << endl << endl;
}
break;
default:
cout << "输入错误!!请重新输入" << endl;
system("pause");
goto there;
}
cout << endl << "是否回到主界面??" << endl << "1.是 2.否" << endl;
cin >> get;
if (get == 2)
syschoose = 1;
else
syschoose = 0;
there://goto 语句的到达地,这是一个标签
system("cls");
}
return 0;
}
city头文件
#pragma once
#include<iostream>
#include<string>
#define MaxSize 50 //将最多数目定为50,宏定义的好处在于可以快速对全局进行更改
using namespace std;
struct dat {
int start;//起点
int end;//终点
int distance; //距离
};
template <class T>
class Map
{
public:
Map(T a[], int n); //构造函数,初始化具有n个顶点的图,结点就是各个城市
~Map() { } //析构函数为空
void EdgeTransform(int start, int end, int distance);//将三元组转化为邻接矩阵
void Print(); //将每个变量输出
int Dijkstra(int start, int end); //计算最短路径
int addcity(string city);//添加城市
int deletecity(int city); //删除城市
void writeroute(dat*& edge, int& edgenum);//route文件读写
void DepthSearch(int v, int s[]);//深度优先搜索参考殷人昆版数据结构P364
//void BreadthSearch(int v, int s[]);//广度优先搜索
private:
T vertex[MaxSize]; //存放图中顶点/城市的数组
int data[MaxSize][MaxSize]; //存放图中边的数组/城市间距离
int nodeNum, edgeNum; //图的顶点数和边数
//void Prim(dat& arr);
};
template <class Type>
class Loading //选择装载
{
public:
void Backtrack(int i); //回溯算法求装载货物的最优解
int n, //货物个数
* x, //当前解
* bestx; //当前最优解
Type* w, //货物重量数组
carrying, //第一批载重量
carryweight, //当前载重量
bestw, //当前最优载重量
remain; //剩余的重量
};
city.cpp文件
#include"city.h"
template <class T>
Map<T>::Map(T a[], int n)
{
nodeNum = n; //顶点数
edgeNum = 0; //边的数目
for (int i = 0; i < nodeNum; i++)
vertex[i] = a[i]; //顶点名称
for (int i = 0; i < MaxSize; i++) //初始化邻接矩阵,邻接矩阵定义见课本
for (int j = 0; j < MaxSize; j++)
if (i == j)
data[i][j] = 0;
else
data[i][j] = 0x3f3f3f3f; //无穷大是0x3f3f3f3f,无穷小是0xc0c0c0c0
}
//将数组转换成领接矩阵
template <class T>
void Map<T>::EdgeTransform(int start, int end, int distance)
{
if (start > MaxSize || end > MaxSize) throw "顶点错误"; //如果输入不正确抛出异常
data[start - 1][end - 1] = distance;
data[end - 1][start - 1] = distance;
edgeNum++;
}
//作用:将每个变量输出
template <class T>
void Map<T>::Print()
{
cout << "顶点数和边数为:" << nodeNum << "," << edgeNum << endl;
cout << "顶点名称为:" << endl;
for (int i = 0; i < nodeNum; i++)
{
cout << vertex[i] << endl;
}
cout << "邻接矩阵为:" << endl;
for (int i = 0; i < nodeNum; i++)
{
for (int j = 0; j < nodeNum; j++)
{
cout << data[i][j] << " ";
}
cout << endl;
}
}
//作用:计算图的最短路径
// 迪杰斯特拉算法:下一条最短路径总是在由已经产生的最短路径再扩充一条
//start 起点 end 终点
template <class T>
int Map<T>::Dijkstra(int start, int end)
{
//定义
if (start > nodeNum || end > nodeNum || start < 0 || end < 0) throw "超出范围";
int dist[MaxSize]; //辅助数组,当前最低权值 该系统中是意思路径长度
int path[MaxSize] = { 0 }; //最短路径,用来存放路径结点
int mark[MaxSize]; //顶点标记 1为已经使用 0为未使用
int max = 0x3f3f3f3f; //相当于原算法中的无穷大
int i, j, u, wmax; //wmax当前最大
//实现
for (i = 0; i < nodeNum; i++)
{
dist[i] = data[start][i]; //将dist初始化(取start与该点的权值)
if (i != start && dist[i] < max) //将path初始化(不连通取-1)
path[i] = start;
else
path[i] = -1;
mark[i] = 0; //将每个点标记为未使用
}
mark[start] = 1; dist[start] = 0; //排除start点
for (u = 0; u < nodeNum - 1; u++) //循环nodeNum-1次直到所有点被排除(已使用)
{
wmax = max;
j = start;
for (i = 0; i < nodeNum; i++) //取start点中权值最低的邻接点
{
if (!mark[i] && dist[i] < wmax)
{
j = i;
wmax = dist[i];
}
}
mark[j] = 1; //将该邻接点标记为已使用
for (i = 0; i < nodeNum; i++) //将其他与start不联通的点用其他点做媒介连上
{
if (!mark[i] && dist[j] + data[j][i] < dist[i])
{
dist[i] = dist[j] + data[j][i]; path[i] = j; //最短通路为j,与start不联通的点初始化(权值为dist【j】+data【j】【i】)
}
}
}
string line = ""; //初始化字符串,用来存放经过的城市
int p = end;
while (p > -1) //遍历路径以打印
{
string spot = vertex[p];
spot += line;
line = " " + spot;
p = path[p];
}
cout << endl << endl << vertex[start] << " 到 " << vertex[end] << endl;
cout << "最短路径为:" << dist[end] <<"公里"<< endl;
cout << "经过城市:" << line << endl << endl;
return dist[end];
}
//作用:在city文件添加城市
template <class T>
int Map<T>::addcity(string city)
{
ifstream infile;
//读 (判断是否有重复的城市)
infile.open("city.txt", ios::in);
if (!infile)
{
cout << "文件无法打开,请检查文件再试" << endl;
return 0;
}
int filelong;
infile >> filelong;
T vertex2[MaxSize];
for (int i = 0; i < filelong; i++)
{
infile >> vertex2[i];
if (vertex2[i] == city) return 2;
}
infile.close();
//写
ofstream outfile;
outfile.open("city.txt", ios::out);
outfile.seekp(0, ios::beg);
if (!outfile)
{
cout << "文件无法打开,请检查文件再试" << endl;
return 0;
}
vertex[nodeNum] = city;
nodeNum++;
outfile << nodeNum << endl;
for (int i = 1; i < nodeNum; i++)
{
outfile << vertex[i - 1] << endl;
}
outfile << vertex[nodeNum - 1];
outfile.close();
return 1;
}
//作用:在city文件删除城市
//一个city 一行
template <class T>
int Map<T>::deletecity(int city)
{
if (city > nodeNum || city <= 0) throw "超出范围";
//先数组修改 再文件修改
ofstream outfile;
outfile.open("city.txt", ios::out);
outfile.seekp(0, ios::beg);
if (!outfile)
{
cout << "文件无法打开,请检查文件再试" << endl;
return 0;
}
//改数组
for (int i = city - 1; i < nodeNum; i++)
{
vertex[i] = vertex[i + 1];
}
nodeNum--;
//写文件
outfile << nodeNum << endl;
for (int i = 0; i < nodeNum - 1; i++)
{
outfile << vertex[i] << endl;
}
outfile << vertex[nodeNum - 1];
outfile.close();
return 1;
}
//作用:在route文件写入操作
//edge边的三元组,是结构体
//edgenum 边的个数
template <class T>
void Map<T>::writeroute(dat*& edge, int& edgenum)//添加路线 文件写入 类写入 main写入
{
ofstream ofs;
ofs.open("route.txt", ios::out);
if (!ofs)
{
cout << "文件无法打开,请检查文件再试" << endl;
return;
}
ofs << edgenum ;
for (int i = 0; i < edgenum - 1; i++)
{ //endl莫名出现问题,只能使用\n代替
ofs << edge[i].start << " " << edge[i].end << " " << edge[i].distance<<"\n";
}
ofs << edge[edgenum - 1].start << " " << edge[edgenum - 1].end << " " << edge[edgenum - 1].distance<<"\n";
ofs.close();
}
//深度优先遍历算法(递归思想)
//v 起点
//mark 标记数组
template <class T>
void Map<T>::DepthSearch(int v, int s[])
{
cout << vertex[v] << " ";
s[v] = 1;//标记已读取
for (int j = 0; j < nodeNum; j++)
{
if (data[v][j] < 0x3f3f3f3f && s[j] == 0)DepthSearch(j, s);
}
}
// Loading类的方法
//作用:回溯算法求最佳解
// i递归的层数
template <class T>
void Loading <T>::Backtrack(int i)// 搜索第i层结点
{
if (i > n)// 到达叶结点
{
if (carryweight > bestw)
{
for (int j = 1; j <= n; j++)
{
bestx[j] = x[j];//更新最优解
bestw = carryweight;
}
}
return;
}
remain -= w[i];
if (carryweight + w[i] <= carrying) // 搜索左子树
{
x[i] = 1;
carryweight += w[i];
Backtrack(i + 1);
carryweight -= w[i];
}
if (carryweight + remain > bestw)
{
x[i] = 0; // 搜索右子树
Backtrack(i + 1);
}
remain += w[i];
}
订单相关的头文件和源文件
#pragma once //防止头文件被多次调用,visual自带
#include<iostream>
#include<string>
#include<sstream>
#include<windows.h> //获取时间
#include <fstream>
using namespace std;
class order
{
string ID; //订单编号
string Date; //创建时间
string Sname; //寄件人
string Sphone; //寄件人电话
string Goods; //货物名
string Sarea; //起始地
string Rarea; //目的地
string Rname; //收件人
string Rphone; //收件人电话
string Worker; //配送员
string Condition; //订单状态
class order* next;//链表模式的下一个指针
private:
public:
order() :next(NULL) {}
void writeToFile(class order* head); //写文件
int readFromFile(class order* head); //读文件
void menu(); //菜单栏
void show(class order* head); //查看全部订单
void add(class order* head); //增加订单
void Delete(class order* head);//删除订单
void Find(class order* head);//查找订单
void findByID(class order* head);//通过id查找订单,以下都是类似方法
void findBySarea(class order* head);
void findByRarea(class order* head);
//以下都是修改用的函数
void revise(class order* head); //调用菜单
void reviseAll(class order* head); //修改所有信息
void reviseGoods(class order* head);//修改商品信息
void reviseSarea(class order* head);//发件
void reviseRarea(class order* head);//收件
void reviseRname(class order* head);//收件人姓名
void reviseRphone(class order* head);//收件人电话号码
void coutt(class order* head);
};
#define _CRT_SECURE_NO_WARNINGS //多次排查后发现必须放在第一行,不然没有用
#include "order.h"
void order::menu() //一中的主菜单栏
{
cout << "***************************************" << endl;
cout << "* 1.查看所有订单 *" << endl;
cout << "* 2.增加订单 *" << endl;
cout << "* 3.删除订单 *" << endl;
cout << "* 4.修改订单 *" << endl;
cout << "* 5.查找订单 *" << endl;
cout << "* 0.退出 *" << endl;
cout << "***************************************" << endl;
}
void order::writeToFile(class order* head) //写文件
{
ofstream outfile;
outfile.open("order_from.txt", ios::out);
while (head->next != NULL)
{
outfile << head->next->ID << '\t';
outfile << head->next->Date << '\t';
outfile << head->next->Sname << '\t';
outfile << head->next->Sphone << '\t';
outfile << head->next->Goods << '\t';
outfile << head->next->Sarea << '\t';
outfile << head->next->Rarea << '\t';
outfile << head->next->Rname << '\t';
outfile << head->next->Rphone << '\t';
outfile << head->next->Worker << '\t';
outfile << head->next->Condition << endl;
head = head->next;
}
outfile.close();
}
int order::readFromFile(class order* head) //读文件
{
class order* p;
p = head;
ifstream infile; //创建文件输出流,有些别扭,但是是正确的
infile.open("order_from.txt", ios::in);//in读文件,out写文件
while (!infile.eof())
//eof就是判断是否读到一个文件结束的标志,当读到最后一行的最后一个数的时候,其实要再读一次才能读到那个标志,这时程序再次进入循环
{
order* newnode = new order;
infile >> newnode->ID;
if (newnode->ID.length() == 0)//所以最后一次要删了,跳出循环,才不会最后多一行
{
delete newnode;
break;
}
infile >> newnode->Date;
infile >> newnode->Sname;
infile >> newnode->Sphone;
infile >> newnode->Goods;
infile >> newnode->Sarea;
infile >> newnode->Rarea;
infile >> newnode->Rname;
infile >> newnode->Rphone;
infile >> newnode->Worker;
infile >> newnode->Condition;
p->next = newnode;
p = p->next;
}
infile.close();
return 0;
}
void order::show(class order* head)//查看全部订单
{
while (head->next != NULL)
{
cout << "订单号:";
cout << head->next->ID << '\t';
cout << "时间:";
cout << head->next->Date << '\t';
cout << "寄件人姓名:";
cout << head->next->Sname << '\t';
cout << "寄件人电话:";
cout << head->next->Sphone << '\t' << endl;
cout << "货物名称:";
cout << head->next->Goods << '\t';
cout << "发货地:";
cout << head->next->Sarea << '\t';
cout << "收货地:";
cout << head->next->Rarea << '\t';
cout << "收件人姓名:";
cout << head->next->Rname << '\t';
cout << "收件人电话:";
cout << head->next->Rphone << '\t' << endl;
cout << "配送员:";
cout << head->next->Worker << '\t';
cout << "运输状态:";
cout << head->next->Condition << endl;
cout << endl;
head = head->next;
}
system("pause");
system("cls");
}
string GetSystemTime() //获取时间函数 ,并且对其进行字符串格式化
{
SYSTEMTIME m_time; //来源于windows.h文件
GetLocalTime(&m_time);
char szDateTime[100] = { 0 };
sprintf(szDateTime, "%02d年%02d月%02d日%02d:%02d:%02d", m_time.wYear, m_time.wMonth,
m_time.wDay, m_time.wHour, m_time.wMinute, m_time.wSecond); //sprintf在格式化使用时比printf好用一些
string time(szDateTime);
return time;
}
void order::add(class order* head) //增加订单
{
string n;
order* p = new order;
order* pr = new order;
p = head;
cout << "请输入新增订单信息" << endl;
cout << "订单号: " << endl;
cin >> n;
while (p->next != NULL)
{
if (p->ID == n)
{
cout << "输入的订单号已存在,请重新输入:" << endl;
p = head;
cin.clear(); //清空缓冲区
cin.sync();
cin >> n;
}
p = p->next;
}
p->next = pr;
pr->next = NULL;
pr->ID = n;
string a;
a = GetSystemTime();
pr->Date = a;
cout << "寄件人姓名: " << endl;
cin >> pr->Sname;
cout << "寄件人电话:" << endl;
cin >> pr->Sphone;
cout << "货物名称:" << endl;
cin >> pr->Goods;
cout << "发货地:" << endl;
cin >> pr->Sarea;
cout << "收货地:" << endl;
cin >> pr->Rarea;
cout << "收件人姓名:" << endl;
cin >> pr->Rname;
cout << "收件人电话:" << endl;
cin >> pr->Rphone;
cout << "配送员:" << endl;
cin >> pr->Worker;
cout << "运输状态:" << endl;
cin >> pr->Condition;
cout << "增加成功!" << endl;
system("pause");
system("cls");
}
void order::coutt(class order* head)
{
cout << "订单号:";
cout << head->next->ID << '\t';
cout << "时间:";
cout << head->next->Date << '\t';
cout << "寄件人姓名:";
cout << head->next->Sname << '\t';
cout << "寄件人电话:";
cout << head->next->Sphone << '\t' << endl;
cout << "货物名称:";
cout << head->next->Goods << '\t';
cout << "发货地:";
cout << head->next->Sarea << '\t';
cout << "收货地:";
cout << head->next->Rarea << '\t';
cout << "收件人姓名:";
cout << head->next->Rname << '\t';
cout << "收件人电话:";
cout << head->next->Rphone << '\t' << endl;
cout << "配送员:";
cout << head->next->Worker << '\t';
cout << "运输状态:";
cout << head->next->Condition << endl;
}
void order::Delete(class order* head)
{
int choose;
string n;
cout << "请输入你想删除订单号:" << endl;
cin >> n;
int flag = 0;
while (head->next != NULL)
{
if (head->next->ID == n)
{
cout << "确认删除该订单信息吗?(是请输入1)" << endl;
cin >> choose;
if (choose == 1)
{//链表常规删除操作
order* p1 = head->next;
head->next = head->next->next;
delete p1;
p1 = NULL;
cout << "删除完毕!" << endl;
flag++;
break;
}
else
{
cout << "已取消操作!" << endl;
flag++;
break;
}
}
head = head->next;
}
if (flag == 0)
{
cout << "没有找到该订单信息!" << endl;
}
system("pause");
system("cls");
}
void order::findByID(class order* head)
{
string n;
int flag = 0;
cout << "请输入订单号:" << endl;
cin >> n;
while (head->next != NULL)
{
if (head->next->ID == n)
{
coutt(head);
flag++;
}
head = head->next;
}
if (flag == 0)
{
cout << "没有找到该订单!" << endl;
}
}
void order::findBySarea(class order* head)//发件地址查询
{
string n;
int flag = 0;
cout << "请请输入货物发货地:" << endl;
cin >> n;
while (head->next != NULL)
{
if (head->next->Sarea == n)
{
coutt(head);
flag++;
}
head = head->next;
}
if (flag == 0)
{
cout << "没有找到该订单!" << endl;
}
}
void order::findByRarea(class order* head)//收件地址查询
{
string n;
int flag = 0;
cout << "请请输入货物收货地:" << endl;
cin >> n;
while (head->next != NULL)
{
if (head->next->Rarea == n)
{
coutt(head);
flag++;
}
head = head->next;
}
if (flag == 0)
{
cout << "没有找到该订单!" << endl;
}
}
void order::Find(class order* head) //查找函数的菜单和功能使用
{
int choose;
cout << "1.按照订单号查找" << endl;
cout << "2.按照货物发货地查找(请输入数字)" << endl;
cout << "3.按照货物收货地查找(请输入数字)" << endl;
cout << "0.退出" << endl;
cin >> choose;
while (choose < 0 || choose>3) //提高健壮性
{
cout << "输入错误,请重新输入:" << endl;
cin >> choose;
}
switch (choose)
{
case 1:
findByID(head);
break;
case 2:
findBySarea(head);
break;
case 3:
findByRarea(head);
break;
}
system("pause");
system("cls");
}
void order::revise(class order* head)
{
int choose = 0;
cout << "1.修改全部" << endl;
cout << "2.修改货物名称" << endl;
cout << "3.修改发货地" << endl;
cout << "4.修改收货地" << endl;
cout << "5.修改收件人姓名" << endl;
cout << "6.修改收件人电话" << endl;
cout << "0.退出" << endl;
cin >> choose;
while (!(choose >= 0 && choose <= 6))
{
cout << "输入错误,请重新输入:" << endl;
cin >> choose;
}
switch (choose)
{
case 1:
reviseAll(head);
break;
case 2:
reviseGoods(head);
break;
case 3:
reviseSarea(head);
break;
case 4:
reviseRarea(head);
break;
case 5:
reviseRname(head);
break;
case 6:
reviseRphone(head);
break;
system("pause");
system("cls");
}
}
void order::reviseAll(class order* head)
{
cout << "输入要修改订单的订单号:" << endl;
int flag = 0;
string temp;
string n;
cin >> temp;
while (head->next != NULL) //遍历链表
{
if (temp == head->next->ID)
{
cout << "输入新货物名称:" << endl;
cin >> n;
head->next->Goods = n;
cout << "输入新发货地:" << endl;
cin >> n;
head->next->Sarea = n;
cout << "输入新收货地:" << endl;
cin >> n;
head->next->Rarea = n;
cout << "输入新收件人姓名:" << endl;
cin >> n;
head->next->Rname = n;
cout << "输入新收件人电话:" << endl;
cin >> n;
head->next->Rphone = n;
cout << "输入新配送员:" << endl;
cin >> n;
head->next->Worker = n;
cout << "输入新运输状态:" << endl;
cin >> n;
head->next->Condition = n;
cout << "修改成功!" << endl;
flag = 1;
break;
}
head = head->next;
}
if (flag == 0)
{
cout << "没有找到该订单!" << endl;
}
}
void order::reviseGoods(class order* head)
{
cout << "输入要修改订单的订单号:" << endl;
int flag = 0;
string temp;
string n;
cin >> temp;
while (head->next != NULL)
{
if (temp == head->next->ID)
{
cout << "输入新货物名称:" << endl;
cin >> n;
head->next->Goods = n;
cout << "修改成功!" << endl;
flag = 1;
break;
}
head = head->next;
}
if (flag == 0)
{
cout << "没有找到该订单!" << endl;
}
}
//修改发货地址
void order::reviseSarea(class order* head)
{
cout << "输入要修改订单的订单号:" << endl;
int flag = 0;
string temp;
string n;
cin >> temp;
while (head->next != NULL)
{
if (temp == head->next->ID)
{
cout << "输入新发货地:" << endl;
cin >> n;
head->next->Sarea = n;
cout << "修改成功!" << endl;
flag = 1;
break;
}
head = head->next;
}
if (flag == 0)
{
cout << "没有找到该订单!" << endl;
}
}
//修改收货地址
void order::reviseRarea(class order* head)
{
cout << "输入要修改订单的订单号:" << endl;
int flag = 0;
string temp;
string n;
cin >> temp;
while (head->next != NULL)
{
if (temp == head->next->ID)
{
cout << "输入新收货地:" << endl;
cin >> n;
head->next->Rarea = n;
cout << "修改成功!" << endl;
flag = 1;
break;
}
head = head->next;
}
if (flag == 0)
{
cout << "没有找到该订单!" << endl;
}
}
//修改姓名
void order::reviseRname(class order* head)
{
cout << "输入要修改订单的订单号:" << endl;
int flag = 0;
string temp;
string n;
cin >> temp;
while (head->next != NULL)
{
if (temp == head->next->ID)
{
cout << "输入新收件人姓名:" << endl;
cin >> n;
head->next->Rname = n;
cout << "修改成功!" << endl;
flag = 1;
break;
}
head = head->next;
}
if (flag == 0)
{
cout << "没有找到该订单!" << endl;
}
}
//修改电话
void order::reviseRphone(class order* head)
{
cout << "输入要修改订单的订单号:" << endl;
int flag = 0;
string temp;
string n; //接受要修改的内容
cin >> temp;
while (head->next != NULL)
{
if (temp == head->next->ID)
{
cout << "输入新收件人电话:" << endl;
cin >> n;
head->next->Rphone = n;
cout << "修改成功!" << endl;
flag = 1;
break;
}
head = head->next;
}
if (flag == 0)
{
cout << "没有找到该订单!" << endl;
}
}
接下来是雇员相关的文件代码
#pragma once
#include<iostream>
#include<string>
#include<sstream>
#include <fstream>
using namespace std;
class employee
{
private:
string ID;
string number;
string name;
class employee* next;
public:
employee() :next(NULL) {}
int readFromFile(class employee* head);
void writeToFile(class employee* head);
void menu();
void add(class employee* head);
void Find(class employee* head);
void findByName(class employee* head);
void findByID(class employee* head);
void findByNumber(class employee* head);
void Delete(class employee* head);
void show(class employee* head);
void revise(class employee* head);
void reviseName(class employee* head);
void reviseNumber(class employee* head);
void reviseAll(class employee* head);
};
#include"employee.h"
#include <iostream>
#include<fstream>
using namespace std;
void employee::menu()
{
cout << "***************************************" << endl;
cout << "* *" << endl;
cout << "* 1.查看所有员工 *" << endl;
cout << "* 2.增加员工信息 *" << endl;
cout << "* 3.删除员工信息 *" << endl;
cout << "* 4.修改员工信息 *" << endl;
cout << "* 5.查找员工信息 *" << endl;
cout << "* 0.退出 *" << endl;
cout << "* *" << endl;
cout << "***************************************" << endl;
}
void employee::Find(class employee* head)
{
int choose;
cout << "1.按照工号查找" << endl;
cout << "2.按照名字查找" << endl;
cout << "3.按照电话查找" << endl;
cin >> choose;
while (choose < 1 || choose>3)
{
cout << "输入错误,请重新输入:" << endl;
cin >> choose;
}
switch (choose)
{
case 1:
findByID(head);
break;
case 2:
findByName(head);
break;
case 3:
findByNumber(head);
break;
}
system("pause");
system("cls");
}
void employee::findByID(class employee* head)
{
string n;
int flag = 0;
cout << "请输入员工工号:" << endl;
cin >> n;
while (head->next != NULL)
{
if (head->next->ID == n)
{
cout << "工号:";
cout << head->next->ID << '\t';
cout << "名字:";
cout << head->next->name << '\t';
cout << "电话:";
cout << head->next->number<< endl;
flag++;
}
head = head->next;
}
if (flag == 0)
{
cout << "没有找到该员工!" << endl;
}
}
void employee::findByName(class employee* head)
{
string n;
int flag = 0;
cout << "请请输入员工名字:" << endl;
cin >> n;
while (head->next != NULL)
{
if (head->next->name == n)
{
cout << "工号:";
cout << head->next->ID << '\t';
cout << "名字:";
cout << head->next->name << '\t';
cout << "电话:";
cout << head->next->number << endl;
flag++;
}
head = head->next;
}
if (flag == 0)
{
cout << "没有找到该员工!" << endl;
}
}
void employee::findByNumber(class employee* head)
{
string n;
int flag = 0;
cout << "请请输入员工电话:" << endl;
cin >> n;
while (head->next != NULL)
{
if (head->next->number == n)
{
cout << "工号:";
cout << head->next->ID << '\t';
cout << "名字:";
cout << head->next->name << '\t';
cout << "电话:";
cout << head->next->number << endl;
flag++;
}
head = head->next;
}
if (flag == 0)
{
cout << "没有找到该员工!" << endl;
}
}
void employee::Delete(class employee* head)
{
int choose;
string n;
cout << "请输入你想删除员工工号:" << endl;
cin >> n;
int flag = 0;
while (head->next != NULL)
{
if (head->next->ID == n)
{
cout << "确认删除该员工信息吗?(是请输入1)" << endl;
cin >> choose;
if (choose == 1)
{
employee* p1 = head->next;
head->next = head->next->next;
delete p1;
p1 = NULL;
cout << "删除完毕!" << endl;
flag++;
break;
}
else
{
cout << "已取消操作!" << endl;
flag++;
break;
}
}
head = head->next;
}
if (flag == 0)
{
cout << "没有找到该员工!" << endl;
}
system("pause");
system("cls");
}
void employee::add(class employee* head)
{
string n;
employee* p = new employee;
employee* pr = new employee;
p = head;
cout << "请输入员工信息" << endl;
cout << "工号: " << endl;
cin >> n;
while (p->next != NULL)
{
if (p->ID == n)
{
cout << "输入工号重复,即将退出!" << endl;
exit(1);
}
p = p->next;
}
p->next = pr;
pr->next = NULL;
pr->ID = n;
cout << "姓名: " << endl;
cin >> pr->name;
cout << "电话: " << endl;
cin >> pr->number;
system("pause");
system("cls");
}
int employee::readFromFile(class employee* head)
{
class employee* p;
p = head;
ifstream infile;
infile.open("工人.txt", ios::in);
while (!infile.eof())//eof就是判断是否读到一个文件结束的标志,当读到最后一行的最后一个数的时候,其实要再读一次才能读到那个标志,这时程序再次进入循环
{
employee* newnode = new employee;
infile >> newnode->ID;
if (newnode->ID.length() == 0)//所以最后一次要删了,跳出循环,才不会最后多一行
{
delete newnode;
break;
}
infile >> newnode->name;
infile >> newnode->number;
p->next = newnode;
p = p->next;
}
infile.close();
return 0;
}
void employee::revise(class employee* head)
{
int choose = 0;
cout << "1.修改员工名字" << endl;
cout << "2.修改员工电话" << endl;
cout << "3.修改全部" << endl;
cin >> choose;
while (!(choose >= 1 && choose <= 3))
{
cout << "输入错误,请重新输入:" << endl;
cin >> choose;
}
switch (choose)
{
case 1:
reviseName(head);
break;
case 2:
reviseNumber(head);
break;
case 3:
reviseAll(head);
break;
}
system("pause");
system("cls");
}
void employee::reviseName(class employee* head)
{
cout << "输入要修改员工的工号:" << endl;
int flag = 0;
string temp;
string n;
cin >> temp;
while (head->next != NULL)
{
if (temp == head->next->ID)
{
cout << "输入新名字:" << endl;
cin >> n;
head->next->name = n;
cout << "修改成功!" << endl;
flag = 1;
break;
}
head = head->next;
}
if (flag == 0)
{
cout << "没有找到该员工!" << endl;
}
}
void employee::reviseNumber(class employee* head)
{
cout << "输入要修改员工的工号:" << endl;
int flag = 0;
string temp;
string n;
cin >> temp;
while (head->next != NULL)
{
if (temp == head->next->ID)
{
cout << "输入新电话:" << endl;
cin >> n;
head->next->number = n;
cout << "修改成功!" << endl;
flag = 1;
break;
}
head = head->next;
}
if (flag == 0)
{
cout << "没有找到该员工!" << endl;
}
}
void employee::reviseAll(class employee* head)
{
cout << "输入要修改员工的工号:" << endl;
int flag = 0;
string temp;
string n;
cin >> temp;
while (head->next != NULL)
{
if (temp == head->next->ID)
{
cout << "输入新名字:" << endl;
cin >> n;
head->next->name = n;
cout << "输入新电话:" << endl;
cin >> n;
head->next->number = n;
cout << "修改成功!" << endl;
flag = 1;
break;
}
head = head->next;
}
if (flag == 0)
{
cout << "没有找到该员工!" << endl;
}
}
void employee::show(class employee* head)
{
while (head->next != NULL)
{
cout << "工号:";
cout << head->next->ID << '\t';
cout << "名字:";
cout << head->next->name << '\t';
cout << "电话:";
cout << head->next->number << endl;
head = head->next;
}
system("pause");
system("cls");
}
void employee::writeToFile(class employee* head)
{
ofstream outfile;
outfile.open("工人.txt", ios::out);
while (head->next != NULL)
{
outfile << head->next->ID << '\t';
outfile << head->next->name << '\t';
outfile << head->next->number << endl;
head = head->next;
}
outfile.close();
}