7. Приложение
Исходный код программы:
#include <iostream>
#include<stdio.h>
#include<math.h>
using std::cout;
using std::cin;
using std::endl;
int Power(float A[9][9],float B[9][9],float C[9][9], int n)
{
int **CC;
int i,j;
CC =new int*[n];
for (i=0; i<n; i++)
{
CC[i]=new int [n];
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
float Sum=0;
for (int q=0; q<n; q++)
{
Sum+=A[i][q]* B[q][j];
}
C[i][j]=Sum;
}
}
return **CC;
}
float func_AND(float ff[150], int schet, float numberofproc, int x) {
//cout<<endl<<"_______________________AND_______________________"<<endl<<endl;
float f[150];
for(int g=0;g<schet;g++)
{
f[g] = ff[g];
}
float f_and[150];
float M_and = 0;
float D_and = 0;
float nproc = numberofproc;//150;
float slag = 0;
float sum = 0;
int sch=schet;
for (int k=0;k<sch;k++)//sch=89
{f_and[k] = 0;
for (int l=1;l<=nproc;l++)
{
slag = f[k];
for (int li=2; li<=nproc; li++)
{
sum = 0;
if (li <= l)
{
for (int ki=0;ki<k;ki++)
{
sum = sum + f[ki];
}
}
else
{
for (int ki=0;ki<=k;ki++)
{
sum = sum + f[ki];
}
}
slag = slag*sum;
}
f_and[k] = f_and[k] + slag;
}
M_and = M_and + (k+1)*f_and[k];
}
float prov1=0;
for (int k=0;k<sch;k++)
{
//cout<<"f_and["<<k<<"] = "<< f_and[k]<<endl;
D_and = D_and + (k+1 - M_and)*(k+1 - M_and)*f_and[k];
prov1+=f_and[k];
}
cout<<"f_and["<<x+1<<"] = "<<f_and[x]<<endl;
if(x==sch-1){
//cout<<"Prov = "<<prov1<<endl<<endl;
cout<<endl<<"M_and = "<<M_and<<endl;
cout<<"D_and = "<<D_and<<endl<<endl;
}
return f_and[x];
}
void func_OR(float ff[150], int schet, float numberofproc) {
cout<<endl<<"_______________________OR_______________________"<<endl<<endl;
float f[150];
for(int g=0;g<schet;g++)
{
f[g] = ff[g];
}
float f_or[150];
float M_or = 0;
float D_or = 0;
float nproc = numberofproc;//150;
float slag = 0;
float sum = 0;
int sch=schet;
//float A[9][9];
for (int k=0;k<sch;k++)
{f_or[k] = 0;
for (int l=1;l<=nproc;l++)
{
slag = f[k];
for (int li=2; li<=nproc; li++)
{
sum = 0;
if (li <= l)
{
for (int ki=k+1;ki<sch;ki++)
{sum = sum + f[ki];}
}
else
{
for (int ki=k;ki<sch;ki++)
{sum = sum + f[ki];}
}
slag = slag*sum;
}
f_or[k] = f_or[k] + slag;
}
M_or = M_or + (k+1)*f_or[k];
}
for (int k=0;k<sch;k++)
{
cout<<"f_or["<<k+1<<"] = "<< f_or[k]<<endl;
D_or = D_or + (k+1 - M_or)*(k+1 - M_or)*f_or[k];
}
cout<<endl;
cout<<"M_or = "<<M_or<<endl;
cout<<"D_or = "<<D_or<<endl;
cout<<endl;
//return f_or[150];
}
float func_2of3(float ff[150], int schet, int x) {
//cout<<endl<<"_______________________2of3_______________________"<<endl<<endl;
float f[150];
for(int g=0;g<schet;g++)
{
f[g] = ff[g];
}
float f_23[150];
float M_23 = 0;
float D_23 = 0;
//float nproc = numberofproc;//3
float slag = 0;
float sum = 0;
int sch=schet;
float sum1 = 0;
float sum2 = 0;
for (int k=0;k<sch;k++)
{
f_23[k] = 0;
//1
sum1 = 0;
sum2 = 0;
for (int ki=0; ki<sch;ki++)
{
if (ki <= k)
{
sum1+=f[ki];
}
if (ki >= k)
{
sum2+=f[ki];
}
}
f_23[k] = f_23[k] + f[k]*sum1*sum2;
//2
sum1 = 0;
sum2 = 0;
for (int ki=0; ki<sch;ki++)
{
if (ki < k)
{
sum1+=f[ki];
}
if (ki >= k)
{
sum2+=f[ki];
}
}
f_23[k] = f_23[k] + f[k]*sum1*sum2;
//3
sum1 = 0;
sum2 = 0;
for (int ki=0; ki<sch;ki++)
{
if (ki <= k)
{
sum1+=f[ki];
}
if (ki > k)
{
sum2+=f[ki];
}
}
f_23[k] = f_23[k] + f[k]*sum1*sum2;
//4
sum1 = 0;
sum2 = 0;
for (int ki=0; ki<sch;ki++)
{
if (ki < k)
{
sum1+=f[ki];
}
if (ki > k)
{
sum2+=f[ki];
}
}
f_23[k] = f_23[k] + f[k]*sum1*sum2;
//5
sum1 = 0;
sum2 = 0;
for (int ki=0; ki<sch;ki++)
{
if (ki <= k)
{
sum1+=f[ki];
}
if (ki > k)
{
sum2+=f[ki];
}
}
f_23[k] = f_23[k] + f[k]*sum1*sum2;
//6
sum1 = 0;
sum2 = 0;
for (int ki=0; ki<sch;ki++)
{
if (ki < k)
{
sum1+=f[ki];
}
if (ki > k)
{
sum2+=f[ki];
}
}
f_23[k] = f_23[k] + f[k]*sum1*sum2;
//7
sum1 = 0;
sum2 = 0;
for (int ki=0; ki<sch;ki++)
{
if (ki < k)
{
sum1+=f[ki];
}
}
f_23[k] = f_23[k] + f[k]*f[k]*sum2;
//cout<<"f_23["<<k<<"] = "<< f_23[k]<<endl;
M_23 = M_23 + (k+1)*f_23[k];
}
float prov2=0;
for (int k=0;k<sch;k++)
{
//cout<<"f_or["<<k<<"] = "<< f_or[k]<<endl;
D_23 = D_23 + (k+1 - M_23)*(k+1 - M_23)*f_23[k];
prov2+=f_23[k];
}
cout<<"f_2of3["<<x+1<<"] = "<<f_23[x]<<endl;
if(x==sch-1){
//cout<<"prov = "<<prov2<<endl<<endl;
cout<<endl<<"M_2of3 = "<<M_23<<endl;
cout<<"D_2of3 = "<<D_23<<endl<<endl;
}
return f_23[x];
}
void func_RESULT(float f1[150], float f2[150], float f3[150], int schet) {
//cout<<endl<<"_______________________RESULT_______________________"<<endl<<endl;
float f_result[150];
float M_result = 0;
float D_result = 0;
float nproc = 3; //3 РАЗНЫХ!
float slag = 0;
float sum = 0;
int sch=schet;
float f22[150];
float f33[150];
float prov3=0;
for (int k=0;k<2*sch;k++)
{
f22[k]=0;
for (int ki=0;ki<k;ki++)
{
f22[k]+=f1[ki]*f2[k-ki];
}
//cout<<"f22["<<k<<"] = "<< f22[k]<<endl;
}
for (int k=0;k<50;k++)//k<3*sch=3*34=102
{
f_result[k] = 0;
f33[k]=0;
for (int ki=0;ki<k;ki++)
{
f33[k]+=f22[ki]*f3[k-ki];
}
f_result[k] = f33[k];
cout<<"f_result["<<k+1<<"] = "<< f_result[k]<<endl;
M_result = M_result + (k+1)*f_result[k];
}
for (int k=0;k<50;k++)
{
prov3+=f_result[k];
D_result = D_result + (k+1 - M_result)*(k+1 - M_result)*f_result[k];
}
//cout<<"prov = "<<prov3<<endl<<endl;
cout<<endl<<"M_result = "<<M_result<<endl;
cout<<"D_result = "<<D_result<<endl<<endl;
}
int main()
{
const int s=9;
float A[s][s]={
{0,0.4,0.6,0,0,0},
{0,0,0,1,0,0},
{0,0.8,0,0,0.2,0},
{0,0,0,0,0,1},
{0,0,0,0.3,0,0.7},
{0,0,0,0,0,1}};
int L=12;
float B[s][s];
float C[s][s];
float f[150];
float proverka;
float M = 0;
float D = 0;
float summf = 0;
float delta = 0.0001;
int n,i,j;
n = s;
cout<<"Matrica:"<<endl;//Вывод матрицы
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<A[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
B[i][j] = A[i][j];
}
}
//sch = 0
f[0] = A[0][n-1];//правый верхний элемент матрицы А
summf = summf + f[0];
proverka = 1 - f[0];
M = M + 1*f[0];
float sumr=0;
int sch = 1;
do
{
cout<<"f["<<sch<<"] = "<< f[sch-1]<<endl;
Power(B,A,C,n);
f[sch] = C[0][n-1] - B[0][n-1];
if (sch>L-1)
sumr= sumr+f[sch];
summf = summf + f[sch];
proverka = proverka - f[sch]; //Из единицы вычитаем f[k], и считаем дальше. Пока превышает дельта
M = M + (sch+1)*f[sch]; //МАТ. ОЖИДАНИЕ
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
B[i][j] = C[i][j];
}
}
sch++;
} while (proverka > delta && sch < 150);
cout<<"f["<<sch<<"] = "<< f[sch-1]<<endl;
cout<<endl<<"Prov = "<< summf <<endl;
cout<<"M = "<< M <<endl;
//cout<<endl<<sch<<endl<<endl;//34
for (i=1;i<sch-1;i++)//
{
D = D + (i - M)*(i - M)*f[i - 1]; //ДИСПЕРСИЯ
}
cout<<"D = "<< D <<endl;
float sko=sqrt(D);
if(M-3*sko>0){
cout<<"Doveritelniy interval = "<<"["<<M<<"-"<< 3*sko <<";"
<<M<<"+"<< 3*sko <<"]" <<" or ""["<<M-3*sko <<";"
<<M+3*sko <<"]" <<endl;}
else {
cout<<"Doveritelniy interval = [0;"
<<M<<"+"<< 3*sko <<"] or [0;"
<<M+3*sko <<"]" <<endl;}
cout<<"Risk sriva vremennogo reglamenta R("<<L<<")="<<sumr<<endl;
float nproc=50; //Количество процессов I
float f_and[150];
cout<<endl<<"_______________________AND_______________________"<<endl<<endl;
for(int x=0;x<sch;x++){
f_and[x]=func_AND(f, sch, nproc, x);
//cout<<"f_and["<<x<<"] = "<<f_and[x]<<endl;
}
//<<endl<<"______________________OR_____________________"<<endl<<endl;
func_OR(f, sch, nproc);
float f_2of3[150];
cout<<endl<<"_______________________2of3_______________________"<<endl<<endl;
for(int x=0;x<sch;x++){
f_2of3[x]=func_2of3(f, sch, x);
//cout<<"f_2of3["<<x<<"] = "<<f_2of3[x]<<endl;
}
cout<<endl<<"______________________RESULT_____________________"<<endl<<endl;
func_RESULT(f_and, f, f_2of3, sch);
return 0;
}