| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 500 人关注过本帖
标题:新人请教程序错在哪,用户输入数据后,就没有输出了
只看楼主 加入收藏
tximist
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2010-9-17
收藏
 问题点数:0 回复次数:9 
新人请教程序错在哪,用户输入数据后,就没有输出了
题目要求:
图片附件: 游客没有浏览图片的权限,请 登录注册

代码:
程序代码:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    #define DEPART_TIME_1 (8 * 60)

    #define DEPART_TIME_2 (9 * 60 + 43)

    #define DEPART_TIME_3 (11 * 60 + 19)

    #define DEPART_TIME_4 (12 * 60 + 47)

    #define DEPART_TIME_5 (14 * 60)

    #define DEPART_TIME_6 (15 * 60 + 45)

    #define DEPART_TIME_7 (19 * 60)

    #define DEPART_TIME_8 (21 * 60 + 45)

    int h, m, user_time, min;

    printf("Enter a 24-hour time(h:m): ");

    scanf("%d:%d", &h, &m);

    user_time = h * 60 + m;

    min = (abs(user_time - DEPART_TIME_1) < abs(user_time - DEPART_TIME_2)) ? abs(user_time - DEPART_TIME_1) : abs(user_time - DEPART_TIME_2);

    min = (min < abs(user_time - DEPART_TIME_3)) ? min : abs(user_time - DEPART_TIME_3);

    min = (min < abs(user_time - DEPART_TIME_4)) ? min : abs(user_time - DEPART_TIME_4);

    min = (min < abs(user_time - DEPART_TIME_5)) ? min : abs(user_time - DEPART_TIME_5);

    min = (min < abs(user_time - DEPART_TIME_6)) ? min : abs(user_time - DEPART_TIME_6);

    min = (min < abs(user_time - DEPART_TIME_7)) ? min : abs(user_time - DEPART_TIME_7);

    min = (min < abs(user_time - DEPART_TIME_8)) ? min : abs(user_time - DEPART_TIME_8);

    switch (user_time - min)
    {
        case DEPART_TIME_1:
            printf("Closest departure time is 8:00 a.m., arriving at 10:16 a.m.");
            break;
        case DEPART_TIME_2:
            printf("Closest departure time is 9:43 a.m., arriving at 11:52 a.m.");
            break;
        case DEPART_TIME_3:
            printf("Closest departure time is 11:19 a.m., arriving at 1:31 p.m.");
            break;
        case DEPART_TIME_4:
            printf("Closest departure time is 12:47 p.m., arriving at 3:00 p.m.");
            break;
        case DEPART_TIME_5:
            printf("Closest departure time is 2:00 p.m., arriving at 4:08 p.m.");
            break;
        case DEPART_TIME_6:
            printf("Closest departure time is 3:45 p.m., arriving at 5:55 p.m.");
            break;
        case DEPART_TIME_7:
            printf("Closest departure time is 7:00 p.m., arriving at 9:20 p.m.");
            break;
        case DEPART_TIME_8:
            printf("Closest departure time is 9:45 p.m., arriving at 11:58 p.m.");
            break;
    }

    return 0;
}
屏幕输出:
图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册

输入9:00后,就没有第二步输出了,请问哪个地方有问题?


[ 本帖最后由 tximist 于 2015-9-20 17:03 编辑 ]
搜索更多相关主题的帖子: 用户 
2015-09-20 16:58
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
点值和区间值匹对。。。

剑栈风樯各苦辛,别时冰雪到时春
2015-09-20 17:18
tximist
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2010-9-17
收藏
得分:0 
回复 2楼 林月儿
可否详解?不是很明白,switch表达式?case表达式?还是啥地方?
2015-09-20 17:22
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
以下是引用tximist在2015-9-20 17:22:23的发言:

可否详解?不是很明白,switch表达式?case表达式?还是啥地方?


switch判断对象应该是连续型数值
判断分支则是点值

