#include<iostream.h>
#include<conio.h>
#define max 20
class addition;
class term{
friend addition;
int coef;
float exp;
};
class addition{
static term array[50];
static int free;
int start;
int finish;
public:
addition()
{
start=free;
finish=free-1;
}
void init(int a,int b)
{
array[free].coef=a;
array[free].exp=b;
free++;
finish++;
}
void add(addition M);
char compare(int v,int s);
void newterm(int c,int e);
void insert();
};
//************
void addition::newterm(int c,int e)
{
if(free>=50)
{
cout<<"error";
return;
}
array[free].coef=c;
array[free].exp=e;
free++;
}
//************
char addition:: compare(int v,int s)
{
if(v==s)
return('=');
if(v<s)
return('<');
return('>');
}
//************
void addition::add(addition M)
{
addition C;
int a=start;
int m=M.start;
C.start=free;
int c;
while((a<=finish)&&(m<=M.finish))
switch(compare(array[a].exp,array[m].exp))
{
case'=':
c=array[a].coef+array[m].coef;
if(c)
newterm(c,array[a].exp);
a++;
m++;
break;
case'<':
newterm(array[m].coef,array[m].exp);
m++;
break;
case'>':
newterm(array[a].coef,array[a].exp);
a++;
}//swich
for(;a<=finish;a++)
newterm(array[a].coef,array[a].exp);
for(;m<=M.finish;m++)
newterm(array[m].coef,array[m].exp);
C.finish=free-1;
cout<<endl<<endl;
cout<<"*************"<<endl<<endl;
cout<<endl<<"F1(X)=";
for(int k=start;k<=finish;k++)
{
if(array[k].exp==0)
cout<<array[k].coef;
else
cout<<array[k].coef<<"X"<<array[k].exp;
if(k!=finish)
cout<<" + ";
}
cout<<endl<<endl<<"F2(X)=";
for(k=M.start;k<=M.finish;k++)
{
if(array[k].exp==0)
cout<<array[k].coef;
else
cout<<array[k].coef<<"X"<<array[k].exp;
if(k!=M.finish)
cout<<" + ";
}
cout<<endl<<endl<<endl<<"F(X)=";
for( k=C.start;k<=C.finish;k++)
{
if(array[k].exp==0)
cout<<array[k].coef;
else
cout<<array[k].coef<<"X"<<array[k].exp;
if(k!=C.finish)
cout<<" + ";
}
return;
}
//************
void addition :: insert()
{
int c;
int b;
do
{
cout<<endl<<"enter a zarib:";
cin>>c;
cout<<endl<<"enter a darege:";
cin>>b;
init(c,b);
cout<<"pleas press a key,end of '.'";
}while(getch()!='.');
}
term addition::array[50];
int addition::free=0;
//************
void main()
{
clrscr();
addition A;
A.insert();
cout<<endl<<endl;
cout<<"************";
cout<<endl;
addition M;
M.insert();
A.add(M);
getch();
}