We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Anyone know how to build a calculator
Page Index Toggle Pages: 1
Anyone know how to build a calculator? (Read 788 times)
Anyone know how to build a calculator?
Feb 5th, 2007, 2:09pm
 
I am trying to create a graph program with creates graphs based on the equations you enter.  (More Specifically, graph complex numbers on the argand diagram, and I think it shouldn't be much harder than normal graphs if you treat i just as any other variable) Naturally, I'd need a built in calculator. I've finished the GUI side of the program, its just the guts I need. As you can see my attempt, it is pathetic. It cannot handle variables, just numbers. There is no order of operation either, so 1+2*2+1=7... Please give some tips.

Code:

void ParseString()
{
String side[]=split(StringEdit,'=');
//LHS and RHS
int Mdlvl;
int Ptlvl;
float Number=0;

int mode=1;// 1=add,2=sub,3=multi,4=div;
int dec;//1 - dec, 2= no


int nI;
int nX;
int nY;

//Lets do linear first
int NRadians;
int distance;
String tempnumber="";
for (int i=0;i<side[1].length();i++)
{
if (side[1].charAt(i)=='1' || side[1].charAt(i)=='2' || side[1].charAt(i)=='3' || side[1].charAt(i)=='4' || side[1].charAt(i)=='5' || side[1].charAt(i)=='6' || side[1].charAt(i)=='7' || side[1].charAt(i)=='8' || side[1].charAt(i)=='9' || side[1].charAt(i)=='0' || side[1].charAt(i)=='.')
{
tempnumber=tempnumber+side[1].charAt(i);
}

if ((side[1].charAt(i)=='/') ||(side[1].charAt(i)=='+') || (side[1].charAt(i)=='-')|| (side[1].charAt(i)=='*') || i==side[1].length()-1)
{
if (mode==1)
{
Number=Number+float(tempnumber);
tempnumber="";
}
if (mode==2)
{
Number=Number-float(tempnumber);
tempnumber="";
}
if (mode==3)
{
Number=Number*float(tempnumber);
tempnumber="";
}
if (mode==4)
{
Number=Number/float(tempnumber);
tempnumber="";
}
}


if (side[1].charAt(i)=='+')
{
mode=1;
}
if (side[1].charAt(i)=='-')
{
mode=2;
}
if (side[1].charAt(i)=='*')
{
mode=3;
}
if (side[1].charAt(i)=='/')
{
mode=4;
}



}
println (Number);
}
Re: Anyone know how to build a calculator?
Reply #1 - Feb 5th, 2007, 5:24pm
 
Most useful equations tend to be a transformation. You would plot f(x) to get y. You know where x is, but you run it through f() to get y.

I put some useful graphs like sigmoid transformation and normal distribution (bell curve) here on Seltar's Snippets.

You should model your operator precedence on the Java standard, so as not to confuse yourself.

http://www.uni-bonn.de/~manfear/javaoperators.php

Considering that you're doing some quite difficult string operations I would recommend performing the task using regular expressions.

http://java.sun.com/docs/books/tutorial/essential/regex/

They're hard to get into, but once you've sat down and played with it for an hour or so you find that you can turn ten lines of Java code into one regex. You can perform matches, replacements and other operations quickly and you can take this skill into other languages (I like Perl me).

Good luck.
Page Index Toggle Pages: 1