# [java] Simple java arithmetic calculator

This topic is 4699 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys! Well a friend and I have been working on a small project to make a java-based arithmetic calculator. All it does it take a string from an inputdialog box and stringtokenize it into components (which are put into an array). Through some if/else statements, we determine if a given number is added or subtracted, and concatenate text to the value (which is eventually stored to a text file). What this means to do is take a string and output it in LCM (little man computer assembly) format. However, this really shouldn't be that important to understand to help us out. What we cant figure out is how to have our program deal with parenthesis in the input string. What we were trying to do is the following: Ex: 12 + 2 + (4 - 1) + 2 1) If the program notices a parenthesis, it evaluates what is inside and calls the returning value TEMP. 2) Then, the program acts like it does currently, outputting 12 + 2 + 3 + 2 However, this is easier said than done. Does anyone have any ideas? Here is the working code we have so far.
 import javax.swing.*;
import java.util.*;
import java.io.*;

public class Main
{

public static void main (String[] args)

{
int i,length,z,j,k,l,m,n,o,f;
String equation,plus,minus,d,e;

equation = JOptionPane.showInputDialog (null, "Type Equation With Spaces");
StringTokenizer st = new StringTokenizer(equation);

z=st.countTokens();
i=0;
String[] token = new String[z];
String[] Numbers = new String[z];

while(st.hasMoreTokens())
{
token = st.nextToken();
i++;
}

plus="+";
minus="-";
j=0;
k=1;
l=0;

FileOutputStream out;
PrintStream p;

try {

out = new FileOutputStream("myfile.txt");
p = new PrintStream(out);

p.println (".LMC");

while(j!=i)
{
d = token[j];

if(d.equals(plus))
{
}

else if(d.equals(minus))
{
p.println ("SUB  NUM" + k);
}

else
{

if(k==1)
{
Numbers[l] = d;
k++;
l++;
}

else
{
Numbers[l] = d;
k++;
l++;
}
}
j++;
}

p.println ("PRINT");
p.println ("STOP");

m=0;
n=1;
o=0;
while(m!=l)
{
e = Numbers[o];
f = e.length();

if (f == 1)
{
p.println (".DC  NUM"+n+" 00"+e);
++m;
++n;
++o;
}

else if (f == 2)
{
p.println (".DC  NUM"+n+" 0"+e);
++m;
++n;
++o;
}

else
{
p.println (".DC  NUM"+n+" "+e);
++m;
++n;
++o;
}
}
p.println (".END");
}

catch (Exception q)
{
javax.swing.JOptionPane.showMessageDialog(null, q.toString());
}

}
} 

##### Share on other sites
There are two common ways to evaluate arithmetic expressions. One uses two stack data structures and the other uses a tree (either explicit or implicit via recursive decent parsing).

Let's say you wanted to evauluate the expression 5*(3+5). Obviously, we evauluate whatever is inside the parenthesis first.

This expression (and all others) can easily be represented by a binary tree:

//      (*)//      / \//    (5) (+)//        / \//      (3) (5)

The way the tree is set up, 3 will be added to 5 and an 8 will be substituted into the node where the + is now. Then 5 will be multiplied by 8 and 40 will be substitued for the *, which happens to be the root node of the tree.

Evaluating an expression once you've constructed a tree for it is straightforward. Perhaps trickier is taking an infix arithmetic expression and constructing a tree from it. You can learn about building the trees here.

##### Share on other sites
Wow, interesting. Thanks for the tutorial link, too! Ill give it a go later tonight

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
12
5. 5

• 14
• 9
• 22
• 9
• 31
• ### Forum Statistics

• Total Topics
632619
• Total Posts
3007491
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!