| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5684 人关注过本帖
标题:程序 数学期望与方差
只看楼主 加入收藏
hangeng
Rank: 2
等 级:论坛游民
帖 子:424
专家分:39
注 册:2007-7-23
结帖率:100%
收藏
 问题点数:0 回复次数:3 
程序 数学期望与方差
高二的数学总是讨厌的!
老师让不停地计算期望与方差!
恰好我的计算器坏了!
所以写了这个程序!
程序代码:
 
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;
//全局变量
int wrong;// 错误编号代码
//函数声明
int start_first(void);
int start_se(void);
void copyright(void);
void abnormity(int a);

 
/**********************************************类部分***********************************************/
// 处理概率分布
class discrete_distributing
{
    

 
/*   注意由于构造函数有问题所以第一版我取消了!

 //构造函数

 discrete_distributing()

 {
  ME=1;
  MV=1;
  c=0;
        //initializion
  for(c=0;c<=1000;c++)
  {
          discrete_num[c]=1;
    discrete_num_p[c]=1;
  }

  

 }
*/
public:
     int gl_input(void);//input data
  float gl_output(void);//output data
  int gl_control(void);//过程控制
  float ME;// mathematical expection
  float MV;// mathematical variance
        int n;//define  how many kinds of value of  ξ will have

 
private:
    

 int y;//arithmometer

    float discrete_num[1000];

 float discrete_num_p[1000];

 int c;//just for discrete_num[c] and sicrete_num_p[c]

 float expection(void);//count mathematical expection

 float variance(void); //count mathematical variance
  
};
/*
*     函数名:gl_control
*     函数作用:控制概率分布的整个过程
*     输入值:viod
*     输出值:NULL
*     返回值:成功返回1
*     备注:属于discrete_distributing类
*/
int discrete_distributing::gl_control(void)
{   

 int nouse;
    //输入函数

 wrong=gl_input();

 if(wrong!=0)

 {
  abnormity(wrong);

 }
    //期望函数

 nouse=expection();

 //方差函数

 nouse=variance();

 //输出函数

 gl_output();
   


 return  1;


 
}

 
/*
*     函数名:gl_input
*     函数作用:输入离散分布变量的ζ和p
*     输入值:NULL
*     输出值:NULL
*     返回值:NULL
*     备注:概率分布
*/
int discrete_distributing::gl_input(void)
{   
    
    int m=1;

 //input n

 cout<<"Please tell how much data would you input?"<<endl;

 cout<<"n=";

 cin>>n;

 cout<<"OK";
    //input distribution series

 for (m=1;m<=n;m++)

 {  
    
       cout<<"Please input  "<<m<<"seires value"<<endl;
    
    //input ζ
    cout<<"ζ["<<m<<"]=";
    cin>>discrete_num[m];
       
    //input  p
    cout<<"p ["<<m<<"]=";
    cin>>discrete_num_p[m];
    if(discrete_num_p[m]<0||discrete_num_p[m]>1)
    {   
     wrong=1;
           abnormity(wrong);
    }

 }
     return  0;
}
/*
*     函数名:expection
*     函数作用:计算概率分布的期望
*     输入值:NULL
*     输出值:ME
*     返回值:ME
*     备注:属于discrete_distributing类
*/
float  discrete_distributing::expection (void)
{

 
    c=1;

 for(c=0;c<=n;c++)

 {
      ME=discrete_num[c]*discrete_num_p[c]+ME;

 }

 return ME;
}

/*
*     函数名:variance
*     函数作用:计算概率分布的方差
*     输入值:NULL
*     输出值:NULL
*     返回值:MV
*     备注:属于discrete_distributing类
*/
float  discrete_distributing::variance(void)
{

 c=1;

 for(c=0;c<=n;c++)

 {
  MV=(discrete_num[c]-ME)*(discrete_num[c]-ME)*discrete_num_p[c]+MV;

 }

 return MV;
}
/*
*     函数名:gl_output
*     函数作用:输出概率分布的期望与方差
*     输入值:NULL
*     输出值:ME  MV
*     返回值:ME or MV
*     备注:属于discrete_distributing类
*/
float discrete_distributing::gl_output(void)
{

 cout<<"期望为"<<ME<<endl;

 cout<<"方差为"<<MV;
   

 return ME;  

 //return MV;
}

 
//处理二项式分布
class binomial_distributing
{
public:
    

