-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter 3. Exercise 10. Primitive calculator.
- Loading branch information
1 parent
f51badf
commit d885c7a
Showing
2 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters