author | mickeyl <mickeyl> | 2005-08-23 10:06:07 (UTC) |
---|---|---|
committer | mickeyl <mickeyl> | 2005-08-23 10:06:07 (UTC) |
commit | d6e8e6bf49bb176ec54efed5eb0d012098f056d7 (patch) (unidiff) | |
tree | 7fc70f3d3b41ee05ddb2ea559c7339a18ef0464b /noncore/tools/calc2/engine.cpp | |
parent | ce5b18efcb27c853ed4af2b603a82d3f89097764 (diff) | |
download | opie-d6e8e6bf49bb176ec54efed5eb0d012098f056d7.zip opie-d6e8e6bf49bb176ec54efed5eb0d012098f056d7.tar.gz opie-d6e8e6bf49bb176ec54efed5eb0d012098f056d7.tar.bz2 |
make calc2 a subdir project
Diffstat (limited to 'noncore/tools/calc2/engine.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/tools/calc2/engine.cpp | 221 |
1 files changed, 0 insertions, 221 deletions
diff --git a/noncore/tools/calc2/engine.cpp b/noncore/tools/calc2/engine.cpp deleted file mode 100644 index 74cd701..0000000 --- a/noncore/tools/calc2/engine.cpp +++ b/dev/null | |||
@@ -1,221 +0,0 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | |||
21 | #include "engine.h" | ||
22 | |||
23 | /* OPIE */ | ||
24 | #include <opie2/odebug.h> | ||
25 | |||
26 | /* QT */ | ||
27 | #include <qstring.h> | ||
28 | #include <qlcdnumber.h> | ||
29 | |||
30 | /* STD */ | ||
31 | #include <math.h> | ||
32 | |||
33 | Data Engine::evalStack (Data num, bool inbrace = FALSE) | ||
34 | { | ||
35 | if (state != sError) { | ||
36 | Instruction *i; | ||
37 | |||
38 | // Pop the next op from the stack | ||
39 | while (!stack.isEmpty () && (braces || !inbrace)) { | ||
40 | i = stack.pop (); | ||
41 | |||
42 | // Check this ops prec vs next ops prec | ||
43 | if (!stack.isEmpty ()) | ||
44 | if (i->precedence <= stack.top()->precedence) | ||
45 | i->acc = evalStack (i->acc, inbrace); | ||
46 | |||
47 | // Evaluate this instruction | ||
48 | num = i->eval (num); | ||
49 | |||
50 | // Error-check ( change this to work for all types ) | ||
51 | if (isnan (num.dbl) || isinf (num.dbl)) { | ||
52 | odebug << "bad result from operation" << oendl; | ||
53 | state = sError; | ||
54 | clearData(&num); | ||
55 | return num; | ||
56 | } | ||
57 | } | ||
58 | } | ||
59 | return num; | ||
60 | } | ||
61 | |||
62 | // Plugins call this to request the stack be evaluated | ||
63 | void Engine::eval () | ||
64 | { | ||
65 | num = evalStack (num); | ||
66 | if (state != sError) { | ||
67 | displayData(num); | ||
68 | state = sStart; | ||
69 | } | ||
70 | // if the user didnt close all their braces, its no big deal | ||
71 | braces = 0; | ||
72 | } | ||
73 | |||
74 | void Engine::immediateInstruction (Instruction * i) | ||
75 | { | ||
76 | if (state != sError) { | ||
77 | i->setRep(currentRep); | ||
78 | num = i->eval (num); | ||
79 | displayData(num); | ||
80 | state = sStart; | ||
81 | } | ||
82 | } | ||
83 | |||
84 | void Engine::pushInstruction (Instruction * i) | ||
85 | { | ||
86 | if (state != sError) { | ||
87 | i->setRep(currentRep); | ||
88 | i->acc = num; | ||
89 | stack.push (i); | ||
90 | state = sStart; | ||
91 | } | ||
92 | } | ||
93 | |||
94 | void Engine::pushValue (char v) | ||
95 | { | ||
96 | if (state == sAppend) { | ||
97 | bool ok = FALSE; | ||
98 | switch (currentRep) { | ||
99 | case rDouble: | ||
100 | displayString.append(v); | ||
101 | num.dbl=displayString.toDouble(&ok); | ||
102 | break; | ||
103 | case rFraction: | ||
104 | break; | ||
105 | default: | ||
106 | displayString.append(v); | ||
107 | num.i=displayString.toInt(&ok, calcBase()); | ||
108 | }; | ||
109 | if (!ok) { | ||
110 | state = sError; | ||
111 | odebug << "pushValue() - num->string conversion" << oendl; | ||
112 | } else { | ||
113 | const QString constString = displayString; | ||
114 | emit(display(constString)); | ||
115 | }; | ||
116 | |||
117 | } else if (state == sStart) { | ||
118 | softReset(); | ||
119 | displayString.truncate(0); | ||
120 | state = sAppend; | ||
121 | pushValue (v); | ||
122 | } else if (state == sError) { | ||
123 | odebug << "in error state" << oendl; | ||
124 | return; | ||
125 | } | ||
126 | } | ||
127 | |||
128 | void Engine::del () | ||
129 | { | ||
130 | bool ok; | ||
131 | switch (currentRep) { | ||
132 | case rDouble: | ||
133 | displayString.truncate(displayString.length()); | ||
134 | num.dbl=displayString.toDouble(&ok); | ||
135 | break; | ||
136 | case rFraction: | ||
137 | odebug << "not available" << oendl; | ||
138 | break; | ||
139 | default: | ||
140 | displayString.truncate(displayString.length()); | ||
141 | num.i = displayString.toInt(&ok, calcBase()); | ||
142 | }; | ||
143 | |||
144 | if (!ok) { | ||
145 | state = sError; | ||
146 | odebug << "del() - num->string conversion" << oendl; | ||
147 | } else { | ||
148 | const QString constString = displayString; | ||
149 | emit(display(constString)); | ||
150 | }; | ||
151 | } | ||
152 | |||
153 | void Engine::displayData(Data d) { | ||
154 | switch (currentRep) { | ||
155 | case rDouble: | ||
156 | displayString.setNum(d.dbl); | ||
157 | break; | ||
158 | case rFraction: | ||
159 | odebug << "fractional display not yet impl" << oendl; | ||
160 | break; | ||
161 | default: | ||
162 | displayString.setNum(d.i, calcBase()); | ||
163 | break; | ||
164 | }; | ||
165 | const QString constString= displayString; | ||
166 | emit(display(constString)); | ||
167 | } | ||
168 | |||
169 | // Returns the base when Rep is an integer type | ||
170 | int Engine::calcBase () { | ||
171 | switch (currentRep) { | ||
172 | case rBin: | ||
173 | return 2; | ||
174 | case rOct: | ||
175 | return 8; | ||
176 | case rDec: | ||
177 | return 10; | ||
178 | case rHex: | ||
179 | return 16; | ||
180 | default: | ||
181 | state = sError; | ||
182 | odebug << "Error - attempt to calc base for non-integer" << oendl; | ||
183 | return 10; | ||
184 | }; | ||
185 | } | ||
186 | |||
187 | // Special instruction for internal use only | ||
188 | class iOpenBrace:public Instruction { | ||
189 | public: | ||
190 | iOpenBrace (Engine *e):Instruction (100) {engine = e;}; | ||
191 | ~iOpenBrace () {}; | ||
192 | |||
193 | Data eval (Data num) { | ||
194 | engine->decBraces(); | ||
195 | return num; | ||
196 | }; | ||
197 | private: | ||
198 | Engine *engine; | ||
199 | }; | ||
200 | |||
201 | void Engine::openBrace() { | ||
202 | pushInstruction(new iOpenBrace(this)); | ||
203 | } | ||
204 | |||
205 | void Engine::closeBrace() { | ||
206 | braces++;evalStack(num,TRUE); | ||
207 | } | ||
208 | |||
209 | // will need to show and hide display widgets | ||
210 | void Engine::setRepresentation(Representation r) { | ||
211 | currentRep = r; | ||
212 | clearData(&num); | ||
213 | clearData(&mem); | ||
214 | state = sStart; | ||
215 | } | ||
216 | |||
217 | void Engine::clearData(Data *d) { | ||
218 | d->i = d->fraction.numerator = d->fraction.denominator = 0; | ||
219 | d->dbl = 0; | ||
220 | } | ||
221 | |||