1 17 18 package calculator.listeners; 19 20 import Jmc.baseGui.*; 21 import Jmc.baseTools.*; 22 import Jmc.commonGui.*; 23 24 33 public class Calculator_li implements base_guiListener 34 { 35 class CHelper 36 { 37 private int cnt = 0; 38 39 protected double v[] = {0,0,0}; 40 protected char o[] = { ' ', ' '}; 41 42 public void pcmf_c () 43 { 44 this.cnt = 0; 45 }; 46 47 public void pcmf_ce () 48 { 49 if (this.cnt >= 1) 50 this.cnt--; 51 }; 52 53 public void pcmf_setVal(double xVal) 54 { 55 v[cnt++] = xVal; 56 }; 57 58 public double pcmf_setOp (char xOp) 59 { 60 if (cnt == 0) 61 return (v[cnt]); 62 63 if (xOp == '=') 64 return (this.pcmf_setOp('+')); 65 66 if (cnt > 1 && (o[cnt-2] == '/')) 67 { 68 v[cnt-2] = v[cnt-2]/v[cnt-1]; 69 cnt = cnt-1; 70 o[cnt-1] = xOp; 71 return (this.pcmf_setOp(xOp)); 72 } 73 if (cnt > 1 && (o[cnt-2] == '*')) 74 { 75 v[cnt-2] = v[cnt-2]*v[cnt-1]; 76 cnt = cnt-1; 77 o[cnt-1] = xOp; 78 return (this.pcmf_setOp(xOp)); 79 } 80 if (cnt > 1 && (o[cnt-2] == '+') && (xOp == '+' || xOp == '-')) 81 { 82 v[cnt-2] = v[cnt-2]+v[cnt-1]; 83 cnt = cnt-1; 84 o[cnt-1] = xOp; 85 return (this.pcmf_setOp(xOp)); 86 } 87 else 88 if (cnt > 1 && (o[cnt-2] == '-') && (xOp == '+' || xOp == '-')) 89 { 90 v[cnt-2] = v[cnt-2]-v[cnt-1]; 91 cnt = cnt-1; 92 o[cnt-1] = xOp; 93 94 return (this.pcmf_setOp(xOp)); 95 } 96 97 o[cnt-1] = xOp; 98 return (v[cnt-1]); 99 } 100 } 101 private String pem_disp = ""; 102 private CHelper pem_calc = new CHelper(); 103 112 public void pcmf_execListener(base_guiObj xParam) throws Exception 113 { 114 base_guiWidget_if l_disp = (base_guiWidget_if)base_registredObject.pcmf_getObjByName("display"); 116 117 String l_value = (String )xParam.pcmf_getValue(); 119 120 if (xParam instanceof base_eventChannel_if) 121 l_value = l_value.substring(l_value.length()-1); 122 123 if (l_value.equals("+")) 125 { 126 this.pem_calc.pcmf_setVal(Double.parseDouble(this.pem_disp)); 127 128 l_disp.pcmf_setValue(Double.toString(pem_calc.pcmf_setOp('+'))); 130 this.pem_disp = "0"; 131 } 132 else 133 if (l_value.equals("-")) 134 { 135 this.pem_calc.pcmf_setVal(Double.parseDouble(this.pem_disp)); 136 137 l_disp.pcmf_setValue(Double.toString(pem_calc.pcmf_setOp('-'))); 139 this.pem_disp = "0"; 140 } 141 else 142 if (l_value.equals("*")) 143 { 144 this.pem_calc.pcmf_setVal(Double.parseDouble(this.pem_disp)); 145 146 l_disp.pcmf_setValue(Double.toString(pem_calc.pcmf_setOp('*'))); 148 this.pem_disp = "0"; 149 } 150 else 151 if (l_value.equals("/")) 152 { 153 this.pem_calc.pcmf_setVal(Double.parseDouble(this.pem_disp)); 154 155 l_disp.pcmf_setValue(Double.toString(pem_calc.pcmf_setOp('/'))); 157 this.pem_disp = "0"; 158 } 159 else 160 if (l_value.equals("C")) 161 { 162 l_disp.pcmf_setValue("0"); 163 this.pem_disp = "0"; 164 this.pem_calc.pcmf_c(); 165 } 166 else 167 if (l_value.equals("CE")) 168 { 169 l_disp.pcmf_setValue("0"); 170 this.pem_disp = "0"; 171 this.pem_calc.pcmf_ce(); 172 } 173 else 174 if (l_value.equals("=")) 175 { 176 this.pem_calc.pcmf_setVal(Double.parseDouble(this.pem_disp)); 177 178 l_disp.pcmf_setValue(Double.toString(pem_calc.pcmf_setOp('='))); 180 this.pem_disp = l_disp.pcmf_getValue().toString(); 181 this.pem_calc.pcmf_c(); 182 } 183 else 184 { 185 if (l_value.equals(".")==false && l_disp.pcmf_getValue().equals("0") || this.pem_disp.equals("0")) 186 this.pem_disp = l_value; 187 else 188 this.pem_disp += l_value; 189 190 l_disp.pcmf_setValue(this.pem_disp); 191 } 192 193 l_disp.pcmf_repaint(); 195 196 return; 197 } 198 } | Popular Tags |