C ++ Primer练习3.13 [英] C++ Primer exercise 3.13

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

问题描述

我已经解决了这个问题,但它很长。像往常一样,我想要一些

编码标准或好的设计意见:

/ * C ++ Primer 4 / e

*第3章 - 图书馆类型


*练习3.13

* STAMEMENT

*在向量中读取一组整数。计算并打印向量的每对*相邻元素的

总和。如果有一个奇怪的

数字,那么告诉*用户关于它并打印最后一个

元素的值而不加总。 *

* /

#include< iostream>

#include< vector>


int main()

{

std :: vector< intivec; / *空向量* /


int ivec_limit = 11;

for(int i = 5; i!= ivec_limit; ++ i)/ * insert迭代* /

ivec.push_back(i);


/ *现在我们将打印vector * / std :: cout<<的元素这些是

元素的向量:\t" ;; for(std :: vector< int> :: const_iterator

iter = ivec.begin(); iter!= ivec.end(); ++ iter)

{

std :: cout<< * iter<< " " ;;

}


std :: cout<< std :: endl;

/ *实际程序* /

for(std :: vector< int> :: iterator iter = ivec.begin(); iter!= ivec.end();

++ iter)

{

if((* iter%2)!= 0)

{

std :: cout<< " \ n"

<< * iter

<< "是一个奇数

<< "不要将其添加到总数中。

<< std :: endl;

if(iter == ivec.begin())

{

std :: cout<< * iter

<< "是向量的第一个元素

<< std :: endl;

}

else

{

std :: cout<< - 最后一个元素是:"

<< *(iter - 1)

<< std :: endl;

}

}

else

{

if ((iter + 1)== ivec.end())

{

std :: cout<< \ n最后一个向量元素是:"

<< * iter

<< std :: endl;

}

else

{

std :: cout<< \ nAdjacent元素是:"

<< * iter

<< " &安培;

<< *(iter + 1)

<< \ n - 和他们的总和是:"

<< * iter + *(iter + 1)

<< std :: endl;

}

}

}

返回0;

}

=====输出=============

[arnuld @ arch cpp]%g ++ -ansi -pedantic - Wall -Wextra ex_03-13.cpp

[arnuld @ arch cpp]%。/ a.out

这些是向量的元素:5 6 7 8 9 10


5是一个奇数,不加总数。 5是第一个元素

vector


相邻元素是:6& 7

- 他们的总和是:13

7是一个奇数,不加总数。 --last元素是:6


相邻元素是:8& 9

- 他们的总和是:17


9是一个奇数,不加总数。 --last元素是:8


向量的最后一个元素是:10

[arnuld @ arch cpp]%


-

- http://arnuld.blogspot.com

解决方案

2007-07-19 10:35,arnuld写道:


i已经解决了这个问题,但它已经很长了。像往常一样,我想要一些

编码标准或好的设计意见:


/ * C ++ Primer 4 / e

*第3章 - 图书馆类型


*练习3.13

* STAMEMENT

*在向量中读取一组整数。计算并打印向量的每对*相邻元素的

总和。如果有一个奇怪的

数字,那么告诉*用户关于它并打印最后一个

元素的值而不加总。 *

* /



抱歉,您还没有解决作者的意图。首先

你应该让用户输入数字。然后你应该打印每对数字的总和

,但是如果用户输入了奇数的

数字,你就不能将最后一个元素与另一个数字相加所以你应该

只需打印它:


所以一个示例运行可能看起来像这样:

-

请输入一些数字:

1

2

3

4

5


您输入了奇数个数字。


对的总和是:

3

7

5

-

或者像这样:

-

请输入一些数字:

1

2

3

4

5

6


对的总和是:

3

7

11

-

Erik Wikstr ?? m


2007年7月19日星期四09:23:21 +0000,Erik Wikstr ?? m写道:


对不起,但你还没有解决作者的意图。首先

你应该让用户输入数字。然后你应该打印每对数字的总和

,但是如果用户输入了奇数的

数字,你就不能将最后一个元素与另一个数字相加所以你应该

只需打印它:



ok,顺便说一句,我发现这句话含糊不清。所以我在

上创建了它可能意味着什么的程序。


所以示例运行可能看起来像这样: - 请输入一些

数字:

1

2

3

4

5


您输入了奇数个数字。


对的总和是:

3

7

5



对不起,经过1.5小时的艰苦工作,我的新计划仍然提供

segmentatioon fault找到一个奇怪的数字列表。偶数金额

这个程序有效:


#include< iostream>

#include< vector>


int main()

