Advertisement
Guest User

Untitled

a guest
May 30th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<fstream>
  4. using namespace std;
  5. const double eps=0.01;
  6. int Zapisvfail(double *x,int stroka,char*name)
  7. {ofstream input2(name);
  8. for(int i=0;i<stroka;i++)
  9. {input2<<x[i]<<endl;}
  10. return 1;}
  11. void Newiazka(double** mass,double*x,double*b,int stolbec,int stroka)
  12. {cout<<"Znachenie neviazki"<<endl;
  13. double sum=0;
  14. for(int i=0;i<=stolbec-1;i++)
  15.     {for(int j=0;j<=stroka;j++)
  16. {sum+=mass[i][j]*x[j];}
  17. cout<<mass[i][stroka]-sum<<endl;}
  18. }
  19. bool proverkadiagonali(double**mass,int stroka,int stolbec)
  20. {for(int i=0;i<stroka;i++)
  21. {double sum=0;
  22. for(int j=0;j<stolbec-1;j++)
  23. {if(j!=i)
  24. {sum+=mass[i][j];}}
  25. if(abs(mass[i][i])>abs(sum))
  26. return 1;
  27. return 0;}}
  28. bool proverka(double**mass,double*b,double*x,double*dx,int stroka,int stolbec)
  29. {for(int i=0;i<stroka;i++)
  30. {if(fabs(x[i]-dx[i])>eps)
  31. {return false;}}
  32. }
  33. void MZ(double**mass,double*b,double*x,double*dx,int stroka,int stolbec)
  34. {double kolvoit=0;
  35. char* name="Metod Zeydelia.txt";
  36. for(;proverka(mass,b,x,dx,stroka,stolbec)==false;)
  37. {for(int u=0;u<stroka;u++)
  38.   {dx[u]=x[u];}
  39. for(int i=0;i<stroka;i++)
  40.     {double sum=0;
  41.           for(int j=0;j<stroka;j++)
  42.             {if(j!=i)
  43.                 {sum+=x[j]*mass[i][j];}}
  44. x[i]=(mass[i][stolbec-1]-sum)/mass[i][i];
  45. }kolvoit++;}
  46. cout<<kolvoit<<endl;
  47. for (int i=0; i<=(stroka-1); i++)
  48. {cout << "x[" << i+1 <<"] = " <<x[i]<<endl;}
  49. Newiazka(mass,x,b,stolbec,stroka);
  50. Zapisvfail(x,stroka,name);
  51. return;}
  52. void MPI(double**mass,double*b,double*x,double*dx,int stroka,int stolbec)
  53. {double kolvoit=0;
  54. char* name="Metod prostih iteraciy.txt";
  55. for(;proverka(mass,b,x,dx,stroka,stolbec)==false;)
  56. {for(int u=0;u<stroka;u++)
  57.   {dx[u]=x[u];}
  58. for(int i=0;i<stroka;i++)
  59.     {double sum=0;
  60.        
  61.           for(int j=0;j<stroka;j++)
  62.             {if(j!=i)
  63.                 {sum+=dx[j]*mass[i][j];}}
  64. x[i]=(mass[i][stolbec-1]-sum)/mass[i][i];
  65. }kolvoit++;}
  66. cout<<kolvoit<<endl;
  67. for (int i=0; i<=(stroka-1); i++)
  68. {cout << "x[" << i+1 <<"] = " <<x[i]<<endl;}
  69. Newiazka(mass,x,b,stolbec,stroka);
  70. Zapisvfail(x,stroka,name);
  71. return;}
  72. int main()
  73. {ifstream input;
  74. input.open("MATRICA.txt");
  75. int i,j,stroka,stolbec;
  76. input>>stroka>>stolbec;
  77. double **mass= new double *[stroka];
  78. for (i = 0; i < stroka; i++)
  79. {mass[i] = new double [stolbec];
  80. for (j = 0; j < stolbec; j++)
  81. input>>mass[i][j];}
  82. input.close();
  83. cout << "Matrix in the file: " << endl;
  84. for (i = 0; i < stroka; i++)
  85. {for (j = 0; j < stolbec; j++)
  86. cout << setw(13) <<mass[i][j];
  87. cout << endl;}
  88. if(proverkadiagonali(mass,stroka,stolbec)==false)
  89. {cout<<"Otsutstvuiet diagonal'noe preobladanie"<<endl;
  90. system("pause");
  91. return 0;}
  92. double *x=new double[stroka];
  93. double *dx=new double[stroka];
  94. for(int k=0;k<stroka;k++)
  95. {x[k]=0;}
  96. double*b;
  97. b=new double[stroka];
  98. for(int n = 0; n <stroka; n++)
  99. {b[n] = mass[n][stroka];}
  100. cout<<"Metod Zeydelia"<<endl;
  101. MZ(mass,b,x,dx,stroka,stolbec);
  102. cout<<"Metod Prostih uteraciy"<<endl;
  103. MPI(mass,b,x,dx,stroka,stolbec);
  104. system("pause");
  105. return 0;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement