Anda di halaman 1dari 5

#include <iostream.

h>
#include <stdio.h>
#include <stdlib.h>
int
int
int
int

p1[11];
p2[11];
ph[22];
ekspo1; int ekspo2;

void penjumlahan ()
{
int a, b;
cout<<" P1 + P2 = ";
if (ekspo1 >= ekspo2)
{
for (a =ekspo1; a>=0; a--)
{
ph[a] = p1[a] + p2[a];
}
for (b=ekspo1; b>=0; b--)
{
if(ph[b] != 0)
{
cout<<ph[b]<<"x"<<b;
if(b!=0)
{
cout<<"+";
}
}
}
}
else
{
for (a=ekspo2; a>=0; a--)
{
ph[a]=p1[a]+p2[a];
}
for (b=ekspo2; b>=0; b--)
{
if (ph[b]!=0)
{
cout<<ph[b]<<"x"<<b;
if(b!=0)
{
cout<<"+";
}
}
}
}
}
void pengurangan()
{
int a, b;
cout<<"P1 - P2 = ";
if(ekspo1>= ekspo2)
{
for(a = ekspo1; a>=0; a--)

{
ph[a] = p1[a] - p2[a];
}
for(b = ekspo1; b>=0; b--)
{
if(ph[b] != 0)
{
cout<<ph[b]<<"x"<<b;
if(b!= 0)
{
cout<<" + ";
}
}
}
}
else
{
for(a= ekspo2; a>=0; a--)
{
ph[a] = p1[a] - p2[a];
}
for(b= ekspo2; b>=0; b--)
{
if(ph[b] != 0)
{
cout<<ph[b]<<"x"<<b;
if(b!= 0)
{
cout<<" + ";
}
}
}
}
}
void perkalian()
{
int p[10][10];
int a, b, c, d, e;
cout<<"P1 * P2 = ";
for(a = ekspo1; a>=0; a--)
{
for(b = ekspo2; b>=0; b--)
{
p[a][b] = p1[a] * p2[b];
}
}
for(c = ekspo1; c>=0; c--)
{
for(d = ekspo2; d>=0; d--)
{
ph[c+d] = ph[c+d] + p[c][d];
}
}

for(e = ekspo1 + ekspo2; e>=0; e--)


{
if(ph[e] != 0)
{
cout<<ph[e]<<"x"<<e;
if(e != 0)
{
cout<<" + ";
}
}
}
}
void turunan ()
{
int a,b,c;
cout<<"P1' = ";
for(a = ekspo1; a>=0; a--)
{
b = a - 1;
ph[b] = p1[a] * a;
}
for(c = ekspo1 - 1; c>=0; c--)
{
if(ph[c] != 0)
{
cout<<ph[c]<<"x"<<c;
if(c != 0)
{
cout<<" + ";
}
}
}
}
int main()
{
char pil, ulang;
int a,b,c,d;
do
{
system("cls");
cout<<"\n====OPERASI POLINOM====\n\n"
<<"Pilih Operasi\n"
<<"1. Penjumlahan\n"
<<"2. Pengurangan\n"
<<"3. Perkalian\n"
<<"4. Turunan\n";
cout<<"\nMasukkan Pilihan....";
cin>>pil;
cout<<"Masukkan pangkat terbesar P1 (max 10) : ";
cin>>ekspo1;
if(pil != '4')
{
cout<<"Masukkan pangkat terbesar P2 (max 10) : ";

cin>>ekspo2;
}
cout<<"\nP1\n";
for(a = ekspo1; a>=0; a--)
{
cout<<"Koefisien suku x"<<a<<" : "; cin>>p1[a];
}
cout<<"\nP1 = ";
for(b = ekspo1; b>=0; b--)
{
if(p1[b] != 0)
{
cout<<p1[b]<<"x"<<b;
if(b!= 0)
{
cout<<" + ";
}
}
}
cout<<"\n\n";
if(pil != '4')
{
cout<<"P2\n";
for(c = ekspo2; c>=0; c--)
{
cout<<"Koefisien suku x"<<c<<" : "; cin>>p2[c];
}
cout<<"\n";
cout<<"P2 = ";
for(d = ekspo2; d>=0; d--)
{
if(p2[d] != 0)
{
cout<<p2[d]<<"x"<<d;
if(d != 0)
{
cout<<" + ";
}
}
}
}
cout<<"\n\n";
cout<<"HASIL\n";
switch (pil)
{
case '1':
penjumlahan();
break;
case '2':
pengurangan();
break;
case '3':
perkalian();

break;
case '4':
turunan();
break;
}
cout<<"\n\nLakukan lagi? (Y/T) "; cin>>ulang;
}
while(ulang == 'Y' || ulang == 'y');
system("cls");
cout<<"====TERIMA KASIH====";
}