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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/*
The project include this file:
Term.h - definition and implemantion of template class Term
Polynomial.h - definition and implemantion of template class Polynomial
main.cpp - Main of the program
*/
//---------------- Term.h ----------------
#ifndef TERM_H_INCLUDED
#define TERM_H_INCLUDED
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <fstream>
#include <sstream>
#include <math.h>
#include <string>
using namespace std;
#define ALL 7
#define COEFF 1
#define EXP 2
#define VAR 4
template <class T>
class Term{
private:
int exp;
T base;
char lit;
bool Compatible(const Term&,int op)const;
public:
bool Same(const Term&,int op=ALL)const;
//---- Costructors ----
Term();
Term(T b,int e,char c);
Term(const Term&);
//---- operators ----
Term& operator = (const Term&);
Term& operator += (const Term&);
Term& operator -= (const Term&);
Term& operator *= (const Term&);
Term& operator /= (const Term&);
bool operator == (const Term& t)const{
return Same(t,ALL);}
bool operator != (const Term& t)const{
return !Same(t,ALL);}
template<class N>friend ostream& operator << (ostream&,const Term<N>&);
//---- Getter & setter ----
T Coeff()const{return base;}
void Coeff(T t){base=t;}
int Exp()const{return exp;}
void Exp(int e){exp=e;}
char Variable()const{return lit;}
void Variable(char c){lit=c;}
};
//---- Implementation ----
template<class T>
bool Term<T>::Compatible(const Term<T>& t, int op)const{
switch(op){
case 0:
case 1:return (lit==t.lit && exp==t.exp);
case 2:
case 3: return (lit==t.lit || lit=='\0' || t.lit=='\0');
}
return false;
}
template<class T>
bool Term<T>::Same(const Term& t,int op)const{
bool result=true;
if (op && COEFF)result &= (base==t.base);
if (op && EXP)result &= (exp==t.exp);
if (op && VAR)result &= (lit==t.lit);
return result;
}
//----Costructors ----
template<class T>
Term<T>::Term():exp(0),base(0),lit('\0'){};
template<class T>
Term<T>::Term(T b,int e,char c){
lit=c;
base=b;
exp=e;
}
template<class T>
Term<T>::Term(const Term& t){
lit=t.lit;
base=t.base;
exp=t.exp;
}
//---- operators ----
template<class T>
Term<T>& Term<T>::operator = (const Term<T>& t){
if(this == &t)return *this;
lit=t.lit;
base=t.base;
exp=t.exp;
return *this;
}
template<class T>
Term<T>& Term<T>::operator += (const Term<T>& t){
if (!Compatible(t,0))return *this;
base+=t.base;
return *this;
}
template<class T>
Term<T>& Term<T>::operator -= (const Term<T>& t){
if (!Compatible(t,1))return *this;
base-=t.base;
return *this;
}
template<class T>
Term<T>& Term<T>::operator *= (const Term<T>& t){
if (!Compatible(t,2))return *this;
base*=t.base;
exp+=t.exp;
return *this;
}
template<class T>
Term<T>& Term<T>::operator /= (const Term<T>& t){
if (!Compatible(t,3))return *this;
base/=t.base;
exp-=t.exp;
return *this;
}
template<class T>
ostream& operator << (ostream& out,const Term<T> &t){
out<<t.Coeff()<<""<<t.Variable()<<"^"<<t.Exp();
return out;
}
#endif // TERM_H_INCLUDED
//---------------- Polynomial.h ----------------
#ifndef POLYNOMIAL_H_INCLUDED
#define POLYNOMIAL_H_INCLUDED
#include "Term.h"
using namespace std;
//template<class T>
//class Polynomial; //forward declaration
//template<class T>
//istream& operator>>(istream& in,Polynomial<T>& obj);//forward declaration
//template<class T>
//ostream& operator <<(ostream& out,const Polynomial<T>& obj);
//---------------------------------------------------------------------------
//class of Term that has two variables of type T
//--------------------------------------------------------
//class of polynomial that will contain a variable of term
template<class T>
class Polynomial{
private:
Term<T>* arrOfTerm;
int num;
Polynomial& Swap(Polynomial&);
public:
Polynomial();//will intialize a polynomial form coeffi 1 and exp1
Polynomial(const Polynomial& obj);//copy constructor
Polynomial(const Term<T> a[],int size);//the poly will be init by an array of
Polynomial(const T[],int,char);
//term which contains coeffi and exp and size is the size of array of term
Polynomial(string);//a poly will be given in string form
~Polynomial();
// String, UnString
string toString()const; // --> added const
void pasrePolynomial(string);
//methods
Term<T> XthTerm()const; //added
void XthTerm(int x);//done
void showXthTerm(int x)const;//done --> added const
T evaluate();//done
//input and display functions
//void getInput();//done
//static Polynomial input();//done
void display(ostream& out=std::cout)const;//done
int NumOfTerms()const{return num;}
//operators =, +=, -=, +, -, ==, !=, <<, []
Polynomial& operator = (const Polynomial& obj);//done
Polynomial& operator +=(const Polynomial& obj);
Polynomial& operator -=(const Polynomial& obj);
//Polynomial& operator *=(const Polynomial& obj);
Polynomial operator + (const Polynomial&);
Polynomial operator - (const Polynomial&);
bool operator ==(const Polynomial& obj)const;//done
bool operator !=(const Polynomial& obj)const;//done
//template<class N> friend istream& operator >> (istream& in,Polynomial<N>& obj);//done
template<class N> friend ostream& operator << (ostream& out,const Polynomial<N>& obj);//done
const Term<T> & operator [] (int x)const;//void operator [](int index);//done
Term<T> & operator[] (int x); //new added
};
|