1 | |
2 | package rossi.dfp; |
3 | |
4 | public class dfpcalc |
5 | { |
6 | public static void main(String args[]) throws Exception |
7 | { |
8 | dfp a, b, tmp; |
9 | dfp r[]; |
10 | String str; |
11 | String dec_a; |
12 | String dec_b; |
13 | String dec_r1; |
14 | String dec_r2; |
15 | String dec_r3; |
16 | String dec_r4; |
17 | String dfpClass = System.getProperty("dfp.class", "rossi.dfp.dfp"); |
18 | boolean display_on = true; |
19 | int operations = 0; |
20 | java.io.DataInputStream in = new java.io.DataInputStream(System.in); |
21 | |
22 | if (args.length > 0 && args[0].charAt(0)=='n') |
23 | display_on = false; |
24 | |
25 | a=(dfp)Class.forName(dfpClass).newInstance(); |
26 | b=a.newInstance("0"); |
27 | r = new dfp[4]; |
28 | r[0] = a.newInstance("0"); |
29 | r[1] = a.newInstance("0"); |
30 | r[2] = a.newInstance("0"); |
31 | r[3] = a.newInstance("0"); |
32 | |
33 | if (display_on) |
34 | { |
35 | System.out.print("\033[2J\1b[H\n\n"); |
36 | |
37 | System.out.print("\n\n\n\n\n\n\n\n\n\nCommands:\n"+ |
38 | "+ B=A+B - B=A/B * B=A*B / B=A/B\n"+ |
39 | "N B=-B S B=sqrt(B) \n"+ |
40 | "X A=B, B=A\n"+ |
41 | "D A=B\n"+ |
42 | "Pn Rn=B Store B in register n\n"+ |
43 | "Rn B=Rn Copy register n to B \n"+ |
44 | "Q Quit\n"); |
45 | } |
46 | |
47 | do |
48 | { |
49 | if (display_on) |
50 | { |
51 | dec_a = a.toString(); |
52 | dec_b = b.toString(); |
53 | dec_r1 = r[0].toString(); |
54 | dec_r2 = r[1].toString(); |
55 | dec_r3 = r[2].toString(); |
56 | dec_r4 = r[3].toString(); |
57 | |
58 | System.out.print("\033[H"+ |
59 | "R1 = "+dec_r1+"\033[K\n"+ |
60 | "R2 = "+dec_r2+"\033[K\n"+ |
61 | "R3 = "+dec_r3+"\033[K\n"+ |
62 | "R4 = "+dec_r4+"\033[K\n"+ |
63 | "-------------------------\033[K\n"+ |
64 | "A = "+dec_a+"\033[K\n"+ |
65 | "B = "+dec_b+"\033[K\n"+ |
66 | "-------------------------\033[K\n"+ |
67 | ">\033[K"); |
68 | } |
69 | str = in.readLine().toUpperCase(); |
70 | if (str.length() == 0) |
71 | str = "0"; |
72 | |
73 | switch (str.charAt(0)) |
74 | { |
75 | case '+': |
76 | b = b.add(a); |
77 | operations++; |
78 | break; |
79 | |
80 | case '-': |
81 | b = a.subtract(b); |
82 | operations++; |
83 | break; |
84 | |
85 | case '*': |
86 | b = b.multiply(a); |
87 | operations++; |
88 | break; |
89 | |
90 | case '/': |
91 | b = a.divide(b); |
92 | operations++; |
93 | break; |
94 | |
95 | case '^': |
96 | b = dfpmath.pow(a, b); |
97 | operations++; |
98 | break; |
99 | |
100 | case 'L': |
101 | b = dfpmath.ln(b); |
102 | break; |
103 | |
104 | case 'E': |
105 | b = dfpmath.exp(b); |
106 | break; |
107 | |
108 | case 'S': |
109 | b = b.sqrt(); |
110 | break; |
111 | |
112 | case 'I': |
113 | b = dfpmath.sin(b); |
114 | break; |
115 | |
116 | case 'O': |
117 | b = dfpmath.cos(b); |
118 | break; |
119 | |
120 | case 'T': |
121 | b = dfpmath.tan(b); |
122 | break; |
123 | |
124 | case 'A': |
125 | b = dfpmath.atan(b); |
126 | break; |
127 | |
128 | /* |
129 | case 'W': |
130 | System.out.println("\n\n"+ |
131 | Integer.toHexString(b.Mant.data[0])+" "+ |
132 | Integer.toHexString(b.Mant.data[1])+" "+ |
133 | Integer.toHexString(b.Mant.data[2])+" "+ |
134 | Integer.toHexString(b.Mant.data[3])+" "+ |
135 | b.Exp); |
136 | break; |
137 | */ |
138 | |
139 | case 'X': |
140 | tmp = b; |
141 | b = a; |
142 | a = tmp; |
143 | break; |
144 | |
145 | case 'D': |
146 | a = a.newInstance(b); |
147 | break; |
148 | |
149 | case 'N': |
150 | b = b.negate(); |
151 | break; |
152 | |
153 | case 'P': |
154 | r[(str.charAt(1)-'0'-1) & 3] = a.newInstance(b); |
155 | break; |
156 | |
157 | case 'R': |
158 | b = a.newInstance(r[(str.charAt(1)-'0'-1) & 3]); |
159 | break; |
160 | |
161 | default: |
162 | a = a.newInstance(b); |
163 | b = a.newInstance(str); |
164 | break; |
165 | } |
166 | |
167 | } while(str.charAt(0) != 'Q'); |
168 | |
169 | /* |
170 | if (!display_on) |
171 | { |
172 | start_time = (time(NULL) - start_time); |
173 | printf("Done %d operations. time = %d seconds. FLOPS = %d\n\n\n", operations, start_time, operations/start_time); |
174 | a.dfp2ascii(dec_a); |
175 | b.dfp2ascii(dec_b); |
176 | r[0].dfp2ascii(dec_r1); |
177 | r[1].dfp2ascii(dec_r2); |
178 | r[2].dfp2ascii(dec_r3); |
179 | r[3].dfp2ascii(dec_r4); |
180 | |
181 | printf( |
182 | "R1 = %s\n" |
183 | "R2 = %s\n" |
184 | "R3 = %s\n" |
185 | "R4 = %s\n" |
186 | "-------------------------\n" |
187 | "A = %s\n" |
188 | "B = %s\n" |
189 | "-------------------------\n" |
190 | ">", dec_r1, dec_r2, dec_r3, dec_r4, dec_a, dec_b); |
191 | } |
192 | */ |
193 | |
194 | } |
195 | } |