Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: implementation of a calculator #302

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions exercises/calculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,76 @@
Multiplication: 8
Division: 2
*/

#include <iostream>
using namespace std;
#define EXIT_SUCCESS 0


float switchOp(int x, int a, int b){
float result;

switch(x){
case 1:{
cout << endl << "SUM = ";
result = a + b;
break;
}

case 2:{
cout << endl << "Difference = ";
result = a - b;
break;
}

case 3:{
cout << endl << "Multiplication = ";
result = a * b;
break;
}

case 4:{
cout << endl << "Division = ";
result = a / b;
break;
}
}
Helias marked this conversation as resolved.
Show resolved Hide resolved
return result;
Helias marked this conversation as resolved.
Show resolved Hide resolved
}

void operations(float a, float b){

int op;

while(true){

cout << endl << "Select the operation: " << endl << "1) SUM" << endl << "2)Difference" << endl << "3)Multiplication" << endl << "4)Division" << endl;
cin >> op;

if(op <1 || op >4){
cerr << "Wrong choice, please select a valid operation number [1-4]" << endl;
}

else if(op == 4 && b == 0){
cerr << "Can't divide a number for 0, please select another operation" << endl;
}

else{
break;
}
}
cout << switchOp(op, a, b) << endl << endl;
}

int main(){

float a,b;
cout << "Insert the first number: ";
cin >> a;
cout << endl << "Insert second number: ";
cin >> b;

operations(a, b);

return EXIT_SUCCESS;
}
Helias marked this conversation as resolved.
Show resolved Hide resolved