如果声明和前pressions质疑意见 [英] If statement and expressions question advice

查看:93
本文介绍了如果声明和前pressions质疑意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是做了这个问题了KN国王的C语言编程:现代Aprroach。这不是功课,我只是自学出书...


  

下表显示了从一个城市到另一个城市的每日航班:结果
  的出发时间的 - 到达时间的结果
  上午8:00 - 10:16结果
  上午9时43分 - 上午11时52分结果
  11:19 - 下午1:31结果
  12:47 - 3:00结果
  下午2:00 - 16:08结果
  下午3:45 - 5:55 PM结果
  下午7:00 - 9:20 PM结果
  下午9:45 - 下午11点58分


  
  

编写一个程序,要求用户输入一个时间(以小时和分钟pssed前$ P $,采用24小时制)。然后,程序将显示出发和到达时间对于其出发时间是最接近于由用户输入的飞行


  
  

输入一个24小时时间 13:15 结果
  的最近depature时间是12:47,到达下午3:00。


  
  

提示的:转换输入到午夜以来在几分钟内pssed时间前$ P $,并将其与出发时间,也是前$ P $午夜以来在几分钟内pssed。例如,13:15是因为午夜,这比任​​何其他发车时间更接近12:47(午夜以来767分钟)13×60 + 15 =795分钟。


到目前为止,我们只涉及基本比较前pressions和if和switch语句,所以我的答案必须基于围绕这些显然并没有什么太花哨。我的code,我已经想出低于,我想知道,如果有人愿意看看它,看看我在正确的轨道上,它似乎工作,但似乎是一个很多code的这样一个小东西。也许这只是教我们所涉及的逻辑。我没有粘贴整个code,剩下的只是同样的事情一遍又一遍,因为它比较值。我没有编程经验,所以请温柔!

感谢您的时间,
安德鲁

 的#include<&stdio.h中GT;INT主(INT ARGC,为const char * argv的[]){
//自午夜航班起飞时间
//上午8点,上午9:45,11:19,12:47
//下午2点,下午3:45,晚上7:00,下午7:45
int类型的= 480,B = 585,C = 679,D = 767,
    E = 840,F = 945,G = 1140,H = 1185;//航班到达时间为各自的发车时间。
INT A1 = 616,B1 = 712,C1 = 811,D1 = 900,
    E1 = 968,F1 = 1075,G1 = 1280,H1 ​​= 1438;INT小时,分钟,时间,T,U;//获取用户的时间的printf(请输入24小时的时间(HH:MM):\\ n);
scanf函数(%d个:%d个,&安培;小时,和放大器;分钟);时间=小时* 60 +分钟;的printf(最近的发车时间);如果(时间下; =一)
    的printf(8:00);
其他
    如果(时间> A和&放大器;时间&下; = b)的{
                t =时间 - 一个;
                ü= B - 时间;
                如果(T< U){
                    的printf(%2d中:%2d中,一/ 60,%60);
                        如果(一个/ 60 == 0)
                            的printf(AM);
                        否则如果(一个/ 60℃; 12)
                            的printf(AM);
                        否则如果(A / 60 == 12)
                            的printf(PM);
                        其他
                            的printf(PM);
                    的printf(,到达%D:%2D的。,A1 / 60,A1%60);
                        IF(A1 / 60 == 0)
                            的printf(AM);
                        否则如果(A1 / 60℃; 12)
                            的printf(AM);
                        否则,如果(A1 / 60 == 12)
                            的printf(PM);
                        其他
                            的printf(PM);
                }
                其他{
                        的printf(%2D:二维%,B / 60,B 60%);
                        如果(B / 60 == 0)
                            的printf(AM);
                        否则如果(B / 60℃; 12)
                            的printf(AM);
                        否则,如果(B / 60 == 12)
                            的printf(PM);
                        其他
                            的printf(PM);
                    的printf(,到达%D:%2D,B1 / 60,B1%60);
                        如果(B1 / 60 == 0)
                            的printf(AM);
                        否则如果(B1 / 60℃; 12)
                            的printf(AM);
                        否则,如果(B1 / 60 == 12)
                            的printf(PM);
                        其他
                            的printf(PM);
                }
    }

根据意见,我一直在考虑的变化:(感谢xamypah和加布)
    ...

  INT小时,分钟,时间,T,U,X,Y;//获取用户的时间的printf(请输入24小时的时间(HH:MM):\\ n);
scanf函数(%d个:%d个,&安培;小时,和放大器;分钟);时间=小时* 60 +分钟;的printf(最近的发车时间);如果(时间下; =一)
    的printf(8:00);
其他
    如果(时间> A和&放大器;时间&下; = b)的{
                t =时间 - 一个;
                ü= B - 时间;                如果(T< = U){
                    X = A;
                    Y = A1;
                }
                其他{
                    X = B:
                    Y = B1;
                }

然后,在节目结束之后的几个以上的

 的printf(%2D:%2D,X / 60,X 60%);如果(X / 60℃; 12)
    的printf(AM);
其他
    的printf(PM);
    的printf(,到达%D:%2D,Y / 60,Y 60%);如果(γ/ 60℃; 12)
    的printf(AM);
其他
    的printf(PM);

其实我不得不做出一些改变为此print语句,否则它显示24小时制的时间后用AM和PM:

 如果(X / 60℃; 12)
    的printf(%d个:%2D AM,X / 60,X 60%);
其他
    的printf(%d个:%2D PM,(X / 60) - 12,X 60%);的printf(到达);如果(γ/ 60℃; 12)
    的printf(%d个:%2D AM,Y / 60,Y 60%);
其他
    的printf(%d个:%2D PM,(Y / 60) - 12,Y 60%);


解决方案

嗯,是的,你是正确的轨道上,但你也可以显著降低code的大小。
你看,在同一块code,我觉得,重复很多时候在你的程序:

 的printf(%2D:%2D,A / 60,%60);
                        如果(一个/ 60 == 0)
                            的printf(AM);
                        否则如果(一个/ 60℃; 12)
                            的printf(AM);
                        否则如果(A / 60 == 12)
                            的printf(PM);
                        其他
                            的printf(PM);
                    的printf(,到达%D:%2D的。,A1 / 60,A1%60);
                        IF(A1 / 60 == 0)
                            的printf(AM);
                        否则如果(A1 / 60℃; 12)
                            的printf(AM);
                        否则,如果(A1 / 60 == 12)
                            的printf(PM);
                        其他
                            的printf(PM);

据重复 A B 等...

和它通常是在执行结束时完成。

所以,如果你打破两部分你的算法:第一个 - 寻找合适的时间范围,第二个 - 打印出来,然后你摆脱掉这个复制/粘贴

I just did this question out of KN King's C Programming: A Modern Aprroach. It's not homework, I'm just teaching myself out of the book...

The following table shows the daily flights from one city to another:
Departure time - Arrival time
8:00am - 10:16am
9:43am - 11:52am
11:19am - 1:31pm
12:47pm - 3:00pm
2:00pm - 4:08pm
3:45pm - 5:55pm
7:00pm - 9:20pm
9:45pm - 11:58pm

Write a program that asks user to enter a time (expressed in hours and minutes, using the 24 hour clock). The program then displays the departure and arrival times for the flight whose departure time is closest to that entered by the user:

Enter a 24 hour time: 13:15
Closest depature time is 12:47pm., arriving at 3:00pm.

Hint: Convert the input into a time expressed in minutes since midnight, and compare it to the departure times, also expressed in minutes since midnight. For example, 13:15 is 13 x 60 + 15 = 795 minutes since midnight, which is closer to 12:47pm (767 minutes since midnight) than to any of the other departure times.

So far we have only covered basic comparison expressions and the if and switch statement, so my answer has to be based around these obviously and nothing too fancy. My code that I've come up with is below, I'm wondering if someone would be willing to take a look at it and see if I'm on the right track, it seems to work but seems to be a lot of code for such a small thing. Maybe it was just to teach us the logic involved. I haven't pasted the whole code, the rest is just the same thing over and over as it compares the values. I have no programming experience, so please be gentle!

Thanks for your time, Andrew

#include <stdio.h>

int main (int argc, const char * argv[]) {


// Flight departure times since midnight
// 8am, 9:45am, 11:19am, 12:47pm
// 2pm, 3:45pm, 7pm, 7:45pm
int a = 480, b = 585, c = 679, d = 767,
    e = 840, f = 945, g = 1140, h = 1185;

// Flight arrival times for respective departure times.
int a1 = 616, b1 = 712, c1 = 811, d1 = 900,
    e1 = 968, f1 = 1075, g1 = 1280, h1 = 1438;

int hours, minutes, time, t, u;

// Get the users time

printf("Enter a 24 hour time (hh:mm): \n");
scanf("%d:%d", &hours, &minutes);

time = hours * 60 + minutes;

printf("Closest departure time is ");

if (time <= a)
    printf("8:00am");
else
    if (time > a && time <= b) {
                t = time - a; 
                u = b - time;
                if (t < u) {
                    printf("%.2d:%.2d", a / 60, a % 60);
                        if (a / 60 == 0)
                            printf("am");
                        else if (a / 60 < 12)
                            printf("am");
                        else if (a / 60 == 12)
                            printf("pm");
                        else
                            printf("pm");
                    printf(", arriving at %d:%.2d", a1 / 60, a1 % 60);
                        if (a1 / 60 == 0)
                            printf("am");
                        else if (a1 / 60 < 12)
                            printf("am");
                        else if (a1 / 60 == 12)
                            printf("pm");
                        else
                            printf("pm");
                }
                else {
                        printf("%.2d:%.2d", b / 60, b % 60);
                        if (b / 60 == 0)
                            printf("am");
                        else if (b / 60 < 12)
                            printf("am");
                        else if (b / 60 == 12)
                            printf("pm");
                        else
                            printf("pm");
                    printf(", arriving at %d:%.2d", b1 / 60, b1 % 60);
                        if (b1 / 60 == 0)
                            printf("am");
                        else if (b1 / 60 < 12)
                            printf("am");
                        else if (b1 / 60 == 12)
                            printf("pm");
                        else
                            printf("pm");
                }
    }

Changes as per advice I've been given: (Thanks xamypah and Gabe) ...

int hours, minutes, time, t, u, x, y;

// Get the users time

printf("Enter a 24 hour time (hh:mm): \n");
scanf("%d:%d", &hours, &minutes);

time = hours * 60 + minutes;

printf("Closest departure time is ");

if (time <= a)
    printf("8:00am");
else
    if (time > a && time <= b) {
                t = time - a; 
                u = b - time;

                if (t <= u) {
                    x = a;
                    y = a1;
                }
                else {
                    x = b;
                    y = b1;
                }

Then at the end of the program after several of the above:

printf("%.2d:%.2d", x / 60, x % 60);

if (x / 60 < 12)
    printf("am");
else 
    printf("pm");
    printf(", arriving at %d:%.2d", y / 60, y % 60);

if (y / 60 < 12)
    printf("am");
else
    printf("pm");

Actually I had to make some changes to that end print statement, or else it was displaying the times in 24 hour format with am and pm after:

if (x / 60 < 12)
    printf("%d:%.2d am", x / 60, x % 60);
else 
    printf("%d:%.2d pm", (x / 60) - 12, x % 60);

printf(", arriving at ");

if (y / 60 < 12)
    printf("%d:%.2d am", y / 60, y % 60);
else
    printf("%d:%.2d pm", (y / 60) - 12, y % 60);

解决方案

Well, yes you are on the right track, but you also can significantly reduce the size of code. Look, the same piece of code, I think, is repeated very often in your program:

                    printf("%.2d:%.2d", a / 60, a % 60);
                        if (a / 60 == 0)
                            printf("am");
                        else if (a / 60 < 12)
                            printf("am");
                        else if (a / 60 == 12)
                            printf("pm");
                        else
                            printf("pm");
                    printf(", arriving at %d:%.2d", a1 / 60, a1 % 60);
                        if (a1 / 60 == 0)
                            printf("am");
                        else if (a1 / 60 < 12)
                            printf("am");
                        else if (a1 / 60 == 12)
                            printf("pm");
                        else
                            printf("pm");

It is repeated for a, for b etc...

And it is usually done at the end of the execution.

So, if you break your algorithm on two parts: first - finding the right time range, and the second - printing it out, then you get rid off this copy/pasting.

这篇关于如果声明和前pressions质疑意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