Recent

Author Topic: Calculator  (Read 12914 times)

T-bear

  • Full Member
  • ***
  • Posts: 160
Calculator
« on: March 27, 2011, 12:07:07 am »
Hi, I try to make a calculator where you just enter the whole operation (like 2/(3*(4+5)) ), press the "="-button, and get the answear. Has anyone made something like this before? How can i do this?
---
Thanks!
  :)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8831
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Calculator
« Reply #1 on: March 27, 2011, 08:41:28 pm »
Easy way: use symbolic package
Hard way: learn compiler techniques

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2663
Re: Calculator
« Reply #2 on: March 28, 2011, 12:00:40 pm »
This sounds like homework. (at least it was when I took programming classes)
The global answer, start parsing and split your expression in (binary) tree nodes.
Then evaluate your tree (leftpart)(operator)(rightpart)
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12523
  • FPC developer.
Re: Calculator
« Reply #3 on: March 28, 2011, 04:38:55 pm »
This sounds like homework. (at least it was when I took programming classes)
The global answer, start parsing and split your expression in (binary) tree nodes.
Then evaluate your tree (leftpart)(operator)(rightpart)

(not per se. IMHO for simple expression a stack based approach (one stack for operators, one for arguments) is easier than actuall building trees)

zoiddani

  • New Member
  • *
  • Posts: 29
Re: Calculator
« Reply #4 on: March 28, 2011, 07:33:02 pm »

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Calculator
« Reply #5 on: March 28, 2011, 08:43:13 pm »
A simple (and understandable) example:

(It makes a single operation with 2 operands and 1 operator)

Code: [Select]
var
  S, Arg1, Arg2, Op :string;
  Index :byte;
  Res :integer;
begin
  S := Edit1.Text;
  Arg1 := '';
  Arg2 := '';
  Op := '';
  Index := 1;
  while S[Index] in ['0'..'9'] do
  begin
    Arg1 := Arg1 + S[Index];
    Inc(Index);
  end;

  while S[Index] = ' ' do
    Inc(Index);

  if S[Index] in ['+','-','*','/'] then
  begin
    op := S[Index];
    Inc(Index);
  end
  else
    begin
      ShowMessage('Operator expected at position ' + IntToStr(Index));
      Edit1.SetFocus;
      Edit1.SelStart := Index -1;
      Edit1.SelLength := 1;
      Exit;
    end;

  while S[Index] = ' ' do
    Inc(Index);

  while S[Index] in ['0'..'9'] do
  begin
    Arg2 := Arg2 + S[Index];
    Inc(Index);
  end;

  case op[1] of
    '+': begin
           Res := StrToInt(Arg1) + StrToInt(Arg2);
           ShowMessage(IntToStr(Res));
         end;
    '-':begin
           Res := StrToInt(Arg1) - StrToInt(Arg2);
           ShowMessage(IntToStr(Res));
         end;
    '*':begin
           Res := StrToInt(Arg1) * StrToInt(Arg2);
           ShowMessage(IntToStr(Res));
         end;
    '/':begin
           Res := StrToInt(Arg1) div StrToInt(Arg2);
           ShowMessage(IntToStr(Res));
         end;
  end;
end;   

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2663
Re: Calculator
« Reply #6 on: March 28, 2011, 08:49:24 pm »
This sounds like homework. (at least it was when I took programming classes)
The global answer, start parsing and split your expression in (binary) tree nodes.
Then evaluate your tree (leftpart)(operator)(rightpart)

(not per se. IMHO for simple expression a stack based approach (one stack for operators, one for arguments) is easier than actuall building trees)

I didn't want to think about it. I only recalled that when I made the assignment we came to a tree based solution
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

idog

  • Full Member
  • ***
  • Posts: 121
    • www.idogendel.com (Hebrew)
Re: Calculator
« Reply #7 on: March 29, 2011, 01:08:19 am »
Hi, I try to make a calculator where you just enter the whole operation (like 2/(3*(4+5)) ), press the "="-button, and get the answear. Has anyone made something like this before? How can i do this?

Using a stack is the "classic" solution for this problem. Whether you choose a stack or a tree, the basis is to be able to parse the string correctly - by identifying numbers and operators, while keeping track of the operator's priority (combine their algebraic priority with the parenthesis-induced priority).

Leledumbo

  • Hero Member
  • *****
  • Posts: 8831
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Calculator
« Reply #8 on: March 29, 2011, 03:14:15 am »
Either way chosen, the OP must break the string into tokens first and identify each correctly, then match them with the expected syntax.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Calculator
« Reply #9 on: March 29, 2011, 06:23:38 am »
There are some libraries for it, like this one:

http://sourceforge.net/projects/tpsystools/

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Calculator
« Reply #10 on: April 07, 2011, 11:24:20 pm »
The attached unit provides a class to calculate math expressions.

Usage:

Code: [Select]
  exp := TStExpression.Create(Application);
  exp.Expression := Edit1.Text;
  ShowMessage(FloatToStr(exp.AnalyzeExpression));
« Last Edit: April 08, 2011, 04:40:51 am by typo »

 

TinyPortal © 2005-2018