c语言课程设计自行车存放管理系统代码
时间: 2023-09-30 12:02:40 浏览: 163
自行车管理系统(C语言)
5星 · 资源好评率100%
以下是一个简单的自行车存放管理系统的代码示例,包括用户登录、车辆入库、车辆出库、查询、统计、排序、报表输出等功能。请注意,这只是一个简单的示例,您可以根据实际需求进行修改和完善。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_SIZE 100 // 最大存储的车辆数
#define FILE_NAME "data.txt" // 存储车辆信息的文件名
// 定义车辆信息结构体
typedef struct {
char plate[10]; // 车牌号
char model[20]; // 车型
char color[10]; // 颜色
char time[20]; // 入库时间
} BikeInfo;
// 全局变量
BikeInfo bikes[MAX_SIZE]; // 存储车辆信息的数组
int bikeCount = 0; // 当前存储的车辆数
// 函数声明
void initBikes(); // 初始化车辆信息
void saveBikes(); // 保存车辆信息到文件中
void loadBikes(); // 从文件中读取车辆信息
void login(); // 用户登录
void addBike(); // 录入车辆信息
void deleteBike(); // 删除车辆信息
void queryBike(); // 查询车辆信息
void statBike(); // 统计车辆信息
void sortBike(); // 排序车辆信息
void printBikes(); // 打印车辆信息
void printMenu(); // 打印菜单
int main() {
initBikes(); // 初始化车辆信息
loadBikes(); // 从文件中读取车辆信息
login(); // 用户登录
return 0;
}
// 初始化车辆信息
void initBikes() {
int i;
for (i = 0; i < MAX_SIZE; i++) {
strcpy(bikes[i].plate, "");
strcpy(bikes[i].model, "");
strcpy(bikes[i].color, "");
strcpy(bikes[i].time, "");
}
}
// 保存车辆信息到文件中
void saveBikes() {
FILE *fp = fopen(FILE_NAME, "w");
if (fp == NULL) {
printf("无法打开文件 %s\n", FILE_NAME);
return;
}
int i;
for (i = 0; i < bikeCount; i++) {
fprintf(fp, "%s %s %s %s\n", bikes[i].plate, bikes[i].model, bikes[i].color, bikes[i].time);
}
fclose(fp);
}
// 从文件中读取车辆信息
void loadBikes() {
FILE *fp = fopen(FILE_NAME, "r");
if (fp == NULL) {
printf("无法打开文件 %s\n", FILE_NAME);
return;
}
while (fscanf(fp, "%s %s %s %s", bikes[bikeCount].plate, bikes[bikeCount].model, bikes[bikeCount].color, bikes[bikeCount].time) != EOF) {
bikeCount++;
}
fclose(fp);
}
// 用户登录
void login() {
char username[20];
char password[20];
printf("请输入用户名:");
scanf("%s", username);
printf("请输入密码:");
scanf("%s", password);
if (strcmp(username, "admin") == 0 && strcmp(password, "123456") == 0) {
printMenu();
} else {
printf("用户名或密码错误!\n");
}
}
// 录入车辆信息
void addBike() {
if (bikeCount >= MAX_SIZE) {
printf("车辆信息已满,无法继续录入!\n");
return;
}
BikeInfo bike;
printf("请输入车牌号:");
scanf("%s", bike.plate);
int i;
for (i = 0; i < bikeCount; i++) {
if (strcmp(bikes[i].plate, bike.plate) == 0) {
printf("该车牌号已存在,请重新输入!\n");
return;
}
}
printf("请输入车型:");
scanf("%s", bike.model);
printf("请输入颜色:");
scanf("%s", bike.color);
time_t now;
time(&now);
strftime(bike.time, sizeof(bike.time), "%Y-%m-%d %H:%M:%S", localtime(&now));
bikes[bikeCount] = bike;
bikeCount++;
saveBikes();
printf("车辆信息录入成功!\n");
}
// 删除车辆信息
void deleteBike() {
char plate[10];
printf("请输入要删除的车牌号:");
scanf("%s", plate);
int i;
for (i = 0; i < bikeCount; i++) {
if (strcmp(bikes[i].plate, plate) == 0) {
int j;
for (j = i; j < bikeCount - 1; j++) {
bikes[j] = bikes[j + 1];
}
bikeCount--;
saveBikes();
printf("车辆信息删除成功!\n");
return;
}
}
printf("未找到该车牌号对应的车辆信息!\n");
}
// 查询车辆信息
void queryBike() {
char keyword[20];
printf("请输入要查询的关键字:");
scanf("%s", keyword);
int i, j;
for (i = 0; i < bikeCount; i++) {
if (strstr(bikes[i].plate, keyword) != NULL || strstr(bikes[i].model, keyword) != NULL || strstr(bikes[i].color, keyword) != NULL) {
printf("车牌号:%s\t车型:%s\t颜色:%s\t入库时间:%s\n", bikes[i].plate, bikes[i].model, bikes[i].color, bikes[i].time);
}
}
}
// 统计车辆信息
void statBike() {
int choice;
printf("请选择统计方式:\n");
printf("1、车型\n");
printf("2、颜色\n");
printf("3、入库时间\n");
printf("请选择:");
scanf("%d", &choice);
if (choice == 1) {
int count[10] = {0}; // 每种车型的数量
int i, j;
for (i = 0; i < bikeCount; i++) {
for (j = 0; j < 10; j++) {
if (strcmp(bikes[i].model, "model1") == 0) {
count[0]++;
} else if (strcmp(bikes[i].model, "model2") == 0) {
count[1]++;
} else if (strcmp(bikes[i].model, "model3") == 0) {
count[2]++;
} else if (strcmp(bikes[i].model, "model4") == 0) {
count[3]++;
} else if (strcmp(bikes[i].model, "model5") == 0) {
count[4]++;
} else if (strcmp(bikes[i].model, "model6") == 0) {
count[5]++;
} else if (strcmp(bikes[i].model, "model7") == 0) {
count[6]++;
} else if (strcmp(bikes[i].model, "model8") == 0) {
count[7]++;
} else if (strcmp(bikes[i].model, "model9") == 0) {
count[8]++;
} else if (strcmp(bikes[i].model, "model10") == 0) {
count[9]++;
}
}
}
printf("车型\t数量\n");
printf("model1\t%d\n", count[0]);
printf("model2\t%d\n", count[1]);
printf("model3\t%d\n", count[2]);
printf("model4\t%d\n", count[3]);
printf("model5\t%d\n", count[4]);
printf("model6\t%d\n", count[5]);
printf("model7\t%d\n", count[6]);
printf("model8\t%d\n", count[7]);
printf("model9\t%d\n", count[8]);
printf("model10\t%d\n", count[9]);
} else if (choice == 2) {
int count[5] = {0}; // 每种颜色的数量
int i, j;
for (i = 0; i < bikeCount; i++) {
for (j = 0; j < 5; j++) {
if (strcmp(bikes[i].color, "red") == 0) {
count[0]++;
} else if (strcmp(bikes[i].color, "blue") == 0) {
count[1]++;
} else if (strcmp(bikes[i].color, "green") == 0) {
count[2]++;
} else if (strcmp(bikes[i].color, "yellow") == 0) {
count[3]++;
} else if (strcmp(bikes[i].color, "black") == 0) {
count[4]++;
}
}
}
printf("颜色\t数量\n");
printf("red\t%d\n", count[0]);
printf("blue\t%d\n", count[1]);
printf("green\t%d\n", count[2]);
printf("yellow\t%d\n", count[3]);
printf("black\t%d\n", count[4]);
} else if (choice == 3) {
char start[20];
char end[20];
printf("请输入起始时间(格式:YYYY-MM-DD hh:mm:ss):");
scanf("%s", start);
printf("请输入结束时间(格式:YYYY-MM-DD hh:mm:ss):");
scanf("%s", end);
int count = 0;
int i;
for (i = 0; i < bikeCount; i++) {
if (strcmp(bikes[i].time, start) >= 0 && strcmp(bikes[i].time, end) <= 0) {
count++;
}
}
printf("在 %s 到 %s 之间入库的车辆数量为:%d\n", start, end, count);
} else {
printf("无效的选择!\n");
}
}
// 排序车辆信息
void sortBike() {
int choice;
printf("请选择排序方式:\n");
printf("1、按车牌号升序排序\n");
printf("2、按车牌号降序排序\n");
printf("3、按车型升序排序\n");
printf("4、按车型降序排序\n");
printf("请选择:");
scanf("%d", &choice);
int i, j;
BikeInfo temp;
if (choice == 1) {
for (i = 0; i < bikeCount - 1; i++) {
for (j = i + 1; j < bikeCount; j++) {
if (strcmp(bikes[i].plate, bikes[j].plate) > 0) {
temp = bikes[i];
bikes[i] = bikes[j];
bikes[j] = temp;
}
}
}
} else if (choice == 2) {
for (i = 0; i < bikeCount - 1; i++) {
for (j = i + 1; j < bikeCount; j++) {
if (strcmp(bikes[i].plate, bikes[j].plate) < 0) {
temp = bikes[i];
bikes[i] = bikes[j];
bikes[j] = temp;
}
阅读全文