#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
FILE *fcoach;
typedef struct coach{
int ID;//班次号
int max_num;//最大乘车人数
int booked;//是否订票
float runhour;//运行时间
struct coach* next;
char departure_station[50];//始发站
char destination[50];//终点站
}coach;
//创建链表
void creadnode(coach** pphead, int id, char* ds, char* dt,int booked,int max_num,float runhour) {
coach* temp = (coach*)malloc(sizeof(coach));
temp->ID = id;
strcpy(temp->destination, dt);
strcpy(temp->departure_station, ds);
temp->booked = booked;
temp->max_num = max_num;
temp->runhour = runhour;
temp->next = NULL;
if (*pphead == NULL)
{
*pphead = temp;
}
else
{
coach* tail = *pphead;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = temp;
}
//写入文件
fcoach = fopen("coach.txt","w");
fprintf(fcoach, "班次号%d 预定人数%d 起始站%s 终点站%s 最大荷载人数%d 行车时间%3.1f", temp->ID, temp->booked, temp->departure_station, temp->destination,temp->max_num,temp->runhour);
}
//打印信息
void printcoach(coach * phead) {
coach* cur = phead;
while (cur != NULL) {
printf("班次号:%d\t", cur->ID);
printf("最大荷载人数%d\t", cur->max_num);
printf("订票人数%d\t", cur->booked);
printf("运行时间/h%3.1f\t", cur->runhour);
printf("始发站:%s\t", cur->departure_station);
printf("终点站:%s\n", cur->destination);
cur = cur->next;
}
}
//查找班次
void findid(coach* phead,int x){
coach* cur = phead;
while (cur != NULL) {
if (x == cur->ID) {
printf("班次号:%d\t", cur->ID);
printf("最大荷载人数%d\t", cur->max_num);
printf("订票人数%d\t", cur->booked);
printf("运行时间/h%3.1f\t", cur->runhour);
printf("始发站:%s\t", cur->departure_station);
printf("终点站:%s\n", cur->destination);
break;
}
else
{
cur = cur->next;
}
}
printf("没找到");
}
//按照终点站查询
void finddestination(coach* phead, char* x){
coach* cur = phead;
while (cur != NULL) {
if (strcmp(cur->destination,x)) {
printf("班次号:%d\t", cur->ID);
printf("最大荷载人数%d\t", cur->max_num);
printf("订票人数%d\t", cur->booked);
printf("运行时间/h%3.1f\t", cur->runhour);
printf("始发站:%s\t", cur->departure_station);
printf("终点站:%s\n", cur->destination);
break;
}
else
{
cur = cur->next;
}
}
printf("没找到");
}
//订票
void book_the_tick(coach* phead, int id) {
coach* cur = phead;
while (cur)
{
if (id == cur->ID && cur->max_num>cur->booked) {
cur->booked++;
printf("订票成功\n");
return ;
}
else
{
cur = cur->next;
}
}
printf("没有该车次\n");
}
void refund(coach* phead, int id) {
coach* cur = phead;
while (cur)
{
if (id == cur->ID && cur->booked > 0) {
cur->booked--;
printf("退票成功");
return;
}
else
{
cur = cur->next;
}
}
printf("没有该车次/没有订票");
}
int main() {
coach* plist = NULL;
int ID;//班次号
int max_num;//最大乘车人数
int booked;//订票人数
float runhour;//运行时间
int choic;
char departure_station[50];//始发站
char destination[50];//终点站
ch:
printf("--------------------------\n");
printf("-----0:退 出-----\n");
printf("-----1:查 找 班 次-----\n");
printf("-----2:查 找 终 点 站-\n");
printf("-----3:订 票-----\n");
printf("-----4:退 票-----\n");
printf("-----5:信 息 打 印-----\n");
printf("-----6:添 加 班 次-----\n");
printf("--------------------------\n");
printf("请选择");
scanf("%d",&choic);
getchar();
switch (choic) {
case 1:
printf("班次:");
scanf("%d", &ID);
findid(plist, ID);
break;
case 2:
printf("终点站:");
scanf("%s", &departure_station);
finddestination(plist, departure_station);
break;
case 3:
printf("需要订的班次:");
scanf("%d", &ID);
book_the_tick(plist, ID);
break;
case 4:
printf("需要退的班次:");
scanf("%d", &ID);
refund(plist, ID);
break;
case 5:
printcoach(plist);
break;
case 6:
printf("班次:");
scanf("%d", &ID);
printf("\n始发站:");
scanf("%s", &departure_station);
printf("\n终点站:");
scanf("%s", &destination);
printf("\n最大乘车人数:");
scanf("%d", &max_num);
printf("\n订票人数:");
scanf("%d", &booked);
printf("\n行车时间:");
scanf("%f", &runhour);
creadnode(&plist, ID, departure_station, destination,booked,max_num,runhour);
break;
case 0:
return 0;
default:
printf("请重新选择");
system("cls");
goto ch;
}
system("pause");
system("cls");
goto ch;
return 0;
}
这个东西是一次对c语言的小练手有一部分功能我想请问各位大佬如果我想输入一个时间在讲输入的时间和本地时间比较的话应该怎么写🥺👉👈🥺👉👈🥺👉👈