MAS2.PAS 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. const
  2. deyst: set of char = ['+','-','*','/','(',')','^'];
  3. var
  4. i1,i2,i3,i4 : byte;
  5. a : string;
  6. ret : longint;
  7. function calc(var sum : longint; num:longint;dey:char) : boolean;
  8. var i : byte;
  9. res : longint;
  10. begin
  11. case dey of
  12. '^': begin
  13. if num = 0 then sum:=1;
  14. res:=sum;
  15. for i := 2 to num do sum:=res*sum;
  16. end;
  17. '+': sum := sum + num;
  18. '-': sum := sum - num;
  19. '*': sum := sum * num;
  20. '/': if num <> 0 then sum := sum div num
  21. else
  22. Begin
  23. calc := false;
  24. exit;
  25. End;
  26. end;
  27. calc := true;
  28. end;
  29. function Findnum(a:string;pos,dir:byte):longint;
  30. var
  31. ret :longint;
  32. st :string;
  33. code :integer;
  34. begin
  35. st:='';
  36. if dir = 1 then inc(pos) else dec(pos);
  37. if dir = 1 then
  38. while (not (a[pos] in deyst))and(pos<>length(a)+1) do
  39. begin
  40. st := st + a[pos];
  41. inc(pos);
  42. end
  43. else
  44. while (not (a[pos] in deyst)) and (pos<>0) do
  45. begin
  46. insert(a[pos],st,1);
  47. dec(pos);
  48. end;
  49. if (pos=1)and(a[pos]='-') then insert(a[pos],st,1);
  50. if st[1] = '.' then st[1] :='-';
  51. Val(st,ret,code);
  52. FindNum:=ret;
  53. end;{FindNum}
  54. Procedure Ends(st:string);
  55. begin
  56. Writeln(st);
  57. Halt(3);
  58. end;
  59. Function Calculate(a:string):string;
  60. var
  61. res : longint;
  62. j,i : byte;
  63. code : integer;
  64. ch : byte;
  65. num1,num2 : longint;
  66. tmp1,tmp2 : string;
  67. min : boolean;
  68. begin
  69. if a[1] = '-' then a[1] := '.';
  70. while pos(' ',a) <> 0 do delete(a,pos(' ',a),1);
  71. if (pos('(',a)<>0) or (pos(')',a)<>0) then
  72. begin
  73. j:=pos('(',a);
  74. if j = 0 then Ends('Ошибка в скобках');
  75. i := 1;
  76. ch := j+1;
  77. while i <> 0 do
  78. begin
  79. if a[ch] = '(' then inc(i);
  80. if a[ch] = ')' then dec(i);
  81. inc(ch);
  82. end;
  83. tmp1:=copy(a,j+1,ch-j-2);
  84. tmp1:=calculate(tmp1);
  85. delete(a,j,ch-j);
  86. insert(tmp1,a,j);
  87. end;
  88. while (pos('^',a)<>0) do
  89. begin
  90. i := pos('^',a);
  91. num1 := findnum(a,i,0);
  92. str(num1,tmp1);
  93. num2 := findnum(a,i,1);
  94. str(num2,tmp2);
  95. calc(num1,num2,a[i]);
  96. delete(a,i-length(tmp1),length(tmp1)+1+length(tmp2));
  97. str(num1,tmp2);
  98. insert(tmp2,a,i-length(tmp1))
  99. end;
  100. while (pos('*',a)<>0)or(pos('/',a)<>0) do
  101. begin
  102. i := pos('*',a);
  103. j := pos('/',a);
  104. if ((i<j) or (j=0)) and (i<>0) then
  105. begin
  106. num1 := findnum(a,i,0);
  107. str(num1,tmp1);
  108. num2 := findnum(a,i,1);
  109. str(num2,tmp2);
  110. calc(num1,num2,a[i]);
  111. delete(a,i-length(tmp1),length(tmp1)+1+length(tmp2));
  112. str(num1,tmp2);
  113. if tmp2[1] = '-' then tmp2[1]:='.';
  114. insert(tmp2,a,i-length(tmp1))
  115. end
  116. else
  117. begin
  118. num1 := findnum(a,j,0);
  119. str(num1,tmp1);
  120. num2 := findnum(a,j,1);
  121. str(num2,tmp2);
  122. calc(num1,num2,a[j]);
  123. delete(a,j-length(tmp1),length(tmp1)+1+length(tmp2));
  124. str(num1,tmp2);
  125. if tmp2[1] = '-' then tmp2[1]:='.';
  126. insert(tmp2,a,j-length(tmp1))
  127. end;
  128. end;
  129. min:=true;
  130. while ((pos('+',a)<>0)or(pos('-',a)<>0)) and min do
  131. begin
  132. i := pos('+',a);
  133. j := pos('-',a);
  134. if ((i<j) or (j=0)) and (i<>0) then
  135. begin
  136. num1 := findnum(a,i,0);
  137. str(num1,tmp1);
  138. num2 := findnum(a,i,1);
  139. str(num2,tmp2);
  140. calc(num1,num2,a[i]);
  141. delete(a,i-length(tmp1),length(tmp1)+1+length(tmp2));
  142. str(num1,tmp2);
  143. if tmp2[1] = '-' then tmp2[1]:='.';
  144. insert(tmp2,a,i-length(tmp1))
  145. end
  146. else
  147. begin
  148. num1 := findnum(a,j,0);
  149. str(num1,tmp1);
  150. num2 := findnum(a,j,1);
  151. str(num2,tmp2);
  152. calc(num1,num2,a[j]);
  153. delete(a,j-length(tmp1),length(tmp1)+1+length(tmp2));
  154. str(num1,tmp2);
  155. if tmp2[1] = '-' then tmp2[1]:='.';
  156. insert(tmp2,a,j-length(tmp1))
  157. end;
  158. end;
  159. calculate:=a;
  160. end;
  161. Function Simply(a:string):longint;
  162. var
  163. res:longint;
  164. code:integer;
  165. begin
  166. a:=Calculate(a);
  167. if a[1] = '.' then a[1] :='-';
  168. val(a,res,code);
  169. Simply:=res;
  170. end;
  171. begin
  172. WriteLN('Введите пример: ');
  173. ReadLn(a);
  174. WriteLn(Simply(a));
  175. readln
  176. end.