加速C ++:练习3-2 [英] Accelerated C++: Exercise 3-2

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

问题描述




这是我在这里发表的第一篇文章,所以请保持温和。我一直在学习c +

+主要使用Andrew Koenig所着的Accelerated C ++和

Barbara E. Moo。


到目前为止,我一直在努力做好准备,而且我已经参加了3-2的练习,需要以下内容:

3 -2。编写一个程序来计算和打印四分位数(即,具有最大值的数字的

四分之一,下一个最高的

四分之一,等等)整数集。


在研究了四分位数后,使用定义的方法

这里: http://www.statcan.ca/english/edu/power/ch12/range.htm (从

我读过的内容,有不同的方法用于计算较低的

和上四分位数)我已经能够编写一个控制台程序了

似乎正确计算。但是,我希望从一个编程的角度来看看我可以改进的建议。


对于那些没有读过上述书的人,它有一种独特的方式来引入C ++,因为第一章直接使用了

字符串,因此有很多基础知识,比如使用函数等等,尚未支付



我的代码如下:


#include<算法>

#include< iostream>

#include< vector>


int main()

{

std :: cout<< 输入一个整数范围,然后输入''end'':" ;;


std :: vector< doubleintegerSet;


int x;

while(std :: cin> x)

integerSet.push_back(x);


sort(integerSet .begin(),integerSet.end());


typedef std :: vector< double> :: size_type vector_Size;


vector_Size size = integerSet.size();

vector_Size mid = size / 2;

double median;

double lowerQuartile;

double upperQuartile;


median = size%2 == 0? (integerSet [mid] + integerSet [mid-1])/ 2

:integerSet [mid];


vector_Size midlq = mid / 2;

lowerQuartile = mid%2 == 0? (integerSet [midlq] +

integerSet [midlq - 1])/ 2

:integerSet [midlq];


vector_Size miduq = size - midlq;

upperQuartile = mid%2 == 0? (integerSet [miduq] +

integerSet [miduq - 1])/ 2

:integerSet [miduq - 1];


int unsigned i = 0;

std :: cout<< 订购数据:;

而(i!=尺寸)

{

std :: cout<< integerSet [i]<< " " ;;

i ++;

}

std :: cout<< std :: endl;

std :: cout<< 中位数: <<中值<< std :: endl;

std :: cout<< 下四分位数: << lowerQuartile<< std :: endl;

std :: cout<< 上四分位数: << upperQuartile<< std :: endl;


返回0;

}


谢谢

Hi,

This is my first post here, so please be gentle. I''ve been studying c+
+ by mostly using the book Accelerated C++ by Andrew Koenig and
Barbara E. Moo.

So far, I''ve been picking things up all right, and I''ve come to an
exercise 3-2 that requires the following:
3-2. Write a program to compute and print the quartiles (that is, the
quarter of the numbers with the largest values, the next highest
quarter, and so on) of a set of integers.

After researching what quartiles are, and using the method defined
here: http://www.statcan.ca/english/edu/power/ch12/range.htm (as from
what I''ve read, there are different methods used for computing lower
and upper quartiles) I''ve been able to write a console program that
seems to calculate correctly. However, I would like advice, from a
programming perspective, on what I could improve.

For those who haven''t read the above book, it has a unique way of
introducing C++, as the first chapter jumps straight into the use of
strings, so a lot of basics, like use of functions and so on, have not
been covered as yet.

My code is as below:

#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
std::cout << "Enter a range of integers followed by ''end'': ";

std::vector<doubleintegerSet;

int x;
while (std::cin >x)
integerSet.push_back(x);

sort(integerSet.begin(), integerSet.end());

typedef std::vector<double>::size_type vector_Size;

vector_Size size = integerSet.size();
vector_Size mid = size / 2;
double median;
double lowerQuartile;
double upperQuartile;

median = size % 2 == 0 ? (integerSet[mid] + integerSet[mid-1]) / 2
: integerSet[mid];

vector_Size midlq = mid / 2;
lowerQuartile = mid % 2 == 0 ? (integerSet[midlq] +
integerSet[midlq - 1]) / 2
: integerSet[midlq];

vector_Size miduq = size - midlq;
upperQuartile = mid % 2 == 0 ? (integerSet[miduq] +
integerSet[miduq - 1]) / 2
: integerSet[miduq - 1];

