zhangsongcui

LINP

Jul 4th, 2011
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include <map>
  2. #include <iostream>
  3. #include <vector>
  4. #include <complex>
  5.  
  6. using namespace std;
  7. class LINP
  8. {
  9. public:
  10.     typedef map<int/*pow*/, double/*num*/, greater<int> > value_type;
  11.  
  12. public:
  13.     LINP(): cont()
  14.     {
  15.         cont[1] = 1;
  16.     };
  17.     LINP(int rhs): cont()
  18.     {
  19.         cont[0] = rhs;
  20.     };
  21.     LINP operator [](int rhs) const//FIXME: Use operator *() to do this
  22.     {
  23.         LINP tmp;
  24.         tmp.cont.clear();
  25.         tmp.cont[rhs] = getNumOfPow(1);
  26.         return tmp;
  27.     }
  28.     LINP operator *(double rhs) const
  29.     {
  30.         LINP tmp(*this);
  31.         for (value_type::iterator it=tmp.cont.begin(); it!=tmp.cont.end(); ++it)
  32.             it->second *= rhs;
  33.         return tmp;
  34.     }
  35.     LINP operator +(const LINP& rhs) const
  36.     {
  37.         LINP tmp(*this);
  38.         for (value_type::const_iterator it=rhs.cont.begin(); it!=rhs.cont.end(); ++it)
  39.             tmp.cont[it->first] += it->second;
  40.         return tmp;
  41.     }
  42.     LINP operator -() const
  43.     {
  44.         LINP tmp(*this);
  45.         for (value_type::iterator it=tmp.cont.begin(); it!=tmp.cont.end(); ++it)
  46.             it->second=-it->second;
  47.         return tmp;
  48.     }
  49.     LINP operator -(const LINP& rhs) const
  50.     {
  51.         return *this+(-rhs);
  52.     }
  53.     friend ostream& operator <<(ostream& os, const LINP& rhs)
  54.     {
  55.         for (value_type::const_iterator it=rhs.cont.begin();
  56.             it!=rhs.cont.end(); ++it)
  57.         {
  58.             if (it->second != 0)
  59.             {
  60.                 if ((it->second == 1 || it->second == -1) && it->first != 0)
  61.                 {
  62.                     if (it->second < 0)
  63.                         cout << '-';
  64.                 }
  65.                 else
  66.                     os << it->second;
  67.                 if (it->first != 0)
  68.                 {
  69.                     cout << "x";
  70.                     if (it->first != 1)
  71.                         cout << '[' << it->first << ']';
  72.                 }
  73.             }
  74.             os << '+';
  75.         }
  76.         cout << "\b \b";
  77.         return os;
  78.     }
  79.     int maxPow() const
  80.     {
  81.         return cont.begin()->first;
  82.     }
  83.     int minPow() const
  84.     {
  85.         return cont.rbegin()->first;
  86.     }
  87.     double getNumOfPow(int Pow) const
  88.     {
  89.         value_type::const_iterator it=cont.find(Pow);
  90.         return it!=cont.end()? it->second : 0;
  91.     }
  92.     vector<complex<double> > solve() const throw(exception) //Solve: *this=0
  93.     {
  94.         if (maxPow()>2 || minPow()<0)
  95.             throw std::exception();
  96.         vector<complex<double> > result;
  97.         result.reserve(maxPow());
  98.         if (maxPow()==1)
  99.             result.push_back(-getNumOfPow(0)/getNumOfPow(1));
  100.         else    // maxPow()==2
  101.         {
  102.             complex<double> delta=pow(getNumOfPow(1), 2)-4*getNumOfPow(2)*getNumOfPow(0);
  103.             result.push_back((-getNumOfPow(1)+sqrt(delta))/(2*getNumOfPow(2)));
  104.             result.push_back((-getNumOfPow(1)-sqrt(delta))/(2*getNumOfPow(2)));
  105.         }
  106.         return result;
  107.     }
  108.  
  109. private:
  110.     value_type cont;
  111. };
  112.  
  113. int main()
  114. {
  115.     LINP linp=LINP()-(LINP()*0.5+1)-1;
  116.     cout << linp << " = 0" << endl;
  117.     cout << "X = " << linp.solve().front().real() << endl << endl;
  118.  
  119.     LINP linp2=LINP()[2]+LINP()*2+1;
  120.     cout << linp2 << " = 0" << endl;
  121.     cout << "X1 = X2 = " << linp2.solve().front().real() << endl << endl;
  122.  
  123.     LINP linp3=LINP()[2]+1;
  124.     cout << linp3 << " = 0" << endl;
  125.     vector<complex<double> > result=linp3.solve();
  126.     cout << "x1 = " << result[0].real() << " + " << result[0].imag() << 'i' << endl
  127.         << "x2 = " << result[1].real() << " + " << result[1].imag() << 'i' << endl << endl;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment