Saya kali ini akan post source code kalkulator sederhana dengan dengan bahasa C++, oke deh langsung saja..
berikut source code nya..
berikut source code nya..
#include <iostream>
#include <string>
#include <limits>
using namespace std;
class Calculator
{
public:
Calculator();
int getAnswer () {
return answer;
};
void setAdd (int a,int b) {
answer=a+b;
};
void setMinus (int a,int b) {
answer=a-b;
};
void setMultiply (int a,int b) {
answer=a*b;
};
void setDivide (int a,int b) {
answer=a/b;
};
private:
double answer;
};
int main()
{
Calculator calc;
double first,second;
int choice;
bool fail,again;
char utro;
do {
do {
cout << "\n\tKalkulator Sederhana" << endl;
cout << "\nPilihan: " << endl;
cout << "1. Jumlah" << endl;
cout << "2. Kurang" << endl;
cout << "3. Kali" << endl;
cout << "4. Bagi" << endl;
cout << "\nSilahkan Pilih: ";
cin >> choice;
fail = cin.fail();
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} while(fail == true);
switch (choice) {
case 1:
cout << "\n\tPenjumlahan" << endl;
cout << "Masukkan Angka pertama : ";
cin >> first;
cout << "Masukkan angka kedua : ";
cin >> second;
calc.setAdd(first,second);
cout << "\nHasil nya : " << calc.getAnswer() << endl;
cout << "Pakai lagi (Y/N)? ";
cin >> utro;
if (utro=='Y' || utro=='y') {
again = true;
} else {
again = false;
}
break;
case 2:
cout << "\n\tPengurangan" << endl;
cout << "Maukkan anggka pertama : ";
cin >> first;
cout << "Masukkan angka kedua : ";
cin >> second;
calc.setMinus(first,second);
cout << "\nHasilnya : " << calc.getAnswer() << endl;
cout << "pakai lagi(Y/N)? ";
cin >> utro;
if (utro=='Y' || utro=='y') {
again = true;
} else {
again = false;
}
break;
case 3:
cout << "\n\tPerkalian!" << endl;
cout << "Masukkan angka pertama : ";
cin >> first;
cout << "Angka kedua : ";
cin >> second;
calc.setMultiply(first,second);
cout << "\nHasil nya : " << calc.getAnswer() << endl;
cout << "Lanjut lagi (Y/N)? ";
cin >> utro;
if (utro=='Y' || utro=='y') {
again = true;
} else {
again = false;
}
break;
case 4:
cout << "\n\tPembagian!" << endl;
cout << "Masukkan angka pertama : ";
cin >> first;
cout << "Masukkan angka kedua : ";
cin >> second;
calc.setDivide(first,second);
cout << "\nThe sum is : " << calc.getAnswer() << endl;
cout << "pakai lagi (Y/N)? ";
cin >> utro;
if (utro=='Y' || utro=='y') {
again = true;
} else {
again = false;
}
break;
default:
cout << "\n\tSalah pilihannya!";
again = true;
break;
}
cin.get();
} while(again == true);
cout << "\n\n\tKalkulator sederhana by ho1onk !\n" << endl;
return 0;
}
Membuat Kalkulator dengan C++
4/
5
Oleh
Holong.Me