{

std :: vector< intivec; / *空向量* / int v_num;


std :: cout<< 请输入一些数字: <<的std :: ENDL; while(std :: cin


> v_num)



ivec.push_back(v_num);


if((ivec.size()%2)!= 0)

{

std :: cout<< 哎呀!你输入了奇数个数字 <<

std :: endl;

}


std :: cout<< 对的总和是: <<的std :: ENDL; / *实际

程序* /

for(std :: vector< int> :: const_iterator iter = ivec.begin(); iter!=

ivec.end(); iter + = 2)

{

if((iter + 1)== ivec.end())

{

std :: cout<< * iter<< std :: endl;

}

else

{

std :: cout<< * iter + *(iter + 1)<< std :: endl;

}

}


返回0;

}

-

- http://arnuld.blogspot .com


在2007-07-19 13:41,arnuld写道:


> 2007年7月19日星期四09:23:21 +0000,Erik Wikstr ?? m写道:



>抱歉,您还没有解决作者的意图。首先,您应该让用户输入数字。然后你应该打印每对数字的总和,但是如果用户输入了奇数个数字,你不能将最后一个元素与另一个数字相加,所以你应该只是
打印它:



ok,顺便说一句,我发现这句话含糊不清。所以我在

上创建了它可能意味着什么的程序。


>所以示例运行可能看起来像这样: - 请输入一些
号码:
1
2
3
4

你输了一个奇数数字。

对的总和是:
3
7
5



抱歉,经过1.5小时的艰苦工作后,我的新计划仍然给出了b = b的段落故障。找到一个奇怪的数字列表。偶数金额

这个程序有效:


#include< iostream>

#include< vector>


int main()

{

std :: vector< intivec; / *空向量* / int v_num;


std :: cout<< 请输入一些数字: <<的std :: ENDL; while(std :: cin


> v_num)



ivec.push_back(v_num);


if((ivec.size()%2)!= 0)

{

std :: cout<< 哎呀!你输入了奇数个数字 <<

std :: endl;

}


std :: cout<< 对的总和是: <<的std :: ENDL; / *实际

程序* /

for(std :: vector< int> :: const_iterator iter = ivec.begin(); iter!=

ivec.end(); iter + = 2)



将支票替换为小于(iter< ivec.end()),因为你是
增加迭代器,每步2步,你将跳过ivec.end()

如果元素数是奇数..


{

if((iter + 1)== ivec.end())

{

std :: cout<< * iter<<的std :: ENDL;



或者添加休息;这里。


}

else

{

std :: cout< ;< * iter + *(iter + 1)<< std :: endl;

}

}


返回0;

}



-

Erik Wikstr ?? m


i have solved the problem but it is quite length. as usual, i wanted some
coding-standards or good-design opinions:
/* C++ Primer 4/e
* chapter 3 - Library Types

* exercise 3.13
* STAMEMENT
* read a set of integers into the vector. calculate and print the
sum of each * pair of adjacent elements of the vector. If there is an odd
number then tell the * user about it and print the value of the last
element without summing it. *
*/
#include <iostream>
#include <vector>

int main()
{
std::vector<intivec; /* empty vector */

int ivec_limit = 11;
for(int i = 5; i != ivec_limit; ++i) /* inserts itegers */
ivec.push_back(i);

/* now we will print the elements of vector */ std::cout << "These are
the elements of vector:\t"; for(std::vector<int>::const_iterator
iter=ivec.begin(); iter != ivec.end(); ++iter)
{
std::cout << *iter << " ";
}

std::cout << std::endl;
/* actual programme */
for(std::vector<int>::iterator iter = ivec.begin(); iter != ivec.end();
++iter)
{
if((*iter % 2) != 0)
{
std::cout << "\n"
<< *iter
<< " is an odd number"
<< " not adding it to the total."
<< std::endl;
if(iter == ivec.begin())
{
std::cout << *iter
<< " is the 1st element of vector"
<< std::endl;
}
else
{
std::cout << "--last element was: "
<< *(iter - 1)
<< std::endl;
}
}
else
{
if((iter + 1) == ivec.end())
{
std::cout << "\nLast element of vector is: "
<< *iter
<< std::endl;
}
else
{
std::cout << "\nAdjacent elements are: "
<< *iter
<< " & "
<< *(iter + 1)
<< "\n--and their sum is: "
<< *iter + *(iter + 1)
<< std::endl;
}
}
}
return 0;
}
===== OUTPUT =============
[arnuld@arch cpp ]% g++ -ansi -pedantic -Wall -Wextra ex_03-13.cpp
[arnuld@arch cpp ]% ./a.out
These are the elements of vector: 5 6 7 8 9 10

5 is an odd number not adding it to the total. 5 is the 1st element of
vector

