Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- void seperateCoefficient(string eqn, int a[])
- {
- bool isNegative = false;
- int i, sum = 0;
- //ax + by + cz
- for(i = 0; eqn[i] != '='; i++){
- if(eqn[i] == '-')
- isNegative = true;
- else if(eqn[i] == '+')
- isNegative = false;
- else if(eqn[i] >= '0' && eqn[i] <= '9'){
- sum *= 10;
- sum += eqn[i] - '0';
- }
- else if(eqn[i] == 'x' || eqn[i] == 'X'){
- if(isNegative)
- sum = -sum;
- a[0] = sum;
- sum = 0;
- }
- else if(eqn[i] == 'y' || eqn[i] == 'Y'){
- if(isNegative)
- sum = -sum;
- a[1] = sum;
- sum = 0;
- }
- else if(eqn[i] == 'z' || eqn[i] == 'Z'){
- if(isNegative)
- sum = -sum;
- a[2] = sum;
- sum = 0;
- }
- }
- // = const
- isNegative = false;
- for(; i < eqn.length(); i++){
- if(eqn[i] == '-')
- isNegative = true;
- else if(eqn[i] >= '0' && eqn[i] <= '9'){
- sum *= 10;
- sum += eqn[i] - '0';
- }
- }
- if(isNegative)
- sum = -sum;
- a[3] = sum;
- }
- int main()
- {
- int a[3][4];
- string eqn;
- for(int i = 0; i < 3; i++){
- getline(cin, eqn);
- seperateCoefficient(eqn, a[i]);
- }
- //printing
- cout << endl << "Co-efficients: " << endl;
- for(int i = 0; i < 3; i++){
- for(int j = 0; j < 4; j++){
- cout << a[i][j] << " ";
- }
- cout << endl;
- }
- double x = 0, y = 0, z = 0;
- for(int i = 0; i < 20; i++){
- x = (a[0][3] - a[0][1]*y - a[0][2]*z) / a[0][0];
- y = (a[1][3] - a[1][0]*x - a[1][2]*z) / a[1][1];
- z = (a[2][3] - a[2][0]*x - a[2][1]*y) / a[2][2];
- }
- cout << endl << "Solution:" << endl;
- cout << "x = " << x << endl;
- cout << "y = " << y << endl;
- cout << "z = " << z << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement