我不明白 [英] I'm not understanding something

查看:78
本文介绍了我不明白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我必须解决的问题:


编写程序将温度输入转换为摄氏或华氏

到另一个等级。提供适当的提示,并接受

温度输入作为double,然后是表示

比例(C或F)的char。执行适当的转换并显示

原始输入和结果。编写函数ftoc和ctof来执行

转换并返回要在main中显示的结果。

转换公式如下,其中C是以度为单位的温度

摄氏度,F等于华氏温度:


C =(5.0 / 9.0)*(F - 32)

F =((9.0 / 5.0)* C)+ 32


摄氏度,反之亦然。

用户输入应该是比规模加倍的形式。对于

示例

32 F代表32华氏度。这是我到目前为止的代码。

编译但不会执行

#include< stdio.h>

#include< stdlib .h>


双倍farTemp(双倍);

双分钱时间(双倍);


int main( )

{

double userTemp;

char scale;

char F,f,C,c;

printf(输入温度和标度(F或C));

scanf("%lf%lf",& userTemp,& scale) );


if(scale == f || scale == F)

centTemp(userTemp);


printf("%f",userTemp);


system(" PAUSE");

返回0;

}


双CentTemp(双倍)


{


返回(5.0 / 9.0)* a -32;

}

This is the problem that I have to solve :

Write a program to convert a temperature input in Celsius or Fahrenheit
to the other scale. Provide an appropriate prompt, and accept the
temperature input as a double, followed by a char representing the
scale (C or F). Perform the appropriate conversion and display the
original input and result. Write functions ftoc and ctof to do the
conversion and return the result to be displayed in main. The
conversion formulas are as follows, where C is a temperature in degrees
Celsius, and F the equivalent in Fahrenheit:

C = (5.0 / 9.0) * (F - 32)
F = ( (9.0 / 5.0) * C ) + 32

So basically if you enter a temp in Fahrenheit I need to convet it to
Celsius and vice versa.
The user input should be in the form of a double then the scale. For
example
32 F which stands for 32 degrees Fahrenheit. Here is my code so far.
Which compiles but will not execute
#include <stdio.h>
#include <stdlib.h>

double farTemp(double);
double centTemp(double);

int main()
{
double userTemp;
char scale;
char F,f,C,c;
printf("Enter Temp & Scale (F Or C ) ");
scanf("%lf%lf", &userTemp,&scale);

if(scale==f || scale== F)
centTemp(userTemp);

printf("%f", userTemp);

system("PAUSE");
return 0;
}

double CentTemp(double a)

{

return (5.0/9.0)*a -32 ;
}

推荐答案

代码中有几处错误。阅读下面的评论。


RadiationX写道:
Several errors in your code. Read comments below.

RadiationX wrote:
这是我必须解决的问题:

编写程序进行转换以摄氏或华氏温度输入其他比例的温度。提供适当的提示,并接受
温度输入为double,然后是表示
比例(C或F)的char。执行适当的转换并显示原始输入和结果。编写函数ftoc和ctof进行
转换并返回要在main中显示的结果。
转换公式如下,其中C是以摄氏度为单位的温度,F是华氏温度的等效值:

C =(5.0 / 9.0)*(F - 32)
F =((9.0 / 5.0)* C)+ 32

所以基本上如果你输入华氏温度,我需要把它转移到摄氏温度,反之亦然。
用户输入应该是双倍的比例。对于
示例
32 F代表32华氏度。这是我到目前为止的代码。
哪些编译但不会执行

#include< stdio.h>
#include< stdlib.h>
double farTemp(double);
double centTemp(double);


您的规范明确指出应该调用这些函数

ftoc和ctof。顺便提一下,你的规范提供的名称比你选择的更明确。如果您没有注意到,ftoc代表

fahrenheit to celsius和ctof为摄氏温度到华氏温度。 farTemp

给出了它返回华氏值的想法,但它并没有给出这个参数意味着什么的线索。学习以明确的方式命名您的函数

和变量。选择模糊标识符

保存几次击键可以使你的代码稍后调试两次,因为很难。

