#include <iostream.h>
#include <string.h>
#include <limits.h>
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\tSimple Calculator" << endl;
cout << "\nChoices: " << endl;
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Multiplication" << endl;
cout << "4. Division" << endl;
cout << "\nPlease Enter Choice: ";
cin >> choice;
fail = cin.fail();
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} while(fail == true);
switch (choice) {
case 1:
cout << "\n\tADDITION!" << endl;
cout << "Enter First Number : ";
cin >> first;
cout << "Enter Second Number : ";
cin >> second;
calc.setAdd(first,second);
cout << "\nThe sum is : " << calc.getAnswer() << endl;
cout << "Do you wish to continue(Y/N)? ";
cin >> utro;
if (utro=='Y' || utro=='y') {
again = true;
} else {
again = false;
}
break;
case 2:
cout << "\n\tSUBTRACTION!" << endl;
cout << "Enter First Number : ";
cin >> first;
cout << "Enter Second Number : ";
cin >> second;
calc.setMinus(first,second);
cout << "\nThe difference is : " << calc.getAnswer() << endl;
cout << "Do you wish to continue(Y/N)? ";
cin >> utro;
if (utro=='Y' || utro=='y') {
again = true;
} else {
again = false;
}
break;
case 3:
cout << "\n\tMULTIPLICATION!" << endl;
cout << "Enter First Number : ";
cin >> first;
cout << "Enter Second Number : ";
cin >> second;
calc.setMultiply(first,second);
cout << "\nThe product is : " << calc.getAnswer() << endl;
cout << "Do you wish to continue(Y/N)? ";
cin >> utro;
if (utro=='Y' || utro=='y') {
again = true;
} else {
again = false;
}
break;
case 4:
cout << "\n\tDIVISION!" << endl;
cout << "Enter First Number : ";
cin >> first;
cout << "Enter Second Number : ";
cin >> second;
calc.setDivide(first,second);
cout << "\nThe sum is : " << calc.getAnswer() << endl;
cout << "Do you wish to continue(Y/N)? ";
cin >> utro;
if (utro=='Y' || utro=='y') {
again = true;
} else {
again = false;
}
break;
default:
cout << "\n\tINVALID CHOICE!";
again = true;
break;
}
cin.get();
} while(again == true);
cout << "\n\n\tThanks for using !\n" << endl;
return 0;
}
#include <string.h>
#include <limits.h>
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\tSimple Calculator" << endl;
cout << "\nChoices: " << endl;
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Multiplication" << endl;
cout << "4. Division" << endl;
cout << "\nPlease Enter Choice: ";
cin >> choice;
fail = cin.fail();
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} while(fail == true);
switch (choice) {
case 1:
cout << "\n\tADDITION!" << endl;
cout << "Enter First Number : ";
cin >> first;
cout << "Enter Second Number : ";
cin >> second;
calc.setAdd(first,second);
cout << "\nThe sum is : " << calc.getAnswer() << endl;
cout << "Do you wish to continue(Y/N)? ";
cin >> utro;
if (utro=='Y' || utro=='y') {
again = true;
} else {
again = false;
}
break;
case 2:
cout << "\n\tSUBTRACTION!" << endl;
cout << "Enter First Number : ";
cin >> first;
cout << "Enter Second Number : ";
cin >> second;
calc.setMinus(first,second);
cout << "\nThe difference is : " << calc.getAnswer() << endl;
cout << "Do you wish to continue(Y/N)? ";
cin >> utro;
if (utro=='Y' || utro=='y') {
again = true;
} else {
again = false;
}
break;
case 3:
cout << "\n\tMULTIPLICATION!" << endl;
cout << "Enter First Number : ";
cin >> first;
cout << "Enter Second Number : ";
cin >> second;
calc.setMultiply(first,second);
cout << "\nThe product is : " << calc.getAnswer() << endl;
cout << "Do you wish to continue(Y/N)? ";
cin >> utro;
if (utro=='Y' || utro=='y') {
again = true;
} else {
again = false;
}
break;
case 4:
cout << "\n\tDIVISION!" << endl;
cout << "Enter First Number : ";
cin >> first;
cout << "Enter Second Number : ";
cin >> second;
calc.setDivide(first,second);
cout << "\nThe sum is : " << calc.getAnswer() << endl;
cout << "Do you wish to continue(Y/N)? ";
cin >> utro;
if (utro=='Y' || utro=='y') {
again = true;
} else {
again = false;
}
break;
default:
cout << "\n\tINVALID CHOICE!";
again = true;
break;
}
cin.get();
} while(again == true);
cout << "\n\n\tThanks for using !\n" << endl;
return 0;
}
No comments:
Post a Comment