Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class tridiag{
  5. public:
  6. int n;
  7. double* a, *b, *c, *f;
  8. double* p, *q, *x;
  9.  
  10. tridiag(){n = 0;}
  11.  
  12. void calcPQ(){
  13. p[0] = c[0]/b[0];
  14. q[0] = f[0]/b[0];
  15. for(int i = 1; i < n; i++){
  16. if(i != n-1){p[i] = c[i]/(b[i] - p[i-1]*a[i-1]);}
  17. q[i] = (f[i] - q[i-1]*a[i-1])/(b[i] - p[i-1]*a[i-1]);
  18. }
  19. calcX();
  20. }
  21. void calcX(){
  22. x[n-1] = q[n-1];
  23. for(int i = n-2; i >= 0; i--){
  24. x[i] = q[i] - p[i]*x[i+1];
  25. }
  26. }
  27. void input(){
  28. cout << "input n:" << endl;
  29. cin >> n;
  30. /////////
  31. a = new double[n-1];
  32. cout << "input a:" << endl;
  33. for(int i = 0; i < n-1; i++){
  34. cin >> a[i];
  35. }
  36. /////////
  37. b = new double[n];
  38. cout << "input b:" << endl;
  39. for(int i = 0; i < n; i++){
  40. cin >> b[i];
  41. }
  42. /////////
  43. c = new double[n-1];
  44. cout << "input c:" << endl;
  45. for(int i = 0; i < n-1; i++){
  46. cin >> c[i];
  47. }
  48. /////////
  49. f = new double[n];
  50. cout << "input f:" << endl;
  51. for(int i = 0; i < n; i++){
  52. cin >> f[i];
  53. }
  54. p = new double[n-1];
  55. q = new double[n];
  56. x = new double[n];
  57. calcPQ();
  58. }
  59. void output(){
  60. for(int i = 0; i < n; i++){
  61. cout << x[i] << ' ';
  62. }
  63. }
  64. };
  65.  
  66. int main(){
  67. tridiag a;
  68. a.input();
  69. a.output();
  70. system("pause");
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement