Write your question here.
How to modify the addterm(), polynomial add()?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include "linkedListType.h"
class term
{
friend ostream& operator<<(ostream& osObject, term& t);
private:
double coef;
int exp;
public:
term(){
coef = 0;
exp =0;}
term(double c, int e){
coef =c;
exp =e;}
~term(){}
void setCoef(double c){coef = c;}
void setExp(int e){exp = e;}
// double getCoef(){return coef;}
// int getExp(){return exp;}
term(const term& other){
coef =other.coef;
exp = other.exp;}
// precondition: exp == other.exp
// the operation for *this + other
term operator+ (const term& other){
term add;
add.coef = coef + other.coef;
add.exp = other.exp;
return add;
}
// precondition: exp == other.exp
// operation for *this - other
term operator- (const term& other){
term sub;
sub.coef = coef + other.coef;
sub.exp = other.exp;
return sub;
}
bool operator!= (const term& other){
if(exp != other.exp)
return true;
else
return false;
}
bool operator== (const term& other){
if (exp = other.exp)
return true;
else
return false;
}
//print a term
void print(){
cout<<coef<<" x^ "<<exp;
}
};
ostream& operator<<(ostream& osObject, term& t)
{
osObject << "(" << t.coef << ", " << t.exp << ") ";
return osObject;
}
//###########################################################
class polynomial
{
private:
linkedListType<term> *poly;
public:
polynomial(){ //default constructor
poly = NULL;
}
~polynomial(){}
polynomial(const polynomial &other){ //copy constructor
poly = other.poly;
}
const polynomial& operator= (const polynomial& other){
if(this != &other)
polynomial(other);
return *this;
}
//append a term to the polynomial
void addTerm(term item){
}
//print the polynomial
void print(){
cout<< endl;
cout<<" + ";
}
//return a new polynomial that is equal to *this + other
//the contents of this and other should not be modified
polynomial& add(polynomial& other){
return *this;
}
//return a new polynomial that is equal to *this - other
//the contents of this and other should not be modified
polynomial& sub(polynomial& other){
return *this;
}
};
#endif
|
ANOTHER CODE
#include "polynomial.h"
#include "linkedListType.h"
using namespace std;
void readPolynomial(string filename, polynomial& p)
{}
int main()
{
string file1 = "poly1.txt";
string file2 = "poly2.txt";
polynomial p1;
readPolynomial(file1, p1);
cout << "P1 : ";
p1.print();
cout << endl;
polynomial p2;
readPolynomial(file2, p2);
cout << "P2 : ";
p2.print();
cout << endl;
cout << "P3 = P1 + P2 : ";
polynomial p3 = p1.add(p2);
p3.print();
cout << endl;
cout << "P4 = P1 - P2 : ";
polynomial p4 = p1.sub(p2);
p4.print();
cout << endl;
cout << "P5 = P3 + P1 : ";
polynomial p5 = p3.add(p1);
p5.print();
cout << endl;
system("pause");
exit(0);
}