int main()
{
double userTemp;
char scale;
char F,f,C,c;


在这里你宣布了一个双倍的用户将保持温度

,另一个将保持由
指定的刻度
用户...和四个没有明确porpouse的变量。当声明函数和变量解释

预期用于变量或逻辑/计算/算法时,添加注释是个好主意

由函数实现。


在旁注中,你没有声明一个变量用于持有你的
计算。你可以没有一个,但我会添加以下

行:


double computedTemp;

printf(" Enter温度和比例(F或C));


请注意,如果您没有向输出发送''\ n'',则无法保证

发送后会出现在屏幕上。因此,当程序执行时,可能会发生

,当程序正在等待用户输入数据时,会显示一个空白屏幕

。修改上面的

行如下:


printf("输入温度和标度(F或C)\ n");


scanf("%lf%lf",& userTemp,& scale);


scanf是危险的,应该不惜一切代价避免,但由于这个

显然是一个玩具程序,我们会坚持下去。但是请注意,

fgets / sscanf组合更加安全。


以上行调用未定义的行为。你已经在

格式字符串中指定了你要读取的2个双打,但是你提供了一个指向浮点数和指向char的指针的
指针。你的规范说明你应该读取一个字符,所以明显的解决方法是更改​​

格式字符串,以实际告诉scanf你想要读取一个字符:


scanf("%lf%c",& userTemp,& scale);


最后,你应该知道scanf可能无法解析其参数。

想象一下,如果用户输入的内容会发生什么:我不能

现在想到一个温度!

scanf返回成功解析的参数数量和分配的
。检查它是2.在像这样的玩具程序中,你最好的

选项将退出并显示错误消息。在真实的软件中,你将需要消耗stdin中的行(scanf会在其中留下内容)和

再次提示用户输入正确的数据。


此外,您应该确保用户提供的数据

是有意义的。如果用户提示''R'为温度等级,你的程序如何表现?

if(scale == f || scale == F)


这里你要比较变量标度的值和变量f的值

,以及变量F的值。注意

f和f都没有初始化为已知值。访问

unitiliazed变量会调用未定义的行为。


我怀疑你真正想做的检查是:( scale ==''f''| |

scale ==''F'')

centTemp(userTemp);


这条线计算摄氏温度,然后把它扔掉。你

真的想把它存放在某个地方。


我看不到所选尺度摄氏度的检查,也没有打电话给

ctof函数。

printf("%f",userTemp);


这将打印用户输入的值。你不要那样。

在输出中指定温度刻度可能是个好主意



系统(" PAUSE");


具体实施,如果你使用你的程序

来自CLI,大多数情况下不需要。

返回0;
}双层CentTemp(双倍)

返回(5.0 / 9.0)* a -32;


这里的一些间距会使这个表达式更具可读性。 }
This is the problem that I have to solve :

Write a program to convert a temperature input in Celsius or Fahrenheit
to the other scale. Provide an appropriate prompt, and accept the
temperature input as a double, followed by a char representing the
scale (C or F). Perform the appropriate conversion and display the
original input and result. Write functions ftoc and ctof to do the
conversion and return the result to be displayed in main. The
conversion formulas are as follows, where C is a temperature in degrees
Celsius, and F the equivalent in Fahrenheit:

C = (5.0 / 9.0) * (F - 32)
F = ( (9.0 / 5.0) * C ) + 32

So basically if you enter a temp in Fahrenheit I need to convet it to
Celsius and vice versa.
The user input should be in the form of a double then the scale. For
example
32 F which stands for 32 degrees Fahrenheit. Here is my code so far.
Which compiles but will not execute
#include <stdio.h>
#include <stdlib.h> double farTemp(double);
double centTemp(double);
Your specification clearly states that those functions should be called
ftoc and ctof. BTW the names your specification provides are much more
explicit than your choice. In case you didn''t notice, ftoc stands for
"fahrenheit to celsius" and ctof for "celsius to fahrenheit". farTemp
gives the idea that it returns a fahrenheit value, but it doesn''t give
a clue about what the argument may mean. Learn to name your functions
and variables in a explicit way. Choosing obfuscated identifiers to
save a few keystrokes can make debugging your code later twice as
difficult.
int main()
{
double userTemp;
char scale;
char F,f,C,c;
Here you have declared a double that will hold the temperature provided
by the user, another one that will hold the scale specified by the
user... and four variables with no clear porpouse. It''s a good idea to
add comments when declaring functions and variables explaining the
intended use for the varible or the logic/computation/algorithm
implemented by the function.

On a side note, you have not declared a variable for holding your
computation. You could make without one, but I would add the following
line:

double computedTemp;
printf("Enter Temp & Scale (F Or C ) ");
Notice that if you don''t send a ''\n'' to the output there''s no guarantee
that the text you sent will appear on the screen. So it may happen that
when the program executes the user is presented with a blank screen
while the program is awaiting for the user to input data. Modify the
above line as follows:

printf("Enter Temp & Scale (F Or C )\n");

scanf("%lf%lf", &userTemp,&scale);
scanf is dangerous and should be avoided at all costs, but since this
is clearly a toy program we''ll stick with it. Be warned however that
the fgets/sscanf combo is much safer.

The above line invokes undefined behaviour. You have specified in the
format string that you want to read 2 doubles, but you provide a
pointer to a float and a pointer to char. Your specification states
that you should read a char, so the obvious fix to this is changing the
format string to actually tell scanf that you want to read a char:

scanf("%lf %c", &userTemp, &scale);

Finally, you should be aware that scanf may fail parsing its arguments.
Imagine what will happen if the user inputs something like: "I can''t
think of one temperature right now!"
scanf returns the number of arguments that where succesfully parsed and
assigned. Check that it''s 2. In a toy program like this one, your best
option would be to exit with an error message. In real software you''ll
have to consume the line from stdin (scanf will leave things in it) and
prompt the user again for correct data.

Furthermore, you should make sure that the data provided by the user
makes sense. How is your program to behave if the user prompts ''R'' as
the temperature scale?
if(scale==f || scale== F)
Here you are comparing the value of the variable scale with the value
of the variable f, and with the value of the variable F. Note that
neither f nor F have been initialized to a known value. Accessing an
unitiliazed variable invokes undefined behaviour.

I suspect that the check you really wanted to make is: (scale == ''f'' ||
scale == ''F'')
centTemp(userTemp);
This line computes the celsius temperature and then thows it away. You
really want to store it somewhere.

I see no check for the selected scale beeing celsius, and no call to
the ctof function.
printf("%f", userTemp);
This will print the value entered by the user. You don''t want that.
Specifying the temperature scale in the output might be a good idea
too.

system("PAUSE");
Implementation specific, and mostly unneeded if you use your program
from a CLI.
return 0;
}

