Вычисление простых арифметических выражений Оформил: DeeCo Автор: http://www.swissdelphicenter.ch ```function Calculate(SMyExpression: string; digits: Byte): string; // Calculate a simple expression // Supported are: Real Numbers, parenthesis var z: Char; ipos: Integer; function StrToReal(chaine: string): Real; var r: Real; Pos: Integer; begin Val(chaine, r, Pos); if Pos > 0 then Val(Copy(chaine, 1, Pos - 1), r, Pos); Result := r; end; function RealToStr(inreal: Extended; digits: Byte): string; var S: string; begin Str(inreal: 0: digits, S); realToStr := S; end; procedure NextChar; var s: string; begin if ipos > Length(SMyExpression) then begin z := #9; Exit; end else begin s := Copy(SMyExpression, ipos, 1); z := s[1]; Inc(ipos); end; if z = ' ' then nextchar; end; function Expression: Real; var w: Real; function Factor: Real; var ws: string; begin Nextchar; if z in ['0'..'9'] then begin ws := ''; repeat ws := ws + z; nextchar until not (z in ['0'..'9', '.']); Factor := StrToReal(ws); end else if z = '(' then begin Factor := Expression; nextchar end else if z = '+' then Factor := +Factor else if Z = '-' then Factor := -Factor; end; function Term: Real; var W: Real; begin W := Factor; while Z in ['*', '/'] do if z = '*' then w := w * Factor else w := w / Factor; Term := w; end; begin w := term; while z in ['+', '-'] do if z = '+' then w := w + term else w := w - term; Expression := w; end; begin ipos := 1; Result := RealToStr(Expression, digits); end; procedure TForm1.Button1Click(Sender: TObject); var sMyExpression: string; begin sMyExpression := '12.5*6+18/3.2+2*(5-6.23)'; ShowMessage(sMyExpression + ' = ' + Calculate(sMyExpression, 3)); end;```
Проект Delphi World © Выпуск 2002 - 2020
Автор проекта: Автоматизация бизнеса
Купить франшизу этого проекта можно здесь.
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "http://delphiworld.narod.ru/base/calc_expressions.html", "fetch_time": 1686260666000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2023-23/segments/1685224655143.72/warc/CC-MAIN-20230608204017-20230608234017-00617.warc.gz", "warc_record_offset": 11995960, "warc_record_length": 3410, "token_count": 545, "char_count": 1795, "score": 3.5, "int_score": 4, "crawl": "CC-MAIN-2023-23", "snapshot_type": "latest", "language": "en", "language_score": 0.17123790085315704, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00029-of-00064.parquet"}