Code:
Program bieu_thuc;
Uses crt;
Type
mang = array [1..100] of integer;
Var
a,b,c:mang;
s:string;
n,t,j:integer;
{ - - - - - - Nhap string - - - - - - - }
Procedure nhap;
begin
Write('Nhap vao bieu thuc can tinh : ');
Readln(s);
end;
{ - - - - - - Loai bo khoang trang trong string - - - - - - - }
Procedure loaiktrang(var o:string);
var k:integer;
begin
k:=pos(#32,s);
While k <> 0 do delete(s,k,1);
end;
{ - - - - - - Tim vi tri cua ')' - - - - - - - }
Function find(l:integer):integer;
var i:integer;
begin
For i:=l to length(s) do
begin
If s[i] = ')' then find:=i;
end;
end;
{ - - - - - - Tao mang_ab - - - - - - }
Procedure tao_mangab;
var i,k:integer;
begin
j:=1;
t:=1;
k:=length(s);
For i:=1 to k do
If s[i]='(' then
begin
a[j]:=i;
j:=j+1;
end;
If s[i]=')' then
begin
b[t]:=i;
t:=t+1;
end;
end;
{ - - - - - - Tinh toan voi dau ngoac- - - - - - }
Procedure tinh(l,r:integer);
var i,k,z,y,x,e:integer;w:string;ch:char;
begin
For i:=l to r do
begin
If s[i]='*' then
begin
val(s[i-1],x,e);s[i]:='+';
val(s[i+1],y,e);s[i+1]:='0';
z:=x*y;
str(z,w);
s[i-1]:=w[1];
end;
If s[i]='/' then
begin
val(s[i-1],x,e);s[i]:='+';
val(s[i+1],y,e);s[i+1]:='0';
z:=x div y;
str(z,w);
s[i-1]:=w[1];
end;
end;
For i:=l to r do
begin
If s[i]='+' then
begin
val(s[i-1],x,e);s[i]:='+';
val(s[i+1],y,e);s[i+1]:='0';
z:=x+y;
str(z,w);
s[i-1]:=w[1];
end;
If s[i]='-' then
begin
val(s[i-1],x,e);s[i]:='+';
val(s[i+1],y,e);s[i+1]:='0';
If i>3 then
begin
s[i-2]:='-';
str(y,w);
s[i-1]:=w[1];
end else
begin
z:=x-y;
str(z,w);
s[i-1]:=w[1];
end;
end;
end;
end;
{ - - - - - - Tinh toan voi dau ngoac- - - - - - }
Procedure tinh_ngoac;
var i,m:integer;
begin
m:=1;
For i:=t downto 1 do
begin
delete(s,a[i],1);
delete(s,b[i]-1+m,1);
tinh(a[i]+m,b[i]-m);
m:=m+1;
end;
end;
{ - - - - - - Tinh truoc dau ngoac - - - - - - }
Procedure tinh_trngoac;
var i,k:integer;
begin
k:=length(s);
If t>1 then
begin
tinh_ngoac;
tinh(1,a[1]);
tinh(b[t],k);
end else
tinh(1,k);
Writeln;
end;
{ - - - - - - - Tinh tu dau - - - - - - - }
Procedure tinhbieuthuc;
var i,k,x,e,y:integer;
begin
k:=length(s);
val(s[1],x,e);
c[1]:=x;
For i:=2 to k do
begin
If s[i]='+' then
begin
val(s[i+1],y,e);
c[1]:=c[1]+y;
end;
If s[i]='-' then
begin
val(s[i+1],y,e);
c[1]:=c[1]-y;
end;
end;
end;
BEGIN
nhap;
loaiktrang(s);
tao_mangab;
tinh_trngoac;
tinhbieuthuc;
Write('Ket qua la ',c[1]);
Readln;
END.
Đây là bài hoàn chỉnh của em, tính được rồi nhưng ở chỗ là khi tính như thế này 5+5+5 thì nó lấy 5+5 ra 10 thì không lưu lại vào string[1] được, em không biết liệu mình có thể cho string[1] chứ 2 chữ số là '10' được không. Có ai chỉ em với.
Bookmarks