Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <map>
- #include <iostream>
- #include <vector>
- #include <complex>
- using namespace std;
- class LINP
- {
- public:
- typedef map<int/*pow*/, double/*num*/, greater<int> > value_type;
- public:
- LINP(): cont()
- {
- cont[1] = 1;
- };
- LINP(int rhs): cont()
- {
- cont[0] = rhs;
- };
- LINP operator [](int rhs) const//FIXME: Use operator *() to do this
- {
- LINP tmp;
- tmp.cont.clear();
- tmp.cont[rhs] = getNumOfPow(1);
- return tmp;
- }
- LINP operator *(double rhs) const
- {
- LINP tmp(*this);
- for (value_type::iterator it=tmp.cont.begin(); it!=tmp.cont.end(); ++it)
- it->second *= rhs;
- return tmp;
- }
- LINP operator +(const LINP& rhs) const
- {
- LINP tmp(*this);
- for (value_type::const_iterator it=rhs.cont.begin(); it!=rhs.cont.end(); ++it)
- tmp.cont[it->first] += it->second;
- return tmp;
- }
- LINP operator -() const
- {
- LINP tmp(*this);
- for (value_type::iterator it=tmp.cont.begin(); it!=tmp.cont.end(); ++it)
- it->second=-it->second;
- return tmp;
- }
- LINP operator -(const LINP& rhs) const
- {
- return *this+(-rhs);
- }
- friend ostream& operator <<(ostream& os, const LINP& rhs)
- {
- for (value_type::const_iterator it=rhs.cont.begin();
- it!=rhs.cont.end(); ++it)
- {
- if (it->second != 0)
- {
- if ((it->second == 1 || it->second == -1) && it->first != 0)
- {
- if (it->second < 0)
- cout << '-';
- }
- else
- os << it->second;
- if (it->first != 0)
- {
- cout << "x";
- if (it->first != 1)
- cout << '[' << it->first << ']';
- }
- }
- os << '+';
- }
- cout << "\b \b";
- return os;
- }
- int maxPow() const
- {
- return cont.begin()->first;
- }
- int minPow() const
- {
- return cont.rbegin()->first;
- }
- double getNumOfPow(int Pow) const
- {
- value_type::const_iterator it=cont.find(Pow);
- return it!=cont.end()? it->second : 0;
- }
- vector<complex<double> > solve() const throw(exception) //Solve: *this=0
- {
- if (maxPow()>2 || minPow()<0)
- throw std::exception();
- vector<complex<double> > result;
- result.reserve(maxPow());
- if (maxPow()==1)
- result.push_back(-getNumOfPow(0)/getNumOfPow(1));
- else // maxPow()==2
- {
- complex<double> delta=pow(getNumOfPow(1), 2)-4*getNumOfPow(2)*getNumOfPow(0);
- result.push_back((-getNumOfPow(1)+sqrt(delta))/(2*getNumOfPow(2)));
- result.push_back((-getNumOfPow(1)-sqrt(delta))/(2*getNumOfPow(2)));
- }
- return result;
- }
- private:
- value_type cont;
- };
- int main()
- {
- LINP linp=LINP()-(LINP()*0.5+1)-1;
- cout << linp << " = 0" << endl;
- cout << "X = " << linp.solve().front().real() << endl << endl;
- LINP linp2=LINP()[2]+LINP()*2+1;
- cout << linp2 << " = 0" << endl;
- cout << "X1 = X2 = " << linp2.solve().front().real() << endl << endl;
- LINP linp3=LINP()[2]+1;
- cout << linp3 << " = 0" << endl;
- vector<complex<double> > result=linp3.solve();
- cout << "x1 = " << result[0].real() << " + " << result[0].imag() << 'i' << endl
- << "x2 = " << result[1].real() << " + " << result[1].imag() << 'i' << endl << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment