鱼C论坛

 找回密码
 立即注册
查看: 1454|回复: 1

用cpp写个程序

[复制链接]
发表于 2023-11-10 09:57:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
用cpp写个程序实现一个停车场管理系统。 用户端可以查询自己信息(姓名、 年龄、 拥有的车辆信
息、缴费情况) 和自己车辆的信息(车牌号、 颜色、 品牌、 进入和离开时间、 应
缴费用等), 并且可以对车辆进行增减、 进入停车场和离开停车场等操作。 管理端
可以查询当前停车场的状态(剩余车位、 在停车辆及车主等信息)、 准入、 准出、 登
记和删除车主及车辆信息等操作。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-10 10:00:16 | 显示全部楼层
下面是一个用C++编写的停车场管理系统的示例程序:

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>

  4. using namespace std;

  5. // 车辆信息结构体
  6. struct Vehicle {
  7.     string licensePlate;
  8.     string color;
  9.     string brand;
  10.     string enterTime;
  11.     string exitTime;
  12.     double payment;
  13. };

  14. // 车主信息结构体
  15. struct Owner {
  16.     string name;
  17.     int age;
  18.     vector<Vehicle> vehicles;
  19.     bool hasPaid;
  20. };

  21. // 全局变量
  22. vector<Owner> owners;
  23. int totalParkingSpaces = 100;
  24. int remainingParkingSpaces = totalParkingSpaces;

  25. // 函数声明
  26. void addOwner();
  27. void removeOwner();
  28. void addVehicle();
  29. void removeVehicle();
  30. void enterParkingLot();
  31. void exitParkingLot();
  32. void displayParkingLotStatus();
  33. void registerPayment();

  34. int main() {
  35.     int choice;
  36.    
  37.     while (true) {
  38.         cout << "停车场管理系统" << endl;
  39.         cout << "1. 添加车主" << endl;
  40.         cout << "2. 删除车主" << endl;
  41.         cout << "3. 添加车辆" << endl;
  42.         cout << "4. 删除车辆" << endl;
  43.         cout << "5. 车辆进入停车场" << endl;
  44.         cout << "6. 车辆离开停车场" << endl;
  45.         cout << "7. 显示停车场状态" << endl;
  46.         cout << "8. 登记缴费" << endl;
  47.         cout << "0. 退出" << endl;
  48.         cout << "请选择操作:";
  49.         cin >> choice;
  50.         
  51.         switch (choice) {
  52.             case 1:
  53.                 addOwner();
  54.                 break;
  55.             case 2:
  56.                 removeOwner();
  57.                 break;
  58.             case 3:
  59.                 addVehicle();
  60.                 break;
  61.             case 4:
  62.                 removeVehicle();
  63.                 break;
  64.             case 5:
  65.                 enterParkingLot();
  66.                 break;
  67.             case 6:
  68.                 exitParkingLot();
  69.                 break;
  70.             case 7:
  71.                 displayParkingLotStatus();
  72.                 break;
  73.             case 8:
  74.                 registerPayment();
  75.                 break;
  76.             case 0:
  77.                 return 0;
  78.             default:
  79.                 cout << "无效的选择!" << endl;
  80.         }
  81.         
  82.         cout << endl;
  83.     }
  84.    
  85.     return 0;
  86. }

  87. // 添加车主
  88. void addOwner() {
  89.     Owner owner;
  90.    
  91.     cout << "请输入车主姓名:";
  92.     cin >> owner.name;
  93.    
  94.     cout << "请输入车主年龄:";
  95.     cin >> owner.age;
  96.    
  97.     cout << "是否已缴费(1-是,0-否):";
  98.     cin >> owner.hasPaid;
  99.    
  100.     owners.push_back(owner);
  101.    
  102.     cout << "车主添加成功!" << endl;
  103. }

  104. // 删除车主
  105. void removeOwner() {
  106.     string name;
  107.     cout << "请输入要删除的车主姓名:";
  108.     cin >> name;
  109.    
  110.     for (int i = 0; i < owners.size(); i++) {
  111.         if (owners[ i].name == name) {
  112.             owners.erase(owners.begin() + i);
  113.             cout << "车主删除成功!" << endl;
  114.             return;
  115.         }
  116.     }
  117.    
  118.     cout << "未找到该车主!" << endl;
  119. }

  120. // 添加车辆
  121. void addVehicle() {
  122.     string name;
  123.     cout << "请输入车主姓名:";
  124.     cin >> name;
  125.    
  126.     for (int i = 0; i < owners.size(); i++) {
  127.         if (owners[ i].name == name) {
  128.             Vehicle vehicle;
  129.             
  130.             cout << "请输入车牌号:";
  131.             cin >> vehicle.licensePlate;
  132.             
  133.             cout << "请输入车辆颜色:";
  134.             cin >> vehicle.color;
  135.             
  136.             cout << "请输入车辆品牌:";
  137.             cin >> vehicle.brand;
  138.             
  139.             owners[ i].vehicles.push_back(vehicle);
  140.             
  141.             cout << "车辆添加成功!" << endl;
  142.             return;
  143.         }
  144.     }
  145.    
  146.     cout << "未找到该车主!" << endl;
  147. }

  148. // 删除车辆
  149. void removeVehicle() {
  150.     string name;
  151.     cout << "请输入车主姓名:";
  152.     cin >> name;
  153.    
  154.     for (int i = 0; i < owners.size(); i++) {
  155.         if (owners[ i].name == name) {
  156.             string licensePlate;
  157.             cout << "请输入要删除的车牌号:";
  158.             cin >> licensePlate;
  159.             
  160.             for (int j = 0; j < owners[ i].vehicles.size(); j++) {
  161.                 if (owners[ i].vehicles[j].licensePlate == licensePlate) {
  162.                     owners[ i].vehicles.erase(owners[ i].vehicles.begin() + j);
  163.                     cout << "车辆删除成功!" << endl;
  164.                     return;
  165.                 }
  166.             }
  167.             
  168.             cout << "未找到该车辆!" << endl;
  169.             return;
  170.         }
  171.     }
  172.    
  173.     cout << "未找到该车主!" << endl;
  174. }

  175. // 车辆进入停车场
  176. void enterParkingLot() {
  177.     string licensePlate;
  178.     cout << "请输入车牌号:";
  179.     cin >> licensePlate;
  180.    
  181.     for (int i = 0; i < owners.size(); i++) {
  182.         for (int j = 0; j < owners[ i].vehicles.size(); j++) {
  183.             if (owners[ i].vehicles[j].licensePlate == licensePlate) {
  184.                 if (remainingParkingSpaces > 0) {
  185.                     owners[ i].vehicles[j].enterTime = "当前时间";
  186.                     remainingParkingSpaces--;
  187.                     cout << "车辆进入停车场成功!" << endl;
  188.                 } else {
  189.                     cout << "停车场已满,无法进入!" << endl;
  190.                 }
  191.                
  192.                 return;
  193.             }
  194.         }
  195.     }
  196.    
  197.     cout << "未找到该车辆!" << endl;
  198. }

  199. // 车辆离开停车场
  200. void exitParkingLot() {
  201.     string licensePlate;
  202.     cout << "请输入车牌号:";
  203.     cin >> licensePlate;
  204.    
  205.     for (int i = 0; i < owners.size(); i++) {
  206.         for (int j = 0; j < owners[ i].vehicles.size(); j++) {
  207.             if (owners[ i].vehicles[j].licensePlate == licensePlate) {
  208.                 owners[ i].vehicles[j].exitTime = "当前时间";
  209.                 remainingParkingSpaces++;
  210.                 cout << "车辆离开停车场成功!" << endl;
  211.                 return;
  212.             }
  213.         }
  214.     }
  215.    
  216.     cout << "未找到该车辆!" << endl;
  217. }

  218. // 显示停车场状态
  219. void displayParkingLotStatus() {
  220.     cout << "总停车位数:" << totalParkingSpaces << endl;
  221.     cout << "剩余停车位数:" << remainingParkingSpaces << endl;
  222.    
  223.     cout << "在停车辆及车主信息:" << endl;
  224.     for (int i = 0; i < owners.size(); i++) {
  225.         for (int j = 0; j < owners[ i].vehicles.size(); j++) {
  226.             cout << "车主姓名:" << owners[ i].name << endl;
  227.             cout << "车辆牌照:" << owners[ i].vehicles[j].licensePlate << endl;
  228.             cout << "车辆颜色:" << owners[ i].vehicles[j].color << endl;
  229.             cout << "车辆品牌:" << owners[ i].vehicles[j].brand << endl;
  230.             cout << "进入时间:" << owners[ i].vehicles[j].enterTime << endl;
  231.             cout << "离开时间:" << owners[ i].vehicles[j].exitTime << endl;
  232.             cout << "应缴费用:" << owners[ i].vehicles[j].payment << endl;
  233.             cout << endl;
  234.         }
  235.     }
  236. }

  237. // 登记缴费
  238. void registerPayment() {
  239.     string licensePlate;
  240.     cout << "请输入车牌号:";
  241.     cin >> licensePlate;
  242.    
  243.     for (int i = 0; i < owners.size(); i++) {
  244.         for (int j = 0; j < owners[ i].vehicles.size(); j++) {
  245.             if (owners[ i].vehicles[j].licensePlate == licensePlate) {
  246.                 owners[ i].vehicles[j].payment = 10.0; // 假设每小时停车费用为10元
  247.                 owners[ i].hasPaid = true;
  248.                 cout << "缴费登记成功!" << endl;
  249.                 return;
  250.             }
  251.         }
  252.     }
  253.    
  254.     cout << "未找到该车辆!" << endl;
  255. }
复制代码


这个程序使用了结构体来存储车主和车辆的信息。它提供了以下功能:

1. 添加车主:输入车主的姓名、年龄和缴费情况,将其添加到车主列表中。
2. 删除车主:输入要删除的车主的姓名,从车主列表中删除该车主。
3. 添加车辆:输入车主的姓名,然后输入车辆的牌照、颜色和品牌,将车辆添加到对应车主的车辆列表中。
4. 删除车辆:输入车主的姓名和要删除的车辆的牌照,从对应车主的车辆列表中删除该车辆。
5. 车辆进入停车场:输入车辆的牌照,将车辆的进入时间记录下来,并减少剩余停车位数。
6. 车辆离开停车场:输入车辆的牌照,将车辆的离开时间记录下来,并增加剩余停车位数。
7. 显示停车场状态:显示总停车位数、剩余停车位数以及在停车辆和车主的信息。
8. 登记缴费:输入车辆的牌照,将车辆的应缴费用设置为固定值,并将车主的缴费情况设置为已缴费。

你可以根据实际需求对程序进行修改和扩展。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-21 14:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表