Recent

Author Topic: The sum of numbers in a string  (Read 15467 times)

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: The sum of numbers in a string
« Reply #15 on: January 26, 2015, 07:21:08 pm »
...It's not a homework, but a small part of the whole program.
I'm not familiar with the TStringList.
These two statements look mutually exclusive to me. At least these days.
No offense, though.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: The sum of numbers in a string
« Reply #16 on: January 26, 2015, 07:35:35 pm »
...It's not a homework, but a small part of the whole program.
I'm not familiar with the TStringList.
These two statements look mutually exclusive to me. At least these days.
I think we deserve some clarification from the original poster.

Meanwhile, here is a lame solution that does the calculation using MS Calc and passes figures using the clipboard. It requires MouseAndKeyInput package and it is a Windows only solution:
Code: [Select]
uses
  Windows, Process, MouseAndKeyInput, Clipbrd;

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var
  p: TProcess;
  s: string;
begin
  s := '150 20 100 10 1';
  s := StringReplace(s, ' ', '+', [rfReplaceAll])+#13;
  Clipboard.AsText := s;
  p := TProcess.Create(Self);
  try
    p.Executable := 'calc';
    p.Execute;
    if WaitForInputIdle(p.Handle, 5000)=0 then
    begin
      KeyInput.Apply([ssCtrl]);
      KeyInput.Press('V');
      KeyInput.Unapply([ssCtrl]);

      KeyInput.Press(VK_RETURN);

      KeyInput.Apply([ssCtrl]);
      KeyInput.Press('C');
      KeyInput.Unapply([ssCtrl]);

      WaitForInputIdle(p.Handle, 5000);// wait for the application
      Sleep(200);// wait for the OS

      Caption := Clipboard.AsText; //<--- The result
    end;
  finally
    p.Terminate(0);
    p.Free;
  end;
end;

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: The sum of numbers in a string
« Reply #17 on: January 26, 2015, 07:59:40 pm »
Meanwhile, here is a lame solution that does the calculation using MS Calc and passes figures using the clipboard. It requires MouseAndKeyInput package and it is a Windows only solution:
  :D  :D
that's awesome!

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: The sum of numbers in a string
« Reply #18 on: January 26, 2015, 09:54:36 pm »
@skalogryz, I am ashamed to say that I am so used to using arrays that I first used a string list only about three months ago. :-[

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: The sum of numbers in a string
« Reply #19 on: January 26, 2015, 10:10:12 pm »
@skalogryz, I am ashamed to say that I am so used to using arrays that I first used a string list only about three months ago. :-[
but can you sum up numbers from a string?

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: The sum of numbers in a string
« Reply #20 on: January 27, 2015, 09:44:14 am »
 Only by looping through the array. I would guess that TStringlist is an array of strings as well and is doing something similar. You youngsters do like your labour saving devices :)

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: The sum of numbers in a string
« Reply #21 on: January 27, 2015, 09:46:34 am »
I love these spontaneous code 'competitions'. They show how many ways there are of doing things.

Michl

  • Full Member
  • ***
  • Posts: 226
Re: The sum of numbers in a string
« Reply #22 on: January 27, 2015, 01:12:06 pm »
A little bit late, but I like:
Code: [Select]
uses ..., StrUtils;

function CalcSum(const s: String): Integer;
var
  aPos: Int32;
  sd: String;
const
  WordSeparator = ' ';
begin
  Result:=0;
  aPos:=1;
  repeat
    sd:=ExtractWord(aPos, s, [WordSeparator]);
    inc(aPos);
    Result:=Result + StrToIntDef(sd, 0);
  until sd='';
end;
Code: [Select]
type
  TLiveSelection = (lsMoney, lsChilds, lsTime);
  TLive = Array[0..1] of TLiveSelection;

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: The sum of numbers in a string
« Reply #23 on: January 27, 2015, 03:21:49 pm »
Only by looping through the array. I would guess that TStringlist is an array of strings as well and is doing something similar. You youngsters do like your labour saving devices :)
That's my point ;) You still can solve the problem.  Doesn't have to be TStringList.
If a developer doesn't know how to solve the problem, as well as doesn't know what TStringList - then it's a student with a home task :)

Speaking of schools, this is my "old-school" version for the task. Single pass, no copy operations or 3d party modules.
Code: [Select]
function Sum(const s: string): LongInt;
var
  i,t,r : LongInt;
begin
  r:=0; t:=0;
  for i:=1 to length(s) do
    if (s[i]>='0') and (s[i]<='9') then begin
      t:=t*10;
      t:=t+ord(s[i])-ord('0');
    end else begin
      r:=r+t;
      t:=0;
    end;
  r:=r+t;
  sum:=r;
end;
it should work even in turbo pascal.
« Last Edit: January 27, 2015, 03:28:25 pm by skalogryz »

 

TinyPortal © 2005-2018