这个节目对吗? [英] Is this program right?

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

问题描述

嘿,这个节目对吗?它计算平均值,调和平均值,b / b二次均值,以及两个数字的标准差。你能给我b $ b校对吗?

这是:


#include< iostream>

#include< cstdlib>

#include< cmath>

using namespace std;


template< T级>

级avg

{

公开:

T operator()(const T& num, const T& num2)const

{

T ret =((num + num2)/ 2);

return ret;

}

T operator()(const T& num,const T& num2,int x)const

{

T ret =(sqrt(pow(num,2)+ pow(num,2)));

返回ret;

}

T operator()(const T& num,const T& num2,int x,int y)const

{

T ret =(2 /(1 / num +) 1 / num2));

返回ret;

}

运算符T()const {return static_cast< T>(value);}

私人:

T值;

};


模板< class T> < br $>
class Stat

{

publ ic:

T operator()(T avg,const T& num,const T& num2)const

{

T ret = num-avg;

T ret2 = num2-avg;

T ret3 =(pow(ret,2)+ pow(ret2,2)/ 2);

T ret4 = sqrt(ret3);

返回ret4;

}

运算符T()const {return static_cast< T>(value);}

private:

T价值;

};


int main()

{

for(;;)

{

long double num;

long double num2;

avg< long double>平均值;

统计< long double> StdDev;

cout<< 输入两个数字: << endl;

cin>> num>> num2;

cout<< 这是平均值: <<固定<<平均值(num,num2)<< endl;

cout<< 这里是二次均值: <<固定<<平均值(num,num2,0)<<

endl;

cout<< 这里是调和的意思: <<固定<<平均值(num,num2,0,0)<<

endl;

cout<< 这里是标准偏差: <<已修复<<

StdDev(平均值(num,num2),num,num2)

<<结束;

}

系统(暂停);

返回0;

}


另外,无论如何我可以做到这一点,所以StdDev可以使用Avg的论点

所以我不需要StdDev拥有Avg''的副本争论?还有

无论如何我可以让Avg和StdDev拥有可变数量的参数;我每次想要做更多(或更少)数字时都不想重新编译?

非常感谢。

解决方案

Protoman写道:

嘿,这个程序对吗?它计算两个数的平均值,调和平均值,二次均值和标准差。你能为我校对吗?
这是:

#include< iostream>
#include< cstdlib>
#include< ; cmath>
使用命名空间std;

模板< class T>
class avg
{
公开:
T operator() (const T& num,const T& num2)const
{ret / =(num + num2)/ 2);
返回ret;
}
T operator()(const T& num,const T& num2,int x)const
{t Ret =(sqrt(pow(num,2)+ pow(num,2)));
返回ret;
}
T operator()(const T& num,const T& num2,int x,int y)const
{ret /> T ret = (2 /(1 / num + 1 / num2));
返回ret;
}
运算符T()const {return static_cast< T>(value);}
私人:
T值;
};

模板< class T>
类Stat
{
公开:
T operator()(T avg,const T& num,const T& num2)const
{t Ret = num-avg;
T ret2 = num2-avg;
T ret3 =(pow(ret,2)+ pow (ret2,2)/ 2);
T ret4 = sqrt(ret3);
返回ret4;
}
运算符T()const {return static_cast< T>(value );}
私人:
T值;
};

int main()
{
for(;;)
{
long double num;
long double num2;
avg< long double>平均值;
Stat< long double> StdDev;
cout<< 输入两个数字: << endl;
cin>> num>> num2;
cout<< 这是平均值: <<固定<<平均值(num,num2)<< endl;
cout<< 这里是二次均值: <<固定<<平均值(num,num2,0)<<
endl;
cout<< 这里是调和的意思: <<固定<<平均值(num,num2,0,0)<<
endl;
cout<< 这里是标准偏差: <<已修复<<<
StdDev(平均值(num,num2),num,num2)
<< endl;
}
系统(暂停);
返回0;
}


如果你说它有用我'我已经准备好相信它了,但是严重的是,b $ b误用了C ++。您需要的任务不是模板类,而是

