Advertisement
makispaiktis

2x2 Systems

May 11th, 2018 (edited)
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     cout << "This is a programme about solving 2x2 system with 2 variables (x and y)." << endl;
  8.     cout << "Give me the factors before x, y and the constant factor of 1st equation." << endl;
  9.     double a1, b1, c1;
  10.     double a2, b2, c2;
  11.     cin >> a1;
  12.     cin >> b1;
  13.     cin >> c1;
  14.     cout << "Now, do the same for the 2nd equation." << endl << endl;
  15.     cin >> a2;
  16.     cin >> b2;
  17.     cin >> c2;
  18.  
  19.     // Για το πώς θα γραφεί η 1η εξίσωση
  20.     if (a1>=0) {
  21.  
  22.         if(b1>=0){
  23.  
  24.             cout << a1 << "x +" << b1 << "y = " << c1 << endl;
  25.         }
  26.  
  27.         else{
  28.  
  29.             cout << a1 << "x" << b1 << "y = " << c1 << endl;
  30.         }
  31.     }
  32.     else{
  33.  
  34.        if(b1>=0){
  35.  
  36.             cout << a1 << "x +" << b1 << "y = " << c1 << endl;
  37.         }
  38.  
  39.        else{
  40.  
  41.             cout << a1 << "x" << b1 << "y = " << c1 << endl;
  42.        }
  43.     }
  44.  
  45.     // Για το πως θα γραφτεί η 2η εξίσωση
  46.  
  47.     if (a2>=0) {
  48.  
  49.         if(b2>=0){
  50.  
  51.             cout << a2 << "x +" << b2 << "y = " << c2 << endl;
  52.         }
  53.  
  54.         else{
  55.  
  56.             cout << a2 << "x" << b2 << "y = " << c2 << endl;
  57.         }
  58.     }
  59.     else{
  60.  
  61.        if(b2>=0){
  62.  
  63.             cout << a2 << "x +" << b2 << "y = " << c2 << endl;
  64.         }
  65.  
  66.        else{
  67.  
  68.             cout << a2 << "x" << b2 << "y = " << c2 << endl;
  69.        }
  70.     }
  71.  
  72.     float Dx = c1*b2 - c2*b1;
  73.     float Dy = a1*c2 - a2*c1;
  74.     float D = a1*b2 - a2*b1;
  75.  
  76.     if (D==0){
  77.  
  78.             if (c1/c2==a1/a2 && c1/c2==b1/b2){
  79.  
  80.                     cout << "This system has infinite solutions: ( x, (" << c1 << "-" << a1 << "x" << "/" << b1 << ") )" << endl;
  81.             }
  82.             else{
  83.                     cout << "No solutions. Impossible system." << endl;
  84.             }
  85.  
  86.     }
  87.  
  88.     else{
  89.             cout << "Solution: " << endl;
  90.             cout << "x = " << Dx/D << endl;
  91.             cout << "y = " << Dy/D << endl;
  92.     }
  93.  
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement