PowerShell Script to Create Simple Calculator
Read-Host, then uses switch to perform the calculation, like: $result = switch ($operator) { '+' { $num1 + $num2 } '-' { $num1 - $num2 } '*' { $num1 * $num2 } '/' { if ($num2 -eq 0) { 'Cannot divide by zero.' } else { $num1 / $num2 } } }.Examples
How to Think About It
switch or if statement. Finally, show the result or handle errors like division by zero.Algorithm
Code
$num1 = [double](Read-Host 'Enter first number') $num2 = [double](Read-Host 'Enter second number') $operator = Read-Host 'Enter operator (+, -, *, /)' switch ($operator) { '+' { $result = $num1 + $num2 } '-' { $result = $num1 - $num2 } '*' { $result = $num1 * $num2 } '/' { if ($num2 -eq 0) { Write-Output 'Cannot divide by zero.' exit } else { $result = $num1 / $num2 } } default { Write-Output 'Invalid operator.'; exit } } Write-Output "Result: $result"
Dry Run
Let's trace the input 5, 3, + through the code
Read first number
$num1 = 5
Read second number
$num2 = 3
Read operator
$operator = '+'
Perform addition
$result = 5 + 3 = 8
Output result
Result: 8
| Step | Variable | Value |
|---|---|---|
| 1 | $num1 | 5 |
| 2 | $num2 | 3 |
| 3 | $operator | + |
| 4 | $result | 8 |
Why This Works
Step 1: Input reading
The script uses Read-Host to get user input as strings, then converts numbers to [double] for calculations.
Step 2: Operation selection
The switch statement chooses the math operation based on the operator entered.
Step 3: Division check
Before dividing, the script checks if the second number is zero to avoid errors and informs the user.
Step 4: Output
The result is printed with Write-Output, showing the final calculation.
Alternative Approaches
$num1 = [double](Read-Host 'Enter first number') $num2 = [double](Read-Host 'Enter second number') $operator = Read-Host 'Enter operator (+, -, *, /)' if ($operator -eq '+') { $result = $num1 + $num2 } elseif ($operator -eq '-') { $result = $num1 - $num2 } elseif ($operator -eq '*') { $result = $num1 * $num2 } elseif ($operator -eq '/') { if ($num2 -eq 0) { Write-Output 'Cannot divide by zero.'; exit } else { $result = $num1 / $num2 } } else { Write-Output 'Invalid operator.'; exit } Write-Output "Result: $result"
function Calculate($a, $b, $op) { switch ($op) { '+' { return $a + $b } '-' { return $a - $b } '*' { return $a * $b } '/' { if ($b -eq 0) { return 'Cannot divide by zero.' } else { return $a / $b } } default { return 'Invalid operator.' } } } $num1 = [double](Read-Host 'Enter first number') $num2 = [double](Read-Host 'Enter second number') $operator = Read-Host 'Enter operator (+, -, *, /)' $result = Calculate $num1 $num2 $operator Write-Output "Result: $result"
Complexity: O(1) time, O(1) space
Time Complexity
The script performs a fixed number of operations regardless of input size, so it runs in constant time O(1).
Space Complexity
It uses a few variables for input and output, so space usage is constant O(1).
Which Approach is Fastest?
All approaches run in constant time; using a function improves readability but does not affect speed.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Switch statement | O(1) | O(1) | Simple, clear operation selection |
| If-else statements | O(1) | O(1) | Beginners familiar with if-else |
| Function encapsulation | O(1) | O(1) | Reusable and cleaner code |