Last active
September 29, 2023 21:14
-
-
Save Moosems/078093f21059dc73219060eb7bfe8660 to your computer and use it in GitHub Desktop.
Cool Mojo Calculator
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
# Cool Mojo Calculator | |
from types.input.input import input | |
def is_float(given_string: String) -> Bool: | |
# Given a string, go through each character and check if it is a digit or a dot. | |
for i in range(len(given_string)): | |
# If the character is a dot, check if it is the last character in the string. | |
if given_string[i] == "." and i == len(given_string) - 1: | |
return False | |
# Normal case, check if the character is a digit or a dot. | |
if isdigit(ord(given_string[i])) or given_string[i] == ".": | |
continue | |
return True | |
fn string_to_float32(owned origin_string: String) raises -> Float32: | |
# Start by checking if the given string is even a float. | |
if not is_float(origin_string): | |
raise Error("The given string is not a float.") | |
# Find the index of the dot. | |
var dot_index: Int = 0 | |
var found_dot: Bool = False | |
for i in range(len(origin_string)): | |
if origin_string[i] == ".": | |
dot_index = i | |
found_dot = True | |
break | |
if not found_dot: | |
dot_index = len(origin_string) | |
origin_string += ".0" | |
# Get the first half of the string and convert it to an integer. | |
var first_half_string: String = origin_string[0:dot_index] | |
if first_half_string == "": | |
first_half_string = "0" | |
let first_half: Int = atol(first_half_string) | |
# Get the second half of the string and convert it to an integer. | |
let second_half: Float32 = Float32(atol(origin_string[dot_index + 1:])) | |
# Get the length of the second half and divide it by 10 to the power of the length to get the float value. | |
let second_half_float: Float32 = second_half / (10 ** len(origin_string[dot_index + 1:])) | |
# Add the two halves together and return the result. | |
let result: Float32 = first_half + second_half_float | |
return result | |
fn main(): | |
# Define some variables. | |
var first_num_word: String = "first number" | |
var second_num_word: String = "second number" | |
let first_num: Float32 | |
let second_num: Float32 | |
# Start the program. | |
print("Welcome to my simple Mojo calculator!") | |
# Get the operation to perform. | |
let operation: String = input("Please enter the operation you would like to perform (+, -, *, /): ") | |
if operation == "/": | |
first_num_word = "dividend" | |
second_num_word = "divisor" | |
# Get the two numbers. | |
while True: | |
try: | |
first_num = string_to_float32(input("Please enter the " + first_num_word + ": ")) | |
second_num = string_to_float32(input("Please enter the " + second_num_word + ": ")) | |
break | |
except e: | |
print("Invalid input, please try again.") | |
# Perform the operation. | |
var result: Float32 = 0.0 | |
if operation == "+": | |
result = first_num + second_num | |
elif operation == "-": | |
result = first_num - second_num | |
elif operation == "*": | |
result = first_num * second_num | |
elif operation == "/": | |
result = first_num / second_num | |
# Print the result. | |
print("The result is: " + String(result)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment