Skip to content

Commit

Permalink
Chapter 3. Exercise 10. Primitive calculator.
Browse files Browse the repository at this point in the history
  • Loading branch information
Grandmother committed Mar 29, 2016
1 parent f51badf commit d885c7a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
46 changes: 46 additions & 0 deletions chapter 3/exercise_10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
#include <cmath>

int main()
{
std::string operation;
double operand1, operand2;
double result = std::nan("1");
bool exit = false;

std::cout << "Enter the operation and it's two operands please: ";
while ( std::cin >> operation >> operand1 >> operand2 )
{
if ( operation == "+" || operation == "plus" )
{
result = operand1 + operand2;
}
else if ( operation == "-" || operation == "minus" )
{
result = operand1 - operand2;
}
else if ( operation == "*" || operation == "mul" )
{
result = operand1 * operand2;
}
else if ( operation == "/" || operation == "div" )
{
result = operand1 / operand2;
}
else
{
result = std::nan("1");
}

if ( std::isnan(result) )
{
std::cout << "Error has occured. Try again.\n";
}
else
{
std::cout << operand1 << " " << operation << " " << operand2 << " = " << result << ";\n";
}
std::cout << "Enter the operation and it's two operands please: ";
}
std::cout << std::endl;
}
4 changes: 2 additions & 2 deletions chapter 3/exercise_9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
int main()
{
std::string number = " ";
std::cout << "\nEnter the numerals please: ";
std::cout << "Enter the numerals please: ";
while( std::cin >> number )
{
if ( number == "zero\n" )
if ( number == "zero" )
{
std::cout << "0\n";
}
Expand Down

0 comments on commit d885c7a

Please sign in to comment.