Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class tridiag{
- public:
- int n;
- double* a, *b, *c, *f;
- double* p, *q, *x;
- tridiag(){n = 0;}
- void calcPQ(){
- p[0] = c[0]/b[0];
- q[0] = f[0]/b[0];
- for(int i = 1; i < n; i++){
- if(i != n-1){p[i] = c[i]/(b[i] - p[i-1]*a[i-1]);}
- q[i] = (f[i] - q[i-1]*a[i-1])/(b[i] - p[i-1]*a[i-1]);
- }
- calcX();
- }
- void calcX(){
- x[n-1] = q[n-1];
- for(int i = n-2; i >= 0; i--){
- x[i] = q[i] - p[i]*x[i+1];
- }
- }
- void input(){
- cout << "input n:" << endl;
- cin >> n;
- /////////
- a = new double[n-1];
- cout << "input a:" << endl;
- for(int i = 0; i < n-1; i++){
- cin >> a[i];
- }
- /////////
- b = new double[n];
- cout << "input b:" << endl;
- for(int i = 0; i < n; i++){
- cin >> b[i];
- }
- /////////
- c = new double[n-1];
- cout << "input c:" << endl;
- for(int i = 0; i < n-1; i++){
- cin >> c[i];
- }
- /////////
- f = new double[n];
- cout << "input f:" << endl;
- for(int i = 0; i < n; i++){
- cin >> f[i];
- }
- p = new double[n-1];
- q = new double[n];
- x = new double[n];
- calcPQ();
- }
- void output(){
- for(int i = 0; i < n; i++){
- cout << x[i] << ' ';
- }
- }
- };
- int main(){
- tridiag a;
- a.input();
- a.output();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement