Basic Calculator
#include <iostream>
using namespace std;
// Function To Handle Incorrect UserInput
float checkInput(float input, string content)
{
while (!cin.good())
{
cout << "Wrong Input, Please Enter It Correctly\n";
cin.clear(); // Clears Previous Input
cin.ignore(100, '\n'); // Ignores Wrong Input Upto 100 Characters
cout << content;
cin >> input;
}
return input;
}
// Function To Select Operator
char selectOperator()
{
char op;
cout << "\n\t\t**** Select Operator ****\n\n\t\t
1. Addition '+'\n\t\t
2. Subtraction '-'\n\t\t
3. Multiplication '*'\n\t\t
4. Division '/'\n\t\t
5. Modulus '%'\n\t\t
6. Exponent '^'\n>>>>";
cin >> op;
return op;
}
// Driver Function
int main()
{
char oper;
double firstNum;
double secondNum;
char userInput;
string content1("\nEnter The Value of First Number : ");
string content2("\nEnter The Value of Second Number : ");
while (true)
{
double expo = 1;
cout << content1;
cin >> firstNum;
firstNum = checkInput(firstNum, content1);
cout << content2;
cin >> secondNum;
secondNum = checkInput(secondNum, content2);
oper = selectOperator();
switch (oper)
{
case '+':
cout.precision(20);
cout << endl << firstNum << " + " << secondNum << " = "
<< firstNum + secondNum << endl;
break;
case '-':
cout.precision(20);
cout << endl << firstNum << " - " << secondNum << " = "
<< firstNum - secondNum << endl;
break;
case '*':
cout.precision(20);
cout << endl << firstNum << " * " << secondNum << " = "
<< firstNum * secondNum << endl;
break;
case '/':
cout.precision(20);
cout << endl << firstNum << " / " << secondNum << " = "
<< firstNum / secondNum << endl;
break;
case '%':
cout.precision(20);
cout << endl << firstNum << " % " << secondNum << " = "
<< long(firstNum) % long(secondNum) << endl;
break;
case '^':
cout.precision(20);
for (int i = 0; i < secondNum; i++)
{
expo = expo * firstNum;
}
cout << endl << firstNum << " ^ " << secondNum << " = "
<< expo << endl;
break;
default:
cout << "\nPlease Enter Correct Input";
break;
}
label1:
cout << "\nPress 'c' To Continue and 'e' To Exit\n";
cin >> userInput;
switch (userInput)
{
case 'c':
break;
case 'e':
exit(0);
default:
cout << "\nPlease Enter Correct Input\n";
goto label1;
}
}
return 0;
}
// Output
// Enter The Value of First Number : 123456789123456789
// Enter The Value of Second Number : 987654321987654321
// **** Select Operator ****
// 1. Addition '+'
// 2. Subtraction '-'
// 3. Multiplication '*'
// 4. Division '/'
// 5. Modulus '%'
// 6. Exponent '^'
// >>>>+
// 123456790519087104 + 987654324152696832 = 1111111114671783936
// Press 'c' To Continue and 'e' To Exit
// c
// Enter The Value of First Number : 987654321987654321
// Enter The Value of Second Number : 123456789123456789
// **** Select Operator ****
// 1. Addition '+'
// 2. Subtraction '-'
// 3. Multiplication '*'
// 4. Division '/'
// 5. Modulus '%'
// 6. Exponent '^'
// >>>>-
// 987654324152696832 - 123456790519087104 = 864197533633609728
// Press 'c' To Continue and 'e' To Exit
// c
// Enter The Value of First Number : 12345678
// Enter The Value of Second Number : 87654321
// **** Select Operator ****
// 1. Addition '+'
// 2. Subtraction '-'
// 3. Multiplication '*'
// 4. Division '/'
// 5. Modulus '%'
// 6. Exponent '^'
// >>>>*
// 12345678 * 87654320 = 1082152010028960
// Press 'c' To Continue and 'e' To Exit
// c
// Enter The Value of First Number : 987654321987654321
// Enter The Value of Second Number : 123456789123456789
// **** Select Operator ****
// 1. Addition '+'
// 2. Subtraction '-'
// 3. Multiplication '*'
// 4. Division '/'
// 5. Modulus '%'
// 6. Exponent '^'
// >>>>/
// 987654324152696832 / 123456790519087104 = 8
// Press 'c' To Continue and 'e' To Exit
// c
// Enter The Value of First Number : 981765243132456798
// Enter The Value of Second Number : 918726534123456768
// **** Select Operator ****
// 1. Addition '+'
// 2. Subtraction '-'
// 3. Multiplication '*'
// 4. Division '/'
// 5. Modulus '%'
// 6. Exponent '^'
// >>>>%
// 981765271154851840 % 918726558682710016 = 0
// Press 'c' To Continue and 'e' To Exit
// c
// Enter The Value of First Number : 2
// Enter The Value of Second Number : 4
// **** Select Operator ****
// 1. Addition '+'
// 2. Subtraction '-'
// 3. Multiplication '*'
// 4. Division '/'
// 5. Modulus '%'
// 6. Exponent '^'
// >>>>^
// 2 ^ 4 = 16
// Press 'c' To Continue and 'e' To Exit
// c
// Enter The Value of First Number : 12345
// Enter The Value of Second Number : 5
// **** Select Operator ****
// 1. Addition '+'
// 2. Subtraction '-'
// 3. Multiplication '*'
// 4. Division '/'
// 5. Modulus '%'
// 6. Exponent '^'
// >>>>^
// 12345 ^ 5 = 2.8671833852463546368e+20
// Press 'c' To Continue and 'e' To Exit
// c
// Enter The Value of First Number : 22
// Enter The Value of Second Number : 5
// Press 'c' To Continue and 'e' To Exit
// e
Comments
Post a Comment