停车场管理系统C++

2024-12-21 03:18:56
推荐回答(1个)
回答1:

我写了一段程序,已经测试通过了。下面是代码:
//============================================================================
// 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
#include
#include

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”。