各位兄弟帮帮忙,一个简单的C程序。

pw163 2011-03-24 02:32:33


小弟是刚学C,碰到这个问题想了半天真不知道如何做才好,请教各兄弟了!


问题是,下面的表格给出了一个城市到另一个城市的每日航班信息。

***************************************************
起飞时间 抵达时间
***************************************************
8:00 a.m. 10:16 a.m.
9:43 a.m. 11:52 a.m.
11:19 a.m. 1:31 p.m.
12.47 p.m. 3:00 p.m.
2:00 p.m. 4:08 p.m.
3:45 p.m. 5:55 p.m.
7:00 p.m. 9:20 p.m.
9:45 p.m. 11:58 p.m.
***************************************************

编写一个程序,要求用户输入一个时间(用24小时制的时分表示)。程序选择起飞时间与用户输入最接近的航班,显示出相应的起飞时间和抵达时间。
Enter a 24-hour time: 13:15
Closest departure time is 12:47 p.m., arriving at 3:00 p.m.
提示:把输入用从午夜开始的分钟数表示。将这个时间与表格里也用从午夜开始的分钟数表示的起飞时间相比。例如,13:15从午夜开始是13 * 60 + 15 = 795分钟,与下午12:47(从午夜开始是767分钟)最接近。


...全文
414 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
terminalnt 2011-04-16
  • 打赏
  • 举报
回复
重发一下
#include <stdio.h>
#include <math.h>

struct flight
{
char * departure;
char * arriving;
unsigned int minutes;
};

/* Search for the appropriate flight and return its pointer.
* This function will find out the most close time's flight compare
* to the given value of minutes.
* @flights: the struct flight
* @num: the number of flights
* @value: the given value of minutes
*/
struct flight * search(struct flight * flights, unsigned int num, unsigned int value)
{
int i,last_diff=32767,diff;
struct flight * prev = NULL;
struct flight * cur = flights;

/*printf("func:search,value:%d\n",value);*/
for(i = 0; i < num; i++) {
diff = abs(cur->minutes - value);
/*printf("func:search,last_diff:%d,diff:%d\n",last_diff,diff);*/
if(last_diff < diff)
return prev;
last_diff = diff;
prev = cur;
cur++;
}
return prev;
}

int main(void)
{
unsigned int hour,minute,value;
struct flight * result = NULL;
struct flight flights[8] = {
"8:00 a.m.", "10:16 a.m.", 480,
"9:43 a.m.", "11:52 a.m.", 583,
"11:19 a.m.", "1:31 p.m.", 679,
"12:47 p.m.", "3:00 p.m.", 767,
"2:00 p.m.", "4:08 p.m.", 840,
"3:45 p.m.", "5:55 p.m.", 945,
"7:00 p.m.", "9:20 p.m.", 1140,
"9:45 p.m.", "11:58 p.m.", 1305
};
system("cls");
printf("Enter a 24-hour time: ");
scanf("%d:%d",&hour,&minute);
value = hour * 60 + minute;
result = search(flights,8,value);
printf("Closest departure time is %s, arriving at %s.\n\n",
result->departure, result->arriving);
system("pause");

return 0;

}
terminalnt 2011-04-16
  • 打赏
  • 举报
回复
我写了个更高效的,更结构化的代码:

#include <stdio.h>
#include <math.h>

struct flight
{
char * departure;
char * arriving;
unsigned int minutes;
};

/* Search for the appropriate flight and return its pointer.
* This function will find out the most close time's flight compare
* to the given value of minutes.
* @flights: the struct flight
* @num: the number of flights
* @value: the given value of minutes
*/
struct flight * search(struct flight * flights, unsigned int num, unsigned int value)
{
int i,last_diff=32767,diff;
struct flight * prev = NULL;
struct flight * cur = flights;

/*printf("func:search,value:%d\n",value);*/
for(i = 0; i < num; i++) {
diff = abs(cur->minutes - value);
/*printf("func:search,last_diff:%d,diff:%d\n",last_diff,diff);*/
if(last_diff < diff)
return prev;
last_diff = diff;
prev = cur;
cur++;
}
return prev;
}

int main(void)
{
unsigned int hour,minute,value;
struct flight * result = NULL;
struct flight flights[8] = {
"8:00 a.m.", "10:16 a.m.", 480,
"9:43 a.m.", "11:52 a.m.", 583,
"11:19 a.m.", "1:31 p.m.", 679,
"12:47 p.m.", "3:00 p.m.", 767,
"2:00 p.m.", "4:08 p.m.", 840,
"3:45 p.m.", "5:55 p.m.", 945,
"7:00 p.m.", "9:20 p.m.", 1140,
"9:45 p.m.", "11:58 p.m.", 1305
};
system("cls");
printf("Enter a 24-hour time: ");
scanf("%d:%d",&hour,&minute);
value = hour * 60 + minute;
result = search(flights,8,value);
printf("Closest departure time is %s, arriving at %s.\n\n",
result->departure, result->arriving);
system("pause");

return 0;

}
pw163 2011-04-09
  • 打赏
  • 举报
回复
各位兄弟,不知这样可行不可行?

#include <stdio.h>

int main(void)
{
int h1 = 8 *60, h2 = 9 * 60 + 43,
h3 = 11 * 60 + 19, h4 = 12 * 60 + 47,
h5 = 14 * 60, h6 = 15 * 60 + 45,
h7 = 19 * 60, h8 = 21 * 60 + 45, user_i1, user_i2, users;

printf("Enter a 24-hour time: ");
scanf("%d:%d", &user_i1, &user_i2);

users = user_i1 * 60 + user_i2;

if (h1 < users && users < h2)
if (users - h1 < h2 - users) {
printf("Closest departure time is 8:00 a.m., arriving at 10:16 a.m.");
} else {
printf("Closest departure time is 9:43 a.m., arriving at 11:52 a.m.");
}
else if (h2 < users && users < h3)
if (users - h2 < h3 - users) {
printf("Closest departure time is 9:43 a.m., arriving at 11:52 a.m.");
} else {
printf("Closest departure time is 11:19 a.m., arriving at 1:31 p.m.");
}
else if (h3 < users && users < h4)
if (users - h3 < h4 - users) {
printf("Closest departure time is 11:19 a.m., arriving at 1:31 p.m.");
} else {
printf("Closest departure time is 12.47 p.m., arriving at 3:00 p.m.");
}
else if (h4 < users && users < h5)
if (users - h4 < h5 - users) {
printf("Closest departure time is 12.47 p.m., arriving at 3:00 p.m.");
} else {
printf("Closest departure time is 2:00 p.m., arriving at 4:08 p.m.");
}
else if (h5 < users && users < h6)
if (users - h5 < h6 - users) {
printf("Closest departure time is 2:00 p.m., arriving at 4:08 p.m.");
} else {
printf("Closest departure time is 3:45 p.m., arriving at 5:55 p.m.");
}
else if (h6 < users && users < h7)
if (users - h6 < h7 - users) {
printf("Closest departure time is 3:45 p.m., arriving at 5:55 p.m.");
} else {
printf("Closest departure time is 7:00 p.m., arriving at 9:20 p.m.");
}
else if (h7 < users && users < h8)
if (users - h7 < h8 - users) {
printf("Closest departure time is 7:00 p.m., arriving at 9:20 p.m.");
} else {
printf("Closest departure time is 9:45 p.m., arriving at 11:58 p.m.");
}

return 0;
}
lt114896 2011-03-25
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
void main()
{
char time_now[5];
int temp[5];
int time;
int i,k;
printf("请输入时间(格式**:**):\n");
scanf("%s",&time_now);
for(i=strlen(time_now)-1,k=0;i>=-1;i--,k++)
{
if(i>-1)
temp[k]=time_now[i]-'0';
else
temp[k]=0;
}
printf("最近接的航班是:\n");
if(time<(8*60+(9-8)*60/2+43/2))
{
printf("航班:8:00 a.m. 10:16 a.m.\n");
}
else if(time<(9*60+((11-9)*60+19-43)/2))
{
printf("航班:9:43 a.m. 11:52 a.m.\n");
}
else if(time<(11*60+((12-11)*60+47-19)/2))
{
printf("航班:11:19 a.m. 1:31 p.m.\n");
}
else if(time<(12*60+((14-12)*60+0-47)/2))
{
printf("航班:12.47 p.m. 3:00 p.m.\n");
}
else if(time<(14*60+((15-14)*60+45-0)/2))
{
printf("航班:2:00 p.m. 4:08 p.m.\n");
}
else if(time<(15*60+((19-15)*60+0-45)/2))
{
printf("航班:3:45 p.m. 5:55 p.m.\n");
}
else if(time<(19*60+((21-19)*60+45-0)/2))
{
printf("航班:7:00 p.m. 9:20 p.m.\n");
}
else if(time<(21*60+45))
{
printf("航班:9:45 p.m. 11:58 p.m.\n");
}
else
{
printf("今天以无航班!");
}
}