 float n;

 float p;

 float ME;

 float MV;

 void ex_input(void);

 float ex_output(void);
    int  binomial_distributing_control(void);
   
private:

 float expection(void);

 float variance(void);
};

/*
*     函数名:binomial_distributing_control
*     函数作用:二项式的过程控制
*     输入值:NULL
*     输出值:NULL
*     返回值:成功则返回1;
*     备注:属于binomial_distributing类
*/
int binomial_distributing::binomial_distributing_control(void)
{   

 int nouse;

 ex_input();

 nouse=expection();

 nouse=variance();

 nouse=ex_output();

 return  1;

}

/*
*     函数名:ex_input
*     函数作用:输入二项式分布变量的n和p
*     输入值:NULL
*     输出值:n和p
*     返回值:NULL
*     备注:
*/
void binomial_distributing::ex_input(void)
{  

 
   cout<<"Please input n !"<<endl;
   cin>>n;
   cout<<"Please input p!"<<endl;
   cin>>p;
   //判断p值如果大于1 则为错误
   if(p>1||p<0)
   {  
    wrong=1;
       abnormity(wrong);
   }

 
}

 
/*
*     函数名:ex_output
*     函数作用:输出概率分布的期望与方差
*     输入值:NULL
*     输出值:ME  MV
*     返回值:ME or MV
*     备注:属于discrete_distributing类
*/
float binomial_distributing::ex_output(void)
{

 cout<<ME;

 cout<<MV;
   

 return ME;  

 //return MV;
}

/*
*     函数名:expection
*     函数作用:计算二项分布的期望
*     输入值:n ;p
*     输出值:ME
*     返回值:ME
*     备注:属于binomial_distribrting类
*/
float binomial_distributing::expection(void)
{
    ME=n*p;

 return ME;
}
/*
*     函数名:variance
*     函数作用:计算二项分布的方差
*     输入值:n ;p
*     输出值:MV
*     返回值:MV
*     备注:属于binomial_distribrting类
*/
float binomial_distributing::variance(void)
{  

 MV=n*p*(1-p);
    

 return MV;
}

 

 
//处理几何分布
class geometrical_distributing
{
public:

 float p;
    

 float ME;

 float MV;

 float jh_input(void);

 float jh_output(void);

 

 int jh_control(void);
private:

 float expection(void);

 float variance(void);

 

};

/*
*     函数名:jh_control
*     函数作用:几何分布的过程控制
*     输入值:NULL
*     输出值:NULL
*     返回值:成功则返回1
*     备注:属于geometrical_distributing类
*/
int geometrical_distributing::jh_control(void)
{

 int nouse;

 nouse=jh_input();
    nouse=expection();

 nouse=variance();

 nouse=jh_output();

 return 0;
}

 
/*
*     函数名:jh_input
*     函数作用:输入p
*     输入值:p
*     输出值:p
*     返回值:p
*     备注:属于geometrical_distributing
*/
float geometrical_distributing::jh_input(void)
{

 cout<<"Please input P!";

 cin>>p;

 return p;
}

/*
*     函数名:jh_output
*     函数作用:输出期望与方差
*     输入值:NULL
*     输出值:ME MV
*     返回值:ME or MV
*     备注:属于geometrical_distributing
*/
float geometrical_distributing::jh_output(void)
{

 cout<<ME;

 cout<<MV;
   

 return ME;  

 //return MV;
}
/*
*     函数名:expection
*     函数作用:计算几何分布的期望
*     输入值:p
*     输出值:ME
*     返回值:ME
*     备注:属于geometrical_distributing类
*/
float geometrical_distributing::expection(void)
{

 ME=1/p;

 return ME;

}
/*
*     函数名:variance
*     函数作用:计算几何分布的方差
*     输入值:p
*     输出值:MV
*     返回值:MV
*     备注:属于geometrical_distributing类
*/
float geometrical_distributing::variance(void)
{

 MV=(1-p)/(p*p);

 return 0;
}

 

 

 

 

 
/*****************************************************************类部分结束******************************************/

//定义一个概率分布对象
discrete_distributing dis;
//定义一个二项式分布对象
binomial_distributing  bin;
//定义一个几何分布对象
geometrical_distributing  geo;

 

 

 

 

