[java] Simple java arithmetic calculator

Started by
1 comment, last by killnine 18 years, 5 months ago
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))
				{
				p.println ("ADD  NUM" + k);
				}

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

			else
				{

				if(k==1)
					{
					p.println ("LOAD NUM1");
					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());
		}

	}
} 
Advertisement
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.
Wow, interesting. Thanks for the tutorial link, too! Ill give it a go later tonight

This topic is closed to new replies.

Advertisement