lt114896 2011-03-25
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
void main()
{
char time_now[5];
int temp[5];
int time;
int i,k;
scanf("%s",&time_now);
for(i=strlen(time_now)-1,k=0;i>=-1;i--,k++)
{
if(i>-1)
temp[k]=time_now[i]-'0';
else
temp[k]=0;
}
printf("最近接的航班是:\n");
if(time<(8*60+(9-8)*60/2+43/2))
{
printf("航班:8:00 a.m. 10:16 a.m.\n");
}
else if(time<(9*60+((11-9)*60+19-43)/2))
{
printf("航班:9:43 a.m. 11:52 a.m.\n");
}
else if(time<(11*60+((12-11)*60+47-19)/2))
{
printf("航班:11:19 a.m. 1:31 p.m.\n");
}
else if(time<(12*60+((14-12)*60+0-47)/2))
{
printf("航班:12.47 p.m. 3:00 p.m.\n");
}
else if(time<(14*60+((15-14)*60+45-0)/2))
{
printf("航班:2:00 p.m. 4:08 p.m.\n");
}
else if(time<(15*60+((19-15)*60+0-45)/2))
{
printf("航班:3:45 p.m. 5:55 p.m.\n");
}
else if(time<(19*60+((21-19)*60+45-0)/2))
{
printf("航班:7:00 p.m. 9:20 p.m.\n");
}
else if(time<(21*60+45))
{
printf("航班:9:45 p.m. 11:58 p.m.\n");
}
else
{
printf("今天以无航班!");
}
}

caoatcao 2011-03-24
  • 打赏
  • 举报
回复
我想说点题外话,你这个有点不合理。
你看当你输入13:15的时候,你告诉客户12:47有起飞航班,没准现在已经12:55了这时候这个航班已经飞走了,所以应该告诉输入时间之后的最近的一个航班2:00;
当然了 按楼主的题意写也是很好写的。
首先做一个航班结构体,
struct fly{
long minute;//起始航班从午夜0点开始算起的总分钟数
char timestart[10];//起始航班时间
char timearrive[10];//到达航班时间
}
用上面的结构体分别把每次航班的起始时间,到达时间和从午夜算起的分钟数存起来

然后用一个变量time来接收你输入的时间,把这个时间转化成分钟数,和你之前存起来的航班结构体里的minute做比较,哪个绝对值小 就把这个航班编号记录然后用指针 输出它的起始航班时间和达到航班时间

pw163 2011-03-24
  • 打赏
  • 举报
回复
可否告知一下如何用switch或if语句写出来?
觅食的猫猫 2011-03-24
  • 打赏
  • 举报
回复
switch试试呢?
terminalnt 2011-03-24
  • 打赏
  • 举报
回复
把时间都按照从午夜到当前时间的总分钟数表示。
建立一个二维数组,每行代表一个航班。
然后把输入的时间转换成总分钟数,和每行第一列的起飞时间对比,找到差值最小的那行。
输出该行航班的起飞和降落时间。
c_losed 2011-03-24
  • 打赏
  • 举报
回复
有点二叉树的意思
看提示的话 就是根据用户输入的值 换算成分钟 然后找一个比较相近的 显示出来

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