Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <map>
- #include <iostream>
- #include <vector>
- #include <complex>
- #include <functional>
- using namespace std;
- class LINP
- {
- public:
- typedef map<int/*Index*/, double/*Coeff*/, greater<int> > value_type;
- public:
- LINP(): cont() {
- cont[1] = 1;
- };
- LINP(int rhs): cont() {
- cont[0] = rhs;
- };
- LINP operator [](int rhs) const {
- LINP tmp;
- tmp.cont.clear();
- tmp.cont[rhs] = getCoeffOfIndex(1);
- return tmp;
- }
- LINP& operator *=(double rhs) {
- for (auto& v : cont)
- v.second *= rhs;
- return *this;
- }
- LINP operator *(double rhs) const {
- LINP tmp(*this);
- tmp *= rhs;
- return tmp;
- }
- LINP operator *=(const LINP& rhs) const {
- // Unimplimented
- }
- LINP operator *(const LINP& rhs) const {
- LINP tmp(*this);
- tmp *= rhs;
- return tmp;
- }
- LINP& operator +=(const LINP& rhs) {
- for (auto& v : rhs.cont)
- this->cont[v.first] += v.second;
- return *this;
- }
- LINP operator +(const LINP& rhs) const {
- LINP tmp(*this);
- tmp += rhs;
- return tmp;
- }
- LINP& operator -=(const LINP& rhs) {
- for (auto& v : rhs.cont)
- this->cont[v.first] -= v.second;
- return *this;
- }
- LINP operator -(const LINP& rhs) const {
- LINP tmp(*this);
- tmp -= rhs;
- return tmp;
- }
- LINP operator -() const {
- LINP tmp(*this);
- for (auto& v : tmp.cont)
- v.second = -v.second;
- return tmp;
- }
- 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 maxIndex() const {
- return cont.begin()->first;
- }
- int minIndex() const {
- return cont.rbegin()->first;
- }
- double getCoeffOfIndex(int Index) const {
- value_type::const_iterator it=cont.find(Index);
- return it != cont.end() ? it->second : 0;
- }
- vector<complex<double> > solve() const throw(exception) {
- if (maxIndex()>2 || minIndex()<0)
- throw std::exception();
- vector<complex<double> > result;
- result.reserve(maxIndex());
- if (maxIndex()==1)
- result.push_back(-getCoeffOfIndex(0)/getCoeffOfIndex(1));
- else // maxPow()==2
- {
- complex<double> delta=pow(getCoeffOfIndex(1), 2)-4*getCoeffOfIndex(2)*getCoeffOfIndex(0);
- result.push_back((-getCoeffOfIndex(1)+sqrt(delta))/(2*getCoeffOfIndex(2)));
- result.push_back((-getCoeffOfIndex(1)-sqrt(delta))/(2*getCoeffOfIndex(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
Advertisement