Adjacent elements are: 6 & 7
--and their sum is: 13

7 is an odd number not adding it to the total. --last element was: 6

Adjacent elements are: 8 & 9
--and their sum is: 17

9 is an odd number not adding it to the total. --last element was: 8

Last element of vector is: 10
[arnuld@arch cpp ]%

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

解决方案

On 2007-07-19 10:35, arnuld wrote:

i have solved the problem but it is quite length. as usual, i wanted some
coding-standards or good-design opinions:
/* C++ Primer 4/e
* chapter 3 - Library Types

* exercise 3.13
* STAMEMENT
* read a set of integers into the vector. calculate and print the
sum of each * pair of adjacent elements of the vector. If there is an odd
number then tell the * user about it and print the value of the last
element without summing it. *
*/

Sorry, but you have not solved the problem the author intended. First of
you should let the user enter the numbers. Then you should print the sum
of each pair of numbers, but if the user entered an odd number of
numbers you can''t sum the last element with another so you you should
just print it:

So an example run could look something like this:
--
Please enter some numbers:
1
2
3
4
5

You entered an odd number of numbers.

The sums of the pairs are:
3
7
5
--
Or like this:
--
Please enter some numbers:
1
2
3
4
5
6

The sums of the pairs are:
3
7
11

--
Erik Wikstr??m


On Thu, 19 Jul 2007 09:23:21 +0000, Erik Wikstr??m wrote:

Sorry, but you have not solved the problem the author intended. First of
you should let the user enter the numbers. Then you should print the sum
of each pair of numbers, but if the user entered an odd number of
numbers you can''t sum the last element with another so you you should
just print it:

ok, BTW, i found the statement ambiguous. so i created the programem on
that basis of what it could mean.

So an example run could look something like this: -- Please enter some
numbers:
1
2
3
4
5

You entered an odd number of numbers.

The sums of the pairs are:
3
7
5

sorry, after 1.5 hours of grueling work, my new programme still gives
"segmentatioon fault" on finding an odd list of numbers. for even amount
of numbers this programme works:

#include <iostream>
#include <vector>

int main()
{
std::vector<intivec; /* empty vector */ int v_num;

std::cout << "Please enter some numbers: " << std::endl; while(std::cin

>v_num)

ivec.push_back(v_num);

if((ivec.size() % 2) != 0)
{
std::cout << "oops!, you enetered an odd number of numbers" <<
std::endl;
}

std::cout << "The sum of the pairs are: " << std::endl; /* actual
programme */
for(std::vector<int>::const_iterator iter = ivec.begin(); iter !=
ivec.end(); iter += 2)
{
if((iter + 1) == ivec.end())
{
std::cout << *iter << std::endl;
}
else
{
std::cout << *iter + *(iter + 1) << std::endl;
}
}

return 0;
}
--
-- http://arnuld.blogspot.com


On 2007-07-19 13:41, arnuld wrote:

>On Thu, 19 Jul 2007 09:23:21 +0000, Erik Wikstr??m wrote:


>Sorry, but you have not solved the problem the author intended. First of
you should let the user enter the numbers. Then you should print the sum
of each pair of numbers, but if the user entered an odd number of
numbers you can''t sum the last element with another so you you should
just print it:


ok, BTW, i found the statement ambiguous. so i created the programem on
that basis of what it could mean.

>So an example run could look something like this: -- Please enter some
numbers:
1
2
3
4
5

You entered an odd number of numbers.

The sums of the pairs are:
3
7
5


sorry, after 1.5 hours of grueling work, my new programme still gives
"segmentatioon fault" on finding an odd list of numbers. for even amount
of numbers this programme works:

#include <iostream>
#include <vector>

int main()
{
std::vector<intivec; /* empty vector */ int v_num;

std::cout << "Please enter some numbers: " << std::endl; while(std::cin

>v_num)

ivec.push_back(v_num);

if((ivec.size() % 2) != 0)
{
std::cout << "oops!, you enetered an odd number of numbers" <<
std::endl;
}

std::cout << "The sum of the pairs are: " << std::endl; /* actual
programme */
for(std::vector<int>::const_iterator iter = ivec.begin(); iter !=
ivec.end(); iter += 2)

Replace the check to a less than (iter < ivec.end()), since you are
increasing the iterator with 2 each step you will step over ivec.end()
if the number of elements is odd..

{
if((iter + 1) == ivec.end())
{
std::cout << *iter << std::endl;

Or add break; here.

}
else
{
std::cout << *iter + *(iter + 1) << std::endl;
}
}

return 0;
}

--
Erik Wikstr??m


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

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