/* # # # # Author: Fabrice B. -> [contact:grafitos@nomade.fr] # # # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version # 2 of the License, or (at your option) any later version. # # */ import java.lang.*; import java.io.*; class automate_calcul { double op1,op2; char operator,val; String operande; int stat; int m_p; private void reset_automate () { op1=op2=0; stat=1; operande="0"; operator=0; m_p=0; } public automate_calcul() { reset_automate(); } private double result() { if(operator=='+') return op1+op2; if(operator=='-') return op1-op2; if(operator=='*') return op1*op2; if(operator=='/') return op1/op2; return 0; } private boolean is_operator() { if(val=='+' || val=='-' || val=='*' || val=='/') return true; else return false; } private boolean is_quantify() { if(val>='0' && val<='9') return true; else return false; } private boolean is_result() { if(val=='=') return true; else return false; } private boolean is_unary() { if(val=='-' || val==',' || val=='R' || val=='X' || val=='C' || val=='E') return true; else return false; } private void unary_operation() { if(val=='-') minus_plus(); if(val=='R')operande=Double.toString(Math.sqrt(number(operande))); if(val=='X')operande=Double.toString(1/number(operande)); if(val==',' && operande.indexOf(',')==-1) operande+=','; if(val=='C') reset_automate(); if(val=='E') operande="0"; } private void operande_add() { if(operande.equals("0")) operande=Character.toString(val); else operande+=val; } public void print_screen() { if(operande.length()>=2 && operande.substring(operande.length()-2,operande.length()).equals(".0")) operande=operande.substring(0,operande.length()-2); System.out.println(operande); } private double number(String e) { return Double.parseDouble(e.replace(',','.')); } private void minus_plus() { switch(m_p) { case 0: operande='-'+operande; m_p=1; break; case 1: operande=operande.substring(1); m_p=0; break; } } public void get_token( String mem ) { val=mem.charAt(0); if(is_unary()) { unary_operation(); } if(is_quantify()) { switch (stat) { case 1: operande_add(); break; case 2: operande=Character.toString(val); stat=3; break; case 3: operande_add(); break; case 4: operande=Character.toString(val); stat=1; break; } } if(is_operator()) { operator=val; switch(stat) { case 1: op1=number(operande); stat=2; break; case 2: break; case 3: op2=number(operande); operande=Double.toString(result()); op1=number(operande); stat=2; break; case 4: op1=number(operande); stat=2; break; } } if(is_result()) { switch(stat) { case 1: break; case 2: break; case 3: op2=number(operande); operande=Double.toString(result()); break; case 4: op1=number(operande); operande=Double.toString(result()); break; } stat=4; } if(val=='%') { switch (stat) { case 1: reset_automate(); break; case 2: op2=number(operande); operande=Double.toString(op2*op1/100); stat=3; break; case 3: op2=number(operande); operande=Double.toString(op2*op1/100); break; case 4: op2=number(operande)*op1/100; operande=Double.toString(result()); break; } } print_screen(); } } class automate_cal { public static void main (String argv[]) { automate_calcul mine=new automate_calcul(); mine.get_token("5"); mine.get_token("0"); mine.get_token("+"); mine.get_token("2"); mine.get_token("5"); mine.get_token("="); mine.get_token("%"); } }