123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
class Pol{ public: Pol(){size=0;pointer=new int[MAX];} Pol(const Pol& t){ size=t.size; pointer=new int[size]; for(int i=0;i<size;i++) *(pointer+i)=*(t.pointer+i); } ~Pol(){ delete[]pointer; } int get_longer(const int a, const int b){ if(a>=b)return a; else return b; } int get_shorter(const int a, const int b){ if(a<=b)return a; else return b; } Pol& operator=(Pol& t){ size=t.size; pointer=new int[size]; for(int i=0;i<size;i++) *(pointer)=*(t.pointer); return *this; } Pol operator +(Pol& t){ Pol sum; int shortest=get_shorter(t.size,size); int longest=get_longer(t.size,size); int difference=longest-shortest; int i; if(t.size<size){ for(i=shortest;i<=difference+shortest;i++) t.pointer[i]=0; } else{ for(i=shortest;i<=difference+shortest;i++) pointer[i]=0; } sum.pointer=new int[longest]; sum.size=longest; for(i=0;i<sum.size;i++) *(sum.pointer+i)=*(pointer+i)+*(t.pointer+i); return sum; } Pol operator -(Pol& t){ Pol sub; int shortest=get_shorter(t.size,size); int longest=get_longer(t.size,size); int difference=longest-shortest; int i; if(t.size<size){ for(i=shortest;i<=difference+shortest;i++) t.pointer[i]=0; } else{ for(i=shortest;i<=difference+shortest;i++) pointer[i]=0; } sub.pointer=new int[longest]; sub.size=longest; for(i=0;i<sub.size;i++) *(sub.pointer+i)=*(pointer+i)-*(t.pointer+i); return sub; } Pol operator *(Pol& t){//problem here//gives extra nonsense term Pol solution; solution.size=t.size+size; solution.pointer=new int[solution.size-1]; for(int i=0;i<t.size;i++) for(int j=0;j<size;j++) solution.pointer[i+j]=+(pointer[j]*t.pointer[i]); return solution; } friend istream& operator>>(istream& in, Pol& t); friend ostream& operator<<(ostream& out, Pol& t); private: int *pointer; int size; }; int main(){ Pol test, test1; cout<<"Enter first polynomial\n"; cin>>test; cout<<"\nEnter second polynomial\n"; cin>>test1; cout<<"\n\n"<<static_cast<char>(ALPHA)<<"(X)= "<<test<<endl; cout<<static_cast<char>(BETA)<<"(X)= "<<test1; cout<<"\n\nAddition\n"; Pol addition=test+test1; cout<<static_cast<char>(THETA)<<"(X)= "<<addition; cout<<"\n\Subtraction\n"; Pol subtraction=test-test1; cout<<static_cast<char>(OMEGA)<<"(X)= "<<subtraction; cout<<"\n\Multiplication\n"; Pol multiplcation=test*test1; cout<<static_cast<char>(GAMMA)<<"(X)= "<<multiplcation; _getch(); return 0; } ostream& operator<<(ostream& out, Pol& t){ for(int i=0;i<t.size;i++){ if(*(t.pointer+i)>=0 && i!=0) out<<"+("<<*(t.pointer+i)<<"X"<<"^"<<i<<")"; else out<<"-("<<abs(*(t.pointer+i))<<"X"<<"^"<<i<<")"; } return out; } istream& operator>>(istream& in, Pol& t){ char input[MAX]; bool correct; int number; do{ cout<<"Enter "<<t.size<<" co-efficient: "; in>>input; for(int i=0;i<strlen(input);i++){ if(isdigit(input[i])){ number=atoi(input); t.pointer[t.size]=number; t.size++; break; } else correct=false; } }while(correct); return in; }
1234567
const int MAX=1000; const int ALPHA=224; const int BETA=225; const int THETA=233; const int OMEGA=234; const int GAMMA=226;