| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- var
- i1,i2,i3,i4 : byte;
- a : string;
- ret : longint;
- function calc(var sum : longint; num:longint;dey:char) : boolean;
- begin
- case dey of
- '+': sum := sum + num;
- '-': sum := sum - num;
- '*': sum := sum * num;
- '/': if num <> 0 then sum := sum div num
- else
- Begin
- calc := false;
- exit;
- End;
- end;
- calc := true;
- end;
- function Findnum(a:string;pos,dir:byte):longint;
- var
- ret :longint;
- st :string;
- code :integer;
- begin
- st:='';
- if dir = 1 then inc(pos) else dec(pos);
- if dir = 1 then
- while (not (a[pos] in ['+','-','*','/','(',')']))and(pos<>length(a)+1) do
- begin
- st := st + a[pos];
- inc(pos);
- end
- else
- while (not (a[pos] in ['+','-','*','/','(',')'])) and (pos<>0) do
- begin
- insert(a[pos],st,1);
- dec(pos);
- end;
- if (pos=1)and(a[pos]='-') then insert(a[pos],st,1);
- if st[1] = '.' then st[1] :='-';
- Val(st,ret,code);
- FindNum:=ret;
- end;{FindNum}
- function Simply(a : string): longint;
- var
- res : longint;
- j,i : byte;
- code : integer;
- num1,num2 :longint;
- tmp1,tmp2 : string;
- min : boolean;
- begin
- if a[1] = '-' then a[1] := '.';
- while pos(' ',a) <> 0 do delete(a,pos(' ',a),1);
- while (pos('*',a)<>0)or(pos('/',a)<>0) do
- begin
- i := pos('*',a);
- j := pos('/',a);
- if ((i<j) or (j=0)) and (i<>0) then
- begin
- num1 := findnum(a,i,0);
- str(num1,tmp1);
- num2 := findnum(a,i,1);
- str(num2,tmp2);
- calc(num1,num2,a[i]);
- delete(a,i-length(tmp1),length(tmp1)+1+length(tmp2));
- str(num1,tmp2);
- if tmp2[1] = '-' then tmp2[1]:='.';
- insert(tmp2,a,i-length(tmp1))
- end
- else
- begin
- num1 := findnum(a,j,0);
- str(num1,tmp1);
- num2 := findnum(a,j,1);
- str(num2,tmp2);
- calc(num1,num2,a[j]);
- delete(a,j-length(tmp1),length(tmp1)+1+length(tmp2));
- str(num2,tmp2);
- if tmp2[1] = '-' then tmp2[1]:='.';
- insert(tmp2,a,j-length(tmp1))
- end;
- end;
- min:=true;
- while ((pos('+',a)<>0)or(pos('-',a)<>0)) and min do
- begin
- i := pos('+',a);
- j := pos('-',a);
- if ((i<j) or (j=0)) and (i<>0) then
- begin
- num1 := findnum(a,i,0);
- str(num1,tmp1);
- num2 := findnum(a,i,1);
- str(num2,tmp2);
- calc(num1,num2,a[i]);
- delete(a,i-length(tmp1),length(tmp1)+1+length(tmp2));
- str(num1,tmp2);
- if tmp2[1] = '-' then tmp2[1]:='.';
- insert(tmp2,a,i-length(tmp1))
- end
- else
- begin
- num1 := findnum(a,j,0);
- str(num1,tmp1);
- num2 := findnum(a,j,1);
- str(num2,tmp2);
- calc(num1,num2,a[j]);
- delete(a,j-length(tmp1),length(tmp1)+1+length(tmp2));
- str(num1,tmp2);
- if tmp2[1] = '-' then tmp2[1]:='.';
- insert(tmp2,a,j-length(tmp1))
- end;
- end;
- if a[1] = '.' then a[1] :='-';
- val(a,res,code);
- Simply:=res;
- end;
- begin
- WriteLN('Введите пример: ');
- ReadLn(a);
- WriteLn(Simply(a));
- readln
- end.
|