首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求解上面的程序错哪了——新手

2012-10-14 
求解下面的程序哪里错了——新手#includestdio.h/*当fahr0.20...200时,打印华氏与摄氏温度的对照表*/main

求解下面的程序哪里错了——新手
#include<stdio.h>
/*当fahr=0.20...200时,打印华氏与摄氏温度的对照表*/
main()
{
int fahr, celsius;
int lower,upper,step;

lower=0;
upper=200;
step=20;

fahr=lower;
while(fahr<=upper) {
celsius=5*(fahr-32)/9;
printf("%d\t%d\n",fahr,celsius);
fahr=fahr+step;
}
}
   


[解决办法]
celsius=5*(fahr-32)/9;
不要用int型数据,应该用浮点型数据

[解决办法]
怎么没有返回值呢?
另外楼主编码习惯好像不是很好~
发代码的时候用记得用插入源代码模式,就是编辑工具上面带#号的
[解决办法]

C/C++ code
0       -1720      -640      460      1580      26100     37120     48140     60160     71180     82200     93Press any key to continue . . .
[解决办法]
整形得到的结果有时候让你比较无语。。。
[解决办法]

之前最好使用double、float计算,最后要取整的话再转int都行
[解决办法]
常量也有类型!
C++ Integer Constants
Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types.

Syntax

integer-constant :

decimal-constant integer-suffixopt
octal-constant integer-suffixopt
hexadecimal-constant integer-suffixopt
'c-char-sequence'

decimal-constant :

nonzero-digit
decimal-constant digit

octal-constant :

0
octal-constant octal-digit

hexadecimal-constant :

0x hexadecimal-digit
0X hexadecimal-digit
hexadecimal-constant hexadecimal-digit

nonzero-digit : one of

1 2 3 4 5 6 7 8 9

octal-digit : one of

0 1 2 3 4 5 6 7

hexadecimal-digit : one of

0 1 2 3 4 5 6 7 8 9
a b c d e f
A B C D E F

integer-suffix :

unsigned-suffix long-suffixopt
long-suffix unsigned-suffixopt

unsigned-suffix : one of

u U

long-suffix : one of

l L

64-bit integer-suffix :

i64

To specify integer constants using octal or hexadecimal notation, use a prefix that denotes the base. To specify an integer constant of a given integral type, use a suffix that denotes the type.

To specify a decimal constant, begin the specification with a nonzero digit. For example:

int i = 157; // Decimal constant
int j = 0198; // Not a decimal number; erroneous octal constant
int k = 0365; // Leading zero specifies octal constant, not decimal

To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. For example:

int i = 0377; // Octal constant
int j = 0397; // Error: 9 is not an octal digit

To specify a hexadecimal constant, begin the specification with 0x or 0X (the case of the “x” does not matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. For example:

int i = 0x3fff; // Hexadecimal constant
int j = 0X3FFF; // Equal to i

To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. For example:

unsigned uVal = 328u; // Unsigned value


long lVal = 0x7FFFFFL; // Long value specified 
// as hex constant
unsigned long ulVal = 0776745ul; // Unsigned long value

-------------------------------
C++ Floating-Point Constants
Floating-point constants specify values that must have a fractional part. These values contain decimal points (.) and can contain exponents.

Syntax

floating-constant :

fractional-constant exponent-partopt floating-suffixopt
digit-sequence exponent-part floating-suffixopt

fractional-constant :

digit-sequenceopt . digit-sequence
digit-sequence .

exponent-part :

e signopt digit-sequence
E signopt digit-sequence

sign : one of

+ –

digit-sequence :

digit
digit-sequence digit

floating-suffix :one of

f l F L

Floating-point constants have a “mantissa,” which specifies the value of the number, an “exponent,” which specifies the magnitude of the number, and an optional suffix that specifies the constant’s type. The mantissa is specified as a sequence of digits followed by a period, followed by an optional sequence of digits representing the fractional part of the number. For example:

18.46
38.

The exponent, if present, specifies the magnitude of the number as a power of 10, as shown in the following example:

18.46e0 // 18.46
18.46e1 // 184.6

If an exponent is present, the trailing decimal point is unnecessary in whole numbers such as 18E0.

Floating-point constants default to type double. By using the suffixes f or l (or F or L — the suffix is not case sensitive), the constant can be specified as float or long double, respectively.

Although long double and double have the same representation, they are not the same type. For example, you can have overloaded functions like

void func( double );

and 

void func( long double );


[解决办法]
怎么个抽法?
没有所谓的一会儿可以,一会儿不可以,注意细节;所谓细节也许只是一个.。
另外这里列的只是第一个版本,应该书上还有讲第二个第三个版本。

[解决办法]

探讨
之前最好使用double、float计算,最后要取整的话再转int都行

[解决办法]
探讨
对不起,我的软件抽了,一会可以,一会不可以,郁闷

[解决办法]
哈~!
[解决办法]
支持下 多看看书
[解决办法]
我的c++6.0可以啊。
[解决办法]
C/C++ code
#include<stdio.h>#include <stdlib.h>/*当fahr=0.20...200时,打印华氏与摄氏温度的对照表*/main(){int fahr, celsius;int lower,upper,step;lower=0;upper=200;step=20;fahr=lower;while(fahr<=upper) {celsius=5*(fahr-32)/9;printf("%d\t%d\n",fahr,celsius);fahr=fahr+step;}system("pause");}
[解决办法]
不知道楼主是什么编译器,我的VC6编译只出了一个‘main’should return value的警告提示,改后没有输出是因为没有添加暂停代码,像15楼那样添加个暂停代码就可以显示了,double int可以强转,应该只会存在精度问题
至于其它的就不太清楚了。
也不知道楼主说的具体问题是什么。。。
[解决办法]
int lower,upper,step;
数据类型
[解决办法]
楼主最好注意隐式类型转换,如果你想要得到正确的结果,就定义为float,这样就有有小数位了。如果长的话,就定义double。
------解决方案--------------------


一楼正解,除法运算用int类型最后取整数位,如果不是结果恰为整数,得到的结果不会准确。另外主函数为啥没返回值类型?

热点排行