| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3343 人关注过本帖
标题:请教一个代码错误,谢谢了
只看楼主 加入收藏
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:0 
考虑的太多,写的比较复杂

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void Tempratures(const double huashi);

int main(void)
{
    double huashi;
    char a[10];   
    unsigned int i,j,count;
    for (i = 1; ; i++)//for循环开始
    {
        memset(a, 0, sizeof(a));
        printf("请输入一个华氏温度\n");
        scanf("%s", a);
        huashi = atof(a);
        count = 0;

        /* 判断是否为一个正常的数字 */
        for (j = 0; j < strlen(a); j++)
        {
            if (a[j] == '.')
            {
                /* 多个小数点 */
                count++;
            }
            
            /* 数字和字母混合 */
            if (((a[j] < '0') &&(a[j] != '.') || (a[j] > '9')) || (a[0] == '.') || (a[strlen(a)-1] == '.') || (count >1))
            {
                printf("输入非数字,退出\n");
                return 0;
            }
        }
        Tempratures(huashi);
    }
    return 0;
}

void Tempratures(const double huashi)
{
    double sheshi;

    sheshi = huashi*9/5 + 32;
    printf("摄氏温度为%.2lf\n", sheshi);
    printf("绝对温度为%.2lf\n", sheshi+273.16);

}


[此贴子已经被作者于2016-3-18 16:01编辑过]

2016-03-18 15:51
屋顶
Rank: 1
等 级:新手上路
帖 子:174
专家分:7
注 册:2016-2-27
收藏
得分:0 
回复 11楼 grmmylbs
看不懂啊,因为我还没学到for循环语句,只用while语句能编出来吗
2016-03-18 15:54
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:0 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void Tempratures(const double huashi);

int main(void)
{
    double huashi;
    char a[10];   
    unsigned int i,j,count;
    while(1)
    {
        memset(a, 0, sizeof(a));
        printf("请输入一个摄氏温度\n");
        scanf("%s", a);
        huashi = atof(a);
        count = 0;

        /* 判断是否为一个正常的数字 */
        j = 0;
        while (j < strlen(a))
        {
            if (a[j] == '.')
            {
                /* 多个小数点 */
                count++;
            }

            /* 数字和字母混合 */
            if (((a[j] < '0') && (a[j] != '.') || (a[j] > '9')) || (a[0] == '.') || (a[strlen(a) - 1] == '.') || (count >1))
            {
                printf("输入非数字,退出\n");
                return 0;
            }
            j++;
        }
        Tempratures(huashi);
    }
    return 0;
}

void Tempratures(const double huashi)
{
    double sheshi;

    sheshi = huashi*9/5 + 32;
    printf("华氏温度为%.2lf\n", sheshi);
    printf("绝对温度为%.2lf\n", sheshi+273.16);

}


[此贴子已经被作者于2016-3-18 16:00编辑过]

2016-03-18 15:57
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
回复 12楼 屋顶
while 和 for 是一样的
程序代码:
#include <stdio.h>

void Temperatures( const double Fahrenheit )
{
    const double Celsius = 1.8 * Fahrenheit + 32.0;
    const double Kelvin = Celsius + 273.16;

    printf( "Fahrenheit = %.2lf\n", Fahrenheit );
    printf( "Celsius    = %.2lf\n", Celsius );
    printf( "Kelvin     = %.2lf\n", Kelvin );
}

int main( void )
{
    double fahrenheit;
    while( scanf("%lf",&fahrenheit) == 1 )
        Temperatures( fahrenheit );

    return 0;
}

输入输出
0
Fahrenheit = 0.00
Celsius    = 32.00
Kelvin     = 305.16
1
Fahrenheit = 1.00
Celsius    = 33.80
Kelvin     = 306.96
-32
Fahrenheit = -32.00
Celsius    = -25.60
Kelvin     = 247.56
q


[此贴子已经被作者于2016-3-18 16:27编辑过]

2016-03-18 16:01
屋顶
Rank: 1
等 级:新手上路
帖 子:174
专家分:7
注 册:2016-2-27
收藏
得分:0 
回复 13楼 grmmylbs
大神看看我这个代码哪里有问题
#include<stdio.h>
void temperatures(double k);
main()
{
    double fahrenheit;
    scanf("%lf",&fahrenheit);
    while((int)fahrenheit!=113)   
        temperatures(fahrenheit);        
    }
void temperatures(double i)
{
    double celsius,kelvin;
    celsius=1.8*i+32.0;
    kelvin=celsius+273.16;
    printf("%lf%lf%lf",i,celsius,kelvin);
    }
谢谢
2016-03-18 16:01
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:0 
while ((int)fahrenheit != 113)
这一句为了表达什么?
2016-03-18 16:07
屋顶
Rank: 1
等 级:新手上路
帖 子:174
专家分:7
注 册:2016-2-27
收藏
得分:0 
回复 16楼 grmmylbs
q的asc码值是113,我知道错了,但是不知道怎么改
2016-03-18 16:11
屋顶
Rank: 1
等 级:新手上路
帖 子:174
专家分:7
注 册:2016-2-27
收藏
得分:0 
回复 14楼 rjsp
while( scanf("%lf",&fahrenheit)==1; )
这一句为什么输入q或非数字字符会结束呢,不明白呢
2016-03-18 16:14
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:0 
这是百度对scanf返回值的解释:

如果只有a被成功读入,返回值为1

如果a未被成功读入,返回值为0

如果遇到错误或遇到end of file,返回值为EOF。

如果是非数字字符的话,就返回0,while循环就终止了。
2016-03-18 16:44
屋顶
Rank: 1
等 级:新手上路
帖 子:174
专家分:7
注 册:2016-2-27
收藏
得分:0 
回复 19楼 grmmylbs
我想问一下,把非数字字符输入给double数据,不能把字符读取成double数据吗
2016-03-18 16:51
快速回复:请教一个代码错误,谢谢了
数据加载中...
 
   



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

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