double CentTemp(double a)

{

return (5.0/9.0)*a -32 ;
Some spacing here would make this expression more readable. }




你应该写一个函数来执行逆计算。


HTH



You should write a function to perform the inverse computation.

HTH


WoW,你真的向我指出了很多错误。谢谢。我要用

来纠正它们,然后我会发布我的更新代码。

WoW, you really pointed out a lot of errors to me. Thank you. I''m going
to work on correcting them and then I''ll post my updated code.


RadiationX写道:
RadiationX wrote:
这是我必须解决的问题:


[snip:从华氏温度转换为摄氏温度]

这是我的代码到目前为止。
哪些编译但不会执行


它没有为我编译(*)。

我'' ma新手C程序员,但(我希望)我的

(非常简短)评论没有问题。


#include< stdio.h>
#include< stdlib.h>

双farTemp(双);
双分温计(双);


后来,在main()中你调用了centTemp - 但你从来没有定义它;你

定义另一个名为`CentTemp''的函数。


[从代码中删除一些空行]

int main( )
{
double userTemp;
char scale;
char F,f,C,c;

printf(" Enter Temp& Scale) (F或C));


printf()应该有一个终止''\ n''

scanf("%lf%lf",& userTemp,&规模);


将两个双打读入一个双精灵和一个char?

我的编译器在此行发出警告。


另外scanf()是获取用户输入的不错选择。

if(scale == f || scale == F)


比较尺度两个未初始化的变量?

....我认为你的意思是别的。

centTemp(userTemp);


调用(不存在的)centTemp函数并丢弃结果。

printf("%f",userTemp);
系统(暂停);
返回0;
}
双CentTemp(双a)
返回(5.0 / 9.0) * a -32;


此计算与您在本文前言中指定的计算

计算的内容不同。

}
This is the problem that I have to solve :
[snip: conversion from Fahrenheit to Celsius]
Here is my code so far.
Which compiles but will not execute
It didn''t compile for me (*).
I''m a newbie C programmer, but (I hope) nothing is wrong with my
(very short) comments.

#include <stdio.h>
#include <stdlib.h>

double farTemp(double);
double centTemp(double);
Later, in main() you call centTemp -- but you never define it; you
define another function called `CentTemp''.

[some blank lines removed from the code]
int main()
{
double userTemp;
char scale;
char F,f,C,c;

printf("Enter Temp & Scale (F Or C ) ");
printf() should have a terminating ''\n''
scanf("%lf%lf", &userTemp,&scale);
Read two doubles into a double and a char?
My compiler issued a warning at this line.

Also scanf() is a bad choice to get user input.
if(scale==f || scale== F)
Compare scale to two uninitialized variables?
.... I think you mean something else.
centTemp(userTemp);
Call the (inexistent) centTemp function and discard the results.
printf("%f", userTemp);

system("PAUSE");
return 0;
}

double CentTemp(double a)
{
return (5.0/9.0)*a -32 ;
This calculation does not calculate the same thing as the calculation
specified in your intro to this article.
}




(*)它没有为我编译 - 这是一个谎言。

实际上它已编译但没有链接。


-

如果你' '通过谷歌发布阅读< http://cfaj.freeshell.org/google>



(*) "It didn''t compile for me" -- that was a lie.
Actually it compiled but didn''t link.

--
If you''re posting through Google read <http://cfaj.freeshell.org/google>


这篇关于我不明白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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