strcalc.PAS 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. Unit strcalc;
  2. Interface
  3. Function StrFunc(f:string;va:real):real;
  4. Function Simply(a:string):real;
  5. Implementation
  6. const
  7. deyst: set of char = ['+','-','*','/','(',')','^','s','c','t','g','û','|'];
  8. function calc(var sum : real; num:real;dey:char) : boolean;
  9. var i : byte;
  10. res : real;
  11. begin
  12. case dey of
  13. '^': begin
  14. if num = 0 then sum:=1;
  15. res:=sum;
  16. for i := 2 to round(num) do sum:=res*sum;
  17. end;
  18. 's': sum := sin(num);
  19. 'c': sum := cos(num);
  20. 't': if cos(num) <> 0 then sum := sin(num)/cos(num)
  21. else sum := sin(num)/0.00001;
  22. 'g': if sin(num) <> 0 then sum := cos(num)/sin(num)
  23. else sum := cos(num)/0.00001;
  24. '+': sum := sum + num;
  25. '-': sum := sum - num;
  26. '*': sum := sum * num;
  27. 'û': sum := sqrt(abs(num));
  28. '|': sum := abs(num);
  29. '/': if num <> 0 then sum := sum / num
  30. else sum := sum / 0.00001;
  31. end;
  32. calc := true;
  33. end;
  34. function Findnum(var res:real;a:string;pos,dir:byte):byte;
  35. var
  36. st :string;
  37. code :integer;
  38. old,ad:byte;
  39. begin
  40. st:='';
  41. ad:=0;
  42. old:=pos;
  43. if dir = 1 then inc(pos) else dec(pos);
  44. if dir = 1 then
  45. while (not (a[pos] in deyst))and(pos<>length(a)+1) do
  46. begin
  47. st := st + a[pos];
  48. inc(pos);
  49. end
  50. else
  51. while (not (a[pos] in deyst)) and (pos<>0) do
  52. begin
  53. insert(a[pos],st,1);
  54. dec(pos);
  55. end;
  56. if (pos=1)and(a[pos]='-') then begin insert(a[pos],st,1);ad:=1 end;
  57. if st[1] = '_' then st[1] :='-';
  58. Val(st,res,code);
  59. FindNum:=abs(old-pos)-1+ad;
  60. end;{FindNum}
  61. Function Calculate(a:string):string;
  62. var
  63. res : real;
  64. j,i : byte;
  65. code : integer;
  66. ch : byte;
  67. num1,num2 : real;
  68. len1,len2 : byte;
  69. tmp1,tmp2 : string;
  70. trig : array [1..5] of byte;
  71. begin
  72. if a[1] = '-' then a[1] := '_';
  73. while pos(' ',a) <> 0 do delete(a,pos(' ',a),1);
  74. while (pos('(',a)<>0) or (pos(')',a)<>0) do
  75. begin
  76. j:=pos('(',a);
  77. if j = 0 then exit;
  78. i := 1;
  79. ch := j+1;
  80. while (i <> 0)and(ch<>length(a)+1) do
  81. begin
  82. if a[ch] = '(' then inc(i);
  83. if a[ch] = ')' then dec(i);
  84. inc(ch);
  85. end;
  86. if (ch = length(a)+1) and (a[ch-1]<>')') then exit;
  87. tmp1:=copy(a,j+1,ch-j-2);
  88. tmp1:=calculate(tmp1);
  89. delete(a,j,ch-j);
  90. insert(tmp1,a,j);
  91. end;
  92. while pos('|',a)<>0 do
  93. begin
  94. i := pos('|',a);
  95. len1 := findnum(num1,a,i,0);
  96. str(num1:0:5,tmp1);
  97. len2 := findnum(num2,a,i,1);
  98. str(num2:0:5,tmp2);
  99. calc(num1,num2,a[i]);
  100. delete(a,i-len1,len1+1+len2);
  101. str(num1:0:5,tmp2);
  102. insert(tmp2,a,i-len1)
  103. end;
  104. while (pos('^',a)<>0)or(pos('û',a)<>0) do
  105. begin
  106. i := pos('^',a);
  107. j := pos('û',a);
  108. if ((j<i) or (i=0)) and (j<>0) then i:=j;
  109. len1 := findnum(num1,a,i,0);
  110. str(num1:0:5,tmp1);
  111. len2 := findnum(num2,a,i,1);
  112. str(num2:0:5,tmp2);
  113. calc(num1,num2,a[i]);
  114. delete(a,i-len1,len1+1+len2);
  115. str(num1:0:5,tmp2);
  116. if tmp2[1] = '-' then tmp2[1]:='_';
  117. insert(tmp2,a,i-len1)
  118. end;
  119. while (pos('s',a)<>0)or(pos('c',a)<>0)or(pos('t',a)<>0)or(pos('g',a)<>0) do
  120. begin
  121. trig[1]:=pos('s',a);
  122. trig[2]:=pos('c',a);
  123. trig[3]:=pos('t',a);
  124. trig[4]:=pos('g',a);
  125. trig[5]:=255;
  126. i:=5;
  127. for j := 1 to 4 do
  128. if (trig[j] < trig[i]) and (trig[j]<>0) then
  129. i := j;
  130. j := trig[i];
  131. len1 := findnum(num1,a,j,0);
  132. str(num1:0:5,tmp1);
  133. len2 := findnum(num2,a,j,1);
  134. str(num2:0:5,tmp2);
  135. calc(num1,num2,a[j]);
  136. delete(a,j-len1,len1+1+len2);
  137. str(num1:0:5,tmp2);
  138. if tmp2[1] = '-' then tmp2[1]:='_';
  139. insert(tmp2,a,j-len1)
  140. end;
  141. while (pos('*',a)<>0)or(pos('/',a)<>0) do
  142. begin
  143. i := pos('*',a);
  144. j := pos('/',a);
  145. if ((i<j) or (j=0)) and (i<>0) then j:=i;
  146. len1 := findnum(num1,a,j,0);
  147. str(num1:0:5,tmp1);
  148. len2 := findnum(num2,a,j,1);
  149. str(num2:0:5,tmp2);
  150. calc(num1,num2,a[j]);
  151. delete(a,j-len1,len1+1+len2);
  152. str(num1:0:5,tmp2);
  153. if tmp2[1] = '-' then tmp2[1]:='_';
  154. insert(tmp2,a,j-len1)
  155. end;
  156. while (pos('+',a)<>0)or(pos('-',a)<>0) do
  157. begin
  158. i := pos('+',a);
  159. j := pos('-',a);
  160. if ((i<j) or (j=0)) and (i<>0) then j := i;
  161. len1 := findnum(num1,a,j,0);
  162. str(num1:0:5,tmp1);
  163. len2 := findnum(num2,a,j,1);
  164. str(num2:0:5,tmp2);
  165. calc(num1,num2,a[j]);
  166. delete(a,j-len1,len1+1+len2);
  167. str(num1:0:5,tmp2);
  168. if tmp2[1] = '-' then tmp2[1]:='_';
  169. insert(tmp2,a,j-len1)
  170. end;
  171. calculate:=a;
  172. end;
  173. Function Simply(a:string):real;
  174. var
  175. res:real;
  176. code:integer;
  177. i : byte;
  178. begin
  179. while pos('sin',a) <> 0 do
  180. begin
  181. i:=pos('sin',a);
  182. delete(a,i,3);
  183. Insert('1s',a,i);
  184. end;
  185. while pos('cos',a) <> 0 do
  186. begin
  187. i:=pos('cos',a);
  188. delete(a,i,3);
  189. Insert('1c',a,i);
  190. end;
  191. while pos('tg',a) <> 0 do
  192. begin
  193. i:=pos('tg',a);
  194. delete(a,i,2);
  195. Insert('1t',a,i);
  196. end;
  197. while pos('ctg',a) <> 0 do
  198. begin
  199. i:=pos('ctg',a);
  200. delete(a,i,3);
  201. Insert('1g',a,i);
  202. end;
  203. while pos('sqrt',a) <> 0 do
  204. begin
  205. i:=pos('sqrt',a);
  206. delete(a,i,4);
  207. Insert('1û',a,i);
  208. end;
  209. while pos('abs',a) <> 0 do
  210. begin
  211. i:=pos('abs',a);
  212. delete(a,i,3);
  213. Insert('1|',a,i);
  214. end;
  215. a:=Calculate(a);
  216. if a[1] = '_' then a[1] :='-';
  217. val(a,res,code);
  218. Simply:=res;
  219. end;
  220. {----------------------------------}
  221. Function StrFunc(f:string;va:real):real;
  222. var p:byte;
  223. sva:string;
  224. begin
  225. str(va:0:5,sva);
  226. if sva[1]='-' then sva[1]:='_';
  227. while pos('x',f)<>0 do
  228. begin
  229. p:=pos('x',f);
  230. delete(f,p,1);
  231. insert(sva,f,p);
  232. end;
  233. strfunc:=simply(f);
  234. end;
  235. end.