Advertisement
zhangsongcui

LINP V1,1

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