剑栈风樯各苦辛,别时冰雪到时春
2015-09-20 17:26
小王KING
Rank: 2
等 级:论坛游民
威 望:1
帖 子:29
专家分:69
注 册:2015-9-9
收藏
得分:0 
9:00,输入
  switch (user_time - min)   //这里的结果是8*60+17,你说他跟下面个那个值匹配?

 min = (abs(user_time - DEPART_TIME_1) < abs(user_time - DEPART_TIME_2)) ? abs(user_time - DEPART_TIME_1) : abs(user_time - DEPART_TIME_2);

    min = (min < abs(user_time - DEPART_TIME_3)) ? min : abs(user_time - DEPART_TIME_3);

    min = (min < abs(user_time - DEPART_TIME_4)) ? min : abs(user_time - DEPART_TIME_4);

    min = (min < abs(user_time - DEPART_TIME_5)) ? min : abs(user_time - DEPART_TIME_5);

    min = (min < abs(user_time - DEPART_TIME_6)) ? min : abs(user_time - DEPART_TIME_6);

    min = (min < abs(user_time - DEPART_TIME_7)) ? min : abs(user_time - DEPART_TIME_7);

    min = (min < abs(user_time - DEPART_TIME_8)) ? min : abs(user_time - DEPART_TIME_8);
//上面7条语句,在比较的时候,要用绝对值比较,才能获得最接近的时间,返回时间差的时候,要获得真实值。例如min=(abs(x)<abs(y))?x,y;
//修改如下
 min = (abs(user_time - DEPART_TIME_1) < abs(user_time - DEPART_TIME_2)) ? (user_time - DEPART_TIME_1) :(user_time - DEPART_TIME_2);

    min = (abs(min) < abs(user_time - DEPART_TIME_3)) ? min : (user_time - DEPART_TIME_3);

    min = (abs(min) < abs(user_time - DEPART_TIME_4)) ? min : (user_time - DEPART_TIME_4);

    min = (abs(min) < abs(user_time - DEPART_TIME_5)) ? min : (user_time - DEPART_TIME_5);

    min = (abs(min) < abs(user_time - DEPART_TIME_6)) ? min : (user_time - DEPART_TIME_6);

    min = (abs(min) < abs(user_time - DEPART_TIME_7)) ? min : (user_time - DEPART_TIME_7);

    min = (abs(min) < abs(user_time - DEPART_TIME_8)) ? min : (user_time - DEPART_TIME_8);
2015-09-20 17:32
tximist
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2010-9-17
收藏
得分:0 
以下是引用林月儿在2015-9-20 17:26:58的发言:

 
 
switch判断对象应该是连续型数值
判断分支则是点值

有无具体例子,或如何修改程序?
2015-09-20 17:35
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
程序代码:
#include <stdio.h>
#include <stdlib.h>
#define DEPART_TIME_1 (8 * 60)

#define DEPART_TIME_2 (9 * 60 + 43)

#define DEPART_TIME_3 (11 * 60 + 19)

#define DEPART_TIME_4 (12 * 60 + 47)

#define DEPART_TIME_5 (14 * 60)

#define DEPART_TIME_6 (15 * 60 + 45)

#define DEPART_TIME_7 (19 * 60)

#define DEPART_TIME_8 (21 * 60 + 45)
int main(void)
{
    
    int a[8]={8 * 60,9 * 60 + 43,11 * 60 + 19,12 * 60 + 47,14 * 60,15 * 60 + 45,19 * 60,21 * 60 + 45} ;
    int h, m, user_time,i;
    
    for(printf("Enter a 24-hour time(h:m): ");
           scanf("%d:%d", &h, &m)!=2 ;
           scanf("%*[^\n]"), printf("%s","Exception: data format , try again: "));

    user_time = h * 60 + m;
    for(i=0;i<7;i++)
        if(a[i]<user_time&&a[i+1]>user_time)break;
    switch (a[i])
    {
        case DEPART_TIME_1:
            printf("Closest departure time is 8:00 a.m., arriving at 10:16 a.m.");
            break;
        case DEPART_TIME_2:
            printf("Closest departure time is 9:43 a.m., arriving at 11:52 a.m.");
            break;
        case DEPART_TIME_3:
            printf("Closest departure time is 11:19 a.m., arriving at 1:31 p.m.");
            break;
        case DEPART_TIME_4:
            printf("Closest departure time is 12:47 p.m., arriving at 3:00 p.m.");
            break;
        case DEPART_TIME_5:
            printf("Closest departure time is 2:00 p.m., arriving at 4:08 p.m.");
            break;
        case DEPART_TIME_6:
            printf("Closest departure time is 3:45 p.m., arriving at 5:55 p.m.");
            break;
        case DEPART_TIME_7:
            printf("Closest departure time is 7:00 p.m., arriving at 9:20 p.m.");
            break;
        case DEPART_TIME_8:
            printf("Closest departure time is 9:45 p.m., arriving at 11:58 p.m.");
            break;
    }

    return 0;
}

剑栈风樯各苦辛,别时冰雪到时春
2015-09-20 17:59
tximist
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2010-9-17
收藏
得分:0 
以下是引用林月儿在2015-9-20 17:59:30的发言:

#include  
#include  
#define DEPART_TIME_1 (8 * 60)
 
#define DEPART_TIME_2 (9 * 60 + 43)
 
#define DEPART_TIME_3 (11 * 60 + 19)
 
#define DEPART_TIME_4 (12 * 60 + 47)
 
#define DEPART_TIME_5 (14 * 60)
 
#define DEPART_TIME_6 (15 * 60 + 45)
 
#define DEPART_TIME_7 (19 * 60)
 
#define DEPART_TIME_8 (21 * 60 + 45)
int main(void)
{
     
    int a[8]={8 * 60,9 * 60 + 43,11 * 60 + 19,12 * 60 + 47,14 * 60,15 * 60 + 45,19 * 60,21 * 60 + 45} ;
    int h, m, user_time,i;
     
    for(printf("Enter a 24-hour time(h:m): ");
           scanf("%d:%d", &h, &m)!=2 ;
           scanf("%*[^\n]"), printf("%s","Exception: data format , try again: "));
 
    user_time = h * 60 + m;
    for(i=0;i<7;i++)
        if(auser_time)break;
    switch (a)
    {
        case DEPART_TIME_1:
            printf("Closest departure time is 8:00 a.m., arriving at 10:16 a.m.");
            break;
        case DEPART_TIME_2:
            printf("Closest departure time is 9:43 a.m., arriving at 11:52 a.m.");
            break;
        case DEPART_TIME_3:
            printf("Closest departure time is 11:19 a.m., arriving at 1:31 p.m.");
            break;
        case DEPART_TIME_4:
            printf("Closest departure time is 12:47 p.m., arriving at 3:00 p.m.");
            break;
        case DEPART_TIME_5:
            printf("Closest departure time is 2:00 p.m., arriving at 4:08 p.m.");
            break;
        case DEPART_TIME_6:
            printf("Closest departure time is 3:45 p.m., arriving at 5:55 p.m.");
            break;
        case DEPART_TIME_7:
            printf("Closest departure time is 7:00 p.m., arriving at 9:20 p.m.");
            break;
        case DEPART_TIME_8:
            printf("Closest departure time is 9:45 p.m., arriving at 11:58 p.m.");
            break;
    }
 
    return 0;
}
谢谢,研究研究,如果规定只用if if/else switch这些选择语句,如何写出来呢?
2015-09-20 18:33
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
以下是引用tximist在2015-9-20 18:33:23的发言:

谢谢,研究研究,如果规定只用if if/else switch这些选择语句,如何写出来呢?


if比较灵活,适用于区间范围值比较
switch形式简洁,应用范围有一定限制,只能是离散值
了解他们的特点加以利用进而优化代码即可

剑栈风樯各苦辛,别时冰雪到时春
2015-09-20 18:37
tximist
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2010-9-17
收藏
得分:0 
以下是引用小王KING在2015-9-20 17:32:11的发言:

9:00,输入
  switch (user_time - min)   //这里的结果是8*60+17,你说他跟下面个那个值匹配?

 min = (abs(user_time - DEPART_TIME_1) < abs(user_time - DEPART_TIME_2)) ? abs(user_time - DEPART_TIME_1) : abs(user_time - DEPART_TIME_2);
 
    min = (min < abs(user_time - DEPART_TIME_3)) ? min : abs(user_time - DEPART_TIME_3);
 
    min = (min < abs(user_time - DEPART_TIME_4)) ? min : abs(user_time - DEPART_TIME_4);
 
    min = (min < abs(user_time - DEPART_TIME_5)) ? min : abs(user_time - DEPART_TIME_5);
 
    min = (min < abs(user_time - DEPART_TIME_6)) ? min : abs(user_time - DEPART_TIME_6);
 
    min = (min < abs(user_time - DEPART_TIME_7)) ? min : abs(user_time - DEPART_TIME_7);
 
    min = (min < abs(user_time - DEPART_TIME_8)) ? min : abs(user_time - DEPART_TIME_8);
//上面7条语句,在比较的时候,要用绝对值比较,才能获得最接近的时间,返回时间差的时候,要获得真实值。例如min=(abs(x)

刚开始,我是想user_time - min这个跟宏定义值DEPART_TIME_2 (9 * 60 + 43)匹配,因为user_time - min == user_time - user_time - DEPART_TIME_2 == DEPART_TIME_2
2015-09-20 18:49
快速回复:新人请教程序错在哪,用户输入数据后,就没有输出了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.091112 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved