2

我想知道如何提示我的程序的最终用户输入他们想要在 C 中从华氏温度转换为摄氏温度的值。

基本上,因为我是一个完全的 n00b 并且我正在编写令人惊叹的“程序”,例如这个:

    //Simple program to convert Fahrenheit to Celsius

    int main (int argc, char *argv[])
    {
     double celsius, fahrenheit, result;

     celsius = result;
     fahrenheit = 27;
     result = (fahrenheit - 32) / 1.8;

     printf("27 degress Fahrenheit is %g degrees Celsius!", result);

     return 0;
    }

如果您知道我的意思,我想为它添加一些实际的“功能”。我不想让它成为一个测试程序,实际上它只是展示一些简单的算术表达式评估,我想让它实际上有点用处。

无论如何,我想知道是否可以使用scanf(3) 手册页中列出的函数来帮助我识别用户输入的数据,然后以某种方式将其存储到 Fahrenheit 变量中。

现在,如果程序在运行时可以提示最终用户一个问题,询问他或她是想从摄氏温度转换为华氏温度还是从华氏温度转换为摄氏温度,那将是非常酷的,但让我们迈出一步有时间,我会等到我读到我书中关于“做出决定”的章节!:)

更新:

删除 kiamlaluno 指出的无用变量结果:

    //Simple program to convert Fahrenheit to Celsius

    int main (int argc, char *argv[])
    {
 double fahrenheit, celsius;

 fahrenheit = 27;
 celsius = (fahrenheit - 32) / 1.8;

 printf("27 degress Fahrenheit is %g degrees Celsius!", celsius);

 return 0;
    }

更新更新:

我一直在尝试合并每个人在此处发布的有用建议,但我的代码遇到了更多问题:

//Simple program to convert Fahrenheit to Celsius and Celsius to Fahrenheit

int main (int argc, char *argv[])
{
int celsius, fahrenheit, celsiusResult, fahrenheitResult;
celsiusResult = (fahrenheit - 32)*(5/9);
fahrenheitResult = (celsius*(9/5)) + 32;
int prompt;

printf("Please press 1 to convert Fahrenheit to Celsius, or 0 to convert Celsius to Fahrenheit please:\n ");
scanf("%i", &prompt);
if(prompt == 1) {
    printf("Please enter a temperature in Fahrenheit to be converted into Celsius!:\n");
    scanf("%i", &fahrenheit);
    printf("%i degress Fahrenheit is %i degrees Celsius!", fahrenheit, celsiusResult);

}
else {
    printf("Please enter a temperature in Celsius to be converted into Fahrenheit:\n");
    scanf("%i", &celsius);
    printf("%i degreses Celsius is %i degrees Fahrenheit", celsius, fahrenheitResult);
}

return 0;
}

一切都很好,除了计算本身,结果完全错误。有一秒钟我认为这可能是因为我将数字本身更改为整数类型,但我再次将它们加倍,它仍然有点棘手。

有什么想法吗?

4

4 回答 4

2

要使用scanf()读取双精度,您需要使用正确的格式字符串。它将%lf用于“长浮点数”(%f单独读入浮点数)。然后,您可以直接读取双精度。打印出来也是一样。

int main (int argc, char *argv[])
{
    double fahrenheit, celsius;

    printf("farenheit? ");     /* write a prompt */
    scanf("%lf", &fahrenheit); /* read a double into the fahrenheit variable */

    celsius = (fahrenheit - 32) / 1.8;

    printf("%lf degress Fahrenheit is %lf degrees Celsius!\n", fahrenheit, celsius);

    return 0;
}

请注意,这根本不处理非数字输入。您需要使用其他输入技术。

[编辑]

你肯定会在你的代码中跳跃,看起来你正在取得很大的进步。:)

为了解决您最新的更新,有几个问题。您的代码实际上并没有计算任何东西,您太早应用了公式。您应该等到fahrenheitcelsius具有有意义的值(即,在用户输入要转换的值之后)。将这些公式移动到函数中以执行转换是一个好主意。您还应该坚持使用双精度而不是整数。使用整数将无法获得所需的精度。

double convert_fahrenheit_to_celsius(double fahrenheit)
{
    return (fahrenheit - 32)*(5/9);
}

double convert_celsius_to_fahrenheit(double celsius)
{
    return (celsius*(9/5)) + 32;
}

int main (int argc, char *argv[])
{
    double celsius, fahrenheit, celsiusResult, fahrenheitResult;
    int prompt;

    printf("Please press 1 to convert Fahrenheit to Celsius, or 0 to convert Celsius to Fahrenheit please:\n ");
    scanf("%i", &prompt);
    if(prompt == 1) {
        printf("Please enter a temperature in Fahrenheit to be converted into Celsius!:\n");
        scanf("%lf", &fahrenheit);

        /* now convert user-input fahrenheit to celsius */
        celsiusResult = convert_fahrenheit_to_celsius(fahrenheit);

        printf("%lf degress Fahrenheit is %lf degrees Celsius!", fahrenheit, celsiusResult);
    }
    else {
        printf("Please enter a temperature in Celsius to be converted into Fahrenheit:\n");
        scanf("%lf", &celsius);

        /* now convert user-input celsius to fahrenheit */
        fahrenheitResult = convert_celsius_to_fahrenheit(celsius);

        printf("%lf degreses Celsius is %lf degrees Fahrenheit", celsius, fahrenheitResult);
    }

    return 0;
}
于 2010-08-07T05:36:51.113 回答
1

好的,如果您想提示用户输入,只需使用带有标签 %lf 的 scanf:

//Simple program to convert Fahrenheit to Celsius

int main (int argc, char *argv[])
{
    double fahrenheit, result;

    printf("Please enter a number for farenheit: ");
    scanf("%lf", &fahrenheit);
    result = (fahrenheit - 32) / 1.8;

    printf("27 degress Fahrenheit is %g degrees Celsius!", result);

    return 0;
}

至于提示用户他/她想要摄氏度还是华氏度,你需要一些叫做 if 语句和 else 语句的东西。

int main (int argc, char *argv[])
{
    double celsius, fahrenheit, result;
    int ForC;

    printf("1 for Celsius or 0 for Fahrenheit plz: ");
    scanf("%d", &ForC);
    if(ForC == 1) {
        scanf("%lf", &fahrenheit);
        result = (fahrenheit - 32) / 1.8;
        printf("fahrenheit to celsius:%lf", result);
    }
    else {
        scanf("%lf", &celsius);
        result = (celsius*9.0)/5.0 + 32;
        printf("celsius to farenheit:%lf", result);
    }

    return 0;
}
于 2010-08-07T05:45:31.940 回答
0

可以通过检查 scanf() 的返回值来处理可能错误的用户输入。它必须等于预期值的数量(在这种情况下为 1)。在其他情况下,应重复输入。

于 2010-08-07T10:21:37.550 回答
0

对于您的 UPDATE UPDATE :从用户那里获得值后尝试进行计算。如果不是,则 的结果值celsius, fahrenheit, celsiusResult, fahrenheitResult是未知的。

于 2010-08-07T22:22:31.933 回答