-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgmExplanation.html
More file actions
147 lines (121 loc) · 5.67 KB
/
pgmExplanation.html
File metadata and controls
147 lines (121 loc) · 5.67 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calc Explained</title>
<h1>Simple Calculator in C++ Explained</h1><hr>
<style>
h1 {text-align: center;}
hr {width: 40%;}
.code {padding-left: 3%;}
</style>
</head>
<body>
<code>
<h2>Full Code</h2>
<p class="code">
// C++ program to create calculator using <br>
// switch statement <br>
#include <iostream> <br>
using namespace std; <br><br>
// Main program <br>
main() <br><br>
{ <br>
 char op; <br>
 float num1, num2; <br><br>
 // It allows user to enter operator i.e. +, -, *, / <br>
 cin >> op; <br><br>
 // It allow user to enter the operands <br>
 cin >> num1 >> num2; <br><br>
 // Switch statement begins <br>
 switch (op) { <br><br>
  // If user enter + <br>
  case '+': <br>
    cout << num1 + num2; <br>
    break; <br><br>
  // If user enter - <br>
  case '-': <br>
    cout << num1 - num2; <br>
    break; <br><br>
  // If user enter * <br>
  case '*': <br>
    cout << num1 * num2; <br>
    break; <br><br>
  // If user enter / <br>
  case '/': <br>
    cout << num1 / num2; <br>
    break; <br><br>
  // If the operator is other than +, -, * or /, <br>
  // error message will display <br>
  default: <br>
    cout << "Error! operator is not correct"; <br>
    break; <br>
 } // switch statement ends <br><br>
 return 0; <br>
}
</p>
<h2>Why a Simple Calculator?</h2>
<p>
This is a simple calculator that anyone can make in c++. There is a simpler calculator but that would only do one mathmatical operation such as addition. While making a c++ program to add any two numbers is great for starting out (ex: 3+3), but there wouldn't be much to explain. <br><br>
The way the program works is it takes the operand (ex: +) and then two numbers (ex: 3 3) and then the program finds the switch statement that corresponds with the operand. In our example the code is finding the + operand and then what's inside the switch statement which would be num + num. In our example this would be 3+3 and it would then give use our answer of 6 as an output.
</p>
<h2>Example Input #1</h2>
<p>
Input:
+ 10 21
</p>
<p>
Output: 31
</p>
<h3>Code Used:</h3>
<p class="code">
main() <br><br>
{ <br>
char op; <br>
float num1, num2; <br><br>
// It allows user to enter operator i.e. +, -, *, / <br>
cin >> op; <br><br>
// It allow user to enter the operands <br>
cin >> num1 >> num2; <br><br>
// Switch statement begins <br>
switch (op) { <br><br>
// If user enter + <br>
case '+': <br>
cout << num1 + num2; <br>
break; <br><br>
</p>
<h2>Example Input #2</h2>
<p>
Input: / 10 2
</p>
<p>
Output: 5
</p>
<h3>Code Used:</h3>
<p class="code">
main() <br><br>
{ <br>
char op; <br>
float num1, num2; <br><br>
// It allows user to enter operator i.e. +, -, *, / <br>
cin >> op; <br><br>
// It allow user to enter the operands <br>
cin >> num1 >> num2; <br><br>
// Switch statement begins <br>
switch (op) { <br><br>
// If user enter / <br>
case '/': <br>
cout << num1 / num2; <br>
break; <br><br>
</p>
<h2><a href="https://www.geeksforgeeks.org/c-c-program-to-make-a-simple-calculator/">GeeksforGeeks</a> Quote</h2>
<q>A simple calculator can be made using a C++ program which is able to add, subtract, multiply and divide, two operands entered by the user. The switch and break statement is used to create a calculator.</q>
<h2>Fun Fact</h2>
<p>
Switch statements were used here but another method can be used is if and else statements. The only issue with that is that users won't get errors if they use the program in a way it wasn't meant to be used.
</p>
</code>
</body>
</html>