注册 登录
编程论坛 C++教室

为什么输出时0?

a99875984 发布于 2012-06-08 08:40, 302 次点击
程序代码:
#include <iostream>
using namespace std;
class wl
{
private:
    float r1,r2,r3,r4,r5,pjz,jdwcz;
public:
    void shuru(float a,float b,float c,float d,float e);
     void pj();
    void jdwc();
};
void  wl::shuru( float a,float b,float c,float d,float e)
{
    r1=a;
    r2=b;
    r3=c;
    r4=d;
    r5=e;
}
void wl::pj ()//平均值
{
    pjz=(r1+r2+r3+r4+r5)/5;
    cout<<pjz<<endl;
}
void wl::jdwc()//绝对误差
{
    jdwcz=(abs(r1-pjz)+abs(r2-pjz)+abs(r3-pjz)+abs(r4-pjz)+abs(r5-pjz))/5;
    cout<<jdwcz<<endl;
}
int main()
{
    float a,b,c,e,d;
    wl r;
        cin>>a>>b>>c>>d>>e;
        r.shuru(a,b,c,d,e);
        r.pj();
        r.jdwc();
    return 0;
}
输入 20.56
20.46
20.76
20.56
20.48
输出为什么是 20.564
                0为什么会是0啊?
2 回复
#2
cqpreson2012-06-09 03:53
iostream里面的abs函数的参数是整数型,r1-pjz这些的结果自动转化为整数型就都变成了0。

浮点型得用math.h里面的abs或者fabs。
#3
a998759842012-06-09 16:34
回复 2楼 cqpreson
谢了
1