Created
April 24, 2022 00:55
-
-
Save Colouratura/6c85c29335a646072ec740fdd88130d1 to your computer and use it in GitHub Desktop.
Simple reverse polish notation calculator written in VB.NET (no division support!)
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
Imports System | |
Module Program | |
Sub PrintHelp() | |
Console.WriteLine("rpncalc - version 0.1 - 2022") | |
Console.WriteLine("To use: type integer or operator") | |
Console.WriteLine("") | |
Console.WriteLine("Operators:") | |
Console.WriteLine(" + Adds all values on the stack together.") | |
Console.WriteLine(" - Substracts all values on the stack from each other.") | |
Console.WriteLine(" * Multiplies all values on the stack together.") | |
Console.WriteLine(" i Print all stack values to the terminal.") | |
Console.WriteLine(" q Quit the REPL.") | |
End Sub | |
Sub Main(args As String()) | |
Dim running As Boolean = True | |
Dim stack As New List(Of Integer) | |
If args.Length <> 0 Then | |
PrintHelp() | |
Return | |
End If | |
While running | |
Console.Write("> ") | |
Dim currentInput As String = Console.ReadLine() | |
currentInput = currentInput.Trim() | |
Select Case currentInput | |
Case "+" | |
Dim result As Integer = 0 | |
For Each item In stack | |
result += item | |
Next | |
stack.Clear() | |
stack.Add(result) | |
Console.WriteLine(String.Format("Result: {0}", result)) | |
Case "-" | |
Dim result As Integer = 0 | |
For Each item In stack | |
result -= item | |
Next | |
stack.Clear() | |
stack.Add(result) | |
Console.WriteLine(String.Format("Result: {0}", result)) | |
Case "*" | |
Dim result As Integer = 1 | |
For Each item In stack | |
result *= item | |
Next | |
stack.Clear() | |
stack.Add(result) | |
Console.WriteLine(String.Format("Result: {0}", result)) | |
Case "i" | |
For Each item In stack | |
Console.WriteLine(item) | |
Next | |
Case "q" | |
running = False | |
Continue While | |
Case Else | |
Try | |
Dim parsed As Integer = Integer.Parse(currentInput) | |
stack.Add(parsed) | |
Catch ex As Exception | |
Console.WriteLine(String.Format("Error! `{0}` is not a valid number or operator!", currentInput)) | |
Continue While | |
End Try | |
End Select | |
End While | |
End Sub | |
End Module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment