用c语言写一个车票管理系统
时间: 2023-12-24 19:03:22 浏览: 102
july,c语言车票管理系统源码,c语言项目
下面是一个简单的车票管理系统的C语言代码示例,包括添加、删除和查询车票信息的基本功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TICKETS 100
struct ticket {
char name[20];
char start[20];
char end[20];
int price;
};
struct ticket tickets[MAX_TICKETS];
int num_tickets = 0;
void add_ticket() {
if (num_tickets >= MAX_TICKETS) {
printf("Error: ticket list is full.\n");
return;
}
struct ticket *t = &tickets[num_tickets++];
printf("Enter name: ");
scanf("%s", t->name);
printf("Enter start location: ");
scanf("%s", t->start);
printf("Enter end location: ");
scanf("%s", t->end);
printf("Enter price: ");
scanf("%d", &t->price);
}
void delete_ticket() {
printf("Enter name of ticket to delete: ");
char name[20];
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_tickets; i++) {
if (strcmp(tickets[i].name, name) == 0) {
found = 1;
for (int j = i + 1; j < num_tickets; j++) {
tickets[j - 1] = tickets[j];
}
num_tickets--;
break;
}
}
if (!found) {
printf("Error: ticket not found.\n");
}
}
void print_ticket(struct ticket *t) {
printf("%s: %s to %s, $%d\n", t->name, t->start, t->end, t->price);
}
void list_tickets() {
for (int i = 0; i < num_tickets; i++) {
print_ticket(&tickets[i]);
}
}
void search_ticket() {
printf("Enter name of ticket to search: ");
char name[20];
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_tickets; i++) {
if (strcmp(tickets[i].name, name) == 0) {
found = 1;
print_ticket(&tickets[i]);
break;
}
}
if (!found) {
printf("Error: ticket not found.\n");
}
}
int main() {
int choice;
do {
printf("\n1. Add ticket\n");
printf("2. Delete ticket\n");
printf("3. List tickets\n");
printf("4. Search ticket\n");
printf("5. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_ticket();
break;
case 2:
delete_ticket();
break;
case 3:
list_tickets();
break;
case 4:
search_ticket();
break;
case 5:
printf("Goodbye.\n");
break;
default:
printf("Error: invalid choice.\n");
}
} while (choice != 5);
return 0;
}
```
在这个示例中,我们使用一个结构体来表示每张车票的信息。add_ticket() 函数用于添加一张新的车票,delete_ticket() 函数用于删除指定的车票,list_tickets() 函数用于列出所有车票,search_ticket() 函数用于搜索指定的车票。main() 函数用于显示菜单选项,并根据用户输入调用相应的函数。
阅读全文