C ++ Primer ex 7.3 [英] C++ Primer ex 7.3

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

问题描述

我想知道这是不是一个很好的设计风格(据我所知,直到chpater 7获得了
):


/ * C ++入门 - 4 / e

*

*练习7.4

*声明

*写一个程序拿两个int paramaters并生成

将第一个提升到第二个幂的结果。写一个

程序来调用你的函数传递两个整数。验证

结果。

*

* /


#include< iostream>


int raise_power(int x,int y)

{

int mult = 1;


while(y)

{

mult = mult * x;

--y;

}


返回mult;

}


int main()

{

int i = 2;

int j = 3;

int mult_result = 8;


std :: cout<< static_cast< bool>(mult_result == raise_power(i,j))

<< std :: endl;

/ *好,从未转换为真,它仍然是1 :( * /

返回0;

}


-
http://arnuld.blogspot.com

i wanted to know if that''s a good design style (upto the knowledge i have
gained till chpater 7):

/* C++ Primer - 4/e
*
* exercise 7.4
* STATEMENT
* write a programme to take two int paramaters and generate the
result of raising the first to the power of the second. write a
programme to call your function passing it two ints. verify the
result.
*
*/

#include <iostream>

int raise_power(int x, int y)
{
int mult = 1;

while( y )
{
mult = mult * x;
--y;
}

return mult;
}

int main()
{
int i = 2;
int j = 3;
int mult_result = 8;

std::cout << static_cast<bool>(mult_result == raise_power(i, j))
<< std::endl;
/* well, never got converted to "true", it alwasy remains 1 :( */
return 0;
}


--
http://arnuld.blogspot.com

推荐答案

8月8日晚上9:59,arnuld < geek.arn ... @ gmail.comwrote:
On Aug 8, 9:59 pm, arnuld <geek.arn...@gmail.comwrote:

i想知道这是不是一个好的设计风格(取决于我的知识

获得直到chpater 7):


/ * C ++ Primer - 4 / e

*

*练习7.4

*语句

*编写一个程序来获取两个int参数并生成

结果,将第一个提升到第二个幂。写一个

程序来调用你的函数传递两个整数。验证

结果。

*

* /


#include< iostream>


int raise_power(int x,int y)
i wanted to know if that''s a good design style (upto the knowledge i have
gained till chpater 7):

/* C++ Primer - 4/e
*
* exercise 7.4
* STATEMENT
* write a programme to take two int paramaters and generate the
result of raising the first to the power of the second. write a
programme to call your function passing it two ints. verify the
result.
*
*/

#include <iostream>

int raise_power(int x, int y)



我建议使用更有意义的变量名而不是''x''

和'' Y ''。想到''base''和''exponent''。

I would suggest using more meaningful variable names other than ''x''
and ''y''. ''base'' and ''exponent'' come to mind.


{

int mult = 1;


而(y)
{
int mult = 1;

while( y )



如果''y'是负数(由于它被定义为

int),这个条件会导致你的程序出现重大错误。

If ''y'' is a negative number (which is allowed since it''s defined as an
int), this while condition will cause a major bug n your program.


{

mult = mult * x;

--y;
{
mult = mult * x;
--y;



你的状况是错误的。但如果它是正确的,你可以简单地写

而(y--)没有

单机减量就能达到相同的效果。

Your while condition is wrong. But if it was correct, you could have
simply written "while( y-- )" to achieve the same result without the
stand-alone decrement.


}


返回很多;


}


int main()

{

int i = 2;

int j = 3;

int mult_result = 8;


std :: cout<< static_cast< bool>(mult_result == raise_power(i,j))

<< std :: endl;

/ *好,从未转换为true,它alwasy仍为1 :( * /

返回0;
< br $>
}


--http://arnuld.blogspot.com
}

return mult;

}

int main()
{
int i = 2;
int j = 3;
int mult_result = 8;

std::cout << static_cast<bool>(mult_result == raise_power(i, j))
<< std::endl;
/* well, never got converted to "true", it alwasy remains 1 :( */
return 0;

}

--http://arnuld.blogspot.com



干杯,

Andre


Cheers,
Andre


arnuld写道:
arnuld wrote:

i想知道是否这是一个很好的设计风格(根据我的知识直到chpater 7获得
):


/ * C ++ Primer - 4 / e
*

*练习7.4

*语句

*编写一个程序来获取两个int参数并生成

将第一个提升到第二个的力量。写一个

程序来调用你的函数传递两个整数。验证

结果。< br $>
*

* /


#include< iostream>


int raise_power (int x,int y)

{

int mult = 1;


wh ile(y)

{

mult = mult * x;

--y;

}


返回mult;

}
i wanted to know if that''s a good design style (upto the knowledge i have
gained till chpater 7):

/* C++ Primer - 4/e
*
* exercise 7.4
* STATEMENT
* write a programme to take two int paramaters and generate the
result of raising the first to the power of the second. write a
programme to call your function passing it two ints. verify the
result.
*
*/

#include <iostream>

int raise_power(int x, int y)
{
int mult = 1;

while( y )
{
mult = mult * x;
--y;
}

return mult;
}



这对于正数是好的,但是怎么样?否定?

That''s OK for positive numbers, but what about negative?


int main()

{

int i = 2;

int j = 3;

int mult_result = 8;


std :: cout<< static_cast< bool>(mult_result == raise_power(i,j))

<<的std :: ENDL;
int main()
{
int i = 2;
int j = 3;
int mult_result = 8;

std::cout << static_cast<bool>(mult_result == raise_power(i, j))
<< std::endl;



野蛮,


断言(raise_power(i,j)== mult_result);


别忘了包括< cassert>


-

Ian Collins。

Be brutal,

assert( raise_power(i, j) == mult_result );

Don''t forget to include <cassert>

--
Ian Collins.


8月9日上午10点14分,Ian Collins< ian-n ... @ hotmail.comwrote:
On Aug 9, 10:14 am, Ian Collins <ian-n...@hotmail.comwrote:

arnuld写道:
arnuld wrote:


... [SNIP] ...
...[SNIP]...


std :: cout<< static_cast< bool>(mult_result == raise_power(i,j))

<<的std :: ENDL;
std::cout << static_cast<bool>(mult_result == raise_power(i, j))
<< std::endl;


野蛮,


断言(raise_power(i,j)== mult_result );


别忘了包含< cassert>
Be brutal,

assert( raise_power(i, j) == mult_result );

Don''t forget to include <cassert>



引发错误:


/ home / arnuld / programming / cpp

that raises an error:

/home/arnuld/programming/cpp


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

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