[槲寄生小记]停车场管理系统(完全体)

仅作记录备份:

#include <iostream>
#include <string>
#include <vector>
#include <bits/stdc++.h>
#define MAX_SIZE 2

using namespace std;

class Car {
public:
    string licensePlate;
    string color;
    string type;
    int arrivalTime;
    int departureTime;

    Car(string licensePlate, string type, string color, int arrivalTime) {
        this->licensePlate = licensePlate;
        this->type = type;
        this->color = color;
        this->arrivalTime = arrivalTime;
    }
};
// 自定义比较函数对象
struct CompareCarType {
    bool operator()(const Car& car1, const Car& car2) {
        return car1.type < car2.type;
    }
};

// 自定义判断条件函数对象
struct IsCarType {
    string type;
    IsCarType(string type) : type(type) {}
    bool operator()(const Car& car) {
        return car.type == type;
    }
};

struct CompareArrivalTime {
    bool operator()(const Car& car1, const Car& car2) {
        return car1.arrivalTime < car2.arrivalTime;
    }
};

struct IsArrivalTime {
    int time;
    IsArrivalTime(int time) : time(time) {}
    bool operator()(const Car& car) {
        return car.arrivalTime == time;
    }
};

class ParkingLot {
private:
    /*vector<Car> parkingLot;
    vector<Car> waitingLine;*/
    int numCars;
    int account;
    int testFlag;

public:
	vector<Car> parkingLot;
    vector<Car> waitingLine;
    ParkingLot() {
        numCars = 0;
        account = 0;
        testFlag = 1;
    }
    vector<Car> getParkingLot() {
        return parkingLot;
    }
    vector<Car> getWaitingLine() {
        return waitingLine;
    }
    int gettestFlag(){
    	return testFlag;
	}
     // 保存功能
    void saveToFile(string filename) {
    	ofstream file(filename.c_str());
        if (file.is_open()) {
            for (int i = 0; i < parkingLot.size(); i++) {
                file << parkingLot[i].licensePlate << " " << parkingLot[i].type << " " << parkingLot[i].color << " " << parkingLot[i].arrivalTime << endl;
            }
            file.close();
            cout << "保存成功!" << endl;
        } else {
            cout << "保存失败!" << endl;
        }
    }

    // 读取功能
    void readFromFile(string filename) {
        ifstream file(filename.c_str());
        if (file.is_open()) {
            string line;
            while (getline(file, line)) {
                stringstream ss(line);
                string licensePlate, type, color;
                int arrivalTime;
                ss >> licensePlate >> type >> color >> arrivalTime;
                Car car(licensePlate, type, color, arrivalTime);
                parkingLot.push_back(car);
            }
            file.close();
            cout << "读取成功!" << endl;
        } else {
            cout << "读取失败!" << endl;
        }
    }

     // 统计总车辆数
    void countTotalCars() {
        int totalCars = parkingLot.size();
        cout << "Total number of cars: " << totalCars << endl;
    }

    // 按车型统计
    void countCarsByType() {
        vector<string> carTypes;
        for (vector<Car>::iterator it = parkingLot.begin(); it != parkingLot.end(); ++it) {
            carTypes.push_back(it->type);
        }

        sort(carTypes.begin(), carTypes.end());

        carTypes.erase(unique(carTypes.begin(), carTypes.end()), carTypes.end());

        cout << "Number of cars by type:" << endl;
        for (vector<string>::iterator it = carTypes.begin(); it != carTypes.end(); ++it) {
            int count = count_if(parkingLot.begin(), parkingLot.end(), IsCarType(*it));
            cout << *it << ": " << count << endl;
        }
    }

