功能介绍
用图表的信息实现不同价格的汽车租赁
1、每辆车有车牌号、品牌、颜色、行驶公里数、租金、租车人,轿车有类型区别,客车有座位数量区别
2、可选择借的车型、借用的天数
3、计算租用的价钱,更改汽车的租用信息
4、还车时更改汽车的租用信息,并增加行驶公里数
程序演示
代码部分
Vehicle.h
//文件Vehicle.h
#pragma once
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
using namespace std;
//基类:车
class Vehicle
{
private:
string vnumber; //车牌
string vbrand; //品牌
string vcolour; //颜色
string vmile; //公里数
string vcost; //租金
string vcondition; //租车人
public:
Vehicle* next;
Vehicle(string number, string brand, string colour, string mile,string cost,string condition);
~Vehicle();
virtual string filename() = 0; //纯虚数 文件名
virtual int cost() = 0; //纯虚数 租金
virtual void bill() = 0; //纯虚数 打印账单
virtual void show()=0; //纯虚数 显示车辆信息
virtual string getType()=0; //纯虚数 得到类型
void vborrow(); //租车函数
void vreturn(); //还车函数
string getNumber(); //分别返回车牌号、品牌、颜色、里程数、日租金、租车人
string getBrand();
string getColour();
string getMile();
string getCost();
string getCondition();
};
//轿车
class Car :public Vehicle
{
private:
string cmodel; //类型
public:
Car(string number, string brand, string colour, string mile,string cost, string condition,string type);
~Car();
virtual string filename() ;
virtual int cost();
virtual void bill() ;
virtual void show();
string getType();
};
//客车
class Bus :public Vehicle
{
private:
string bseat; //座位
public:
Bus(string number, string brand, string colour, string mile,string cost, string condition,string seat);
~Bus();
virtual string filename() ;
virtual int cost();
virtual void bill() ;
virtual void show();
string getType();
};
Vehicle.cpp
//文件Vehicle.cpp
#include "vehicle.h"
using namespace std;
//基类:车
Vehicle::Vehicle(string number, string brand, string colour, string mile,string cost, string condition)
{
vnumber = number;
vbrand = brand;
vcolour = colour;
vmile = mile;
vcost = cost;
vcondition = condition;
}
Vehicle::~Vehicle()
{
}
void Vehicle::vborrow() //租车函数
{
cout << "请输入租车人的姓名" << endl;
cin >> vcondition;
}
void Vehicle::vreturn()//还车函数
{
int n,m;
cout << "请输入增加的公里数" << endl;
cin >> m;
n = std::stoi(vmile)+m;
vmile = std::to_string(n);
vcondition = "未租出";
}
string Vehicle::getNumber()
{
return vnumber;
}
string Vehicle::getBrand()
{
return vbrand;
}
string Vehicle::getColour()
{
return vcolour;
}
string Vehicle::getMile()
{
return vmile;
}
string Vehicle::getCost()
{
return vcost;
}
string Vehicle::getCondition()
{
return vcondition;
}
//轿车
Car::Car(string number, string brand, string colour, string mile,string cost, string condition,string type)
:Vehicle(number, brand, colour, mile,cost, condition)
{
cmodel = type;
}
Car::~Car()
{
}
string Car::filename() //返回文件名
{
return "Car.txt";
}
int Car::cost() //返回租金
{
int n;
n = std::stoi(Car::getCost()) ;
return n;
}
void Car::show() //显示车辆信息
{
cout << setw(15) <<cmodel<<setw(15)
<< Car::getNumber ()<< setw(15)
<< Car::getBrand ()<< setw(15)
<< Car::getColour()<< setw(15)
<< Car::getMile()<< setw(15)
<< Car::getCost() << setw(15)
<< Car::getCondition() << endl;
}
void Car::bill() //账单
{
cout <<endl<< "您租用了轿车" << cmodel << "," << "每日租金为" << Car::cost() << "元。" << endl;
}
string Car::getType() //返回类型
{
return cmodel;
}
//大巴
Bus::Bus(string number, string brand, string colour, string mile, string cost, string condition,string type)
:Vehicle(number, brand, colour, mile, cost, condition)
{
bseat = type;
}
Bus::~Bus()
{
}
string Bus::filename() //返回文件名
{
return "Bus.txt";
}
int Bus::cost() //返回日租金
{
int n;
n = std::stoi(Bus::getCost());
return n;
}
void Bus::show() //显示车辆信息
{
cout <<setw(15)<< bseat << setw(15)
<< Bus::getNumber() << setw(15)
<< Bus::getBrand() << setw(15)
<< Bus::getColour() << setw(15)
<< Bus::getMile() << setw(15)
<< Bus::getCost() << setw(15)
<< Bus::getCondition() << endl;
}
void Bus::bill() //账单
{
cout <<endl<< "您租用了客车" << bseat << "," << "每日租金为" << Bus::cost() << "元。" << endl;
}
string Bus::getType() //座位
{
return bseat;
}
VehicleList.h
//VehicleList.h
#pragma once
#include "Vehicle.h"
#include"function.h"
using namespace std;
//链表
class VehicleList
{
private:
Vehicle* head; //链表头
public:
void CarList(); //建立轿车链表并从文件读取
void BusList(); //建立客车链表并从文件读取
void showlist();//显示链表内容
void save(); //保存文件
int borrow(int* n);//租车、计算价格等等
int return_(); //换车、增加里程数等等
void freep(); //释放链表
};
VehicleList.cpp
#include "vehicle.h"
using namespace std;
//基类:车
Vehicle::Vehicle(string number, string brand, string colour, string mile,string cost, string condition)
{
vnumber = number;
vbrand = brand;
vcolour = colour;
vmile = mile;
vcost = cost;
vcondition = condition;
}
Vehicle::~Vehicle()
{
}
void Vehicle::vborrow() //租车函数
{
cout << "请输入租车人的姓名" << endl;
cin >> vcondition;
}
void Vehicle::vreturn()//还车函数
{
int n,m;
cout << "请输入增加的公里数" << endl;
cin >> m;
n = std::stoi(vmile)+m;
vmile = std::to_string(n);
vcondition = "未租出";
}
string Vehicle::getNumber()
{
return vnumber;
}
string Vehicle::getBrand()
{
return vbrand;
}
string Vehicle::getColour()
{
return vcolour;
}
string Vehicle::getMile()
{
return vmile;
}
string Vehicle::getCost()
{
return vcost;
}
string Vehicle::getCondition()
{
return vcondition;
}
//轿车
Car::Car(string number, string brand, string colour, string mile,string cost, string condition,string type)
:Vehicle(number, brand, colour, mile,cost, condition)
{
cmodel = type;
}
Car::~Car()
{
}
string Car::filename() //返回文件名
{
return "Car.txt";
}
int Car::cost() //返回租金
{
int n;
n = std::stoi(Car::getCost()) ;
return n;
}
void Car::show() //显示车辆信息
{
cout << setw(15) <<cmodel<<setw(15)
<< Car::getNumber ()<< setw(15)
<< Car::getBrand ()<< setw(15)
<< Car::getColour()<< setw(15)
<< Car::getMile()<< setw(15)
<< Car::getCost() << setw(15)
<< Car::getCondition() << endl;
}
void Car::bill() //账单
{
cout <<endl<< "您租用了轿车" << cmodel << "," << "每日租金为" << Car::cost() << "元。" << endl;
}
string Car::getType() //返回类型
{
return cmodel;
}
//大巴
Bus::Bus(string number, string brand, string colour, string mile, string cost, string condition,string type)
:Vehicle(number, brand, colour, mile, cost, condition)
{
bseat = type;
}
Bus::~Bus()
{
}
string Bus::filename() //返回文件名
{
return "Bus.txt";
}
int Bus::cost() //返回日租金
{
int n;
n = std::stoi(Bus::getCost());
return n;
}
void Bus::show() //显示车辆信息
{
cout <<setw(15)<< bseat << setw(15)
<< Bus::getNumber() << setw(15)
<< Bus::getBrand() << setw(15)
<< Bus::getColour() << setw(15)
<< Bus::getMile() << setw(15)
<< Bus::getCost() << setw(15)
<< Bus::getCondition() << endl;
}
void Bus::bill() //账单
{
cout <<endl<< "您租用了客车" << bseat << "," << "每日租金为" << Bus::cost() << "元。" << endl;
}
string Bus::getType() //座位
{
return bseat;
}
function.h
//function.h
#pragma once
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
void clearscreen();//清屏
void mainorder(); //主目录
void order1(); //目录1
void order21(); //主目录2的第1个
void order22(int n); //主目录2的第2个
void order23(int a, int b, int c, int d); //主目录的第3个
void order3(); //目录3
int getorder(); //选择
function.cpp
//function.cpp
#include"function.h"
using namespace std;
void clearscreen()
{
system("cls");
}
void mainorder() //主目录
{
clearscreen();
cout << "~~~~~~~~~~~~~~~~~~~~~" << endl
<< "~ 汽车租赁系统 ~" << endl
<< "~ ~" << endl
<< "~~~~~~~~~~~~~~~~~~~~~" << endl
<< "1、查看车辆信息" << endl
<< "2、租车" << endl
<< "3、还车" << endl
<< "4、退出系统" << endl;
}
void order1()
{
cout << "~~~~~~~~~~~~~~~~~~~~~" << endl
<< "~ 查看车辆信息 ~" << endl
<< "~ ~" << endl
<< "~~~~~~~~~~~~~~~~~~~~~" << endl
<< "1、轿车" << endl
<< "2、客车" << endl
<< "3、返回上一级" << endl;
}
void order21()
{
cout << "~~~~~~~~~~~~~~~~~~~~~" << endl
<< "~ 租车 ~" << endl
<< "~ ~" << endl
<< "~~~~~~~~~~~~~~~~~~~~~" << endl
<< "请输入要租用的车辆数:" << endl;
}
void order22(int n)
{
cout << "~~~~~~~~~~~~~~~~~~~~~" << endl
<< "~ 租车 ~" << endl
<< "~ ~" << endl
<< "~~~~~~~~~~~~~~~~~~~~~" << endl
<< "请选择租用的第"<<n<<"辆车的类型:" << endl
<< "1、轿车" << endl
<< "2、客车" << endl;
}
void order23(int a,int b,int c,int d)
{
cout << "~~~~~~~~~~~~~~~~~~~~~" << endl
<< "~ 租车订单 ~" << endl
<< "~ ~" << endl
<< "~~~~~~~~~~~~~~~~~~~~~" << endl
<< "本次租车情况:" << endl
<< "轿车" << a << "辆" << endl
<< "客车" << b << "辆" << endl
<< "租用" << c << "天" << endl
<< "共计" << (d * c) << "元" << endl;
}
void order3()
{
cout << "~~~~~~~~~~~~~~~~~~~~~" << endl
<< "~ 还车 ~" << endl
<< "~ ~" << endl
<< "~~~~~~~~~~~~~~~~~~~~~" << endl
<< "1、轿车" << endl
<< "2、客车" << endl
<< "3、返回上一级" << endl;
}
int getorder() //选择
{
int order = 0;
cin >> order;
return order;
}
main.cpp
#include "VehicleList.h"
using namespace std;
int main()
{
VehicleList list;
while (1)
{
mainorder();
switch (getorder())
{
case 1:
{
int m = 1;
while (m)
{
clearscreen();
order1();
switch (getorder())
{
case 1:
list.CarList();
list.showlist();
list.freep();
system("pause");
break;
case 2:
list.BusList();
list.showlist();
list.freep();
system("pause");
break;
case 3:
m = 0;
break;
}
}
}
break;
case 2:
{
clearscreen();
int money=0,days=0, number, number1=0 , number2=0;
order21();
cin >> number;
cout << "要租用的天数:" << endl;
cin >> days;
for (int i = 1; i <= number; i++)
{
clearscreen();
order22(i);
switch (getorder())
{
case 1:
number1++;
list.CarList();
list.borrow(&money);
list.save();
list.freep();
system("pause");
break;
case 2:
number2++;
list.BusList();
list.borrow(&money);
list.save();
list.freep();
system("pause");
break;
}
}
clearscreen();
order23(number1,number2,days,money);
system("pause");
}
break;
case 3:
{
int m = 1;
while (m)
{
clearscreen();
order3();
switch (getorder())
{
case 1:
list.CarList();
list.return_();
list.save();
list.freep();
system("pause");
break;
case 2:
list.BusList();
list.return_();
list.save();
list.freep();
system("pause");
break;
case 3:
m = 0;
break;
}
}
}
break;
case 4:
cout <<endl<< "您已退出系统,欢迎下次使用" << endl;
return 0;
}
}
}
文件格式
Bus.txt
Car.txt