#include <cstdlib> |
#include <iostream> |
using namespace std; |
class jumlah{ |
friend istream& operator>>(istream&,jumlah&); |
friend ostream& operator<<(ostream&,const jumlah&); |
public : |
jumlah (int a=0,int b=0):x(a),y(b) {} |
double hasil(); |
// operator double()const; |
private: |
double x,y; |
double jum; |
// void reduce(); |
}; |
int main(int argc, char *argv[]) |
{ |
int jum; |
jumlah a,b; |
//jumlah b; |
cin>>a; |
cin>>b; |
cout<<a<<endl; |
cout<<b<<endl; |
//jum=(a+b); |
// cout<<jum; |
system("PAUSE"); |
return EXIT_SUCCESS; |
} |
//jumlah::jumlah (int a) { |
//x+y; |
// } |
//jumlah::operator double(){ |
// return double x+y;} |
double jumlah::hasil(){ |
return x+y;} |
istream& operator>>(istream& istr,jumlah& p){ |
cout<<"Masukan Nilai ke-1 : ";istr>>p.x; |
cout<<"Masukan Nilai ke-2 : ";istr>>p.y; |
cout<<endl; |
return istr; |
} |
ostream& operator<<(ostream& ostr,const jumlah& p){ |
ostr<<"TOTAL JUMLAH : "<<p.x+p.y<<endl; |
ostr<<"TOTAL KURANG : "<<p.x-p.y<<endl; |
ostr<<"TOTAL KALI : "<<p.x*p.y<<endl; |
ostr<<"TOTAL BAGI : "<<p.x/p.y<<endl; |
return ostr; |
} |
Selasa, 29 Maret 2011
PENJUMLAHAN
NILAI KE HURUF
#include <cstdlib> |
#include <iostream> |
using namespace std; |
class Nilai{ |
friend ostream& operator<<(ostream&,Nilai&); |
friend istream& operator>>(istream&,Nilai&); |
public: |
Nilai(){}; |
void konversikan(){ |
if((nilai>0)&&(nilai<=20)) |
nilai_huruf='E'; |
else if((nilai > 20)&&(nilai<=40)) |
nilai_huruf='D'; |
else if((nilai > 40)&&(nilai<=60)) |
nilai_huruf='C'; |
else if((nilai>60)&&(nilai<=80)) |
nilai_huruf='B'; |
else nilai_huruf='A'; |
} |
private: |
int nilai; |
char nilai_huruf; |
}; |
istream& operator>>(istream& in,Nilai& masukan){ |
cout<<"MASUKKAN NILAI = "; |
in>>masukan.nilai; |
return in; |
}; |
ostream& operator<<(ostream& out,Nilai&keluaran){ |
out<<"NILAI HURUF = "<<keluaran.nilai_huruf; |
return out; |
}; |
int main() |
{ |
Nilai angka; |
cin>>angka; |
angka.konversikan(); |
cout<<angka; |
system("PAUSE"); |
return EXIT_SUCCESS; |
} |
AKAR PERSAMAAN KUADRAT
#include <iostream> |
#include <math.h> |
#include <conio.h> |
using namespace std; |
class akar{ |
friend ostream& operator<<(ostream&, akar&); |
friend istream& operator>>(istream&, akar&); |
public: |
akar(); |
int disk(){ return B*B-4*A*C;} |
float akar1(){ return(-B+sqrt(disk()))/(2*A);} |
float akar2(){ return(-B-sqrt(disk()))/(2*A);} |
void hitung_akar(); |
void cetak_disk(){cout<<"DISKRIMINAN = "<<disk()<<endl;} |
void cetak_akar(){ |
cout<<"x1="<<akar1()<<endl; |
cout<<"x2="<<akar2()<<endl; |
} |
private: |
int A,B,C; |
float x1,x2; |
}; |
ostream& operator<<(ostream& out,akar& keluaran){ |
keluaran.cetak_disk(); |
if(keluaran.disk()>=0)keluaran.cetak_akar(); |
else out<<"AKAR IMAJINER \n"; |
return out; |
} |
istream& operator>>(istream& in,akar& masukan){ |
cout<<"KOEFISIEN PANGKAT 2 = ";in>>masukan.A; |
cout<<"KOEFISIEN PANGKAT 1 = ";in>>masukan.B; |
cout<<"KOEFISIEN PANGKAT 0 = ";in>>masukan.C; |
return in; |
} |
akar::akar(){ |
cout<<"MENGHITUNG AKARA PERSAMAAN KUADRAT \n"; |
} |
void akar::hitung_akar(){ |
if(A==0){ |
cout<<"BUKAN PERSAMAAN KUADRAT \n"; |
cout<<"HARGA AKAR = "<<-C/B;} else{ |
if(disk()>0){ |
x1=akar1(); |
x2=akar2(); |
} else if(disk()==0){ |
x1=akar1(); |
x2=x1; |
} |
} |
} |
int main(){ |
akar masalah; |
cin>>masalah; |
masalah.hitung_akar(); |
cout<<masalah; |
system("PAUSE"); |
return EXIT_SUCCESS; |
} |
Langganan:
Postingan (Atom)