Jumat, 05 Oktober 2012

Uji kompetensi SDATA pertemuan ke-3 Polinomial

#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];
}
}

int 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();
return 0;
}

 

Copyright © YunusBasuki® Design by YunusBasuki

HOME