Recent

Author Topic: building a Calculator using AI  (Read 382 times)

srvaldez

  • Full Member
  • ***
  • Posts: 190
building a Calculator using AI
« on: January 12, 2026, 01:23:53 pm »
https://forum.lazarus.freepascal.org/index.php/topic,73222.msg574110.html#msg574110

Hi jamie
yes, RPN is relatively easy to do but I wanted an algebraic calculator
the latest version with the logic done by Grok is in the form of an expression evaluator with buttons, not exactly what I wanted
it's functional but there's no error trapping which could lead to a crash with bad input

<Edit> fixed the logic of the EE button
I am pleasantly surprised with the capability of Grok, most of the time it spits out working code, sometimes a minor modification is needed
I asked Grok in regard to the function EvaluateExpression, can it be modified so that it will evaluate partial expressions between '(' and ')' and replace that part of the expression with the value as soon as the ')' is encountered ?
and it explained and provided the changes needed, it's integration was quite easy and it works, the expression "2*(5+6)" is promptly replaced with "2*11"
trig functions are next and after that add the tricky keyboard support

link to the Grok conversation https://grok.com/share/c2hhcmQtMg_e83465ec-c3ef-4084-8699-446324e89f25

« Last Edit: January 12, 2026, 01:43:51 pm by srvaldez »

srvaldez

  • Full Member
  • ***
  • Posts: 190
Re: building a Calculator using AI
« Reply #1 on: January 12, 2026, 02:05:41 pm »
I mentioned that sometimes a little modification is needed, in the following function I had to add begin, end to the case statements
Code: Pascal  [Select][+][-]
  1.   function GetOperator(OpChar: Char): TOperator;
  2.   begin
  3.     Result.Op := OpChar;
  4.     case OpChar of
  5.       '+', '-': Result.Prec := 2; Result.Assoc := 'L';
  6.       '*', '/': Result.Prec := 3; Result.Assoc := 'L';
  7.       else raise Exception.Create('Invalid operator');
  8.     end;
  9.   end;
  10.  

Code: Pascal  [Select][+][-]
  1.       '+', '-': begin Result.Prec := 2; Result.Assoc := 'L'; end;
  2.       '*', '/': begin Result.Prec := 3; Result.Assoc := 'L'; end;
  3.  

Dzandaa

  • Hero Member
  • *****
  • Posts: 520
  • From C# to Lazarus
Re: building a Calculator using AI
« Reply #2 on: January 12, 2026, 03:51:20 pm »
Hi,

You can look at this:

https://github.com/hailan20181112/pascal_Advanced_Calculator

It's in Chinese but you can use a translator like Google translator.

B->
 
Regards,
Dzandaa

 

TinyPortal © 2005-2018