int unsigned i = 0;
std::cout << "Ordered Data: ";
while (i != size)
{
std::cout << integerSet[i] << " ";
i++;
}
std::cout << std::endl;
std::cout << "Median: " << median << std::endl;
std::cout << "Lower Quartile: " << lowerQuartile << std::endl;
std::cout << "Upper Quartile: " << upperQuartile << std::endl;

return 0;
}

Thanks

推荐答案

Xernoth写道:
Xernoth wrote:

对于那些没有读过上述书的人,它有一种独特的方式来引入C ++,因为第一章直接使用了

字符串,所以很多基础知识,如函数的使用等,都有尚未支付

For those who haven''t read the above book, it has a unique way of
introducing C++, as the first chapter jumps straight into the use of
strings, so a lot of basics, like use of functions and so on, have not
been covered as yet.



该程序看起来写得很好!只是注意:键入取消

std :: vector< double> :: size_type是没用的:直接使用std :: size_t。


而且,当然你会注意到,你的程序执行三次同样的动作,因为四分位数可以被视为

上下的中位数分配的一半。一旦你开始学习

函数,你就可以尝试实现一个

函数,完成这项工作并调用它三次。


问候,


Zeppe

The program seems well written! Just a note: it''s useless to typedef the
std::vector<double>::size_type: use directly std::size_t.

And, of course you''ll have noticed, your program performs three times
the same action, as the quartiles can be viewed as the medians for the
upper and lower half of the distribution. Once you''ll start studying the
function, it would be interesting for you to try implementing a
function that do this job and calling it three times.

Regards,

Zeppe




" Xernoth" ; < Jo ******** @ googlemail.comwrote in message

news:11 ********************** @ d57g2000hsg.googlegr oups.com ...

"Xernoth" <Jo********@googlemail.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...




这是我的第一篇帖子,所以请成为温和。我一直在学习c +

+主要使用Andrew Koenig所着的Accelerated C ++和

Barbara E. Moo。


到目前为止,我一直在努力做好准备,而且我已经参加了3-2的练习,需要以下内容:

3 -2。编写一个程序来计算和打印四分位数(即,具有最大值的数字的

四分之一,下一个最高的

四分之一,等等)整数集。


在研究了四分位数后,使用定义的方法

这里: http://www.statcan.ca/english/edu/power/ch12/range.htm (从

我读过的内容,有不同的方法用于计算较低的

和上四分位数)我已经能够编写一个控制台程序了

似乎正确计算。但是,我希望从一个编程的角度来看看我可以改进的建议。


对于那些没有读过上述书的人,它有一种独特的方式来引入C ++,因为第一章直接使用了

字符串,因此有很多基础知识,比如使用函数等等,尚未支付



我的代码如下:


#include<算法>

#include< iostream>

#include< vector>


int main()

{

std :: cout<< 输入一个整数范围,然后输入''end'':" ;;


std :: vector< doubleintegerSet;
Hi,

This is my first post here, so please be gentle. I''ve been studying c+
+ by mostly using the book Accelerated C++ by Andrew Koenig and
Barbara E. Moo.

So far, I''ve been picking things up all right, and I''ve come to an
exercise 3-2 that requires the following:
3-2. Write a program to compute and print the quartiles (that is, the
quarter of the numbers with the largest values, the next highest
quarter, and so on) of a set of integers.

After researching what quartiles are, and using the method defined
here: http://www.statcan.ca/english/edu/power/ch12/range.htm (as from
what I''ve read, there are different methods used for computing lower
and upper quartiles) I''ve been able to write a console program that
seems to calculate correctly. However, I would like advice, from a
programming perspective, on what I could improve.

For those who haven''t read the above book, it has a unique way of
introducing C++, as the first chapter jumps straight into the use of
strings, so a lot of basics, like use of functions and so on, have not
been covered as yet.

My code is as below:

#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
std::cout << "Enter a range of integers followed by ''end'': ";

std::vector<doubleintegerSet;



一个名为integerSet的double向量...?更好


std :: vector< intintegerSet;

A vector of double called integerSet...? Better

std::vector<intintegerSet;


>

int x;

while(std :: cin> x)

integerSet.push_back(x);


sort(integerSet.begin( ),integerSet.end());


