C++ assignment 辅导讲解、讲解留学生C++
- 首页 >> C/C++编程//公路车辆管理系统
//main.cpp
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
//车主类
class carmen
{
public:
carmen();//默认构造函数
private:
string name;//车主姓名
string postcard;//车主身份证号码
};
//违规记录类
class violate
{
public:
violate();//默认构造函数
private:
string menName;//车主姓名
string memPostCard;//车主身份证号
string carNo;//车牌号
string time;//时间
string space;//地点
};
//车辆类
class cars
{
public:
cars();//默认构造函数
private:
string kind;//类型
string color;//颜色
string no;//车牌号
};
//组合类
class manage
{
public:
manage();//默认构造函数
void query_car_no();//车牌查询
void query_car_men();//车主查询
void newcar();//新车登记办证
void rework();//二手车过户
void delet();//旧车报废
void vioRecord();//违规登记
void vio_query(const string no);//违规查询
private:
cars acar;//车辆类对象
violate avio;//违规类对象
carmen acarmen;//车主类对象
};
cars::cars()
{
}
carmen::carmen()
{
}
violate::violate()
{
}
manage::manage()
{
}
//新车登记办证
void manage::newcar()
{
system("cls");
string kind;//车型
string color;
string no;
string name;
string postcard;//身份证
cout << endl << "新车登记办证" << endl << endl;
cout << "请根据提示在下面输入新车信息 : " << endl << endl;
cout << "车主姓名 : ";
cin >> name;
cout << endl << "身份证号 : ";
cin >> postcard;
cout << endl << " 车型 : ";
cin >> kind;
cout << endl << " 颜色 : ";
cin >> color;
cout << endl << " 车牌号 : ";
cin >> no;
ofstream car("d:\\cars.txt", ios::app);
assert(car);
car << setiosflags(ios::left) << setw(20) << postcard << " "
<< no << endl;
car.close();
string username = "car\\"+postcard+".txt";
ofstream carinfo(username.c_str());
carinfo << "车主姓名 : " << name << endl << endl
<< "身份证号 : " << postcard << endl << endl
<< " 车型 : " << kind << endl << endl
<< " 颜色 : " << color << endl << endl
<< " 车牌号 : " << no << endl << endl;
carinfo.close();
cout << endl << "新车信息录入完成!!!" << endl << endl;
system("pause");
}
//
//二手车过户
void manage::rework()
{
system("cls");
string kind;
string color;
string no;
string name;
string postcard;
string no1;
cout << endl << "二手车过户" << endl << endl;
cout << "请输入你想过户的车辆的车牌号 : ";
cin >> no1;
ifstream car("d:\\cars.txt");
if (!car)
{
cout << endl << "这辆车不存在!!!" << endl << endl;
system("pause");
return;
}
bool flag = false;//布尔型
ofstream temp("d:\\temp.txt");
while (car >> postcard >> no)
{
if (no == no1)
{
flag = true;
string carinfo = "car\\"+postcard+".txt";
ifstream car1(carinfo.c_str());
cout << endl << "你想过户的车辆信息如下 : " << endl << endl;
car1>>carinfo;
cout << car1.rdbuf() << endl;
car1.close();
cout << endl << "请在下面输入过户后的车主信息 : " << endl;
cout << endl << "新车主姓名 : ";
cin >> name;
cout << endl << "身份证号码 : ";
cin >> postcard;
ifstream car4(carinfo.c_str());
assert(car4);
string str;
getline(car4, str);
getline(car4, str);
getline(car4, str);
car4 >> str >> str >> kind
>> str >> str >> color
>> str >> str >> no;
car4.close();
carinfo = "car\\"+postcard+".txt";
ofstream car3(carinfo.c_str());
assert(car3);
car3 << "车主姓名 : " << name << endl << endl
<< "身份证号 : " << postcard << endl << endl
<< " 车型 : " << kind << endl << endl
<< " 颜色 : " << color << endl << endl
<< " 车牌号 : " << no << endl << endl;
car3.close();
}
temp << setiosflags(ios::left) << setw(20) << postcard << " "
<< no << endl;
}
temp.close();
car.close();
if (flag)
{
ofstream car2("d:\\cars.txt");
ifstream temp1("d:\\temp.txt");
car2 << temp1.rdbuf() << endl;
cout << endl << "这辆车过户完成!!!" << endl << endl;
}
else
cout << endl << "这辆车不存在!!!" << endl << endl;
system("pause");
}
//违章登记
void manage::vioRecord()
{
system("cls");
string no;
string time;
string place;
cout << endl << "违章登记" << endl << endl;
cout << "请根据提示输入违章车辆相关信息 : " << endl << endl;
cout << "车牌号码 : ";
cin >> no;
cout << endl << "违章时间 : ";
cin >> time;
cout << endl << "违章地点 : ";
cin >> place;
string voiInfo = "car\\"+no+".txt";
ofstream voiInfoFile(voiInfo.c_str(), ios::app);
assert(voiInfoFile);
voiInfoFile << setiosflags(ios::left) << setw(30) << time << " "
<< place << endl;
voiInfoFile.close();
cout << endl << "违章登记完成!!!" << endl << endl;
system("pause");
}
//旧车报废
void manage::delet()
{
string no;
string name;
string postcard;
string no1;
system("cls");
cout << endl << "旧车报废" << endl << endl;
cout << "请输入你想报废的车辆的车牌号 : ";
cin >> no1;
ifstream car("d:\\cars.txt");
if (!car)
{
cout << endl << "对不起,这辆车不存在!!!" << endl << endl;
system("pause");
}
ofstream temp("d\\temp.txt");
assert(temp);
bool flag = false;
bool flag1 = true;
while (car >> postcard >> no)
{
if (no == no1)
{
flag = true;
string carinfo = "car\\"+postcard+".txt";
ifstream carinfofile(carinfo.c_str());
assert(carinfofile);
cout << endl << "你想报废的车辆信息如下 : " << endl << endl;
cout << carinfofile.rdbuf() << endl << endl;
cout << "你确定要将它报废吗(y/n) : ";
if (getch() == 'n')
{
flag1 = false;
break;
}
}
else
{
temp << setiosflags(ios::left) << setw(20) << postcard << " "
<< no << endl;
}
}
car.close();
temp.close();
if (flag)
{
if (flag1)
{
ofstream car1("d:\\cars.txt");
assert(car1);
ifstream temp1("d:\\temp.txt");
assert(temp1);
car1 << temp1.rdbuf() << endl;
car1.close();
temp1.close();
cout << endl << endl << "这辆车已经成功报废!!!" << endl << endl;
}
else
{
cout << endl << endl << "这辆车你没有报废!!!" << endl << endl;
}
}
else
{
cout << endl << endl << "对不起, 你想报废的车辆不存在!!!" << endl << endl;
}
system("pause");
}
//
//车牌查询
void manage::query_car_no()
{
system("cls");
string findNo;
cout << endl << "根据车牌查询车辆信息" << endl << endl;
cout << "请输入车牌号 : ";
cin >> findNo;
string no;
string postcard;
ifstream car("d:\\cars.txt");
if (!car)
{
cout << endl << "对不起,记录中没有这辆车的相关信息!!!" << endl << endl;
system("pause");
return;
}
bool flag = false;
while (car >> postcard >> no)
{
if (no == findNo)
{
flag = true;
string carInfo = "car\\"+postcard+".txt";
ifstream carInfoFile(carInfo.c_str());
assert(carInfoFile);
cout << endl << "你查询的车辆信息如下 : " << endl << endl;
cout << carInfoFile.rdbuf() << endl;
carInfoFile.close();
vio_query(no);//违章
break;
}
}
car.close();
if (flag)
{
cout << endl << "信息查询完成!!" << endl << endl;
}
else
{
cout << endl << "对不起,这个车辆的信息不存在!!!" << endl << endl;
}
system("pause");
}
//车主查询
void manage::query_car_men()
{
system("cls");
cout << endl << "车主查询" << endl << endl;
cout << "请输入车主的身份证号码 : ";
string postcard1;
cin >> postcard1;
string postcard;
string no;
ifstream car("d:\\cars.txt");
if (!car)
{
cout << endl << "对不起,记录中没有这辆车的信息!!!" << endl << endl;
system("pause");
return;
}
bool flag = false;
while (car >> postcard >> no)
{
if (postcard == postcard1)
{
flag = true;
string carInfo = "car\\"+postcard+".txt";
ifstream carInfoFile(carInfo.c_str());
assert(carInfoFile);
cout << endl << "你要查询的车辆信息如下 : " << endl << endl;
cout << carInfoFile.rdbuf() << endl;
carInfoFile.close(); //
vio_query(no);//违章
break;
}
}
car.close();
if (flag)
{
cout << endl << "信息查询完成!!!" << endl << endl;
}
else
{
cout << endl << "对不起,记录中没有这辆车的信息!!!" << endl << endl;
}
system("pause");
}
//违章查询
void manage::vio_query(const string no)
{
string vioInfo = "car\\"+no+".txt";
ifstream vioFile(vioInfo.c_str());
if (!vioFile)
{
cout << endl << "这辆车没有违章!!!" << endl << endl;
return;
}
cout << endl << "违章记录如下 : " << endl << endl;
cout << "时间\t\t\t\t地点" << endl << endl;
cout << vioFile.rdbuf() << endl << endl;
}
char inter_face()
{
system("cls");
cout << endl;
cout <<"\t ◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇" <<endl
<<"\t || ||" <<endl
<<"\t ◇ 车 辆 管 理 系 统 ◇" <<endl
<<"\t || ||" <<endl
<<"\t ◇ 1. 新车登记办证 ◇" <<endl
<<"\t || ||" <<endl
<<"\t ◇ 2. 二手车过户 ◇" <<endl
<<"\t || ||" <<endl
<<"\t || 3. 违章登记 ||" <<endl
<<"\t || ||" <<endl
<<"\t ◇ 4. 旧车报废 ◇" <<endl
<<"\t || ||" <<endl
<<"\t ◇ 5. 根据车牌查询车辆信息 ◇" <<endl
<<"\t || ||" <<endl
<<"\t ◇ 6. 车主查询 ◇" <<endl
<<"\t || ||" <<endl
<<"\t ◇ 7. 退出系统 ◇" <<endl
<<"\t || ||" <<endl
<<"\t ◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇" <<endl <<"\t\t\t";
return getch();
}
int main()
{
printf("**************");
system("cls");
system("mode com cols=80 lines=30&color Df");
manage objcet;
while (1)
{
switch (inter_face())
{
case '1':
objcet.newcar();
break;
case '2':
objcet.rework();
break;
case '3':
objcet.vioRecord();
break;
case '4':
objcet.delet();
break;
case '5':
objcet.query_car_no();
break;
case '6':
objcet.query_car_men();
break;
case '7':
cout << endl << endl << "\t\t谢谢使用!!!" << endl << endl << "\t\t";
exit(0);
break;
default:
break;
}
}
return 0;
}