模板函数。对于实例


模板< class T>

T mean(const T& num,const T& num2)

{

T ret =((num + num2)/ 2);

返回ret;

}


模板< class T>

T quadratic_mean(const T& num,const T& num2)

{

T ret = (sqrt(pow(num,2)+ pow(num,2)));

返回ret;

}


使用模板功能,其余代码变得更简单,更自然。


long double num;

long double num2 ;

cout<< 输入两个数字: << endl;

cin>> num>> num2;

cout<< 这是平均值: <<固定<< mean(num,num2)<< endl;

cout<< 这里是二次均值: <<已修复<<

quadratic_mean(num,num2)<<


因为您没有使用课程,所以您不必声明您在原始代码中执行的愚蠢avg

和StdDev变量。你有没有

来为你的重载运算符添加虚假的额外int参数。

另外,无论如何我能做到这一点,所以StdDev可以使用Avg的参数
所以我不需要StdDev拥有Avg'的论据副本?


我不这么认为,而且我不清楚你为什么要这样做。

还有吗?无论如何,我可以让Avg和StdDev拥有可变数量的参数;我不想每次想要做更多(或更少)的数字时重新编译?


是的,使用矢量或数组。

非常感谢。




john


Protoman写道:

嘿,这个程序对吗?它计算两个数的平均值,调和平均值,二次均值和标准差。你能为我校对吗?
这是:

#include< iostream>
#include< cstdlib>
#include< ; cmath>
使用命名空间std;

模板< class T>
class avg
{
公开:
T operator() (const T& num,const T& num2)const
{ret / =(num + num2)/ 2);
返回ret;
}
T operator()(const T& num,const T& num2,int x)const
{t Ret =(sqrt(pow(num,2)+ pow(num,2)));
返回ret;
}
T operator()(const T& num,const T& num2,int x,int y)const
{ret /> T ret = (2 /(1 / num + 1 / num2));
返回ret;
}
运算符T()const {return static_cast< T>(value);}
私人:
T值;
};


这令人困惑。为什么要把它变成一个类?使用模板函数

代替。允许程序员覆盖operator()可能是一个很酷的

功能,但你在这里滥用,因为可以从区分的虚拟

参数中辨别出来不同的平均计算。为适当命名函数(例如Mean,

HarmonicMean等)更清楚了。

。最后一个运算符不需要static_cast,

因为值是T.而且,值永远不会被初始化,因为它是私有的,因此运算符总是返回垃圾。

模板< class T>
类Stat
公共:
T operator()(T avg,const T& num,const T& num2)const
{t Ret = num-avg;
T ret2 = num2-avg;
T ret3 =(pow(ret,2)+ pow(ret2) ,2)/ 2);


这个公式看起来很可疑。

T ret4 = sqrt(ret3);
返回ret4;
}
运算符T()const {return static_cast< T>(value);}
private:
T值;
};


同样的原则适用于此。不要把它作为一个班级,不要使用

运算符()。

