From efb5638e7725cdee6b9cb18c57d302db3960b0a0 Mon Sep 17 00:00:00 2001 From: anas16D Date: Fri, 30 Jul 2021 23:10:15 +0530 Subject: [PATCH] Added some boundary conditions added integer overflow, changed wrong use of float, and added a ifdef for system cls which doesn't run on linux. --- Calculator/calculator.cpp | 227 ++++++++++++++++++++++---------------- 1 file changed, 134 insertions(+), 93 deletions(-) diff --git a/Calculator/calculator.cpp b/Calculator/calculator.cpp index 8fac648..d8d3eae 100644 --- a/Calculator/calculator.cpp +++ b/Calculator/calculator.cpp @@ -1,8 +1,10 @@ #include #include -#include +#include #include #include + + using namespace std; void add(); void sub(); @@ -11,120 +13,159 @@ void division(); void sqr(); void srt(); void exit(); + int main() { -system("cls"); -int opr; -// display different operation of the calculator -do -{ -cout << "Select any operation from the C++ Calculator" - "\n1 = Addition" - "\n2 = Subtraction" - "\n3 = Multiplication" - "\n4 = Division" - "\n5 = Square" - "\n6 = Square Root" - "\n7 = Exit" - "\n \n Make a choice: "; - cin >> opr; - - switch (opr) + #ifdef linux + system("clear"); + #endif + + #if defined _WIN32 || defined _WIN64 + system("cls"); + #endif + int opr; + // display different operation of the calculator + do { - case 1: - add(); // call add() function to find the Addition - break; - case 2: - sub(); // call sub() function to find the subtraction - break; - case 3: - multi(); // call multi() function to find the multiplication - break; - case 4: - division(); // call division() function to find the division - break; - case 5: - sqr(); // call sqr() function to find the square of a number - break; - case 6: - srt(); // call srt() function to find the Square Root of the given number - break; - case 7: - exit(0); // terminate the program - break; - default: - cout <<"Something is wrong..!!"; - break; - } - cout <<" \n------------------------------\n"; + cout << "Select any operation from the C++ Calculator" + "\n1 = Addition" + "\n2 = Subtraction" + "\n3 = Multiplication" + "\n4 = Division" + "\n5 = Square" + "\n6 = Square Root" + "\n7 = Exit" + "\n \n Make a choice: "; + cin >> opr; + + switch (opr) + { + case 1: + add(); // call add() function to find the Addition + break; + case 2: + sub(); // call sub() function to find the subtraction + break; + case 3: + multi(); // call multi() function to find the multiplication + break; + case 4: + division(); // call division() function to find the division + break; + case 5: + sqr(); // call sqr() function to find the square of a number + break; + case 6: + srt(); // call srt() function to find the Square Root of the given number + break; + case 7: + exit(0); // terminate the program + break; + default: + cout <<"Something is wrong..!!"; + break; + } + cout <<" \n------------------------------\n"; }while(opr != 7); - getch(); - } + + cin.get(); + } void add() { -int n, sum = 0, i, number; -cout <<"How many numbers you want to add: "; -cin >> n; -cout << "Please enter the number one by one: \n"; -for (i = 1; i <= n; i++) -{ -cin >> number; -sum = sum + number; -} -cout << "\n Sum of the numbers = "<< sum; + int n, sum = 0, i, number; + cout <<"How many numbers you want to add: "; + cin >> n; + cout << "Please enter the number one by one: \n"; + for (i = 1; i <= n; i++) + { + cin >> number; + if(INT_MAX - sum < number) // checking for overflow + { + cout << "\n Integer Overflow"; + cin.clear(); + cin.ignore(INT_MAX, '\n'); // clearing the input stream buffer + return; + } + sum = sum + number; + } + cout << "\n Sum of the numbers = "<< sum; } + void sub() { -int num1, num2, z; -cout <<" \n Enter the First number = "; -cin >> num1; -cout << "\n Enter the Second number = "; -cin >> num2; -z = num1 - num2; -cout <<"\n Subtraction of the number = " << z; + int num1, num2, z; + cout <<" \n Enter the First number = "; + cin >> num1; + cout << "\n Enter the Second number = "; + cin >> num2; + z = num1 - num2; + cout <<"\n Subtraction of the number = " << z; } + void multi() { -int num1, num2, mul; -cout <<" \n Enter the First number = "; -cin >> num1; -cout << "\n Enter the Second number = "; -cin >> num2; -mul = num1 * num2; -cout <<"\n Multiplication of two numbers = " << mul; + int num1, num2, mul; + cout <<" \n Enter the First number = "; + cin >> num1; + cout << "\n Enter the Second number = "; + cin >> num2; + mul = num1 * num2; + cout <<"\n Multiplication of two numbers = " << mul; } + void division() { -int num1, num2, div = 0; -cout <<" \n Enter the First number = "; -cin >> num1; -cout << "\n Enter the Second number = "; -cin >> num2; -while ( num2 == 0) + float num1, num2; + float div = 0 ; + cout <<" \n Enter the First number = "; + cin >> num1; + cout << "\n Enter the Second number = "; + cin >> num2; + while ( num2 == 0) { - cout << "\n Divisor canot be zero" - "\n Please enter the divisor once again: "; + cout << "\n Divisor canot be zero" + "\n Please enter the divisor once again: "; cin >> num2; - } -div = num1 / num2; -cout <<"\n Division of two numbers = " << div; + } + div = num1 / num2; + cout <<"\n Division of two numbers = " << div; } + void sqr() { -int num1; -float sq; -cout <<" \n Enter a number to find the Square: "; -cin >> num1; -sq = num1 * num1; -cout <<" \n Square of " << num1<< " is : "<< sq; + int num1; + unsigned int sq; + cout <<" \n Enter a number to find the Square: "; + cin >> num1; + + + if(abs(num1) > UINT_MAX/abs(num1)) // checking for overflow + { + cout << "\n Integer Overflow"; + cin.clear(); + cin.ignore(INT_MAX, '\n'); + return; + } + + sq = num1 * num1; + cout <<" \n Square of " << num1<< " is : "<< sq; } + void srt() { -float q; -int num1; -cout << "\n Enter the number to find the Square Root:"; -cin >> num1; -q = sqrt(num1); -cout <<" \n Square Root of " << num1<< " is : "<< q; + float q; + int num1; + cout << "\n Enter the number to find the Square Root:"; + + cin >> num1; + + if(num1 < 0) // checking for neative numbers + { + cout << "\nNot posible to find square root!!"; + return; + } + q = sqrt(num1); + + cout <<" \n Square Root of " << num1<< " is : "<< q; }