这篇文章对于初学编程的小白来说相当适用,可以拿来学习或者练习会有不一样的体会~
由于这是作业要求,所以有不足的地方希望大家多多指正~
首先建立如下源.cpp和头文件.h
<一>首先展示的是三个头文件的源代码
(1)car.h
#pragma once
#include <iostream>
#include<string>
#include<vector>
using namespace std;
class car
{public:
void add();//添加车辆
void display();//显示车辆
void ask();//查询车辆
void xiugai();//修改车辆
void tongji();//统计车辆
void delet();//删除车辆
void user_add();
void user_display();
void user_ask();
private:
string cp;
string lx;
string color;
float money;
float intime;
int start_time;
int end_time;
int s_time;
};
(2)manager.h
#pragma once
#include <iostream>
#include<string>
using namespace std;
class manager
{
public:
void caidan2();
int cost();
};
(3)user.h
#pragma once
#include <iostream>
#include<string>
using namespace std;
class user
{
public:
void caidan1();
};
<二>下边的是四个源文件源代码
(1)main.cpp
#include <iostream>
#include<string>
#include"user.h"
#include"manager.h"
using namespace std;
int main()
{
while (true) //选择过后再次循环
{
system("cls"); //清屏
int ch1; //我们要增加局部变量,减少局部变量。
cout << "*******欢迎使用停车场管理系统*******"<<endl;
cout << " |1.管理员界面 |" << endl;
cout << " |2.普通人员界面 |" << endl;
cout << " |3.退出 |" << endl;
cout << " 请输入你的选择: " << endl;
cin >> ch1;
switch (ch1)
{
case 1:manager b; //管理员的子程序
b.caidan2();
break;
case 2:user a;
a.caidan1(); //普通人员的子程序
case 3:exit(0); //switch的退出,拓展一下if的退出
default:cout << "请输入1、2、3" << endl; //防止有其他选择
break;
}
system("pause");
}
return 0;
}
(2)manager.cpp
#include <iostream>
#include<string>
#include"car.h"
#include"manager.h"
#include<time.h>
using namespace std;
void manager::caidan2()
{
car b;
while (true) //选择过后再次循环
{
system("cls"); //清屏
int ch2; //我们要增加局部变量,减少局部变量。
cout << "*******管理员选项卡*******" << endl;
cout << " 1.增加车辆 " << endl;
cout <<" 2.显示所有车辆信息 " << endl;
cout << " 3.查询指定车辆信息 " << endl;
cout << " 4.修改车辆信息 " << endl;
cout << " 5.删除车辆信息 " << endl;
cout << " 6.统计车辆信息 " << endl;
cout << " 7.退出管理员用 户" << endl;
cout << " 请输入你的选择: " << endl;
cin >> ch2;
switch (ch2)
{
case 1:b.add();
break;
case 2:b.display();
break;
case 3:b.ask();
break;
case 4:b.xiugai();
break;
case 5:b.delet();
break;
case 6:b.tongji();
break;
case 7:return;
break;
default:cout << "请输入1、2、3、4、5、6、7" << endl; //防止有其他选择
break;
}
system("pause");
}
}
int manager::cost()//计算花费
{
time_t time1;
time(&time1);
return(int)time1;
}
(3)user.cpp
#include <iostream>
#include<string>
#include"user.h"
#include"car.h"
using namespace std;
void user::caidan1()
{
car b;
while (true) //选择过后再次循环
{
system("cls"); //清屏
int ch2; //我们要增加局部变量,减少局部变量。
cout << "*******yo选项卡*******" << endl;
cout << "1.显示车辆信息" << endl;
cout << "2.查询车辆" << endl;
cout << "3.统计车辆" << endl;
cout << "4.退出普通用户" << endl;
cout << "请输入你的选择:" << endl;
cin >> ch2;
switch (ch2)
{
case 1:
b.display();//对应car中显示车辆的指令
break;
case 2: b.ask();
break;
case 3:b.tongji();
break;
case 4:return;
break;
default:cout << "请输入1、2、3、4" << endl; //防止有其他选择
break;
}
system("pause");
}
}
(4)car.cpp
#include <iostream>
#include<string>
#include"car.h"
#include"manager.h"
#include <fstream>
#include <iomanip>
#include<time.h>
using namespace std;
//string p, l, c;
car c;
manager b;
void car::add()//增加车辆
{
AA:
cout << "请输入要增加的车辆号"<<endl;
cin >> cp;
ifstream infile("车辆管理.txt", ios::in | ios::app);
string str[100];
string t;
int i = 0;
while (!infile.eof())
{
infile >> str[i];
getline(infile, t);
i++;
}
for (auto&num : str)
{
if (num == cp)
{
cout << "车牌重复";
goto AA;
}
}
cout << "请输入车的类型:" << endl;
cin >> lx;
cout << "请输入车的颜色:" << endl;
cin >> color;
ofstream outfile("车辆管理.txt", ios::in|ios::app);
c.start_time = b.cost();
outfile << cp<<" " << lx <<" "<< color<<" "<<c.start_time<<endl;
outfile.close();
c.start_time = b.cost();
string a;
cout << "是否继续添加:" << endl;
cin >> a;
if (a == "是" || a == "yes" || a == "y" || a == "Y")
goto AA;
}
void car::display()//显示车辆
{
ifstream inf("车辆管理.txt",ios::in|ios::out); //括号里写文件地址,要包含头文件 fstream
string line;
while (getline(inf, line))
{
cout << line<<endl;
}
}
void car::ask()//查询车辆
{
string carlicense,str;
bool flag = true;
ifstream infile("车辆管理.txt", ios::in);
if (!infile )
{
cout << "文件打不开" << endl;
return;
}
AA:
cout << "请输入要查询车辆的车牌号:";
cin >> carlicense;
while (infile >> cp)
{
getline(infile, str);
if (cp == carlicense)
{
cout << "您要查找的车辆信息为:" << endl;
cout << carlicense << str << endl;
flag = false;
break;
}
}
if (flag)
{
cout << "该车辆不存在!"<<endl;
}
string a;
cout << "是否继续查询" << endl;
cin >> a;
if (a == "是" || a == "yes" || a == "y" || a == "Y")
goto AA;
}
void car::delet()//删除车辆
{
ifstream infile("车辆管理.txt", ios::in);
ofstream outfile("修改过后的车辆信息.txt", ios::out);
if (!infile || !outfile)
{
cout << "文件打不开" << endl;
return;
}
string carlicense, name, str, str1;
bool flag = true;
cout << "输入要删除的车牌:" << endl;
cin >> carlicense;
while (infile >> cp)//这里的意思是吧infile的内容传给cp
{
getline(infile, str1);
if (cp == carlicense)//用来判断infile的内容是否和输入的相同,这里连接了外界输入和文件里的内容
{
//cout << str1;
flag = false;
break;
}
outfile << cp << " " << str1 << endl;
}
if (flag==true)
cout << "该车牌号不存在" << endl;
else
{
while (getline(infile, str1))
outfile << str1 << endl;
outfile.close();
infile.close();
fstream outf("修改过后的车辆信息.txt", ios::in);
fstream inf("车辆管理.txt", ios::out);
if (!inf || !outf)
{
cout << "打开失败" << endl;
return;
}
else
while (getline(outf, str))
{
inf<< str << endl;
}
cout << "删除成功" << endl;//这里可以增加一个确认信息的条件
inf.close();
outf.close();
}
c.end_time = b.cost();
c.s_time = c.end_time - c.start_time;
c.money = c.s_time / 3600.0 * 10;
cout << "停车花费为:" << c.money << endl;
}
void car::xiugai()//修改车辆
{
ifstream infile("车辆管理.txt", ios::in);
ofstream outfile("修改过后的车辆信息.txt", ios::out);
if (!infile || !outfile)
{
cout << "文件打不开" << endl;
return;
}
string name, str,cp1;
bool flag = true;
AA:cout << "请输入要修改的车牌号:" << endl;
cin >> cp;
while (infile >> name)
{
getline(infile, str);
if (name== cp)
{
cout << "修改后车牌号:" << endl;
cin >> cp;
cout << "修改后车的类型:" << endl;
cin >> lx;
cout << "修改后车的颜色:" << endl;
cin >>color;
outfile << cp << " " << lx << " " << color << endl;
flag = false;
break;
}
outfile << cp << " " << str<< endl;
}
if (flag)
cout << "修改车辆数据不存在" << endl;
else
{
while (getline(infile, str))
outfile << str << endl;
outfile.close();
infile.close();
fstream outf("修改过后的车辆信息.txt", ios::in);
fstream inf("车辆管理.txt", ios::out);
if (!inf || !outf)
{
cout << "打开失败" << endl;
return;
}
else
while (getline(outf, str))
{
inf << str << endl;
}
inf.close();
outf.close();
}
string a;
cout << "是否继续修改" << endl;
cin >> a;
if (a == "是" || a == "yes" || a == "y" || a == "Y")
goto AA;
}
void car::tongji()//统计车辆
{
ifstream infile("车辆管理.txt", ios::in);
string str;
int count = 0;
while (infile)
{
getline(infile, str);
if (str.length() > 0)
count++;
}
cout << "车辆的数量为:" << count << endl;
infile.close();
}