typedef std :: vector< double> :: size_type vector_Size;
>
int x;
while (std::cin >x)
integerSet.push_back(x);

sort(integerSet.begin(), integerSet.end());

typedef std::vector<double>::size_type vector_Size;



请改用std :: size_t。

Use std::size_t instead.


>

vector_Size size = integerSet.size();

vector_Size mid = size / 2;

double median;

double lowerQuartile;

double upperQuartile;


median = size%2 == 0? (integerSet [mid] + integerSet [mid-1])/ 2

:integerSet [mid];
>
vector_Size size = integerSet.size();
vector_Size mid = size / 2;
double median;
double lowerQuartile;
double upperQuartile;

median = size % 2 == 0 ? (integerSet[mid] + integerSet[mid-1]) / 2
: integerSet[mid];



如果你将integerSet的声明更改为vector< intabove,你需要

除以2.0而不是2得到浮点除法:


中位数=大小%2 == 0? (integerSet [mid] + integerSet [mid-1])/ 2.0:

integerSet [mid];


以下同样适用。

If you changed the declaration of integerSet to vector<intabove, you need
to divide by 2.0 instead of 2 to get floating point division:

median = size % 2 == 0 ? (integerSet[mid] + integerSet[mid-1]) / 2.0 :
integerSet[mid];

The same applies below.


>

vector_Size midlq = mid / 2;

lowerQuartile = mid%2 == 0? (integerSet [midlq] +

integerSet [midlq - 1])/ 2

:integerSet [midlq];


vector_Size miduq = size - midlq;

upperQuartile = mid%2 == 0? (integerSet [miduq] +

integerSet [miduq - 1])/ 2

:integerSet [miduq - 1];


int unsigned i = 0;

std :: cout<< 订购数据:;

而(i!=尺寸)

{

std :: cout<< integerSet [i]<< " " ;;

i ++;

}
>
vector_Size midlq = mid / 2;
lowerQuartile = mid % 2 == 0 ? (integerSet[midlq] +
integerSet[midlq - 1]) / 2
: integerSet[midlq];

vector_Size miduq = size - midlq;
upperQuartile = mid % 2 == 0 ? (integerSet[miduq] +
integerSet[miduq - 1]) / 2
: integerSet[miduq - 1];

int unsigned i = 0;
std::cout << "Ordered Data: ";
while (i != size)
{
std::cout << integerSet[i] << " ";
i++;
}



虽然上述循环有效,但更惯用的方法是


for(std :: size_t i = 0; i< size; i ++){

std :: cout<< integerSet [i]<< " " ;;

}


或者,使用迭代器:


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

integerSet.end(); iter ++){

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

}

While the above loop works, a more idiomatic way would be

for (std::size_t i = 0; i < size; i++) {
std::cout << integerSet[i] << " ";
}

Or, using an iterator:

for (std::vector<int>::iterator iter = integerSet.begin(); iter !=
integerSet.end(); iter++) {
std::cout << *iter << " ";
}


std :: cout<< std :: endl;

std :: cout<< 中位数: <<中值<< std :: endl;

std :: cout<< 下四分位数: << lowerQuartile<< std :: endl;

std :: cout<< 上四分位数: << upperQuartile<< std :: endl;


返回0;

}
std::cout << std::endl;
std::cout << "Median: " << median << std::endl;
std::cout << "Lower Quartile: " << lowerQuartile << std::endl;
std::cout << "Upper Quartile: " << upperQuartile << std::endl;

return 0;
}



除此之外,寻找好:)


-

Jonas


Apart from that, looking good :)

--
Jonas


4月12日晚上11点44分, zeppe< z ... @nospam.remove.all.this.email.it>

写道:
On Apr 12, 11:44 pm, zeppe <z...@nospam.remove.all.this.email.it>
wrote:

该程序似乎很好书面!只是注意:键入定义

std :: vector< double> :: size_type是没用的:直接使用std :: size_t。
The program seems well written! Just a note: it''s useless to typedef the
std::vector<double>::size_type: use directly std::size_t.



谢谢你,我对std :: size_t,

的使用不是很熟悉,但我会回顾一下并了解如何在当前的

代码中实现它。

Thanks for that, I''m not very familiar with the uses of std::size_t,
but I''ll review and see how it can be implemented in the current
code.


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

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