int main()
{
for(; ;)
{
long double num;
long double num2;
avg< long double>平均值;
Stat< long double> StdDev;
cout<< 输入两个数字: << endl;
cin>> num>> num2;
cout<< 这是平均值: <<固定<<平均值(num,num2)<< endl;
cout<< 这里是二次均值: <<固定<<平均值(num,num2,0)<<
endl;
cout<< 这里是调和的意思: <<固定<<平均值(num,num2,0,0)<<
endl;
cout<< 这里是标准偏差: <<已修复<<<
StdDev(平均值(num,num2),num,num2)
<<系统(暂停);
返回0;
}

此外,无论如何我还能做到这一点StdDev可以使用Avg的论点
所以我不需要StdDev拥有Avg的论据副本吗?无论如何,无论如何我可以让Avg和StdDev拥有可变数量的参数;我不希望每次想要做更多(或更少)数字时重新编译?
非常感谢。




你可以使用_Modern C ++ Design_中的TypeLists和Loki库

http ://sf.net/projects/loki-lib )获取可变数量的模板

参数。 Boost可能有类似的东西。无论如何,如果你按照我的建议修改程序,你就不需要它们了。


干杯! --M


我想知道你是否,mlimber,曾经统计过;那个公式那个

似乎很可疑是方差和标准差公式合并

合二为一。我已经用这种方式完成了我的课程转换操作多年来和

他们工作得很好。


Hey, is this program right? It calculates the average, harmonic mean,
quadratic mean, and the standard deviation for two numbers. Can you
proofread it for me?
Here it is:

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

template <class T>
class avg
{
public:
T operator()(const T& num, const T& num2)const
{
T ret=((num+num2)/2);
return ret;
}
T operator()(const T& num, const T& num2, int x)const
{
T ret=(sqrt(pow(num,2)+pow(num,2)));
return ret;
}
T operator()(const T& num, const T& num2,int x,int y)const
{
T ret=(2/(1/num+1/num2));
return ret;
}
operator T()const{return static_cast<T>(value);}
private:
T value;
};

template <class T>
class Stat
{
public:
T operator()(T avg,const T& num,const T& num2)const
{
T ret=num-avg;
T ret2=num2-avg;
T ret3=(pow(ret,2)+pow(ret2,2)/2);
T ret4=sqrt(ret3);
return ret4;
}
operator T()const{return static_cast<T>(value);}
private:
T value;
};

int main()
{
for(;;)
{
long double num;
long double num2;
avg<long double> Avg;
Stat<long double> StdDev;
cout << "Enter two numbers: " << endl;
cin >> num >> num2;
cout << "Here''s the average: " << fixed << Avg(num,num2) << endl;
cout << "Here''s the quadratic mean: " << fixed << Avg(num,num2,0) <<
endl;
cout << "Here''s the harmonic mean: " << fixed << Avg(num,num2,0,0) <<
endl;
cout << "Here''s the standard deviation: " << fixed <<
StdDev(Avg(num,num2),num,num2)
<< endl;
}
system ("PAUSE");
return 0;
}

Also, is there anyway I can make it so StdDev can use Avg''s arguments
so I don''t need StdDev to have copies of Avg''s arguments? And is there
anyway I can make Avg and StdDev have variable numbers of paramaters; I
don''t want to recompile everytime I want to do more (or less) numbers?
Thanks a lot.

解决方案

Protoman wrote:

Hey, is this program right? It calculates the average, harmonic mean,
quadratic mean, and the standard deviation for two numbers. Can you
proofread it for me?
Here it is:

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

template <class T>
class avg
{
public:
T operator()(const T& num, const T& num2)const
{
T ret=((num+num2)/2);
return ret;
}
T operator()(const T& num, const T& num2, int x)const
{
T ret=(sqrt(pow(num,2)+pow(num,2)));
return ret;
}
T operator()(const T& num, const T& num2,int x,int y)const
{
T ret=(2/(1/num+1/num2));
return ret;
}
operator T()const{return static_cast<T>(value);}
private:
T value;
};

template <class T>
class Stat
{
public:
T operator()(T avg,const T& num,const T& num2)const
{
T ret=num-avg;
T ret2=num2-avg;
T ret3=(pow(ret,2)+pow(ret2,2)/2);
T ret4=sqrt(ret3);
return ret4;
}
operator T()const{return static_cast<T>(value);}
private:
T value;
};

int main()
{
for(;;)
{
long double num;
long double num2;
avg<long double> Avg;
Stat<long double> StdDev;
cout << "Enter two numbers: " << endl;
cin >> num >> num2;
cout << "Here''s the average: " << fixed << Avg(num,num2) << endl;
cout << "Here''s the quadratic mean: " << fixed << Avg(num,num2,0) <<
endl;
cout << "Here''s the harmonic mean: " << fixed << Avg(num,num2,0,0) <<
endl;
cout << "Here''s the standard deviation: " << fixed <<
StdDev(Avg(num,num2),num,num2)
<< endl;
}
system ("PAUSE");
return 0;
}
If you say it works I''m prepared to believe it, but it is seriously
misusing C++. What you need for the task is not template classes but
template functions. For instaance

template <class T>
T mean(const T& num, const T& num2)
{
T ret=((num+num2)/2);
return ret;
}

template <class T>
T quadratic_mean(const T& num, const T& num2)
{
T ret=(sqrt(pow(num,2)+pow(num,2)));
return ret;
}

With template functions the rest of your code becomes simpler and more
natural.

long double num;
long double num2;
cout << "Enter two numbers: " << endl;
cin >> num >> num2;
cout << "Here''s the average: " << fixed << mean(num,num2) << endl;
cout << "Here''s the quadratic mean: " << fixed <<
quadratic_mean(num,num2) <<

Because you aren''t using classes you don''t have to declare the silly avg
and StdDev variables that you did in your original code. Not do you have
to add the spurious extra int parameters to your overloaded operators.
Also, is there anyway I can make it so StdDev can use Avg''s arguments
so I don''t need StdDev to have copies of Avg''s arguments?
I don''t think so, and it''s not clear to me why you would want to do this.
And is there
anyway I can make Avg and StdDev have variable numbers of paramaters; I
don''t want to recompile everytime I want to do more (or less) numbers?
Yes, use a vector or an array.
Thanks a lot.



john


Protoman wrote:

Hey, is this program right? It calculates the average, harmonic mean,
quadratic mean, and the standard deviation for two numbers. Can you
proofread it for me?
Here it is:

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

template <class T>
class avg
{
public:
T operator()(const T& num, const T& num2)const
{
T ret=((num+num2)/2);
return ret;
}
T operator()(const T& num, const T& num2, int x)const
{
T ret=(sqrt(pow(num,2)+pow(num,2)));
return ret;
}
T operator()(const T& num, const T& num2,int x,int y)const
{
T ret=(2/(1/num+1/num2));
return ret;
}
operator T()const{return static_cast<T>(value);}
private:
T value;
};
This is confusing. Why make it a class at all? Use template functions
instead. Allowing the programmer to override operator() may be a cool
feature, but you''re abusing here, as can be discerned from the dummy
parameters needed to distinguish the different mean calculations. It is
far clearer to name the functions appropriately (e.g. Mean,
HarmonicMean, etc.). The last operator doesn''t need a static_cast,
since value is T. Also, value can never be initialized since it is
private, so that operator always returns garbage.
template <class T>
class Stat
{
public:
T operator()(T avg,const T& num,const T& num2)const
{
T ret=num-avg;
T ret2=num2-avg;
T ret3=(pow(ret,2)+pow(ret2,2)/2);
This formula seems fishy.
T ret4=sqrt(ret3);
return ret4;
}
operator T()const{return static_cast<T>(value);}
private:
T value;
};
The same principle applies here. Don''t make it a class and don''t use
operator().

int main()
{
for(;;)
{
long double num;
long double num2;
avg<long double> Avg;
Stat<long double> StdDev;
cout << "Enter two numbers: " << endl;
cin >> num >> num2;
cout << "Here''s the average: " << fixed << Avg(num,num2) << endl;
cout << "Here''s the quadratic mean: " << fixed << Avg(num,num2,0) <<
endl;
cout << "Here''s the harmonic mean: " << fixed << Avg(num,num2,0,0) <<
endl;
cout << "Here''s the standard deviation: " << fixed <<
StdDev(Avg(num,num2),num,num2)
<< endl;
}
system ("PAUSE");
return 0;
}

Also, is there anyway I can make it so StdDev can use Avg''s arguments
so I don''t need StdDev to have copies of Avg''s arguments? And is there
anyway I can make Avg and StdDev have variable numbers of paramaters; I
don''t want to recompile everytime I want to do more (or less) numbers?
Thanks a lot.



You can use TypeLists from _Modern C++ Design_ and the Loki library
(http://sf.net/projects/loki-lib) to get a variable number of template
parameters. Boost may have something similar. In any case, you
shouldn''t need them if you alter the program as I suggested above.

Cheers! --M


I''m wondering if you, mlimber, ever took statistics; that formula "that
seems fishy" is the variance and standard deviation formula combined
into one. And I''ve done my class conversion ops that way for years and
they worked just fine.


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

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