#include<iostream>
#include<cctype>
#include<cstdlib>
#include<vector>
#include<list>
#include<string>
#include<algorithm>
using namespace std;
class Variable
{
public:
Variable()
{
}
Variable(char c, int i)
{
id = c;
exp = i;
}
bool operator==(const Variable& v)const
{
return id == v.id && exp == v.exp;
}
bool operator<(const Variable& v)const
{
return id < v.id;
}
char id;
int exp;
};
class Term
{
public:
Term()
{
coeff = 0;
}
int coeff;
vector<Variable>vars;
bool operator==(const Term&)const;
bool operator!=(const Term& term)const
{
return !(*this == term);
}
bool operator<(const Term&)const;
bool operator>(const Term& term)const
{
return *this != term && (*this < term);
}
int min(int n, int m)const
{
return (n < m) ? n : m;
}
};
class Polynomial
{
public:
Polynomial()
{
}
Polynomial operator+( Polynomial&)const;
void error(char* s)
{
cerr << s << endl;
exit(1);
}
private:
list<Term>terms;
friend istream& operator>>(istream&, Polynomial&);
friend ostream& operator<<(ostream&, const Polynomial&);
};
bool Term::operator==(const Term& term)const
{
int i = 0;
for (int i = 0; i < min(vars.size(), term.vars.size()) && vars[i] == term.vars[i]; i++);
return i == vars.size() && vars.size() == term.vars.size();
}
bool Term::operator<(const Term& term2)const
{
if (vars.size() == 0)
return false;
else if (term2.vars.size() == 0)
return true;
for (int i = 0; i < min(vars.size(), term2.vars.size()); i++)
if (vars[i].id < term2.vars[i].id)
return true;
else if (term2.vars[i].id < vars[i].id)
return false;
else if (vars[i].exp < term2.vars[i].exp)
return true;
else if (term2.vars[i].exp < vars[i].exp)
return false;
return ((int)vars.size() - (int)term2.vars.size() < 0) ? true : false;
}
Polynomial Polynomial::operator+( Polynomial& polyn2)const
{
Polynomial result;
list<Term>::iterator p1, p2;
bool erased;
for (p1 = terms.begin(); p1 != terms.end(); p1++)
result.terms.push_back(*p1);
for (p1 = polyn2.terms.begin(); p1 != polyn2.terms.end(); p1++)
result.terms.push_back(*p1);
for (p1 = result.terms.begin(); p1 != result.terms.end();)
{
for (p2 = p1, p2++, erased = false; p2 != result.terms.end(); p2++)
if (*p1 == *p2)
{
p1->coeff += p2->coeff;
result.terms.erase(p2);
if (p1->coeff == 0)
result.terms.erase(p1);
erased = true;
}
if (erased)
p1 = result.terms.begin();
else p1++;
}
result.terms.sort();
return result;
}
for (p1 = terms.begin(); p1 != terms.end(); p1++)
报错信息
E0349 没有与这些操作数匹配的 "=" 运算符
C2679 二元“=”: 没有找到接受“std::_List_const_iterator<std::_List_val<std::_List_simple_types<_Ty>>>”类型的右操作数的运算符(或没有可接受的转换)
等号报错,怎么改?求指教