home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / DRMATRIX.ZIP / EXAMPLES / SOLVER / SOLVER.CPP next >
Encoding:
C/C++ Source or Header  |  1996-11-20  |  1.7 KB  |  80 lines

  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4.  
  5. #include "drmatrix.hpp"
  6.  
  7. void solve(int,int);
  8.  
  9. int main()
  10. {
  11.     unsigned neq,nvar;
  12.     clrscr();
  13.     cout << "Solver\n"
  14.          << "Demo Program for Doctor Matrix programming package\n"
  15.          << "by Zvika Ben-Haim\n"
  16.          << "Copyright (c) 1996\n\n"
  17.          << "Enter the number of equations: ";
  18.     cin  >> neq;
  19.     cout << "Enter the number of variables: ";
  20.     cin  >> nvar;
  21.     solve(neq,nvar);
  22.     return 0;
  23. }
  24.  
  25. void solve(int neq,int nvar)
  26. {
  27.  
  28.     int i,j,n;
  29.     Matrix<float> M(neq,nvar+1);
  30.     clrscr();
  31.     cout << "Enter a matrix of order " << neq << 'x' << nvar+1 << '.' << endl
  32.          << "Each row contains one equation." << endl
  33.          << "For instance, the equation a+2b+3c=1 would be enter as:" << endl
  34.          << "1 2 3 1" << endl
  35.          << "Remember to leave coefficients of 0 for equations with missing variables." << endl;
  36.     cin  >> M;
  37.     Matrix<float> R = M.canon();
  38.     for(n=neq; n>=1; n--)
  39.     {
  40.         i=R.lead(n);
  41.         if(i==nvar+1)
  42.         {
  43.             cout << "No solution.\n";
  44.             return;
  45.         }
  46.         if(i!=nvar+2) break;
  47.     }
  48.     if(n==0)
  49.     {
  50.         cout << "All possible combinations are solutions.\n";
  51.         return;
  52.     }
  53.     if(n==nvar)
  54.     {
  55.         cout << "The single solution is:\n";
  56.         for(i=1; i<=n; i++)
  57.             cout << (char)('a'+i-1) << " = " << R(i,nvar+1) << endl;
  58.         return;
  59.     }
  60.     cout << "The symbolic solution is:\n";
  61.     for(i=1; i<=n; i++)
  62.     {
  63.         j = R.lead(i);
  64.         cout << (char)('a'+j-1) << " = " << R(i,nvar+1);
  65.         for(j++; j<=nvar; j++)
  66.             if(R(i,j)!=0.)
  67.             {
  68.                 if(R(i,j)>0.)
  69.                     cout << " - ";
  70.                 else
  71.                     cout << " + ";
  72.                 if(abs(R(i,j))!=1.)
  73.                     cout << abs(R(i,j));
  74.                 cout << (char)('a'+j-1);
  75.             }
  76.         cout << endl;
  77.     }
  78.     cout << "All remaining variables can be chosen arbitrarily.\n";
  79. }
  80.