Algoritma kalkulator ()
Deklarasi:
a,b,c : float { input }
hasil,hasil1 : float { output }
Deskripsi:
Read(a,b,c)
hasil=tambah <= a+b
hasil=kurang <= a-b
hasil=kali <= a x b
hasil=bagi <= a / b
hasil=per x <= 1 / a
hasil=pangkat *<= a
hasil=akar <= sqrt(a)
hasil=kuadrat <= a x a
hasil=kubik <= a x a x a
hasil=faktorial <= fak*i
hasil1=sines <= sin(c)
hasil1=cosines <= cos(c)
hasil1=tangen <= tan(c)
Write (hasil)
Berikut programnya :
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>
class kalkulator {
friend istream& operator >> (istream&, kalkulator&);
friend ostream& operator << (ostream&, const kalkulator&);
public :
kalkulator();
void menu();
int tambah();
int kurang();
int kali();
float bagi();
float perx();
int pangkat();
int akar();
int kuadrat();
int kubik();
long faktorial();
void sines();
void cosines();
void tangen();
private:
float a,b,c,hasil;
};
istream& operator>>(istream& in, kalkulator& masuk){
cout<<"\t Selamat datang di program kalkulator sederhana ini \n";
cout<<"\t Selamat mencoba \n";
cout<<"\t Semoga Bermanfaat untuk Kalian \n"<<endl;
cout<<"\t ==================================== \n";
cout<<"\t | PROGRAM KALKULATOR SEDERHANA |\n";
cout<<"\t | by Indri Fitri Yadini (10018023) |\n";
cout<<"\t | UNIVERSITAS AHMAD DAHLAN |\n";
cout<<"\t ==================================== \n"<<endl;
cout<<"*********************************************\n";
cout<<"| KALKULATOR |\n";
cout<<"*********************************************\n";
cout<<"| ___ ___ ___ ___ ___ ___ ___ |\n";
cout<<"| |sin| |x^2| | / | | + | | 7 | | 8 | | 9 | |\n";
cout<<"| |___| |___| |___| |___| |___| |___| |___| |\n";
cout<<"| ___ ___ ___ ___ ___ ___ ___ |\n";
cout<<"| |cos| |x^3| |1/x| | - | | 4 | | 5 | | 6 | |\n";
cout<<"| |___| |___| |___| |___| |___| |___| |___| |\n";
cout<<"| ___ ___ ___ ___ ___ ___ ___ |\n";
cout<<"| |tan| | n!| |x^y| | * | | 1 | | 2 | | 3 | |\n";
cout<<"| |___| |___| |___| |___| |___| |___| |___| |\n";
cout<<"| ___ ___ ___ ___ ___ ___ ___ |\n";
cout<<"| |sqr| |CE | | C | | . | | 0 | |+/-| | = | |\n";
cout<<"| |___| |___| |___| |___| |___| |___| |___| |\n";
cout<<"|___________________________________________|\n"<<endl;
cout<<"1. Tambah\n";
cout<<"2. Kurang\n";
cout<<"3. Kali\n";
cout<<"4. Bagi\n";
cout<<"5. Per x\n";
cout<<"6. Pangkat\n" ;
cout<<"7. Akar\n" ;
cout<<"8. Kuadrat\n";
cout<<"9. Kubik\n";
cout<<"10. Faktorial\n";
cout<<"11. Sines\n";
cout<<"12. Cosines\n";
cout<<"13. Tangen\n";
cout<<"14. Keluar\n"<<endl;
cout<<"Masukan pilihan anda : ";
int pilihan;
cin>>pilihan;
if (pilihan==1)
{
masuk.tambah();}
else if (pilihan==2)
{
masuk.kurang();}
else if (pilihan==3)
{
masuk.kali();}
else if (pilihan==4)
{
masuk.bagi();}
else if (pilihan==5)
{
masuk.perx();}
else if (pilihan==6)
{
masuk.pangkat();}
else if (pilihan==7)
{
masuk.akar();}
else if (pilihan==8)
{
masuk.kuadrat();}
else if (pilihan==9)
{
masuk.kubik();}
else if (pilihan==10)
{
masuk.faktorial();}
else if (pilihan==11)
{
masuk.sines();}
else if (pilihan==12)
{
masuk.cosines();}
else if (pilihan==13)
{
masuk.tangen();}
else if (pilihan==14)
{
cout<<"Anda telah keluar dari program"<<endl;}
else
cout<<"Inputan Anda Salah\n";
return in;
}
ostream& operator<<(ostream& out, const kalkulator& keluar){
out<<"\t Terima Kasih....:)"<<endl;
out<<"\t :: created by indie emerald g bastian ::";
return out;}
kalkulator::kalkulator(){} //konstruktor
int kalkulator::tambah(){
cout<<"Masukan bilangan pertama : ";
cin>>a;
cout<<"Masukan bilangan kedua : ";
cin>>b;
hasil = a+b;
cout<<"Hasil tambah "<<a<<" + "<<b<<" = "<<hasil<<endl;}
int kalkulator::kurang(){
cout<<"Masukan bilangan pertama : ";
cin>>a;
cout<<"Masukan bilangan kedua : ";
cin>>b;
hasil=a-b;
cout<<"Hasil kurang "<<a<<" - "<<b<<" = "<<hasil<<endl;}
int kalkulator::kali(){
cout<<"Masukan bilangan pertama : ";
cin>>a;
cout<<"Masukan bilangan kedua : ";
cin>>b;
hasil=a*b;
cout<<"Hasil kali "<<a<<" * "<<b<<"= "<<hasil<<endl;}
float kalkulator::bagi(){
cout<<"Masukan bilangan pertama : ";
cin>>a;
cout<<"Masukan bilangan kedua : ";
cin>>b;
hasil=a/b;
cout<<"Hasil bagi "<<a<<" dan "<<b<<"= "<<hasil<<endl;}
float kalkulator::perx(){
cout<<"Masukan bilangan : ";
cin>>a;
hasil=1/a;
cout<<"Hasil 1 per x = "<<hasil<<endl;}
int kalkulator::pangkat(){
cout<<"Masukan bilangan : ";
cin>>a;
cout<<"Masukan pangkat : ";
cin>>b;
int hasil=1;
for(int i=1;i<=b;i++)
hasil*=a;
cout<<"Hasil "<<a<<" pangkat "<<b<<" = "<<hasil<<endl;}
int kalkulator::akar(){
cout<<"Masukan bilangan : ";
cin>>a;
hasil=sqrt(a);
cout<<"Hasil akar = "<<hasil<<endl;}
int kalkulator::kuadrat(){
cout<<"Masukan bilangan : ";
cin>>a;
hasil=a*a;
cout<<"Hasil kuadrat = "<<hasil<<endl;}
int kalkulator::kubik(){
cout<<"Masukan bilangan : ";
cin>>a;
hasil=a*a*a;
cout<<"Hasil kubik = "<<hasil<<endl;}
long kalkulator::faktorial(){
cout<<"Masukan bilangan : ";
cin>>a;
long fak=1;
for(int i=1;i<=a;i++)
fak=fak*i;
cout<<"Hasil "<<a<<"faktorial = "<<fak<<endl;}
void kalkulator::sines(){
cout<<"Masukkan bilangan : ";
cin>>c;
hasil = sin( c );
cout << "Hasil sin "<<c<<" = "<<hasil<<endl;}
void kalkulator::cosines(){
cout<<"Masukkan bilangan : ";
cin>>c;
hasil = cos( c );
cout << "Hasil cos "<<c<<" = "<<hasil<<endl;}
void kalkulator::tangen(){
cout<<"Masukkan bilangan : ";
cin>>c;
hasil = tan( c );
cout << "Hasil tan "<<c<<" = "<<hasil<<endl;}
int main(){
kalkulator indri;
cin>>indri;
cout<<indri;
getch();
return 0;
}