Skip to content

Instantly share code, notes, and snippets.

@mmandersheid
Created March 11, 2010 15:25
Show Gist options
  • Save mmandersheid/329233 to your computer and use it in GitHub Desktop.
Save mmandersheid/329233 to your computer and use it in GitHub Desktop.
A postfix calculator
import java.awt.*;
import java.awt.event.*;
public class Calculator {
private DoubleStack memory;
private CharStack operators;
private String postfix;
private double numbers[];
public Calculator(){
memory = new DoubleStack();
operators = new CharStack();
numbers = new double[100];
postfix = "";
}
public double getValue(String postfix, double x){
int i = 0;
memory.clear();
for(int n=0;n<postfix.length();n++){
char ch = postfix.charAt(n);
if('0'<=ch && ch<='9')
memory.push(ch-'0');
else {
switch(ch) {
case 'x':
memory.push(x);
break;
case '+':
double b = memory.pop();
double a = memory.pop();
memory.push(a+b);
break;
case '$':
memory.push(-memory.pop());
break;
case '-':
b = memory.pop();
a = memory.pop();
memory.push(a-b);
break;
case '*':
b = memory.pop();
a = memory.pop();
memory.push(a*b);
break;
case '/':
b = memory.pop();
a = memory.pop();
memory.push(a/b);
break;
case '^':
b = memory.pop();
a = memory.pop();
memory.push(Math.pow(a,b));
break;
}
}
}
return memory.pop();
}
public String infixToPostfix(String infix){
int n = 0;
postfix = "";
infix+=" ";
operators.clear();
boolean firstTime = true;
for(int i=0; i<infix.length();i++){
char ch = infix.charAt(i);
if(ch =='-' && firstTime)
ch='$';
if(ch!=' ')
firstTime = false;
if(isADigit(ch))
postfix += ch;
if(ch == 'x')
postfix += ch;
else if("+-*/^$".indexOf(ch)>=0){
while(leftFirst(operators.peek(),ch))
postfix += operators.pop();
operators.push(ch);
}
else if(ch == '('){
operators.push(ch);
firstTime = true;
}
else if(ch == ')') {
while(operators.peek()!='(')
postfix += operators.pop();
operators.pop();
}
}
while(!operators.empty())
postfix += operators.pop();
return postfix;
}
private boolean leftFirst(char a, char b)
{
if(a =='^' && b=='^')
return false;
int r = rank(a);
int s = rank(b);
return r>=s;
}
private int rank(char ch){
switch(ch) {
case'+': case'-':
return 1;
case'*': case'/':
return 2;
case'^':
return 3;
default:
return 0;
}
}
private boolean isADigit(char ch){
if((ch>='0' && ch<='9') || ch=='.')
return true;
return false;
}
}
public class CharStack {
private char data[];
private int top;
public CharStack(){
data = new char[100];
top = -1;
}
public void push(char c){
data[++top] = c;
}
public char pop(){
if(top<0)
return'(';
return data[top--];
}
public boolean empty(){
if(top==-1)
return true;
return false;
}
public char peek(){
if(top<0)
return '(';
return data[top];
}
public void clear(){
top=-1;
}
}
public class DoubleStack {
private double data[];
private int top;
public DoubleStack(){
data = new double[100];
top = -1;
}
public void push(double c){
data[++top] = c;
}
public double pop(){
return data[top--];
}
public boolean empty(){
if(top==-1)
return true;
return false;
}
public void clear(){
top=-1;
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class PanelApplet extends Applet
{
PostfixPanel panel;
public void init()
{
setLayout(null);
panel = new PostfixPanel();
add(panel);
panel.setSize(getSize().width, getSize().height);
panel.setLocation(0,0);
panel.setVisible(true);
panel.requestFocus();
if (panel instanceof KeyListener)
addKeyListener((KeyListener)panel);
}
}
import java.awt.*;
import java.awt.event.*;
public class PostfixPanel extends Panel implements ActionListener {
private Label postfixLabel, valueLabel, xLabel, infixLabel;
private TextField postfixField, valueField, xField, infixField;
private Calculator calculator;
public PostfixPanel(){
setBackground(new Color(100,0,50));
setLayout(null);
calculator = new Calculator();
postfixLabel = new Label("postfix:");
postfixLabel.setLocation(10,150);
postfixLabel.setSize(100,20);
postfixLabel.setForeground(Color.white);
add(postfixLabel);
postfixField = new TextField("");
postfixField.setLocation(10,170);
postfixField.setSize(200,20);
postfixField.setBackground(Color.white);
add(postfixField);
xLabel = new Label("x:");
xLabel.setLocation(10,60);
xLabel.setSize(20,20);
xLabel.setForeground(Color.white);
add(xLabel);
infixLabel = new Label("infix:");
infixLabel.setLocation(10,10);
infixLabel.setSize(100,20);
infixLabel.setForeground(Color.white);
add(infixLabel);
infixField = new TextField("");
infixField.setLocation(10,30);
infixField.setSize(200,20);
infixField.setBackground(Color.white);
add(infixField);
xField = new TextField("");
xField.setLocation(10,80);
xField.setSize(200,20);
xField.setBackground(Color.white);
add(xField);
valueLabel = new Label("value:");
valueLabel.setLocation(10,200);
valueLabel.setSize(100,20);
valueLabel.setForeground(Color.white);
add(valueLabel);
valueField = new TextField("");
valueField.setLocation(10,220);
valueField.setSize(200,20);
valueField.setBackground(Color.white);
add(valueField);
infixField.addActionListener(this);
xField.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
postfixField.setText(calculator.infixToPostfix(infixField.getText()));
String postfix = postfixField.getText();
double x = convert(xField.getText());
double value = calculator.getValue(postfix, x);
valueField.setText("" + value);
}
public static double convert(String s)
{
try
{
return (new Double(s)).doubleValue();
}
catch (NumberFormatException nfe)
{
return -9876.54;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment