停车场管理系统C++

要求完成如下功能:
1.车辆到达; 2.车辆离开; 3.停车状况; 4. 退出系统;

第1个回答  2010-01-24
我写了一段程序,已经测试通过了。下面是代码:
//============================================================================
// Name : park.cpp
// Author : SilvernWing
// Version :
// Copyright : It's only a test, you can copy it anywhere for free.
// Description : Programming for parking in C++, Ansi-style
//============================================================================

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

// Come to parking
bool come(){
ifstream fin("number.txt");
int freeNumber;
fin >> freeNumber;
fin.close();
if(freeNumber <= 0){
cout << "Sorry, there is no free seat!" << endl;
}
else{
cout << "There are " << freeNumber << " parking space left." << endl;
cout << "Success, you can park your car." << endl;
ofstream fout("number.txt");
fout << freeNumber - 1;
fout.close();
}
return true;
}

// Leave the park lot
bool leave(){
ifstream fin("number.txt");
int freeNumber;
fin >> freeNumber;
fin.close();
if(freeNumber >= 20){
cout << "Sorry, there is no your car in the park!" << endl;
}
else{
cout << "Success, you can drive your car to leave." << endl;
cout << "And there are " << freeNumber + 1 << " parking space left." << endl;
ofstream fout("number.txt");
fout << freeNumber + 1;
fout.close();
}
return true;
}

// Show the number of the number of parking space.
void show(){
ifstream fin("number.txt");
int freeNumber;
fin >> freeNumber;
fin.close();
cout << "There are " << freeNumber << " parking space left." << endl;
}

// show the commands for user to input.
void showCommand(){
cout << endl << endl << endl;
cout << "1: come to park!" << endl;
cout << "2: leave the park." << endl;
cout << "3: show the number of free parking space." << endl;
cout << "4: quit" << endl;
cout << "Please write your command:" << endl;
}

int main() {
string command;
do{
showCommand();
cin >> command;
if(command == "1")come();
else if(command == "2")leave();
else if(command == "3")show();
else if(command != "quit" && command != "4")cout << "Error input, please check your input!" << endl;
}while(command != "quit" && command != "4");
return 0;
}
你要运行程序还需要一个文件,应该和上面的代码放在同一个路径,文件名叫“number.txt”,里面内容写个数就行,我写的是“20”。
相似回答