/*
Calculator v1.0
(C) Sudipto Ghosh. 2018.
*/
#include <iostream>
using namespace std;
// get values for required operations
int getValue(){
cout << "Enter an integer: ";
int a; cin >> a;
return a;
}
// get the type of operation to be performed
int decideOperation(){
cout << "What do you want me to do with them?" << endl;
cout << "(1) Addition" << endl << "(2) Multiplication." << endl;
cout << "(3) Subtraction" << endl << "(4) Division."<< endl;
cout << "Your reply: ";
int a; cin >> a;
return a;
}
int add(int x, int y){
return x + y;
}
int multiply(int x, int y){
return x * y;
}
int subtract(int x, int y){
return x - y;
}
int divide(int x, int y){
return x / y;
}
// perform mathematical calculations based on
// decideOperation() on variables: 'x' and 'y'
// using function call to getValue()
int doCalculation(){
// acquire the variables
cout << "What is the first integer?" << endl;
int x = getValue();
cout << "What is the second one?" << endl;
int y = getValue();
// prompt user to decide type of operation
int o = decideOperation(); int r;
// addition
if (o==1) {
r = add(x,y);
}
// multiplication
if (o==2) {
r = multiply(x,y);
}
// subtraction
if (o==3) {
r = subtract(x,y);
}
// division (integer)
if (o==4) {
r = divide(x,y);
}
return r;
}
int main()
{
// greet the user with a friendly message
cout << "Haaallloooo!!" << endl;
cout << "Welcome to Calculator v1.0" << endl << "------------------------" << endl;
cout << "Give me the values to operate on, will ya?" << endl;
// get value of result
int r = doCalculation();
// print the result on console
cout << "The result is " << r << "." << endl;
return 0;
}
Just another simple calculator but with a lot of function calls. Learning about functions, that's all.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.