C语言实现停车场管理系统
这个代码实现了一个基本的停车场管理系统,包括停车、结算费用和显示停车记录等功能。你可以根据自己的需求修改代码,添加更多功能。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define MAX_CAPACITY 100typedef struct {int id;char licensePlate[10];int timeIn;int timeOut;float cost;
} ParkingRecord;ParkingRecord parkingLot[MAX_CAPACITY];
int currentCapacity = 0;void showMenu() {printf("Welcome to the Parking Lot Management System!\n");printf("1. Park a car\n");printf("2. Check out a car\n");printf("3. Show parking records\n");printf("4. Exit\n");printf("Please enter your choice: ");
}void parkCar() {if (currentCapacity >= MAX_CAPACITY) {printf("Sorry, the parking lot is full.\n");return;}ParkingRecord newRecord;printf("Please enter the license plate number (up to 9 characters): ");scanf("%s", newRecord.licensePlate);printf("Please enter the time in (in minutes since 00:00): ");scanf("%d", &newRecord.timeIn);newRecord.id = currentCapacity + 1;parkingLot[currentCapacity] = newRecord;currentCapacity++;printf("The car is parked at spot %d.\n", newRecord.id);
}void checkOutCar() {int id;printf("Please enter the parking spot number: ");scanf("%d", &id);if (id < 1 || id > currentCapacity) {printf("Invalid parking spot number.\n");return;}ParkingRecord *record = &parkingLot[id-1];if (record->timeOut > 0) {printf("The car has already been checked out.\n");return;}printf("Please enter the time out (in minutes since 00:00): ");scanf("%d", &record->timeOut);float hours = (record->timeOut - record->timeIn) / 60.0;record->cost = hours * 1.5;printf("The cost for parking is $%.2f.\n", record->cost);
}void showRecords() {printf("Parking Records:\n");printf("ID\tLicense Plate\tTime In\tTime Out\tCost\n");for (int i = 0; i < currentCapacity; i++) {ParkingRecord record = parkingLot[i];printf("%d\t%s\t%d\t%d\t$%.2f\n", record.id, record.licensePlate,record.timeIn, record.timeOut, record.cost);}
}int main() {int choice;do {showMenu();scanf("%d", &choice);switch (choice) {case 1:parkCar();break;case 2:checkOutCar();break;case 3:showRecords();break;case 4:printf("Thank you for using the Parking Lot Management System!\n");break;default:printf("Invalid choice.\n");}} while (choice != 4);return 0;
}
#########################分隔符########################################
接下来来解释下代码
这个代码实现了一个简单的停车场管理系统,包括停车、结算费用和显示停车记录等功能。下面是代码的主要部分:
定义结构体 ParkingRecord
typedef struct {int id;char licensePlate[10];int timeIn;int timeOut;float cost;
} ParkingRecord;
这个结构体用来表示一条停车记录,包括停车位编号、车牌号、进入时间、离开时间和停车费用。
定义数组 parkingLot 和变量 currentCapacity
ParkingRecord parkingLot[MAX_CAPACITY];
int currentCapacity = 0;
这个数组表示停车场的所有停车位,最多可以容纳 MAX_CAPACITY 辆车。变量 currentCapacity 表示当前停车场中已停车辆的数量。
定义函数 showMenu
void showMenu() {printf("Welcome to the Parking Lot Management System!\n");printf("1. Park a car\n");printf("2. Check out a car\n");printf("3. Show parking records\n");printf("4. Exit\n");printf("Please enter your choice: ");
}
这个函数用来显示菜单,让用户选择相应的操作。
定义函数 parkCar
void parkCar() {if (currentCapacity >= MAX_CAPACITY) {printf("Sorry, the parking lot is full.\n");return;}ParkingRecord newRecord;printf("Please enter the license plate number (up to 9 characters): ");scanf("%s", newRecord.licensePlate);printf("Please enter the time in (in minutes since 00:00): ");scanf("%d", &newRecord.timeIn);newRecord.id = currentCapacity + 1;parkingLot[currentCapacity] = newRecord;currentCapacity++;printf("The car is parked at spot %d.\n", newRecord.id);
}
这个函数用来实现停车功能。如果停车场已经满了,则输出提示信息并返回。否则,让用户输入车牌号和进入时间,创建一条停车记录,并将其添加到 parkingLot 数组中。最后,输出停车位编号。
定义函数 checkOutCar
void checkOutCar() {int id;printf("Please enter the parking spot number: ");scanf("%d", &id);if (id < 1 || id > currentCapacity) {printf("Invalid parking spot number.\n");return;}ParkingRecord *record = &parkingLot[id-1];if (record->timeOut > 0) {printf("The car has already been checked out.\n");return;}printf("Please enter the time out (in minutes since 00:00): ");scanf("%d", &record->timeOut);float hours = (record->timeOut - record->timeIn) / 60.0;record->cost = hours * 1.5;printf("The cost for parking is $%.2f.\n", record->cost);
}
``
定义函数 showParkingRecords
void showParkingRecords() {if (currentCapacity == 0) {printf("There is no car parked in the parking lot.\n");return;}printf("Parking records:\n");printf("ID\tLicense Plate\tTime In\tTime Out\tCost\n");for (int i = 0; i < currentCapacity; i++) {ParkingRecord record = parkingLot[i];printf("%d\t%s\t\t%d\t%d\t\t%.2f\n",record.id, record.licensePlate, record.timeIn, record.timeOut, record.cost);}
}
这个函数用来显示停车记录。如果当前停车场中没有车辆,则输出提示信息并返回。否则,按照指定格式输出所有停车记录。
定义函数 main
int main() {int choice;do {showMenu();scanf("%d", &choice);switch (choice) {case 1:parkCar();break;case 2:checkOutCar();break;case 3:showParkingRecords();break;case 4:printf("Thank you for using the Parking Lot Management System!\n");break;default:printf("Invalid choice. Please try again.\n");break;}} while (choice != 4);return 0;
}
这个函数是程序的主函数,用来接收用户的输入,并根据用户的选择调用相应的函数。通过一个 do-while 循环,一直显示菜单,直到用户选择退出。
C语言实现停车场管理系统相关推荐
- 基于C/C++语言的停车场管理系统编程课程设计超详细
详细代码讨论加我QQ:1271370903 程序设计课程实践 基于C语言的停车管理系统编程设计 1.课程目的 本程序旨在训练学生的C语言基本编程能力,通过串联C语言输入输出.循环语句.子函数设计.数组 ...
- 停车场管理系统c语言查询,停车场管理系统c语言.doc
实用标准文案 精彩文档 学号 2015 2015-2016学年 第二学期 <高级语言程序设计> 课程设计报告 题目: 停车场管理系统 专业: 计算机科学与技术 班级: 15级计科<1 ...
- 停车场管理系统c语言程序,c语言程序设计 停车场管理系统 停车场有1-20个车位号,设计一个停车场管理系统,实现停车场管理...
#include #include #include #include #define max 3 #define price 1 int b=1; typedef struct { int day; ...
- 停车场管理系统程序设计c语言数据结构,数据结构(C语言)—停车场管理系统...
2014-01-02 回答 #include #include #include #include #define max 3 #define price 1 int b=1; typedef str ...
- 基于C语言的停车场管理系统编程(二)简单版
一.系统功能 (1)通过菜单的形式实现人机交互界面 (2)实现便道上停车信息显示 (3)实现录入进入停车场的车辆车牌号 二.设计思想 1.人机交互界面 2.录入进入车牌信息 3.显示已停停车场情况 下 ...
- C语言之停车场管理系统
本人软工学生一枚,学艺不精.有不足之处请谅解 ^ _ ^ 有什么问题还希望您能多批评指正,不吝指教. 蟹蟹你来看鸭~~ 1.问题描述 1)有一个两层的停车场,每层有6个车位,当第一层车位停满后才允 许 ...
- C语言停车场管理系统
C语言停车场管理系统 [问题描述] 某停车场是一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出.汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南端最先到达的第一辆车停放在车 ...
- c语言课程设计报告停车系统,停车场管理系统C语言课程设计
<停车场管理系统C语言课程设计>由会员分享,可在线阅读,更多相关<停车场管理系统C语言课程设计(27页珍藏版)>请在人人文库网上搜索. 1.计算机科学与技术系课程设计报告20 ...
- 停车场管理系统(C语言顺序栈+链栈+链队列)
一.实验目的 1.根据停车场管理系统的要求,利用结构化程序设计方法以及C的编程思想来完成系统的设计,使用数据结构中的栈.队列进行分析: 2.按功能定义函数或书写多个文件,进行模块化设计,各个功能模块用 ...
最新文章
- 深入浅出Redis五种基本数据类型
- phpsso.php 注入漏洞,PHPCMS各种注入漏洞补丁
- Spring Boot+JWT+Shiro+MyBatisPlus 实现 RESTful 快速开发后端脚手架
- C++实现数组中求第K大数
- iOS 静态库代码混淆方案
- 获取keras中间层输出、模型保存与加载
- git 可视化工具_WEB开发者必备工具集
- iKcamp出品|全网最新|微信小程序|基于最新版1.0开发者工具之初中级培训教程分享...
- 剑指offer之快速排序
- 28. 实现strStr()
- python字符转换unicode编码_Python字符编码转换Unicode和str
- 详解30道Vue面试题
- 恐龙涂色游戏 - 恐龙画画世界填色游戏
- 从零开始的运维之路【标题党】
- 让你两分钟明白什么是ERP
- 第一次创建百度脑图介绍自己,把创建过程分享一下吧,嘿嘿。
- 贝叶斯 - 《贝叶斯统计》笔记
- 爱快中的虚拟机不能获取IPV4地址
- mysql records_MySQL 基本操作 · LYF_Records
- JAVA学习笔记 03 - JAVA语言程序结构