    // 按到达时间统计
    void countCarsByArrivalTime() {
        vector<int> arrivalTimes;
        for (vector<Car>::iterator it = parkingLot.begin(); it != parkingLot.end(); ++it) {
            arrivalTimes.push_back(it->arrivalTime);
        }

        sort(arrivalTimes.begin(), arrivalTimes.end());

        arrivalTimes.erase(unique(arrivalTimes.begin(), arrivalTimes.end()), arrivalTimes.end());

        cout << "Number of cars by arrival time:" << endl;
        for (vector<int>::iterator it = arrivalTimes.begin(); it != arrivalTimes.end(); ++it) {
            int count = count_if(parkingLot.begin(), parkingLot.end(), IsArrivalTime(*it));
            cout << *it << ": " << count << endl;
        }
    }
    void editCar(const string& licensePlate, const string& type,const string& color,const int& arrivalTime) {
    	int flag1=0; 
        for (vector<Car>::iterator it = parkingLot.begin(); it != parkingLot.end(); ++it) {
            if (it->licensePlate == licensePlate) {
                it->type = type;
                it->color = color;
                it->arrivalTime = arrivalTime;
                flag1=1;
                break;
            }
        }
        if(flag1==0){
        	cout<<"N/A";
		}
    }

    void deleteCar(const string& licensePlate) {
        for (vector<Car>::iterator it = parkingLot.begin(); it != parkingLot.end(); ++it) {
            if (it->licensePlate == licensePlate) {
                parkingLot.erase(it);
                break;
            }
        }
    }

    void enterParkingLot(string licensePlate, string type, string color, int arrivalTime) {
        if (testFlag <= MAX_SIZE) {
            account++;
            Car newCar(licensePlate, type, color, arrivalTime);
            parkingLot.push_back(newCar);
            numCars++;
            cout << "Car with license plate " << licensePlate << " entered the parking lot." << endl;
        }
        if (testFlag > MAX_SIZE) {
            Car waitingCar(licensePlate, type, color, arrivalTime);
            waitingLine.push_back(waitingCar);
            cout << "Parking lot is full. Car with license plate " << licensePlate << " cannot enter." << endl;
        }
        testFlag++;
    }
    void exitParkingLot(string licensePlate, string type, string color, int departureTime) {
        int found = 0;
        for (int i = 0; i < numCars; i++) {
            if (parkingLot[i].licensePlate == licensePlate) {
                int parkingTime = departureTime - parkingLot[i].arrivalTime;
                int parkingFee = parkingTime * 10; // Assuming parking fee is 10 per hour
                cout << "Car with license plate " << licensePlate << " exited the parking lot. Parking fee: " << parkingFee << endl;
                found = 1;
                parkingLot.erase(parkingLot.begin() + i); // Remove the car from the parking lot
                numCars--;
                testFlag--;
                if (!waitingLine.empty()) {
                    parkingLot.push_back(waitingLine[0]);
                    numCars++;
                    waitingLine.erase(waitingLine.begin());
                }
                break;
            }
        }
        if (!found) {
            cout << "Car with license plate " << licensePlate << " is not in the parking lot." << endl;
        }
    }

    void searchCar1(string licensePlate) {
        int found = 0;
        int account1=0;
        for (int i = 0; i < numCars; i++) {
            if (parkingLot[i].licensePlate == licensePlate) {
                /*cout << "Car with license plate " << licensePlate << " is in the parking lot." << endl;*/
                cout << "Type: " << parkingLot[i].type << "  ";
                cout << "Color: " << parkingLot[i].color << "  ";
                cout << "Arrival Time: " << parkingLot[i].arrivalTime << endl;
                found = 1;
                account1++;
                break;
            }
        }
        if (!found) {
            cout << "Car with license plate " << licensePlate << " is not in the parking lot." << endl;
        }
    }
    void searchCar2(string type) {
    	int account2=0;
        int found = 0;
        for (int i = 0; i < numCars; i++) {
            /*if (parkingLot[i].type == type) {
                cout << "Car with type " << type << " is in the parking lot." << endl;
                found = 1;
            }*/
            if (parkingLot[i].type == type) {
            	cout << "licensePlate: " << parkingLot[i].licensePlate <<"  ";
                cout << "Color: " << parkingLot[i].color << "  ";
                cout << "Arrival Time: " << parkingLot[i].arrivalTime <<endl;
                found = 1;
                account2++;
        	}
		}
        if (!found) {
            cout << "Car with type " << type << " is not in the parking lot." << endl;
        }
    }
    void searchCar3(string color) {
    	int account3=0;
        int found = 0;
        for (int i = 0; i < numCars; i++) {
            /*if (parkingLot[i].color == color) {
                cout << "Car with color " << color << " is in the parking lot." << endl;
                found = 1;
            }*/
            if (parkingLot[i].color == color) {
            	cout << "licensePlate: " << parkingLot[i].licensePlate <<"  ";
                cout << "Type: " << parkingLot[i].type << "  ";
                cout << "Arrival Time: " << parkingLot[i].arrivalTime <<endl;
                found = 1;
                account3++;
        	}
		}
        if (!found) {
            cout << "Car with color " << color << " is not in the parking lot." << endl;
        }
    }
    
