用C++写一个汽车租赁管理系统
时间: 2023-11-09 17:08:44 浏览: 179
好的,下面是一个简单的汽车租赁管理系统的C代码示例:
```
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_SIZE 100
struct Car{
int car_id;
char car_name[20];
char car_type[20];
int car_price;
int car_rented;
};
struct Car cars[MAX_SIZE];
int num_cars = 0;
void add_car(){
struct Car new_car;
printf("Enter car id: ");
scanf("%d",&new_car.car_id);
printf("Enter car name: ");
scanf("%s",new_car.car_name);
printf("Enter car type: ");
scanf("%s",new_car.car_type);
printf("Enter car price per day: ");
scanf("%d",&new_car.car_price);
new_car.car_rented = 0;
cars[num_cars] = new_car;
num_cars++;
printf("Car added successfully!\n");
}
void list_available_cars(){
printf("Available cars:\n");
for(int i=0;i<num_cars;i++){
if(cars[i].car_rented == 0){
printf("%d - %s (%s) - %d$ per day\n",cars[i].car_id,cars[i].car_name,cars[i].car_type,cars[i].car_price);
}
}
}
void rent_car(){
int id;
printf("Enter car id to rent: ");
scanf("%d",&id);
for(int i=0;i<num_cars;i++){
if(cars[i].car_id == id){
if(cars[i].car_rented == 1){
printf("Car already rented!\n");
return;
}
cars[i].car_rented = 1;
printf("Car rented successfully!\n");
return;
}
}
printf("Invalid car id!\n");
}
void return_car(){
int id;
printf("Enter car id to return: ");
scanf("%d",&id);
for(int i=0;i<num_cars;i++){
if(cars[i].car_id == id){
if(cars[i].car_rented == 0){
printf("Car not rented!\n");
return;
}
cars[i].car_rented = 0;
printf("Car returned successfully!\n");
return;
}
}
printf("Invalid car id!\n");
}
int main(){
int choice;
while(1){
printf("1. Add car\n");
printf("2. List available cars\n");
printf("3. Rent car\n");
printf("4. Return car\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: add_car();
break;
case 2: list_available_cars();
break;
case 3: rent_car();
break;
case 4: return_car();
break;
case 5: exit(0);
default: printf("Invalid choice!\n");
}
}
return 0;
}
```
这个代码实现了以下功能:
- 添加汽车信息
- 列出可租用汽车
- 租用汽车
- 归还汽车
- 退出系统
你可以根据自己的需求和实际情况对代码进行适当的修改和完善。
阅读全文