-
Notifications
You must be signed in to change notification settings - Fork 0
/
CS116-PA2.cpp
76 lines (71 loc) · 2.62 KB
/
CS116-PA2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//Author: Brandon Tran
//Date: 05/04/2020
//Purpose: Create a simple calculator
#include <iostream> // Access input output stream
#include <iomanip> // Access manipulators
using namespace std; // Access cout, endl, cin
int main()
{
int x = 1; //counts line number
double N1, N2{}; //initialize variables
char operation{};
cout << "Welcome to use the Simple Calculator of Brandon Tran!" << endl;
while (true) //an infinite loop that can be stopped by user input of @
{ //while loop
cout << x++ << "============================================================." << endl;
cout << "Please enter your number, operator, and number > ";
cin >> N1 >> operation >> N2;
if (operation == '-')
{
cout << "Result: " << N1 << " " << operation << " " << N2 << " = " << N1 - N2 << endl;
}
else if (operation == '+')
{
cout << "Result: " << N1 << " " << operation << " " << N2 << " = " << N1 + N2 << endl;
}
else if (operation == '%')
{
while (N2 == 0)
{
cout << "Zero is not valid for division. Please enter a valid number > ";
cin >> N2;
}
if (N2 != 0)
{
cout << "Result: " << N1 << " " << operation << " " << N2 << " = " << fmod(N1, N2) << endl;
}
}
else if (operation == '/')
{
while (N2 == 0)
{
cout << "Zero is not valid for division. Please enter a valid number > ";
cin >> N2;
}
if (N2 != 0)
{
cout << "Result: " << N1 << " " << operation << " " << N2 << " = " << N1 / N2 << endl;
}
}
else if (operation == '*')
{
cout << "Result: " << N1 << " " << operation << " " << N2 << " = " << N1 * N2 << endl;
}
else if (operation == '^')
{
cout << "Result: " << N1 << " " << operation << " " << N2 << " = " << pow(N1, N2) << endl;
}
else if (operation == '@') //exit while loop if operation == @
{
break;
}
else
{
cout << "Sorry, the operator " << operation << " is not valid!" << endl;
}
}; //end of while loop
cout << "Thank you for using the Simple Calculator of Brandon Tran!" << endl;
cout << x++ << "============================================================." << endl;
return 0; //Indicates successful completion
}
// end main()