int main(void)
{
     
    int inutility=0;
     

 inutility=start_first();

 inutility=start_se();
     return 0;
}
/*
*     函数名:start_first
*     函数作用:输出各项选项
*     输入值:NULL
*     输出值:NUll
*     返回值:1
*     备注:
*/
int start_first(void)
{   
    printf("\t ╭────────────────────────────╮\n");
    printf("\t ∣              数学期望与方差1.0                         ∣\n");
    printf("\t |————————————————————————————|\n");
    printf("\t | \t    1.  离散分布变量                    |\n");
    printf("\t |                            |\n");
    printf("\t | \t    2. 二项式分布变量                   |\n");
    printf("\t |                            |\n");
    printf("\t | \t    3. 几何分布变量               |\n");
    printf("\t |                            |\n");
    printf("\t | \t    4. 版权                                   |\n");

 printf("\t  |                                                        |\n");

 printf("\t  |  \t        5.  退出                                    |\n");
    printf("\t |                      制作者:韩耿|\n");        
    printf("\t  ╰────────────────────────────╯\n");


 return 0;
}
/*
*     函数名:start_se
*     函数作用:选择选项
*     输入值:NULL
*     输出值:程序代号
*     返回值:1
*     备注:
*/
int start_se(void)
{
     int n;
  cout<<"请输入您需要的选项的代码"<<endl<<"n=";
  cin>>n;
  switch (n)
  {
  case 1:  
   {
    dis.gl_control();
    break;
   }
  case 2:
   {  
    bin.binomial_distributing_control();
    break;
          
   }
  case 3:
   {
    geo.jh_input();
    break;

   }
  case 4:
   {
    copyright();
    break;
   }
  case 5:
   {
    exit(0);
    break;
   }
  default:
   {
    exit(0);
    break;
   }
  
  }
  


 return 0;
}

/*
*     函数名:copyright
*     函数作用:输入版权信息
*     输入值:NULL
*     输出值:NULL
*     返回值:NULL
*     备注:
*/
void copyright(void)
{

 /*版权及作者*/
         cout<<"程序名称:数学期望与方差";      
         printf("版本:1.0\n");
         printf("作者姓名:韩耿\n");
         printf("作者的电子邮箱:   xingpinglixiang@ \n");
   printf("Thank for my father's help!");
         //system("start http://xijiao.); 打开一个网址 
         
}
/*
*     函数名:abnormity
*     函数作用:异常处理
*     输入值:NULL
*     输出值:NULL
*     返回值:NULL
*     备注:
*/
void abnormity(int a)
{
    int  nouse;
   cout<<"Sorry !";
   cout<<"The computer has appear unkonwn bug!";
   cout<<"Although author tied not to make mistakes! ";
   cout<<"You can send an e-mail to xingpinglixiang@ for the mistakes!";
   cout<<"Sorry!";
   cout<<"Please put any key to restar!";
   
   switch  (a)
   {
   case 1 :
    printf("错误编号:【1】\n");
    printf("概率不能大于1或小于0!\n");
   
     
   };

 
   system("pause");
   nouse=main();
}
/*
*     函数名:exit
*     函数作用:退出
*     输入值:NULL
*     输出值:NULL
*     返回值:NULL
*     备注:
*/
void  exit(void)
{
  abort();
}


 

希望大家指出程序的所有错误!

这是第一个用C++写的!

ps:虽然是C++但我喜欢C板块啊!

下面的压缩包包扩所有的文件了!
数学期望与方差.rar (907.82 KB)


有错请提出!
我绝对改正!
谢谢!
搜索更多相关主题的帖子: 方差 数学期望 void 计算器 int 
2008-07-12 22:51
hangeng
Rank: 2
等 级:论坛游民
帖 子:424
专家分:39
注 册:2007-7-23
收藏
得分:0 
顶!

  雨水冲不进窗来,在玻璃上痛哭。但它至少奋斗过。
2008-07-19 23:02
病书生
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2007-11-8
收藏
得分:0 
这里是C区,不是C++
2008-07-20 00:47
zx043
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-11-12
收藏
得分:0 
顶一个~
2012-11-12 11:32
快速回复:程序 数学期望与方差
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022769 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved