1. Simple calculator - nothing fancy

```
/**program description:
*
* simple calculator written for beginners.
* This program asks user to enter an arithmetic
* expression that contains two numbers and one
* operator. User will be given the result of the
* calculation. Operators allowed to be used
* are: + - * / % ...
*
* Author: X-Shab
* Copyright 2008 - 2009
*
*/
#include<iostream>
#include<string>
using namespace std;
const string EXIT = "-1";
void add(float num1, float num2)
{
cout<<"Result is: "<< num1 + num2 <<endl;
}
void subtract(float num1, float num2)
{
cout<<"Result is: "<< num1 - num2 <<endl;
}
void multiply(float num1, float num2)
{
cout<<"Result is: "<< num1 * num2 <<endl;
}
void divide(float numerator, float denominator)
{
if (denominator != 0)
cout<<"Result is: "<< numerator / denominator <<endl;
else
cout<<"You can not divide by denominator\n";
}
void modulus(float num1, float num2)
{
cout<<"Result is: "<< (int)num1 % (int)num2 <<endl;
}
int main(void)
{
string mathematicalOperation;
float num1, num2;
char operatorSign;
cout << "Please enter an arithmetic expression (-1 to Exit)\n";
cin >> mathematicalOperation;
//search for operator sign and perform calculation
while(mathematicalOperation.compare(EXIT))
{
int getFirstDigitIndex = mathematicalOperation.find_first_of("0123456789");
int operatorSignIndex = mathematicalOperation.find_first_of("+-*/%", getFirstDigitIndex);
if (operatorSignIndex != -1)
{
operatorSign = mathematicalOperation.at(operatorSignIndex);
string firstNumber = mathematicalOperation.substr(0,operatorSignIndex);
string secondNumber = mathematicalOperation.substr(operatorSignIndex + 1);
//convert numbers from string to float
num1 = (float)atof(firstNumber.c_str());
num2 = (float)atof(secondNumber.c_str());
switch(operatorSign)
{
case '+':
add(num1,num2);
break;
case '-':
subtract(num1,num2);
break;
case '*':
multiply(num1,num2);
break;
case '/':
divide(num1,num2);
break;
case '%':
modulus(num1,num2);
break;
}
}
cout<<"Please Enter Another Expression or Enter -1 to Exit:\n";
cin>>mathematicalOperation;
}
cout<<"SEE YOU LATER!\n";
return 0;
}
```

2. Simple calculator using function template

```
/**program description:
*
* simple calculator written for intermediates.
* This program asks user to enter an arithmetic
* expression that contains two numbers and one
* operator. User will be given the result of the
* calculation. Operators allowed to be used
* are: + - * / % ...
*
* Author: X-Shab
* Copyright 2008 - 2009
*
*/
#include<iostream>
#include<string>
using namespace std;
const string EXIT = "-1";
template <class T>
void performCalculation(T num1, T num2, const char operatorSign)
{
switch(operatorSign)
{
case '+':
cout<<"Result is: "<< num1 + num2 <<endl;
break;
case '-':
cout<<"Result is: "<< num1 - num2 <<endl;
break;
case '*':
cout<<"Result is: "<< num1 * num2 <<endl;
break;
case '/':
if (num2 != 0)
cout<<"Result is: "<< num1 / num2 <<endl;
else
cout<<"You Can Not Divide by Zero\n";
break;
case '%':
cout<<"Result is: "<< (int)num1 % (int)num2 <<endl;
break;
default:
cout<<"wrong operator sign"<<endl;
}
}
int main(void)
{
string mathematicalOperation;
float num1, num2;
char operatorSign;
cout << "Please enter an arithmetic expression (-1 to Exit)\n";
cin >> mathematicalOperation;
while(mathematicalOperation.compare(EXIT))
{
int getFirstDigitIndex = mathematicalOperation.find_first_of("0123456789");
int operatorSignIndex = mathematicalOperation.find_first_of("+-*/%", getFirstDigitIndex);
if (operatorSignIndex != -1)
{
operatorSign = mathematicalOperation.at(operatorSignIndex);
string firstNumber = mathematicalOperation.substr(0,operatorSignIndex);
string secondNumber = mathematicalOperation.substr(operatorSignIndex + 1);
//convert numbers from string to float
num1 = (float)atof(firstNumber.c_str());
num2 = (float)atof(secondNumber.c_str());
performCalculation(num1, num2, operatorSign);
}
cout<<"Please Enter Another Expression or Enter -1 to Exit:\n";
cin>>mathematicalOperation;
}
cout<<"SEE YOU LATER!\n";
return 0;
}
```

1. Is the template in the second example is structured and used correctly?

2. I tried to make my code easy to understand by putting descriptive names. Do you find it easy to follow? Is the code structure well written?