nesesito ayuda con un programa

celina
21 de Marzo del 2005
nesesito ayuda con un programa en c++ de suma resta y multiplicacion de polinomios el programa se debe hacer con clases

noel solw
21 de Marzo del 2005
Te envio un programa que hace, mas o menos, lo que solicitas. Espero que te sea util.

-----------------------------------------------------------------------

// program k14a13.h - page 281
// polynomial - header file for program k14a13.cpp
// 1/6/2002
// written in Borland CPP ver 3.1

#include <conio.h>
#include<iostream.h>
#include <iomanip.h>

class Polynomial
{
private:
unsigned int size;
float *p;
public:
Polynomial(unsigned int,const float*);
Polynomial(const Polynomial &);
~Polynomial();
int operator==(const Polynomial &) const;
Polynomial &operator=(const Polynomial &);
Polynomial operator+(const Polynomial &) const;
Polynomial operator-(const Polynomial &) const;
Polynomial operator-();
Polynomial operator*(const Polynomial &) const;
Polynomial operator/(const Polynomial &) const;
Polynomial operator%(const Polynomial &) const;
friend ostream &operator<<(ostream &,const Polynomial &);
friend istream &operator>>(istream &,Polynomial &);
}; // POLYNOMIAL
// ----------------------- public functions ---------------------------
Polynomial::Polynomial(unsigned int SIZE = 0,const float *data = NULL)
{
if(!SIZE)
{
size = 0;
p = NULL;
}
else
{
size = SIZE;
p = new float[size];
if(data)
{
for(int i = 0;i < size;i++)
p[i] = data[i];
}
else
for(int i = 0;i < size;i++)
p[i] = 0;
}
} // POLYNOMIAL CONSTRUCTOR

Polynomial::Polynomial(const Polynomial &poly)
{
size = poly.size;
if(size)
{
p = new float[size];
for(int i = 0;i < size;i++)
p[i] = poly.p[i];
}
} // POLYNOMIAL COPY CONSTRUCTOR

Polynomial::~Polynomial()
{
if(p)
delete []p;
} // POLYNOMIAL DESTRUCTOR

int Polynomial::operator==(const Polynomial &right) const
{
if(size != right.size)
return 0;
for(int i = 0;i < size;i++)
if(p[i] != right.p[i])
return 0;
return 1;
} // POLYNOMIAL OPERATOR EQUAL


Polynomial &Polynomial::operator=(const Polynomial &right)
{
if(&right != this)
{
int len = right.size;
if(size != len)
{
delete []p;
size = len;
p = new float[size];
}
for(int i = 0;i < size;i++)
p[i] = right.p[i];
}
return *this;
} // POLYNOMIAL OPERATOR ASSIGNAMENT

Polynomial Polynomial::operator+(const Polynomial &right) const
{
Polynomial ret;
if(size >= right.size)
{
ret = *this;
int i = size,
j = right.size;
while(j)
ret.p[--i] += right.p[--j];
}
else
{
ret = right;
int i = right.size,
j = size;
while(j)
ret.p[--i] += p[--j];
}
return ret;
} // POLYNOMIAL OPERATOR PLUS

Polynomial Polynomial::operator-(const Polynomial &right) const
{
Polynomial temp = right,ret = *this + (-temp);
return ret;
} // POLYNOMIAL OPERATOR MINUS

Polynomial Polynomial::operator-()
{
Polynomial ret(size);
for(int i = 0;i < size;i++)
ret.p[i] = -p[i];
return ret;
} // POLYNOMIAL OPERATOR UNARY MINUS

Polynomial Polynomial::operator*(const Polynomial &right) const
{
Polynomial ret(size+right.size-1);
for(int i = 0;i < size;i++)
for(int j = 0;j < right.size;j++)
ret.p[i+j] += p[i]*right.p[j];
return ret;
} // OPERATOR MULT

Polynomial Polynomial::operator/(const Polynomial &right) const
{
if(size < right.size)
{
Polynomial ret(1);
return ret;
}
else
{
Polynomial ret(size-right.size+1),
temp(*this);
for(int i = 0; i <= size-right.size; i++)
{
ret.p[i] = temp.p[i] / right.p[0];
for(int j = 0; j < right.size; j++)
temp.p[i+j] -= ret.p[i]*right.p[j];
}
return ret;
}
} // OPERATOR DIV

Polynomial Polynomial::operator%(const Polynomial &right) const
{
Polynomial ret(right.size),temp;
temp = (*this/right)*right;
return *this - temp;
} // OPERATOR MOD
// ----------------------- friend functions ---------------------------
ostream &operator<<(ostream &out,const Polynomial &right)
{
switch(right.size)
{
case 0 : out << ". . . empty polynomial";
break;
case 1 : out << "{" << right.p[0] << "}";
break;
default : int i = 0;
while(!right.p[i++]);
i--;
out << "{" << right.p[i++];
while(i < right.size)
out << "," << right.p[i++];
out << "}";
break;
}
return out;
} // OPERATOR <<

istream &operator>>(istream &in,Polynomial &right)
{
cout << "type polynomial size : ";
in >> right.size;
right.p = new float[right.size];
for(int i = 0;i < right.size;i++)
{
cout << " p[" << i << "] = ";
in >> right.p[i];
}
return in;
} // OPERATOR >>
// --------------------------------------------------------------------


// program k14a13.cpp - page 281
// polynomial.
// 1/6/2002
// written in Borland CPP ver 3.1

#include "k14a13.h"

char *msg[] = {"FALSE","TRUE"};

void Process()
{
float aa[4] = {1,2,3,4},
bb[7] = {1,2,3,4,5,6,7},
ff[3] = {1,2,3},
gg[5] = {1,2,3,4,5},
hh[6] = {7,-8,3,-4,2,5},
ii[3] = {1,-1,1};
Polynomial a(4,aa),b(7,bb),c,d,f(3,ff),g(5,gg),h(6,hh),i(3,ii);
cout << setw(10) << "a = " << a << endl;
cout << setw(10) << "b = " << b << endl;
cout << setw(10) << "c = " << c << endl;
c = b;
cout << setw(10) << "c = " << c << endl;
cout << endl;
cout << a << " == " << b << " ---> " << msg[a==b] << endl;
cout << b << " == " << c << " ---> " << msg[b==c] << endl << endl;
cin >> d;
cout << setw(10) << "d = " << d << endl << endl;
d = a + b;
cout << a << " + " << b << " = " << d << endl;
d = b + a;
cout << b << " + " << a << " = " << d << endl;
d = a - b;
cout << a << " - " << b << " = " << d << endl;
d = b - a;
cout << b << " - " << a << " = " << d << endl;
d = f * g;
cout << f << " * " << g << " = " << d << endl;
d = h / i;
cout << h << " / " << i << " = " << d << endl;
d = i / h;
cout << i << " / " << h << " = " << d << endl;
d = h % i;
cout << h << " % " << i << " = " << d << endl;
d = i % h;
cout << i << " % " << h << " = " << d << endl;
} // PROCESS

void main()
{
clrscr();
cout << setprecision(3) << "unsorted set.n";
cout << "-----------------------------------------------------"
"-------------------------n";
Process();
cout << "-----------------------------------------------------"
"-------------------------n";
cout << "end of program - good bye ! ! !n";
getch();
} // MAIN

/*
unsorted set.
------------------------------------------------------------------------------
a = {1,2,3,4}
b = {1,2,3,4,5,6,7}
c = . . . empty polynomial
c = {1,2,3,4,5,6,7}

{1,2,3,4} == {1,2,3,4,5,6,7} ---> FALSE
{1,2,3,4,5,6,7} == {1,2,3,4,5,6,7} ---> TRUE

type polynomial size :
p[0] = 1
p[1] = 2
p[2] = 3
p[3] = 4
p[4] = 5
d = {1,2,3,4,5}

{1,2,3,4} + {1,2,3,4,5,6,7} = {1,2,3,5,7,9,11}
{1,2,3,4,5,6,7} + {1,2,3,4} = {1,2,3,5,7,9,11}
{1,2,3,4} - {1,2,3,4,5,6,7} = {-1,-2,-3,-3,-3,-3,-3}
{1,2,3,4,5,6,7} - {1,2,3,4} = {1,2,3,3,3,3,3}
{1,2,3} * {1,2,3,4,5} = {1,4,10,16,22,22,15}
{7,-8,3,-4,2,5} / {1,-1,1} = {7,-1,-5,-8}
{1,-1,1} / {7,-8,3,-4,2,5} = {0}
{7,-8,3,-4,2,5} % {1,-1,1} = {-1,13}
{1,-1,1} % {7,-8,3,-4,2,5} = {1,-1,1}
------------------------------------------------------------------------------
end of program - good bye ! ! !

*/