-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculat.pas
162 lines (155 loc) · 4.5 KB
/
calculat.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
{
Calculator - a dos calculator to test pcalcexp
Copyright (C) 2003 Gert van den Berg
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 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
}
{$IFDEF DELPHI}
{$Define EXCEPT}
{$ENDIF}
{$IFDEF FPC}
{$Define EXCEPT}
{$Mode Delphi}
{$ENDIF}
Program Calculator;
Uses Crt, pcalcexp{, calcexp};
Var str : shortstring;
post : postft;
i : byte;
r,s : extended;
Begin
Repeat
TextColor(7);
TextBackground(0);
ClrScr;
Writeln('Tik asb. ''n som in. Tik "Q" om uit te gaan.');
Write('> '); Readln(str);
If length(str) > 0 then
If Upcase(str[1]) = 'Q' then halt;
ClrScr;
Writeln('String: ',str);
Writeln;
Writeln('Converting to postfix...');
{$IFDEF FPC}
try
string2postf(str,post);
except
TextColor(LightRed);
Writeln('Warning! PCalcExp error no. ',pcalcexp.calcerror,' encountered.');
Writeln('Message: ',pcalcErrorMsG(pcalcexp.calcerror));
TextColor(7);
End;
{$ELSE}
string2postf(str,post);
If pcalcexp.calcerror <> 0 then
Begin
TextColor(LightRed);
Writeln('Warning! PCalcExp error no. ',pcalcexp.calcerror,' encountered.');
Writeln('Message: ',pcalcErrorMsG(pcalcexp.calcerror));
TextColor(7);
End;
{$ENDIF}
Writeln('Done');
Writeln;
Writeln ('TYPE Value');
Writeln ('--------- ------');
For i := 1 to post.count do
Begin
case post.items[i].itemtype of
valu : Begin
Write('VALUE ');
Writeln(post.items[i].valu:1:5);
End;
oper : Begin
Write('OPERATOR ');
Writeln(post.items[i].oper);
End;
func : Begin
Write('FUNCTION ');
Writeln(post.items[i].func);
End;
cons : Begin
Write('CONSTANT ');
Writeln(post.items[i].func);
End;
v1 : Writeln('VAR x');
v2 : Writeln('VAR y');
v3 : Writeln('VAR z');
else
Writeln('Unknown itemtype encountered');
end;
End;
Writeln;
{$IFNDEF EXCEPT}
if pcalcexp.calcerror = 0 then r := CalcPostfix(post,0,0,0) else
Begin
r := 0;
Writeln('Error while converting.');
End;
Writeln('Result = ',r:1:10);
If pcalcexp.calcerror <> 0 then
Begin
TextColor(LightRed);
Writeln('Warning! PCalcExp error no. ',pcalcexp.calcerror,' encountered.');
Writeln(' Message: '+pcalcerrormsg(pcalcexp.calcerror));
TextColor(7);
End;
Writeln;
If Pcalcexp.calcresult <> 0 then r := 0;
{$ELSE}
if pcalcexp.calcerror = 0 then
try
r := CalcPostfix(post,0,0,0);
except
r := 0;
End
else Begin
r := 0;
Writeln('Error while converting.');
End;
Writeln('Result = ',r:1:10);
If pcalcexp.calcerror <> 0 then
Begin
TextColor(LightRed);
Writeln('Warning! PCalcExp error no. ',pcalcexp.calcerror,' encountered.');
Writeln(' Message: '+pcalcerrormsg(pcalcexp.calcerror));
TextColor(7);
End;
Writeln;
If Pcalcexp.calcresult <> 0 then r := 0;
{$ENDIF}
{ s := Calculate(str);
Writeln('Result volgens calcexp = ',s:1:10);
If calcexp.calcerror <> 0 then
Begin
TextColor(LightRed);
Writeln('Warning! CalcExp error no. ',calcexp.calcerror,' encountered.');
TextColor(7);
End;
If calcexp.calcresult <> 0 then s := 0;
Writeln;
If (round(r*10000) = round(s*10000)) then
Begin
TextColor(LightGreen);
Writeln('Items Match');
TextColor(7);
Writeln;
End else
Begin
TextColor(LightRed);
Writeln('Items don''t match!!! Difference = ',abs(r-s):0:8);
TextColor(7);
Writeln;
End; }
Writeln('Press any key to continue...');Readkey;
Until False;
End.