用c语言实现单车管理 总体需求 单车信息的组成部分为:单车编号(不能重复)、所属城市、标记(是否租借)、单价、骑行次数、骑行总时长、总收入等 租车信息的组成部分为:会员名、单车编号、租借时间、归还时间、租金等 功能需求 1)新单车购入:添加单车信息2)单车查询:根据不同内容如租借标记、单价等查询并显示信息。 3)单车租借:输入会员名、单车编号、租借时间,修改租借标记(每个会员一次只可租借1辆单车) 4)单车归还:输入会员名、编号名称、归还日期,修改租借标记,计算租金。在该操作结束前,应 输出该会员此次归还所需支付的总租金。 (7)单车报废。 6)加入统计功能,可以统计会员租车信息等。要求用到动态链表,并用文件表示
时间: 2023-07-10 20:41:45 浏览: 52
好的,我会根据你的要求进行回答。以下是实现单车管理的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义单车结构体
typedef struct bike {
char id[20]; // 单车编号
char city[20]; // 所属城市
int rented; // 是否租借,1表示已租借,0表示未租借
float price; // 单价
int times; // 骑行次数
int duration; // 骑行总时长
float income; // 总收入
struct bike *next; // 指向下一个单车结构体的指针
} Bike;
// 定义租车信息结构体
typedef struct rental {
char name[20]; // 会员名
char id[20]; // 单车编号
char start_time[20]; // 租借时间
char end_time[20]; // 归还时间
float rent; // 租金
} Rental;
// 定义动态链表
Bike *head = NULL;
// 读取单车信息文件
void read_bike_file() {
FILE *fp;
fp = fopen("bike.txt", "r");
if (fp == NULL) {
printf("File open error!\n");
exit(1);
}
while (!feof(fp)) {
Bike *p = (Bike *)malloc(sizeof(Bike));
fscanf(fp, "%s %s %d %f %d %d %f", p->id, p->city, &p->rented, &p->price, &p->times, &p->duration, &p->income);
p->next = head;
head = p;
}
fclose(fp);
}
// 保存单车信息到文件
void save_bike_file() {
FILE *fp;
fp = fopen("bike.txt", "w");
if (fp == NULL) {
printf("File open error!\n");
exit(1);
}
Bike *p = head;
while (p != NULL) {
fprintf(fp, "%s %s %d %f %d %d %f\n", p->id, p->city, p->rented, p->price, p->times, p->duration, p->income);
p = p->next;
}
fclose(fp);
}
// 输出单车信息
void print_bike_info(Bike *p) {
printf("ID: %s\n", p->id);
printf("City: %s\n", p->city);
if (p->rented == 1) {
printf("Rented: Yes\n");
} else {
printf("Rented: No\n");
}
printf("Price: %.2f\n", p->price);
printf("Times: %d\n", p->times);
printf("Duration: %d\n", p->duration);
printf("Income: %.2f\n", p->income);
}
// 输出租车信息
void print_rental_info(Rental *p) {
printf("Name: %s\n", p->name);
printf("ID: %s\n", p->id);
printf("Start Time: %s\n", p->start_time);
printf("End Time: %s\n", p->end_time);
printf("Rent: %.2f\n", p->rent);
}
// 添加单车信息
void add_bike() {
Bike *p = (Bike *)malloc(sizeof(Bike));
printf("Enter bike information:\n");
printf("ID: ");
scanf("%s", p->id);
// 检查单车编号是否重复
Bike *q = head;
while (q != NULL) {
if (strcmp(q->id, p->id) == 0) {
printf("ID already exists!\n");
free(p);
return;
}
q = q->next;
}
printf("City: ");
scanf("%s", p->city);
printf("Price: ");
scanf("%f", &p->price);
p->rented = 0;
p->times = 0;
p->duration = 0;
p->income = 0;
p->next = head;
head = p;
printf("Bike information added successfully!\n");
}
// 查询单车信息
void search_bike() {
int option;
printf("Enter search option:\n");
printf("1. Rented\n");
printf("2. Price\n");
printf("Option: ");
scanf("%d", &option);
Bike *p = head;
while (p != NULL) {
switch (option) {
case 1:
if (p->rented == 1) {
print_bike_info(p);
}
break;
case 2:
printf("Enter price range: ");
float min_price, max_price;
scanf("%f %f", &min_price, &max_price);
if (p->price >= min_price && p->price <= max_price) {
print_bike_info(p);
}
break;
default:
printf("Invalid option!\n");
return;
}
p = p->next;
}
}
// 租借单车
void rent_bike() {
char name[20];
char id[20];
printf("Enter rental information:\n");
printf("Name: ");
scanf("%s", name);
printf("ID: ");
scanf("%s", id);
// 检查会员是否已经租借过单车
Bike *p = head;
while (p != NULL) {
if (strcmp(p->id, id) == 0) {
if (p->rented == 1) {
printf("Bike already rented!\n");
return;
}
p->rented = 1;
break;
}
p = p->next;
}
if (p == NULL) {
printf("Invalid bike ID!\n");
return;
}
Rental *r = (Rental *)malloc(sizeof(Rental));
strcpy(r->name, name);
strcpy(r->id, id);
printf("Start Time: ");
scanf("%s", r->start_time);
r->rent = p->price;
printf("Bike rented successfully!\n");
}
// 归还单车
void return_bike() {
char name[20];
char id[20];
printf("Enter rental information:\n");
printf("Name: ");
scanf("%s", name);
printf("ID: ");
scanf("%s", id);
Rental *r = (Rental *)malloc(sizeof(Rental));
strcpy(r->name, name);
strcpy(r->id, id);
printf("End Time: ");
scanf("%s", r->end_time);
// 计算租金
Bike *p = head;
while (p != NULL) {
if (strcmp(p->id, r->id) == 0) {
if (p->rented == 0) {
printf("Bike not rented!\n");
return;
}
p->rented = 0;
p->times++;
p->duration += (atoi(r->end_time) - atoi(r->start_time));
p->income += r->rent;
break;
}
p = p->next;
}
if (p == NULL) {
printf("Invalid bike ID!\n");
return;
}
r->rent = p->price * (atoi(r->end_time) - atoi(r->start_time)) / 60.0;
print_rental_info(r);
free(r);
}
// 报废单车
void scrap_bike() {
char id[20];
printf("Enter bike ID: ");
scanf("%s", id);
Bike *p = head;
Bike *prev = NULL;
while (p != NULL) {
if (strcmp(p->id, id) == 0) {
if (p->rented == 1) {
printf("Cannot scrap rented bike!\n");
return;
}
if (prev == NULL) {
head = p->next;
} else {
prev->next = p->next;
}
printf("Bike scrapped successfully!\n");
free(p);
return;
}
prev = p;
p = p->next;
}
printf("Invalid bike ID!\n");
}
// 统计会员租车信息
void stat_rental() {
char name[20];
printf("Enter member name: ");
scanf("%s", name);
Rental *p = (Rental *)malloc(sizeof(Rental));
FILE *fp;
fp = fopen("rental.txt", "r");
if (fp == NULL) {
printf("File open error!\n");
exit(1);
}
int count = 0;
float total_rent = 0;
while (!feof(fp)) {
fscanf(fp, "%s %s %s %s %f", p->name, p->id, p->start_time, p->end_time, &p->rent);
if (strcmp(p->name, name) == 0) {
count++;
total_rent += p->rent;
}
}
fclose(fp);
printf("Total rentals: %d\n", count);
printf("Total rent: %.2f\n", total_rent);
free(p);
}
int main() {
read_bike_file();
while (1) {
int option;
printf("\nChoose an option:\n");
printf("1. Add bike\n");
printf("2. Search bike\n");
printf("3. Rent bike\n");
printf("4. Return bike\n");
printf("5. Scrap bike\n");
printf("6. Stat rental\n");
printf("7. Exit\n");
printf("Option: ");
scanf("%d", &option);
switch (option) {
case 1:
add_bike();
break;
case 2:
search_bike();
break;
case 3:
rent_bike();
break;
case 4:
return_bike();
break;
case 5:
scrap_bike();
break;
case 6:
stat_rental();
break;
case 7:
save_bike_file();
return 0;
default:
printf("Invalid option!\n");
break;
}
}
}
```
注意:以上代码中使用了文件读写来保存单车信息和租车信息。单车信息保存在`bike.txt`文件中,每行表示一个单车的信息,各字段之间用空格分隔;租车信息保存在`rental.txt`文件中,每行表示一次租车的信息,各字段之间用空格分隔。在每次修改单车信息或进行统计操作时,需要读取`bike.txt`和`rental.txt`文件,修改完后再保存回文件中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)