Program Polinomial C++

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

class poli{
friend ostream& operator << (ostream& , poli& );
friend istream& operator >> (istream& , poli& );
public:
poli();
void penjumlahan(const poli&, const poli&);
void nilai(int);
private:
int elemen[100];
int banyak;
};

poli::poli(){
for(int i=0;i<banyak;i++)
elemen[i];
}
istream& operator >> (istream& in, poli& a){
cout<<“banyak elemen : “; in>>a.banyak;
cout<<“masukkan data polinomial : \n”;
for(int i=0;i<a.banyak;i++){
cout<<“variabel pangkat “<<i<<” :”;
cin>>a.elemen[i];
}
return in;
}
void poli::nilai(int i){
banyak= i;
}
ostream& operator << (ostream& out, poli& a){

for(int i=(a.banyak-1);i>=0;i–){
cout<<a.elemen[i];
if(i!=0) cout<<“x^”<<i<<“+”;
}
cout<<endl;
return out;
}
void poli::penjumlahan(const poli& a, const poli& b){
if(a.banyak>b.banyak)banyak=a.banyak;
else banyak=b.banyak;
for(int i=0;i<banyak;i++){
if ((a.banyak-1)<i) elemen[i]=b.elemen[i];
else if ((b.banyak-1)<i) elemen[i]=a.elemen[i];
else elemen[i]=a.elemen[i]+b.elemen[i];
}
}

void main(){
poli x, y, z;
cin>>x;
cout<<x;
cin>> y;
cout<<y;
z.penjumlahan(x,y);
cout<<“\n\nhasil penjumlahan 2 polinomial\n”<<z<<endl;
getch();
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s