我又来问题了 C++

来源:百度知道 编辑:UC知道 时间:2024/07/06 09:35:18
算 平均数和偏差值的
只算 1到4个数 输入大于4的数显示“sorry,the program can only test 1,2,3 or 4value”.

例如输入 1
RUN:
Do you wish to test 1,2,3 or4 numbers (enter 0 to end the program): 1
enter first value:
average: standard deviation:

如果输入 4
run:
Do you wish to test 1,2,3 or4 numbers (enter 0 to end the program): 4
enter first value:
enter second value:
enter third value:
enter fourth value:
average: standard deviation:
输入5:
RUN:
sorry,the program can only test 1,2,3 or 4value.

输入0结束程序

希望老师们越简单越好啊 太难我看不懂。

偏差值公式:根号(每个数减平均数的平方之和除以个数)

楼上用的c语言,上面好像要求用c++呢,
我下面的没有用到<iocmath>这个头文件,因为计算很简单,我也没用过这个文件,即使用的话,调一下函数应该很简单的,下面写了一下,看看行不行:
#include <iostream>
using namespace std;

class num
{
public:
int i;
float shuzu[4];//放四个输入的数,没有就为0;
float average;//放平均值
float deviation;//放偏差值
num()//构造函数
{
i=0;//要算几个数的
shuzu[0]=0;
shuzu[1]=0;
shuzu[2]=0;
shuzu[3]=0;
average=0;
deviation=0;
}
int average_and_deviation()
{int j=0;
average=(shuzu[0]+shuzu[1]+shuzu[2]+shuzu[3])/i;//算平均值
//算差值
for(j=0;j<i;j++)
{
deviation=deviation+(shuzu[j]-average)*(shuzu[j]-average);//deviation暂时放方差
}
deviation=deviation/i;//由方差得偏差
return 0;
}
};

int main()
{
int over=1;
while(1)
{
num a;
cout <<"Do you wish to test 1,2,3 or4 numbers (enter 0 to end the program):"