app-1: Inches to cm (centimeters) converter (Code)

by shian ⌂ @, Israel, Thursday, February 06, 2020, 16:44 (20 days ago) @ shian

A bit hard for really beginners - yet interesting!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


-- app-1: Inches to cm (centimeters) converter

-- ( 1 in = 2.54 cm; 1 cm = 0.39370079 in )

-- I used the editor 'edu', from 'Lib2 version 1.41' to create this
-- application. In 'edu' press Control-K on any keyword to get help.


-- include the standard routine 'prompt_number()' from library get.e
include get.e


-- constants (read-only variables of any kind) are making the code
-- easier to read, understand and maintain - so let's use them for
-- the variable 'choice':
constant QUIT = 0,
     IN_TO_CM = 1,
     CM_TO_IN = 2


constant SCREEN = 1 -- Yes, the screen device number is always 1


-- variables: atom can hold any number; sequence can hold any sequence
--            of numbers or complex structure of numbers, or a "string"
--            (there is no actual string-type in Euphoria...):
sequence prompt
atom choice, number, result


-- clean the screen (the "black-box"/"terminal"/"console")
clear_screen()


-- let's impress the user a bit with this title ('\n'=new line):
puts(SCREEN, "An Advanced in2cm Converter(c)\n")
puts(SCREEN, "------------------------------\n\n")


-- run the program inside an endless loop:
while 1 = 1 do

    -- choose what to convert (press 0 to quit)
    prompt = "Enter [0,1,2] - Quit, inch-to-cm, cm-to-inch: "
    choice = prompt_number(prompt, {0, 2})

    choice = floor(choice) -- round down the user's choice


    if choice = IN_TO_CM then
        number = prompt_number("\tEnter value in inch: ", {})
        result = number * 2.54
        printf(SCREEN, "\t%g inches are %g centimeters", {number, result})

    elsif choice = CM_TO_IN then
        number = prompt_number("\tEnter value in centimeters: ", {})
        result = number * 0.39370079
        printf(SCREEN, "\t%g centimeters are %g inches", {number, result})

    elsif choice = QUIT then
        puts(SCREEN, "----------------------------------------------\n")
        puts(SCREEN, "Thanks for using our advanced in2cm converter!\n")
        exit -- exit the program's while-loop (end the program)

    end if

    puts(SCREEN, "\n\n\n") -- print 3 empty new-lines

end while -- return to the start of this loop

-- here the program ends.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--
"Everyone knows the truth about himself, except the fool who considers himself wise". (www.y-dat.com).


Complete thread:

 RSS Feed of thread

powered by my little forum