program calc apptype console type real double const prs pri array of b

  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
163
164
165
166
167
168
169
170
171
172
program calc;
{$apptype console}
type
real=double;
const
prs='+-*/(';
pri:array [1..5] of byte = (1,1,2,2,0);
var
s1,s2:string;
q:array[0..500] of real;
w:array[0..500] of char;
n,len,len2,i,j:longint;
t:real;
ch:char;
procedure push(x:real);
begin
inc(len);
q[len]:=x;
end;
function pop:real;
begin
pop:=q[len];
q[len]:=0;
dec(len);
end;
procedure pushc(x:char);
begin
inc(len2);
w[len2]:=x;
end;
function popc:char;
begin
popc:=w[len2];
w[len2]:=#0;
dec(len2);
end;
function oper(s1,s2:real;s3:char):real;
var
s:string;
x,y,z:real;
tmp:integer;
begin
x:=s1;
y:=s2;
case s3 of
'+':z:=x+y;
'-':z:=x-y;
'*':z:=x*y;
'/':z:=x/y;
end;
oper:=z;
end;
procedure prechange(var s:string);
var
i:longint;
begin
if s[1]='-' then s:='0'+s;
i:=1;
while i<=n do if (s[i]='(')and(s[i+1]='-') then insert('0',s,i+1) else inc(i);
end;
function change(s:string):string;
var
i:longint;
rezs:string;
c:boolean;
begin
c:=false;
for i:=1 to n do begin
if not(s[i] in ['+','-','*','/','(',')']) then begin
if c then rezs:=rezs+' ';
rezs:=rezs+s[i];
c:=false;
end
else begin
c:=true;
if s[i]='(' then pushc(s[i]) else
if s[i]=')' then begin
while w[len2]<>'(' do begin
rezs:=rezs+' '+popc;
end;
popc;
end else
if s[i] in ['+','-','*','/'] then begin
while pri[pos(w[len2],prs)]>=pri[pos(s[i],prs)] do rezs:=rezs+' '+popc;
pushc(s[i]);
end;
end;
end;
while len2<>0 do rezs:=rezs+' '+popc;
change:=rezs;
end;
function count(s:string):real;
var
ss:string;
x,s1,s2:real;
chh,s3:char;
p,i,j:longint;
tmp:integer;
begin
i:=0;
repeat
j:=i+1;
repeat inc(i) until s[i]=' ';
ss:=copy(s,j,i-j);
chh:=ss[1];
if not(chh in ['+','-','*','/']) then begin
val(ss,p,tmp);
push(p);
end
else begin
s2:=pop;
s1:=pop;
s3:=chh;
push(oper(s1,s2,s3));
end;
until i>=n;
x:=pop;
count:=x;
end;
procedure writeL(x:real);
var
y,a,b:longint;
q:real;
begin
y:=trunc(x);
b:=0;
if abs(x-y)<(1e-12) then
writeln(y)
else begin
if y>0 then a:=round(ln(y)/ln(10))+1 else a:=1;
q:=x;
repeat
q:=q*10;
inc(b);
until abs(q-trunc(q))<(1e-12);
writeln(x:a+b:b);
end;
end;
begin
repeat
writeln('Enter expression');
readln(s1);
n:=length(s1);
prechange(s1);
n:=length(s1);
s2:=change(s1);
if s2[1]=' ' then delete(s2,1,1);
s2:=s2+' ';
n:=length(s2);
t:=count(s2);
writeL(t);
writeln('One more expression?(Y/N)');
readln(ch);
until upcase(ch)='N';
end.