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

问题描述

它工作正常。我发布它是为了了解你的观点(请记住,我在第6章是
,所以我没有遇到类似功能的stuf):


/ * C ++入门 - 4 / e

*

*练习6.20

*声明

*写一个程序到rad从标准

输入的一系列字符串,直到相同的单词连续出现两次或所有

单词都被读取。使用while循环一次读取一个单词。使用

break语句终止循环如果awords中出现两次

继承&打印那个单词,否则打印出没有单词的消息

重复。

*

* /

#包括< iostream>

#include< string>


int main()

{

std :: string cstr,pstr;

bool same_str = false;


while(std :: cin> cstr)

{

if(cstr == pstr)

{

same_str = true;

break;

}


pstr = cstr;

}

if(same_str)

{

std :: cout<< " \ n''"

<< cstr

<< "''重复了\ nn ;;

}

其他

{

std :: cout << \ nno word was wasant\\\
;

}


返回0;

}


-


解决方案



那么,重点是什么?




就个人而言,当其他方法可以轻易使用时,我不赞成使用break语句。我宁愿做(未经测试的代码)



while(!same_str&& std :: cin> cstr)





所以,重点是什么?



点什么?


-

Erik Wikstr?m

it works fine. i am posting it to know your views (please remember, i am
at chapter 6, so i have not encountered stuf like Functions):

/* C++ Primer - 4/e
*
* exercise 6.20
* STATEMENT
* write a programme to rad a sequence of strings from standard
input until either the same word occurs twice in succession or all the
words have been read. use the while loop to read a word at a time. use
the break statement to terminate the loop if awords occurs twice in
succession & print that word or else print the message that no word was
repeated.
*
*/
#include <iostream>
#include <string>

int main()
{
std::string cstr, pstr;
bool same_str = false;

while(std::cin >cstr)
{
if(cstr == pstr)
{
same_str = true;
break;
}

pstr = cstr;
}
if(same_str)
{
std::cout << "\n''"
<< cstr
<< "'' was repeated\n";
}
else
{
std::cout << "\nno word was repeated\n";
}

return 0;
}

--
http://arnuld.blogspot.com

解决方案

So, what''s the point?


Personally, I''m not in favor of the break statement when other methods can
be easily used. I would rather do (untested code)

while(! same_str && std::cin >cstr)




So, what''s the point?

Point of what?

--
Erik Wikstr?m


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

10-11 19:54