Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define pb push_back
- vector<vector<double> > getSubMatrix(int c, vector< vector<double> > mat)
- {
- vector< vector<double> > submat;
- int N = mat.size();
- for(int i=1; i<N; i++)
- {
- vector<double> row;
- for(int j=0; j<N; j++)
- {
- if(j != c)
- {
- row.pb(mat[i][j]);
- }
- }
- submat.pb(row);
- }
- return submat;
- }
- double det(vector<vector<double> > mat)
- {
- double ret = 0;
- int N = mat.size();
- if(N == 1)
- {
- return mat[0][0];
- }
- else
- {
- for(int c=0; c<N; c++)
- {
- ret += pow(-1.0, c) * mat[0][c] * det(getSubMatrix(c, mat));
- }
- }
- return ret;
- }
- struct Solver{
- vector<string>generalEquation;
- void solve( vector<string> ge, vector<vector<double> > mat)
- {
- int N = mat.size();
- for(int c = 0; c<N; c++)
- {
- double d = det(getSubMatrix(c, mat));
- if( (c%2) == 1)
- {
- d = -d;
- }
- if(d > 0.0)
- {
- cout<<"+";
- }
- if(abs(d) > 0.0)
- {
- cout<<d<<ge[c];
- }
- }
- cout<<" = 0"<<endl;
- }
- };
- struct Circle{
- vector<double> row(double x, double y)
- {
- vector<double>ret;
- ret.pb(x*x + y*y);
- ret.pb(x);
- ret.pb(y);
- ret.pb(1);
- return ret;
- }
- void run()
- {
- vector<string>ge({"(x^2 + y^2)", "x", "y", ""});
- int N = ge.size();
- vector< vector<double> > mat;
- vector<double>tmp;
- for(int i=0; i<N; i++)
- {
- tmp.pb(1.0);
- }
- mat.pb(tmp);
- cout<<"Please input 3 points on the circle (x, y): "<<endl;
- double x, y;
- for(int i=0; i<3; i++)
- {
- cin>>x>>y;
- mat.pb(row(x, y));
- }
- cout<<"Equation: ";
- Solver solver;
- solver.solve(ge, mat);
- }
- };
- struct Plane{
- vector<double> row(double x, double y, double z)
- {
- vector<double>ret;
- ret.pb(x);
- ret.pb(y);
- ret.pb(z);
- ret.pb(1);
- return ret;
- }
- void run()
- {
- vector<string>ge({"x", "y", "z", ""});
- int N = ge.size();
- vector< vector<double> > mat;
- vector<double>tmp;
- for(int i=0; i<N; i++)
- {
- tmp.pb(1.0);
- }
- mat.pb(tmp);
- cout<<"Please input 3 points on the plane (x, y, z): "<<endl;
- double x, y, z;
- for(int i=0; i<3; i++)
- {
- cin>>x>>y>>z;
- mat.pb(row(x, y, z));
- }
- cout<<"Equation: ";
- Solver solver;
- solver.solve(ge, mat);
- }
- };
- struct Parabola{
- vector<double> row(double x, double y)
- {
- vector<double>ret;
- ret.pb(y);
- ret.pb(x*x);
- ret.pb(x);
- ret.pb(1);
- return ret;
- }
- void run()
- {
- vector<string>ge({"y", "x^2", "x", ""});
- int N = ge.size();
- vector< vector<double> > mat;
- vector<double>tmp;
- for(int i=0; i<N; i++)
- {
- tmp.pb(1.0);
- }
- mat.pb(tmp);
- cout<<"Please input 3 points on the parabola: (x, y) "<<endl;
- double x, y;
- for(int i=0; i<3; i++)
- {
- cin>>x>>y;
- mat.pb(row(x, y));
- }
- cout<<"Determinant Equation:"<<endl;
- cout<<"|\ty\tx^2\tx\t1\t|"<<endl;
- for(int i=1; i<=3; i++)
- {
- cout<<"|\t";
- for(int j=0; j<N; j++)
- {
- cout<<mat[i][j]<<"\t";
- }
- cout<<"|";
- if(i == 1)
- {
- cout<<"\t=\t0";
- }
- cout<<endl;
- }
- cout<<"And Equation of the Parabola"<<endl;
- Solver solver;
- solver.solve(ge, mat);
- }
- };
- int main()
- {
- int option = -1;
- while(true)
- {
- if(option != -1) cout<<endl;
- cout<<"1: Circle"<<endl<<"2: Plane"<<endl<<"3: Parabola"<<endl<<"0: Exit"<<endl;
- cin>>option;
- if(option == 1)
- {
- Circle c;
- c.run();
- }
- else if(option == 2)
- {
- Plane p;
- p.run();
- }
- else if(option == 3)
- {
- Parabola parabola;
- parabola.run();
- }
- else if(option == 0)
- {
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement