Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// 1807062
- #include<bits/stdc++.h>
- #include<string.h>
- using namespace std;
- #define see(x) cout<<endl<<#x<<" : "<<(x)<<endl;
- double a[15];
- double get_X(double y,double z)
- {
- return (a[4]-a[2]*y-a[3]*z)/a[1];
- }
- double get_Y(double x,double z)
- {
- return (a[8]-a[5]*x-a[7]*z)/a[6];
- }
- double get_Z(double x,double y)
- {
- return (a[12]-a[9]*x-a[10]*y)/a[11];
- }
- int index = 1;
- double get_Values(string s)
- {
- int i = 0, to = 0;
- while(s[i]!='x')i++;
- double power = 0, value = 0;
- if(s[0]=='-')to = 1;
- if(i==0)value = 1;
- for(int j = i-1;j>=to;j--)
- value += pow(10.0,power++) * (s[j]-'0');
- if(to) value = -value;
- a[index++] = value;
- i++; /// '+' or '-'
- char cy = s[i]; /// sign of the value
- while(s[i]!='y')i++;
- power = 0,value = 0;
- int j = i-1;
- if(s[j]==cy)value=1;
- while(1)
- {
- if(s[j]=='+' || s[j]=='-')break;
- value += pow(10.0,power++) * (s[j]-'0'),j--;
- }
- if(cy=='-')value = -value;
- a[index++] = value;
- i++; /// '+' or '-'
- char cz = s[i];
- while(s[i]!='z')i++;
- power = 0,value = 0;
- j = i-1;
- if(s[j]==cz)value=1;
- while(1)
- {
- if(s[j]=='+' || s[j]=='-')break;
- value += pow(10.0,power++) * (s[j]-'0'),j--;
- }
- if(cz=='-')value = -value;
- a[index++] = value;
- i++; /// '='
- to = i;
- power = 0,value = 0;
- if(s[i+1]=='-') to++;
- for(int j = s.size()-1;j>to;j--)
- value += pow(10.0,power++) * (s[j]-'0');
- if(to!=i)value = -value;
- a[index++] = value;
- }
- double Gauss_Seidel()
- {
- double X = 0, Y = 0, Z = 0;
- for(int it = 0; it< 30; it++)
- {
- printf("Iteration No. %d : ",it+1);
- X = get_X(Y,Z);
- Y = get_Y(X,Z);
- Z = get_Z(X,Y);
- printf("X = %lf, Y = %lf, Z = %lf\n",X,Y,Z);
- }
- printf("\nAnswer : \nX = %lf, Y = %lf, Z = %lf\n",X,Y,Z);
- }
- int main()
- {
- // ios::sync_with_stdio(0);
- // cin.tie(NULL);
- ifstream in("input.txt");
- string s;
- puts("The three equations are : ");
- while(in>>s)
- {
- get_Values(s);
- cout<<s<<endl;
- }
- puts("The constants are : ");
- cout<<"a1 = "<<a[1]<<endl;
- cout<<"b1 = "<<a[2]<<endl;
- cout<<"c1 = "<<a[3]<<endl;
- cout<<"d1 = "<<a[4]<<endl<<endl;
- cout<<"a2 = "<<a[5]<<endl;
- cout<<"b2 = "<<a[6]<<endl;
- cout<<"c2 = "<<a[7]<<endl;
- cout<<"d2 = "<<a[8]<<endl<<endl;
- cout<<"a3 = "<<a[9]<<endl;
- cout<<"b3 = "<<a[10]<<endl;
- cout<<"c3 = "<<a[11]<<endl;
- cout<<"d3 = "<<a[12]<<endl<<endl;
- Gauss_Seidel();
- in.close();
- return 0;
- }
- /*
- 27x+6y-z=85
- 6x+15y+z=72
- x+y+54z=110
- -7x+5y-3z=16
- -3x-5y+2z=-8
- -5x+3y-7z=0
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement