Arithmetica User's Manual

 

Table of Contents

Section 1 ~ Intro to and General Overview of Arithmetica
Section 2 ~ Specific Features: Examples, and Expected Output
2.1 Constants
2.2 Binary Operators
2.3 Unary Operators
2.4 Parentheses
2.5 Assignment
2.6 Variables
  2.7 Comments
  2.8 Postfix Unary Operators
Section 3 ~ Possible Extensions to Arithmetica

 

Section 1 ~ Intro and General Overview of Arithmetica

         Welcome to Arithmetica, a simple interactive arithmetic interpreter. This program allows users to enter simple arithmetic expressions while the program keeps track of them. The program utilizes exptression trees to keep track of these. Types of Expressions the user can input are explained in the following section. To begin the program, enter arithmetica at the prompt. The program looks like this when first started:

    
Welcome to Arithmetica!
->

Section 2 ~ Specific Features: Examples and Expected Output

2.1 Constants

User can enter any integer constant, and the program will output the constant, also saving the constant as its current expression.

Welcome to Arithmetica
-> 5
5
-> 100
100

 

2.2 Binary Operators
 

Binary Operators combine 2 expressions into a single expression (in precedence order). The following binary operators have been implemented, in precedence order from highest to lowest

^
power
%
remainder
*
times
/
divide
+
plus
-
minus

The following shows program output when binary operators are used:

Welcome to Arithmetica
-> 2 + 3 * 8
13
-> 5 % 2
1
-> 5 - 2 + 6 * 2 / 3
7

 

2.3 Unary Operators

Unary operators prefix an expression. Negation is implemented here. This is translated as a '-' is placed directly before an operand or parenthesis. If there is a space between the '-' and any other input, it is assumed to be a minus rather than a negative. The negative sign has a higher precedence than all implemented binary operators. The following shows an example output.

Welcome to Arithmetica
-> -2
-2
-> 3 * -5
-15
-> - 10
-25

 

2.4 Parentheses

Parentheses () are also implemented, and they have the highest precedence. More example output:

Welcome to Arithmetica
-> (2 + 3) * 8
30
-> (5 % 2) - (5 - 4)
0
-> 5 + (2 + 6)
13

 

2.5 Assignment

Expressions can be assigned to a specific variable that the user specifies. This is done using '=' and it has the lowest precedence. Expressions not assigned to a variable are saved as current. Variables can be retrieved and evaluated at any time. Any variable name user inputs that has not already been defined is defined as 0. Following output:

Welcome to Arithmetica
-> a = 5
5
-> 2 + 7
9
-> current
9
-> a
5

 

2.6 Variable

Not only can a user assign expressions to variables, these variables' values can be used in future expressions.

Welcome to Arithmetica
-> a = 5
5
-> 2 + 7
9
-> a + current
14
-> current
14

 

2.7 Comments

Lines of input beginning with a '#' are taken as comments and are ignored.

Welcome to Arithmetica
-> #adfjdaf
user input comment, ignored!
->

 

2.8 Postfix Unary Operators

As extra credit, ability to evaluate a factorial '!' was implemented.

Welcome to Arithmetica
-> 3!
6
-> 2 * 0!
2

 

Section 3 ~ Possible Extensions to Arithmetica

          Not all of the extra credit was implemented. Unary functions are possible, it would involve, I think, making the function names "special" words that cannot be used as variables. It would be the same implementation as parentheses, but with these function names as part of the operator, and can be evaluated in exptree.

 

 

 

 

Arithmetica
my CPS 108 page