哪位C语言 高手帮帮忙 !!!不胜感激

2025-01-01 11:08:35
推荐回答(1个)
回答1:

我今天晚上给你写好.
写好了,花了好长时间写和调试,费了不少力气啊,呵呵.说实话,100分有点少哦,不然到现在肯定有会的人给你解答了.
程序如下,我运行过了,各个功能保证可以实现,你只要先用1功能录入一写信息资料,以1 8:00 郫县 广汉 2 45 30 这样的格式录入.然后再浏览查询,买票等等.因为我的编译器不支持汉字,所以调试的时候很多提示语我用英文写的,不过都是些简单的,相信你看的懂.如果有问题问我q23824056.

#include
#include
#include

typedef struct information
{
int numb;
char starttime[10];
char begin[21];
char end[21];
float lasttime;
int canload;
int alreadyload;
}INFORMATION;

void AddInformation(void)
{
FILE *fp;
INFORMATION info;
printf("please input the information you want to insert,input 0 to return back:\n");
scanf("%d", &info.numb);
if(info.numb == 0) return;
scanf("%s%s%s%f%d%d", info.starttime, info.begin, info.end, &info.lasttime, &info.canload, &info.alreadyload);
if((fp = fopen("c:\\information.txt", "a+")) == 0)
{
printf("open file error!");
exit(0);
}

fprintf(fp, "%d %s %s %s %f %d %d\n", info.numb, info.starttime, info.begin, info.end, info.lasttime, info.canload, info.alreadyload);

fclose(fp);
}

int GetHour(char *s)
{
char *p, *q;
p = (char *)malloc(10);
strcpy(p, s);
q = p;
while(*q != ':')
{
q++;
}
*q = '\0';
return atoi(p);
}

int GetMinute(char *s)
{
char *p;
p = (char *)malloc(10);
strcpy(p, s);
while(*p != ':')
{
p++;
}
p++;
return atoi(p);
}

void ShowInformation(void)
{
FILE *fp;
INFORMATION info;
time_t t;
struct tm *timeinfo;
if((fp = fopen("c:\\information.txt", "r+")) == 0)
{
printf("open file error!");
exit(0);
}
time(&t);
timeinfo = localtime(&t);
while(!feof(fp))
{
fscanf(fp, "%d%s%s%s%f%d%d\n", &info.numb, info.starttime, info.begin, info.end, &info.lasttime, &info.canload, &info.alreadyload);
printf("%d %s %s %s %f %d %d ", info.numb, info.starttime, info.begin, info.end, info.lasttime, info.canload, info.alreadyload);
if(GetHour(info.starttime) < timeinfo->tm_hour) printf("此班已发出!\n");
else if(GetHour(info.starttime) == timeinfo->tm_hour)
{
if(GetMinute(info.starttime)<=timeinfo->tm_min) printf("此班已发出!\n");
else printf("\n");
}
else printf("\n");
}
fclose(fp);
}

void SearchbyNum(void)
{
FILE *fp;
INFORMATION info;
int bc;
printf("please input the number,input 0 to return back:\n");
scanf("%d", &bc);
if(0 == bc) return;
if((fp = fopen("c:\\information.txt", "r+")) == 0)
{
printf("open file error!");
exit(0);
}
while(!feof(fp))
{
fscanf(fp, "%d%s%s%s%f%d%d\n", &info.numb, info.starttime, info.begin, info.end, &info.lasttime, &info.canload, &info.alreadyload);
if(info.numb == bc)
{
printf("%d %s %s %s %f %d %d\n", info.numb, info.starttime, info.begin, info.end, info.lasttime, info.canload, info.alreadyload);

}
}
fclose(fp);
}

void SearchbyEnd(void)
{
FILE *fp;
INFORMATION info;
char zhongdian[100];
printf("please input the place,input 0 to return back:\n");
scanf("%s", zhongdian);
if(strcmp(zhongdian, "0") == 0) return;
if((fp = fopen("c:\\information.txt", "r+")) == 0)
{
printf("open file error!");
exit(0);
}
while(!feof(fp))
{
fscanf(fp, "%d%s%s%s%f%d%d\n", &info.numb, info.starttime, info.begin, info.end, &info.lasttime, &info.canload, &info.alreadyload);
if(strcmp(zhongdian, info.end) == 0)
{
printf("%d %s %s %s %f %d %d\n", info.numb, info.starttime, info.begin, info.end, info.lasttime, info.canload, info.alreadyload);

}
}
fclose(fp);
}

void SearchWay(void)
{
int i;
printf("1. search by number\n");
printf("2. search by end\n");
printf("0. return back\n");
printf("which way do you want?\n");
scanf("%d", &i);
switch(i)
{
case 1: SearchbyNum(); break;
case 2: SearchbyEnd(); break;
case 0: return;
default : break;
}
}

void Buy(void)
{
FILE *fp;
INFORMATION info;
time_t t;
struct tm *timeinfo;
int bc;
long pos, pos1;
printf("please input the banci you want to buy,input 0 to return back:\n");
scanf("%d", &bc);
if(0 == bc) return;
if((fp = fopen("c:\\information.txt", "r+")) == 0)
{
printf("open file error!");
exit(0);
}
time(&t);
timeinfo = localtime(&t);
while(1)
{
pos1 = ftell(fp);
fscanf(fp, "%d%s%s%s%f%d%d\n", &info.numb, info.starttime, info.begin, info.end, &info.lasttime, &info.canload, &info.alreadyload);

pos = ftell(fp) - pos1;
if(info.numb == bc)
{

if(info.alreadyload < info.canload)
{
if(GetHour(info.starttime) < timeinfo->tm_hour)
{
printf("sorry, the plane has taken off!");
}
else if(GetHour(info.starttime) == timeinfo->tm_hour)
{
if(GetMinute(info.starttime) <= timeinfo->tm_min) printf("sorry, the plane has taken off!");
else
{
printf("take your ticket, bye!");
info.alreadyload ++;
fseek(fp, -pos, SEEK_CUR);
fprintf(fp, "%d %s %s %s %f %d %d\n", info.numb, info.starttime, info.begin, info.end, info.lasttime, info.canload, info.alreadyload);
}
}
else
{
printf("take your ticket, bye!");
info.alreadyload ++;
fseek(fp, -pos, SEEK_CUR);
fprintf(fp, "%d %s %s %s %f %d %d\n", info.numb, info.starttime, info.begin, info.end, info.lasttime, info.canload, info.alreadyload);
}
}
else printf("sorry, the tickets are all sold out!");
break;
}
}
fclose(fp);
}

void Cancel(void)
{
FILE *fp;
INFORMATION info;
time_t t;
struct tm *timeinfo;
int bc;
long pos, pos1;
printf("please input the banci you want to cancel,input 0 to return back:\n");
scanf("%d", &bc);
if(0 == bc) return;
if((fp = fopen("c:\\information.txt", "r+")) == 0)
{
printf("open file error!");
exit(0);
}
time(&t);
timeinfo = localtime(&t);
while(1)
{
pos1 = ftell(fp);
fscanf(fp, "%d%s%s%s%f%d%d\n", &info.numb, info.starttime, info.begin, info.end, &info.lasttime, &info.canload, &info.alreadyload);
pos = ftell(fp) - pos1;
if(info.numb == bc)
{
if(GetHour(info.starttime) < timeinfo->tm_hour)
{
printf("sorry, the plane has taken off!");
}
else if(GetHour(info.starttime) == timeinfo->tm_hour)
{
if(GetMinute(info.starttime) <= timeinfo->tm_min) printf("sorry, the plane has taken off!");
else
{
printf("your ticket is canceled, bye!");
info.alreadyload --;
fseek(fp, -pos, SEEK_CUR);
fprintf(fp, "%d %s %s %s %f %d %d\n", info.numb, info.starttime, info.begin, info.end, info.lasttime, info.canload, info.alreadyload);
}
}
else
{
printf("your ticket is canceled, bye!");
info.alreadyload --;
fseek(fp, -pos, SEEK_CUR);
fprintf(fp, "%d %s %s %s %f %d %d\n", info.numb, info.starttime, info.begin, info.end, info.lasttime, info.canload, info.alreadyload);
}
break;
}
}
fclose(fp);
}

void ForTicket(void)
{
int i;
printf("1. I want to buy a ticket\n");
printf("2. I want to return a ticket\n");
printf("0. return back\n");
printf("which do you want?");
scanf("%d", &i);
switch(i)
{
case 1: Buy(); break;
case 2: Cancel(); break;
case 0: return;
default : break;
}
}

int main()
{
int i;
while(1)
{
printf("\n1. 录入班次信息\n");
printf("2. 浏览班次信息\n");
printf("3. 查询路线\n");
printf("4. 售票退票\n");
printf("\nPlease input a number from 1 to 4 like above ,use 0 to exit!\n");
scanf("%d",&i);
switch(i)
{
case 0: return 0;
case 1: AddInformation(); break;
case 2: ShowInformation(); break;
case 3: SearchWay(); break;
case 4: ForTicket(); break;
default :
printf("Wrong number!\n"); break;
}
}
}