请评论 [英] please comment

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

问题描述

因为我不在学校进行c编程你能否请

告诉我这个程序是否有什么不好


/ * EX4-9 .C函数调用其他函数* /


#include< stdio.h>


float div(float a,float b);

浮动多(浮动a,浮动b);

无效调用(无效);


浮动a,b,c ;

char z;


int main(无效)

{


printf("输入m to multiply或d to divid:");

scanf("%c"& z);

printf("输入两个数字:");

scanf("%f%f"& a,& b);


call() ;


返回0;

}

浮动div(浮动a,浮动b)

{

浮动c;


if(b == 0)

printf("除数不应为零) \ n");

其他

c = a / b;

返回c;

/ *或返回(a / b); * /

}


浮动多(浮动a,浮动b)

{

int c;

返回(c = a * b);

/ *或返回(a * b); * /

}


无效通话(无效)

{


如果(z ==''m'')

c = multi(a,b);

else

c = div(a,b) ;


if(z!=''m''&& z!=''d''){

c = 0;

printf("不正确的operator \ n");

}

printf(" Output%f\ n",c);


}

解决方案

amanayin写道:

因为我不在c编程学校可以请你告诉我这个程序有什么不好吗

/ * EX4-9.C功能调用其他功能* /
#include< stdio.h>

浮点div(浮点数a,浮点数b);


除非你有充分的理由,例如使用庞大的数据集,

你应该使用double而不是float。使用花车保存的几个字节

不值得麻烦,在很多情况下

无论如何都要将花车转换为双打。

float multi(float a,float b);
void call(void);

float a,b,c;
char z;


除非有非常好的理由使用它们,否则应避免使用全局变量。在这个程序中,使用全局变量并不是理所当然的。

int main(void)
{/>
printf("输入m乘以或d为divid:");
scanf("%c"& z);


使用fgets和sscanf(或atof)

组合进行交互式输入更安全。例如,假设用户输入mm而不是m来输入
。你还应该检查一下scanf的

返回值。

printf("输入两个数字:");
scanf("%f%f" ;,& a,& b);

调用();

返回0;
}
浮点div(浮点数a,浮点数b)
{
浮动c;

if(b == 0)


一般情况下,不应测试浮点数平等的数字。

printf(除数不应该是零\ n);
其他
c = a / b;
返回c ;


如果b == 0,此时c包含垃圾。

/ *或返回(a / b); * /


如果b == 0则不是。

}

浮动多(浮动a,浮动b) {
int c;


c应为浮动或双倍。

返回(c = a * b);
/ *或返回(a * b); * /


替代方案更可取。

}

无效呼叫(无效)
{

if(z ==''m'')
c = multi(a,b);

c = div(a,b);


如果z不是m,你可以调用div。

if(z!=''m''&& z!=''d''){
c = 0;
printf("运算符不正确);
}


此检查应提前,或者作为上述if / else的一部分。

printf(" Output%f\ n,c);

}






amanayin写道:

因为我不在学校进行c编程,请你告诉我这个程序有什么不好吗



< snip>


除了您收到的评论外:


制作call()的正文一个清晰的开关(你的错误,你打电话给

" div()"即使c不是''d''如果你有一个
切换我打赌)。


从call()返回成功/失败指示tomain()因此它可以同时返回成功/失败的操作系统。


为变量使用更有意义的名称(例如分子和

" denominator" indiv()而不是a和b,结果而不是c

无处不在,操作数 ;而不是Z到处都是。)


Delcare变量每行一个为clairty(对比你可能

think" char * a, b;表示它实际意味着什么 - " char * a; char b;"


没什么大不了的,但是从main()你可以返回EXIT_SUCCESS或

EXIT_FAILURE而不是0或1 - 如果你想使用它们,请包括stdlib.h。


需要考虑的事项 - 你将如何检测它以及你会怎样如果您在multi()中计算的结果,请执行

溢出(即

大于可以持有的最大值)类型float?

现在你只需提前充电并返回一个

的值,该值可能小于原始数字。


问候,


Ed。



On Sun,2003年11月9日12:19:37 -0500,TM Sommers写道:

amanayin写道:


浮动div(浮动a,浮动b)
{/>浮动c ;


如果你这个函数将返回更可预测的结果

#include< math.h>然后初始化c:


float c = INFINITY;





float c = HUGE_VALF;

if(b == 0)



一般来说,不应该测试浮点数是否相等。




我认为这是一个糟糕的评论。这是一般的。但是在这个

代码中直接比较零是正确的事情,那么为什么

会混淆这个问题呢?在我看来,与明确的浮点价值比较

会更好:


if(b == 0.0)
< blockquote class =post_quotes>

printf("除数不应为零\ n);

c = a / b;
return c;




As i am not at school for c programming can you please
tell me if there is anything bad about this program

/* EX4-9.C FUNCTION TO CALL OTHER FUNCTIONS */

#include<stdio.h>

float div(float a, float b);
float multi(float a, float b);
void call(void);

float a,b,c;
char z;

int main(void)
{

printf("Enter m to multiply or d to divid:");
scanf("%c",&z);
printf("Enter two numbers: ");
scanf("%f %f", &a, &b);

call();

return 0;
}
float div(float a, float b)
{
float c;

if(b == 0)
printf("The divisor should not be a zero\n");
else
c = a / b;
return c;
/* OR return(a / b); */
}

float multi(float a, float b)
{
int c;
return (c = a * b);
/* OR return(a * b); */
}

void call(void)
{

if(z == ''m'')
c = multi(a,b);
else
c = div(a,b);

if(z != ''m'' && z != ''d''){
c = 0;
printf("Incorrect operator\n");
}
printf("Output %f\n",c);

}

解决方案

amanayin wrote:

As i am not at school for c programming can you please
tell me if there is anything bad about this program

/* EX4-9.C FUNCTION TO CALL OTHER FUNCTIONS */

#include<stdio.h>

float div(float a, float b);
Unless you have a very good reason, such as using huge data sets,
you should use double instead of float. The few bytes you save
by using floats are not worth the trouble, and in many situations
the floats are converted to doubles anyway.
float multi(float a, float b);
void call(void);

float a,b,c;
char z;
Global variables should be avoided unless there is a very good
reason to use them. In this program, the use of globals is not
justified.
int main(void)
{

printf("Enter m to multiply or d to divid:");
scanf("%c",&z);
It is safer to use a combination of fgets and sscanf (or atof)
for interactive input. Suppose, for instance, that the user
typed in ''mm'' instead of just ''m''. You should also check the
return value of scanf.
printf("Enter two numbers: ");
scanf("%f %f", &a, &b);

call();

return 0;
}
float div(float a, float b)
{
float c;

if(b == 0)
In general, one should not test floating point numbers for equality.
printf("The divisor should not be a zero\n");
else
c = a / b;
return c;
If b == 0, at this point c contains garbage.
/* OR return(a / b); */
Not if b == 0.
}

float multi(float a, float b)
{
int c;
c should be float or double.
return (c = a * b);
/* OR return(a * b); */
The alternative is preferable.
}

void call(void)
{

if(z == ''m'')
c = multi(a,b);
else
c = div(a,b);
Here you call div if z is anything other than ''m''.
if(z != ''m'' && z != ''d''){
c = 0;
printf("Incorrect operator\n");
}
This check should come earlier, or be made part of the if/else above.
printf("Output %f\n",c);

}






amanayin wrote:

As i am not at school for c programming can you please
tell me if there is anything bad about this program


<snip>

In addition to comments you already received:

Make the body of "call()" a switch for clarity (your bug where you call
"div()" even if "c" is not ''d'' wouldn''t have happened if you had a
switch I bet).

Return a success/fail indication from "call()" to "main()" so it can
likewise return success/fail to the OS.

Use more meaningful names for variables (e.g. "numerator" and
"denominator" in "div()" instead of "a" and "b", "result" instead of "c"
everywhere, "operand" instead of "z" everywhere).

Delcare variables one per-line for clairty (contrast what you might
think "char *a,b;" means with what it actually means - "char *a; char b;")

No big deal, but from main() you could return EXIT_SUCCESS or
EXIT_FAILURE instead of 0 or 1 - include stdlib.h if you want to use them.

Something to consider - how will you detect it and what will you do
about it if the result of your calculation in "multi()" overflows (i.e.
is larger than the maximum value that can be held by) the type "float"?
Right now you''ll just charge ahead with the calulation and return a
value that may be less than either original number.

Regards,

Ed.



On Sun, 09 Nov 2003 12:19:37 -0500, T.M. Sommers wrote:

amanayin wrote:


float div(float a, float b)
{
float c;
The function will return more predictable results if you
#include <math.h> and then initialize c:

float c = INFINITY;

or

float c = HUGE_VALF;

if(b == 0)



In general, one should not test floating point numbers for equality.



I think this is a poor comment. It is true "in general". But in this
code a direct comparison to zero is the correct thing to do, so why
confuse the issue? It would be better, in my opinion, to compare against
an explicitly floating point value:

if (b == 0.0)

printf("The divisor should not be a zero\n");
else
c = a / b;
return c;




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

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