x++ / Tutorials / 4. Calculator
Table of Contents
Lesson
From the last three lessons, you learned how to print values into the terminal, get user inputs, modify strings and numbers, and create branches. In this lesson, you will be using all of this knowledge to construct a calculator using x++. Even though it may seem difficult at first, it is just made up of simple components. Let's break it down.
Let's introduce the program and get some inputs from the user using the prt
and read
operators:
:: main.xpp
prt "Welcome to the x++ calculator!"
prt "-----"
read "Please enter your first number: " ?a
read "Please enter your second number: " ?b
prt "-----"
You can also use escape codes to format your texts, such as \t
to indent your text:
:: main.xpp
prt "Welcome to the x++ calculator!"
prt "-----"
read "Please enter your first number: " ?a
read "Please enter your second number: " ?b
prt "-----"
prt "Operators"
prt "\t[A] Addition"
prt "\t[S] Subtraction"
prt "\t[M] Multiplication"
prt "\t[D] Division"
read "Please enter your operator by typing the letter within the bracket: " ?o
prt "-----"
Currently, the numbers you got from the user are a string. You need to parse it into a number using the int
operator so you can use mathematical operators on it. You can also uppercase your operator using the upr
operator so you can allow both lowercase and uppercase answers:
:: main.xpp
prt "Welcome to the x++ calculator!"
prt "-----"
read "Please enter your first number: " ?a
read "Please enter your second number: " ?b
prt "-----"
prt "Operators"
prt "\t[A] Addition"
prt "\t[S] Subtraction"
prt "\t[M] Multiplication"
prt "\t[D] Division"
read "Please enter your operator by typing the letter within the bracket: " ?o
prt "-----"
int a
int b
upr o
Now we can use the if
operator to check what operator the user selected and act accordingly. Currently, there are four types of mathematical operators: add
, sub
, mul
, and div
. Let's use them:
:: main.xpp
prt "Welcome to the x++ calculator!"
prt "-----"
read "Please enter your first number: " ?a
read "Please enter your second number: " ?b
prt "-----"
prt "Operators"
prt "\t[A] Addition"
prt "\t[S] Subtraction"
prt "\t[M] Multiplication"
prt "\t[D] Division"
read "Please enter your operator by typing the letter within the bracket: " ?o
prt "-----"
int a
int b
upr o
if (o == "A") { add a b ?c } \
(o == "S") { sub a b ?c } \
(o == "M") { mul a b ?c } \
(o == "D") { div a b ?c }
Since you defined c
as the answer to the equation, you can simply output it to the terminal. Using string interpolation, you can also use variables within your strings:
:: main.xpp
prt "Welcome to the x++ calculator!"
prt "-----"
read "Please enter your first number: " ?a
read "Please enter your second number: " ?b
prt "-----"
prt "Operators"
prt "\t[A] Addition"
prt "\t[S] Subtraction"
prt "\t[M] Multiplication"
prt "\t[D] Division"
read "Please enter your operator by typing the letter within the bracket: " ?o
prt "-----"
int a
int b
upr o
if (o == "A") { add a b ?c } \
(o == "S") { sub a b ?c } \
(o == "M") { mul a b ?c } \
(o == "D") { div a b ?c }
prt "The answer to that equation is $(c)."
Tada! You got a working calculator. How cool is that?
Last Updated: March 9th, 2024 by iiPython