MAS.PAS 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. var
  2. i1,i2,i3,i4 : byte;
  3. a : string;
  4. ret : longint;
  5. function calc(var sum : longint; num:longint;dey:char) : boolean;
  6. begin
  7. case dey of
  8. '+': sum := sum + num;
  9. '-': sum := sum - num;
  10. '*': sum := sum * num;
  11. '/': if num <> 0 then sum := sum div num
  12. else
  13. Begin
  14. calc := false;
  15. exit;
  16. End;
  17. end;
  18. calc := true;
  19. end;
  20. function Findnum(a:string;pos,dir:byte):longint;
  21. var
  22. ret :longint;
  23. st :string;
  24. code :integer;
  25. begin
  26. st:='';
  27. if dir = 1 then inc(pos) else dec(pos);
  28. if dir = 1 then
  29. while (not (a[pos] in ['+','-','*','/','(',')']))and(pos<>length(a)+1) do
  30. begin
  31. st := st + a[pos];
  32. inc(pos);
  33. end
  34. else
  35. while (not (a[pos] in ['+','-','*','/','(',')'])) and (pos<>0) do
  36. begin
  37. insert(a[pos],st,1);
  38. dec(pos);
  39. end;
  40. if (pos=1)and(a[pos]='-') then insert(a[pos],st,1);
  41. if st[1] = '.' then st[1] :='-';
  42. Val(st,ret,code);
  43. FindNum:=ret;
  44. end;{FindNum}
  45. function Simply(a : string): longint;
  46. var
  47. res : longint;
  48. j,i : byte;
  49. code : integer;
  50. num1,num2 :longint;
  51. tmp1,tmp2 : string;
  52. min : boolean;
  53. begin
  54. if a[1] = '-' then a[1] := '.';
  55. while pos(' ',a) <> 0 do delete(a,pos(' ',a),1);
  56. while (pos('*',a)<>0)or(pos('/',a)<>0) do
  57. begin
  58. i := pos('*',a);
  59. j := pos('/',a);
  60. if ((i<j) or (j=0)) and (i<>0) then
  61. begin
  62. num1 := findnum(a,i,0);
  63. str(num1,tmp1);
  64. num2 := findnum(a,i,1);
  65. str(num2,tmp2);
  66. calc(num1,num2,a[i]);
  67. delete(a,i-length(tmp1),length(tmp1)+1+length(tmp2));
  68. str(num1,tmp2);
  69. if tmp2[1] = '-' then tmp2[1]:='.';
  70. insert(tmp2,a,i-length(tmp1))
  71. end
  72. else
  73. begin
  74. num1 := findnum(a,j,0);
  75. str(num1,tmp1);
  76. num2 := findnum(a,j,1);
  77. str(num2,tmp2);
  78. calc(num1,num2,a[j]);
  79. delete(a,j-length(tmp1),length(tmp1)+1+length(tmp2));
  80. str(num2,tmp2);
  81. if tmp2[1] = '-' then tmp2[1]:='.';
  82. insert(tmp2,a,j-length(tmp1))
  83. end;
  84. end;
  85. min:=true;
  86. while ((pos('+',a)<>0)or(pos('-',a)<>0)) and min do
  87. begin
  88. i := pos('+',a);
  89. j := pos('-',a);
  90. if ((i<j) or (j=0)) and (i<>0) then
  91. begin
  92. num1 := findnum(a,i,0);
  93. str(num1,tmp1);
  94. num2 := findnum(a,i,1);
  95. str(num2,tmp2);
  96. calc(num1,num2,a[i]);
  97. delete(a,i-length(tmp1),length(tmp1)+1+length(tmp2));
  98. str(num1,tmp2);
  99. if tmp2[1] = '-' then tmp2[1]:='.';
  100. insert(tmp2,a,i-length(tmp1))
  101. end
  102. else
  103. begin
  104. num1 := findnum(a,j,0);
  105. str(num1,tmp1);
  106. num2 := findnum(a,j,1);
  107. str(num2,tmp2);
  108. calc(num1,num2,a[j]);
  109. delete(a,j-length(tmp1),length(tmp1)+1+length(tmp2));
  110. str(num1,tmp2);
  111. if tmp2[1] = '-' then tmp2[1]:='.';
  112. insert(tmp2,a,j-length(tmp1))
  113. end;
  114. end;
  115. if a[1] = '.' then a[1] :='-';
  116. val(a,res,code);
  117. Simply:=res;
  118. end;
  119. begin
  120. WriteLN('Введите пример: ');
  121. ReadLn(a);
  122. WriteLn(Simply(a));
  123. readln
  124. end.