    void searchCar4(int arrivalTime) {
    	int account4=0;
        int found = 0;
        for (int i = 0; i < numCars; i++) {
            /*if (parkingLot[i].color == color) {
                cout << "Car with color " << color << " is in the parking lot." << endl;
                found = 1;
            }*/
            if (parkingLot[i].arrivalTime == arrivalTime) {
            	cout << "licensePlate: " << parkingLot[i].licensePlate <<"  ";
                cout << "Type: " << parkingLot[i].type << "  ";
                cout << "color: " << parkingLot[i].color <<endl;
                found = 1;
                account4++;
        	}
		}
        if (!found) {
            cout << "Car with arrivalTime " << arrivalTime << " is not in the parking lot." << endl;
        }
    }

    void parkingLotDataCheck() {
        int flag = 0;
        for (int i = 0; i < numCars; i++) {
            if (!parkingLot[i].licensePlate.empty()) {
                cout << i + 1 << ". licensePlate " << parkingLot[i].licensePlate << " type " << parkingLot[i].type << " color " << parkingLot[i].color << endl;
                flag = 1;
            }
        }
        if (flag == 0) {
            cout << "No cars in the parking lot." << endl;
        }
    }
};

int main() {
    ParkingLot parkingLot;
    int choice, arrivalTime, departureTime;
    int m=0;
    /*m1=parkingLot.getnumCars();*/
    int flag = 0;
    string licensePlate, color, type;
    while (1) {
    	start:
        cout << "\n\n**************************Parking management system****************************" << endl;
        cout << "                            ******************************" << endl;
        cout << "                              1. Enter parking lot" << endl;
        cout << "                              2. Exit parking lot" << endl;
        cout << "                              3. Enter searching system" << endl;
        cout << "                              4. Parking lot data check" << endl;
        cout << "                              5. Data input & output" << endl;
        cout << "                              6. Quit" << endl;
        cout << "Enter your choice: ";
        cin >> choice;
        
        switch (choice) {
            case 1:
            	
                cout<<"Enter license plate: ";
                cin>>licensePlate;

                if (m > 0) {
                    for (int i = 0; i < m; i++) {
                        if (licensePlate== parkingLot.getParkingLot()[i].licensePlate) {
                            printf("Add duplicate data, cancel the addition.\n");
                            flag = 1;
                            break;
                        }
                    }
                    for (int i = 0; i < parkingLot.waitingLine.size(); i++) {
                        if (licensePlate== parkingLot.getWaitingLine()[i].licensePlate) {
                            printf("Add duplicate data, cancel the addition.\n");
                            flag = 1;
                            break;
                        }
                    }}
                

                if (flag != 1) {
                    cout<<"Enter type: ";
                    cin>>type;
                    cout<<"Enter color: ";
                    cin>> color;
                    printf("Enter arrival time: ");
                    scanf("%d", &arrivalTime);
                    parkingLot.enterParkingLot(licensePlate, type, color, arrivalTime);
                    m++;
                }
                flag = 0;
                break;

            case 2:
                cout << "Enter license plate: ";
                cin >> licensePlate;
                cout << "Enter type: ";
                cin >> type;
                cout << "Enter color: ";
                cin >> color;
                cout << "Enter departure time: ";
                cin >> departureTime;
                parkingLot.exitParkingLot(licensePlate, type, color, departureTime);
                break;
            case 3:
            	int yourchoice;
            	while(true)
				{
                cout <<"************************"<<endl;
                cout <<"   1.search license"<<endl;
                cout <<"   2.search type"<<endl;
                cout <<"   3.search color"<<endl;
                cout <<"   4.search arrivalTime"<<endl;
                cout <<"   5.enter the editor mode"<<endl;
                cout <<"   6.exit the search system"<<endl;
                cout << "  Enter your choice: ";
                cin>>yourchoice;
                
                switch(yourchoice)
					{
                	case 1:
                		cout << "Enter license plate: ";
                        cin >> licensePlate;
                		parkingLot.searchCar1(licensePlate);
                		break;
                	case 2:
                		cout << "Enter type: ";
                		cin >> type;
                		parkingLot.searchCar2(type);
                		break;
                	case 3:
                		cout << "Enter color: ";
                		cin >> color;
                		parkingLot.searchCar3(color);
                		break;
                	case 4:
                		cout << "Enter arrivalTime: ";
                		cin >> arrivalTime;
                		parkingLot.searchCar4(arrivalTime);
                		break;
                	case 5:
                		int yourchoice;
            			while(true)
						{
                		cout <<"************************"<<endl;
                		cout <<"   1.edit information"<<endl;
                		cout <<"   2.delete information"<<endl;
                		cout <<"   3.exit editor mode"<<endl;
                		cout << "  Enter your choice: ";
                		cin>>yourchoice;
                
                		switch(yourchoice)
						{
							case 1:
								parkingLot.parkingLotDataCheck();
								cout << "Please enter the license plate number to be edited:";
    							cin >> licensePlate;
    							cout << "New Type:";
    							cin >> type;
    							cout << "New Color:";
    							cin >> color;
    							cout << "New arrivaltime:";
    							cin >> arrivalTime;
    							parkingLot.editCar(licensePlate, type, color, arrivalTime);
    							break;
    						case 2:
    							parkingLot.parkingLotDataCheck();
    							cout << "Please enter the license plate number to be :";
    							cin >> licensePlate;
								parkingLot.deleteCar(licensePlate);
								break;
							case 3:
								goto start;
								
								}
						}
                	case 6:
                		goto start;
                	default:
                		cout << "Invalid choice. Please try again." << endl;
					}
				}
				
            case 4:
                parkingLot.parkingLotDataCheck();
                int choice1;
                while(true){
                	cout<<"************"<<endl;
                	cout<<"  1.TOTAl type"<<endl;
                	cout<<"  2.TOTAl arrival time"<<endl;
                	cout<<"  3.TOTAl "<<endl;
					cout<<"  4.exit" <<endl;
					cout<<"your choice:"<<endl;
					cin>>choice1;
					
					switch(choice1)
					{
						case 1:
							parkingLot.countCarsByType();
							break;
						case 2:
							parkingLot.countCarsByArrivalTime();
							break;
						case 3:
							parkingLot.countTotalCars();
							break;
						case 4:
							goto start;
						default:
                		cout << "Invalid choice. Please try again." << endl;	
					}
				}
                break;
            case 5:
                int choice2;
                while(true){
                	cout<<"1.Data input"<<endl;
                	cout<<"2.Data output"<<endl;
                	cout<<"3.exit"<<endl;
                	cin>>choice2;
                	string filename = "C:\\Users\\yangx\\Desktop\\parking lot\\test.cpp";//"C:\Users\yangx\Desktop\parking lot\testing.docx"
                	switch(choice2)
					{
                		case 1:
                			parkingLot.saveToFile("parking_lot.txt");
                			break; 
                		case 2:
                			parkingLot.readFromFile("parking_lot.txt");
                			break; 
                		case 3:
                			goto start;
                			break; 
                		default:
                			cout << "Invalid choice. Please try again." << endl;
							break;	
					}
				}
            case 6:
                exit(0);
            
            default:
                cout << "Invalid choice. Please try again." << endl;
        }
    }

    return 0;
}

未经许可不得使用于其他设计

  • 13
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值