用C++写的一个统计软件(简单版本)

#include<iostream>

#include<math.h>

using namespace std;

typedef struct valuearry

{

double value;

struct valuearry *nextvalue;

}Value;

double summation(Value *h)

{

Value *p;

p=h;

double s=0;

while(p!=NULL)

{

s+=p->value;

p=p->nextvalue;

}

return s;

}

double average(Value *h)

{

Value *p=h;

double s=0;

int number=0;

while(p!=NULL)

{

s+=p->value;

number++;

p=p->nextvalue;

}

return (s/number);

}

int count(Value *h)

{

Value *p=h;

int n=0;

while(p!=NULL)

{

n++;

p=p->nextvalue;

}

return n;

}

double variation(Value *h)

{

Value *p=h;

double s=0,ss=0;

int n=0;

while(p!=NULL)

{

s+=p->value;

ss+=(p->value)*(p->value);

n++;

p=p->nextvalue;

}

return (ss-s*s/n);

}

double coefvar(Value *h)

{

double stdev,mean;

stdev=variation(h);

stdev/=count(h)-1;

mean=average(h);

return (sqrt(stdev)/mean);

}

double maxvalue(Value *h)

{

Value *p=h;

double max;

max=h->value;

while(h!=NULL)

{

if(h->value>max)

max=h->value;

h=h->nextvalue;

}

return max;

}

double minvalue(Value *h)

{

Value *p=h;

double min;

min=h->value;

while(h!=NULL)

{

if(h->value<min)

min=h->value;

h=h->nextvalue;

}

return min;

}

double parvar(Value *h1,Value *h2)

{

double sp=0,s1=0,s2=0;

Value *p1=h1,*p2=h2;

int n=0;

while(p1!=NULL)

{

sp+=(p1->value)*(p2->value);

s1+=p1->value;

s2+=p2->value;

n++;

p1=p1->nextvalue;

p2=p2->nextvalue;

}

sp-=s1*s2/n;

return sp;

}

class Data

{

public:

Data(){}

Data(string st)

{

name=st;

head=createvalue();

sum=summation(head);

mean=average(head);

number=count(head);

var=variation(head)/(number-1);

cv=coefvar(head);

max=maxvalue(head);

min=minvalue(head);

}

string name;

Value *head;

double sum,mean,var,cv,max,min;

int number;

Data *nextdata;

Value *createvalue()

{

Value *h=NULL,*p=NULL,*q=NULL;

string st;

while(1)

{

p=new Value;

cin>>p->value;

if(h==NULL)

h=p;

else

q->nextvalue=p;

q=p;

cin>>st;

if(st==”end”)

break;

}

if(q!=NULL)

q->nextvalue=NULL;

return h;

}

virtual void show()

{

Value *p;

p=head;

cout<<name<<“:”<<endl;

while(p!=NULL)

{

cout<<‘t'<<p->value;

p=p->nextvalue;

}

cout<<endl;

}

};

Data *createdata()

{

Data *h=NULL,*p=NULL,*q=NULL;

string st;

while(1)

{

cin>>st;

if(st==”end”) break;

else

{

p=new Data(st);

if(h==NULL)

h=p;

else

q->nextdata=p;

q=p;

}

}

if(q!=NULL)

q->nextdata=NULL;

return h;

}

void sortdata(Data *head)

{

Data *p,*q;

Data temp;

p=q=head;

while(p->nextdata!=NULL)

{

q=p->nextdata;

while(q!=NULL)

{

if((p->mean)>(q->mean))

{

temp.name=p->name;

temp.head=p->head;

temp.sum=p->sum;

temp.mean=p->mean;

temp.number=p->number;

temp.nextdata=NULL;

p->name=q->name;

p->head=q->head;

p->sum=q->sum;

p->mean=q->mean;

p->number=q->number;

q->name=temp.name;

q->head=temp.head;

q->sum=temp.sum;

q->mean=temp.mean;

q->number=temp.number;

}

q=q->nextdata;

}

p=p->nextdata;

}

}

class Mean:public Data

{

public:

Data *head,*p,*q;

Mean()

{

head=createdata();

p=q=head;

}

virtual void show()

{

cout<<“t”<<“Sum”<<“t”<<“Mean”<<“t”<<“Max”<<“t”<<“Min”<<“t”<<“Std Dev”<<endl;

while(head!=NULL)

{

cout<<head->name<<“t”<<head->sum<<“t”<<head->mean<<“t”<<head->max<<“t”<<head->min<<“t”<<sqrt(head->var)<<endl;

head=head->nextdata;

}

cout<<endl;

}

};

class MeanA:public Data

{

public:

Data *head,*p,*q;

MeanA()

{

head=createdata();

p=q=head;

}

virtual void show()

{

cout<<“t”<<“Sum”<<“t”<<“N”<<“t”<<“Mean”<<“t”<<“Max”<<“t”<<“Min”<<“t”<<“Std Dev”<<“t”<<“Coef of Var”<<endl;

while(head!=NULL)

{

cout<<head->name<<“t”<<head->sum<<“t”<<head->number<<“t”<<head->mean<<“t”<<head->max<<“t”<<head->min<<“t”<<sqrt(head->var)<<“t”<<head->cv<<endl;

head=head->nextdata;

}

cout<<endl;

}

};

class Sort:public Data

{

public:

Data *head;

Sort()

{

head=createdata();

}

virtual void show()

{

sortdata(head);

int i=1;

cout<<“No.”<<“t”<<“Name”<<“t”<<“Sum”<<“t”<<“N”<<“t”<<“Mean”<<endl;

while(head!=NULL)

{

cout<<i<<“:”<<“t”<<head->name<<“t”<<head->sum<<“t”<<head->number<<“t”<<head->mean<<endl;

head=head->nextdata;

i++;

}

}

};

class Reglation:public Data

{

public:

Data *p1,*p2;

double reg1,reg2,r;

string name1,name2;

Reglation()

{

cin>>name1;

p1=new Data(name1);

cin>>name2;

p2=new Data(name2);

reg1=parvar(p1->head,p2->head)/variation(p1->head);

reg2=average(p2->head)-average(p1->head)*reg1;

r=parvar(p1->head,p2->head)/sqrt((variation(p1->head)*variation(p2->head)));

}

virtual void show()

{

cout<<p2->name<<“=”<<reg1<<p1->name<<“+”<<reg2<<endl;

cout<<“R-square:”<<r<<endl;

}

};

class Correlate:public Data

{

public:

double corr;

Data *head;

Correlate()

{

head=createdata();

}

virtual void show()

{

Data *p,*q;

p=q=head;

while(p!=NULL)

{

cout<<“t”<<p->name;

p=p->nextdata;

}

cout<<endl;

p=q=head;

while(p!=NULL)

{

q=p;

cout<<q->name;

while(q!=NULL)

{

corr=parvar(p->head,q->head)/(sqrt((variation(p->head))*(variation(q->head))));

cout<<“t”<<corr;

q=q->nextdata;

}

p=p->nextdata;

cout<<endl;

}

}

};

int main()

{

Data *head;

string command;

cout<<“_____________________________________________________________________________”<<endl;

cout<<“| |”<<endl;

cout<<“| |”<<endl;

cout<<“| 欢迎使用本STATISTIC软件 |”<<endl;

cout<<“| 制作人:曹堃 |”<<endl;

cout<<“| 如需帮助请按下help命令 |”<<endl;

cout<<“| 版本 :2012-3.0 |”<<endl;

cout<<“| 编写语言:C++ |”<<endl;

cout<<“| 版权和最终解释权归本人所有 |”<<endl;

cout<<“| |”<<endl;

cout<<“|___________________________________________________________________________|”<<endl;

while(1)

{

cout<<“START”<<“:”<<“\”<<“>”;

cin>>command;

if(command==”end”) break;

else if(command==”help”)

{

cout<<“Means Proceduce press ‘mean’ command”<<endl;

cout<<“To see All Mean Result press ‘mean,all’ command”<<endl;

cout<<“Sort Class with Mean press ‘sort’ command”<<endl;

cout<<“Reg press ‘reg’ command”<<endl;

cout<<“Corr press ‘corr’ command”<<endl;

}

else if(command==”mean”)

{

head=new Mean;

head->show();

delete head;

}

else if(command==”mean,all”)

{

head=new MeanA;

head->show();

delete head;

}

else if(command==”sort”)

{

head=new Sort;

head->show();

delete head;

}

else if(command==”reg”)

{

head=new Reglation;

head->show();

delete head;

}

else if(command==”corr”)

{

head=new Correlate;

head->show();

delete head;

}

else

cout<<“Not Such a Command”<<endl;

}

}

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年4月17日
下一篇 2019年4月17日

相关推荐