Advertisement
hasib_mo

MAT 125 - Assignment

Mar 20th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.38 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define pb push_back
  5.  
  6.  
  7. vector<vector<double> > getSubMatrix(int c, vector< vector<double> > mat)
  8. {
  9.         vector< vector<double> > submat;
  10.         int N = mat.size();
  11.  
  12.         for(int i=1; i<N; i++)
  13.         {
  14.                 vector<double> row;
  15.  
  16.                 for(int j=0; j<N; j++)
  17.                 {
  18.                         if(j != c)
  19.                         {
  20.                                 row.pb(mat[i][j]);
  21.                         }
  22.                 }
  23.                 submat.pb(row);
  24.         }
  25.         return submat;
  26. }
  27.  
  28.  
  29. double det(vector<vector<double> > mat)
  30. {
  31.         double ret = 0;
  32.         int N = mat.size();
  33.  
  34.         if(N == 1)
  35.         {
  36.                 return mat[0][0];
  37.         }
  38.         else
  39.         {
  40.                 for(int c=0; c<N; c++)
  41.                 {
  42.                         ret += pow(-1.0, c) * mat[0][c] * det(getSubMatrix(c, mat));
  43.                 }
  44.         }
  45.  
  46.         return ret;
  47. }
  48.  
  49. struct Solver{
  50.  
  51.         vector<string>generalEquation;
  52.  
  53.         void solve( vector<string> ge, vector<vector<double> > mat)
  54.         {
  55.                 int N = mat.size();
  56.                
  57.                 for(int c = 0; c<N; c++)
  58.                 {
  59.                         double d = det(getSubMatrix(c, mat));
  60.  
  61.                         if( (c%2) == 1)
  62.                         {
  63.                                 d = -d;
  64.                         }
  65.  
  66.                         if(d > 0.0)
  67.                         {
  68.                                 cout<<"+";
  69.                         }
  70.  
  71.                         if(abs(d) > 0.0)
  72.                         {
  73.                                 cout<<d<<ge[c];
  74.  
  75.                         }
  76.  
  77.                 }
  78.  
  79.                 cout<<" = 0"<<endl;
  80.         }
  81.  
  82. };
  83.  
  84.  
  85.  
  86. struct Circle{
  87.  
  88.         vector<double> row(double x, double y)
  89.         {
  90.                 vector<double>ret;
  91.  
  92.                 ret.pb(x*x + y*y);
  93.                 ret.pb(x);
  94.                 ret.pb(y);
  95.                 ret.pb(1);
  96.  
  97.                 return ret;
  98.  
  99.         }
  100.  
  101.         void run()
  102.         {
  103.                 vector<string>ge({"(x^2 + y^2)", "x", "y", ""});
  104.                 int N = ge.size();
  105.  
  106.                 vector< vector<double> > mat;
  107.                
  108.                 vector<double>tmp;
  109.  
  110.                 for(int i=0; i<N; i++)
  111.                 {
  112.                         tmp.pb(1.0);
  113.                 }
  114.  
  115.                 mat.pb(tmp);
  116.  
  117.                 cout<<"Please input 3 points on the circle (x, y): "<<endl;
  118.  
  119.                 double x, y;
  120.  
  121.                 for(int i=0; i<3; i++)
  122.                 {
  123.                         cin>>x>>y;
  124.  
  125.                         mat.pb(row(x, y));
  126.                 }
  127.  
  128.  
  129.                 cout<<"Equation: ";
  130.  
  131.  
  132.                 Solver solver;
  133.  
  134.                 solver.solve(ge, mat);
  135.         }
  136.  
  137. };
  138.  
  139.  
  140. struct Plane{
  141.  
  142.         vector<double> row(double x, double y, double z)
  143.         {
  144.                 vector<double>ret;
  145.  
  146.                 ret.pb(x);
  147.                 ret.pb(y);
  148.                 ret.pb(z);
  149.                 ret.pb(1);
  150.  
  151.                 return ret;
  152.  
  153.         }
  154.  
  155.         void run()
  156.         {
  157.                 vector<string>ge({"x", "y", "z", ""});
  158.                 int N = ge.size();
  159.  
  160.                 vector< vector<double> > mat;
  161.                
  162.                 vector<double>tmp;
  163.  
  164.                 for(int i=0; i<N; i++)
  165.                 {
  166.                         tmp.pb(1.0);
  167.                 }
  168.  
  169.                 mat.pb(tmp);
  170.  
  171.                 cout<<"Please input 3 points on the plane (x, y, z): "<<endl;
  172.  
  173.                 double x, y, z;
  174.  
  175.                 for(int i=0; i<3; i++)
  176.                 {
  177.                         cin>>x>>y>>z;
  178.  
  179.                         mat.pb(row(x, y, z));
  180.                 }
  181.  
  182.  
  183.                 cout<<"Equation: ";
  184.  
  185.  
  186.                 Solver solver;
  187.  
  188.                 solver.solve(ge, mat);
  189.         }
  190.  
  191. };
  192.  
  193.  
  194. struct Parabola{
  195.  
  196.         vector<double> row(double x, double y)
  197.         {
  198.                 vector<double>ret;
  199.                
  200.                 ret.pb(y);
  201.                 ret.pb(x*x);
  202.                 ret.pb(x);
  203.                 ret.pb(1);
  204.  
  205.                 return ret;
  206.  
  207.         }
  208.  
  209.         void run()
  210.         {
  211.                 vector<string>ge({"y", "x^2", "x", ""});
  212.                 int N = ge.size();
  213.  
  214.                 vector< vector<double> > mat;
  215.                
  216.                 vector<double>tmp;
  217.  
  218.                 for(int i=0; i<N; i++)
  219.                 {
  220.                         tmp.pb(1.0);
  221.                 }
  222.  
  223.                 mat.pb(tmp);
  224.  
  225.                 cout<<"Please input 3 points on the parabola: (x, y) "<<endl;
  226.  
  227.                 double x, y;
  228.  
  229.                 for(int i=0; i<3; i++)
  230.                 {
  231.                         cin>>x>>y;
  232.  
  233.                         mat.pb(row(x, y));
  234.                 }
  235.                
  236.                 cout<<"Determinant Equation:"<<endl;
  237.                 cout<<"|\ty\tx^2\tx\t1\t|"<<endl;
  238.  
  239.                 for(int i=1; i<=3; i++)
  240.                 {
  241.                         cout<<"|\t";
  242.                         for(int j=0; j<N; j++)
  243.                         {
  244.                                 cout<<mat[i][j]<<"\t";
  245.                         }
  246.                         cout<<"|";
  247.  
  248.                         if(i == 1)
  249.                         {
  250.                                 cout<<"\t=\t0";
  251.                         }
  252.                         cout<<endl;
  253.                 }
  254.  
  255.                 cout<<"And Equation of the Parabola"<<endl;
  256.                 Solver solver;
  257.                 solver.solve(ge, mat);
  258.         }
  259. };
  260.  
  261.  
  262. int main()
  263. {
  264.         int option = -1;
  265.         while(true)
  266.         {
  267.                 if(option != -1) cout<<endl;
  268.  
  269.                 cout<<"1: Circle"<<endl<<"2: Plane"<<endl<<"3: Parabola"<<endl<<"0: Exit"<<endl;
  270.  
  271.                 cin>>option;
  272.                 if(option == 1)
  273.                 {
  274.                         Circle c;
  275.                         c.run();
  276.                 }
  277.                 else if(option == 2)
  278.                 {
  279.                         Plane p;
  280.                         p.run();
  281.                 }
  282.                 else if(option == 3)
  283.                 {
  284.                         Parabola parabola;
  285.                         parabola.run();
  286.                 }
  287.                 else if(option == 0)
  288.                 {
  289.                         break;
  290.                 }
  291.         }
  292.  
  293.  
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement