Recent

Author Topic: help editchange  (Read 5264 times)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: help editchange
« Reply #15 on: January 20, 2019, 07:15:31 am »
it works now thanks how about when i input 275 in edit1 , edit2 will output 3.18  and edit3 will output 2.97 and edit4 will output 6.56  is it possible

Are those numbers calculated some way or are they taken out of a hat?
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

theflash09

  • New member
  • *
  • Posts: 9
Re: help editchange
« Reply #16 on: January 20, 2019, 11:42:11 am »
Yes, it can. Try this new code:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Edit1: TEdit;
  16.     Edit2: TEdit;
  17.     Edit3: TEdit;
  18.     Edit4: TEdit;
  19.     Label1: TLabel;
  20.     procedure Edit1EditingDone(Sender: TObject);
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure TForm1.Edit1EditingDone(Sender: TObject);
  33. const
  34.   num1 = 275;
  35.   num2 = 3.18;
  36.   num3 = 2.97;
  37.   num4 = 6.56;
  38. begin
  39.   if Edit1.Text = num1.ToString then
  40.   begin
  41.     Edit2.Text := num2.ToString;
  42.     Edit3.Text := num3.ToString;
  43.     Edit4.Text := num4.ToString;
  44.   end;
  45. end;
  46.  
  47. end.
thanks,  how to code this formula in pascal  edit2.text*edit3.text*edit4.text divide by 10 then divide 1000 then plus 6.77

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: help editchange
« Reply #17 on: January 20, 2019, 05:05:46 pm »
how to code this formula in pascal  edit2.text*edit3.text*edit4.text divide by 10 then divide 1000 then plus 6.77

Code: Pascal  [Select][+][-]
  1. function Calculate: Double;
  2. var
  3.   i, j, k: Integer; {or Single or whatever it is}
  4.  
  5. begin
  6.   i ;= StrToInt(edit2.Text);
  7.   j ;= StrToInt(edit3.Text);
  8.   k ;= StrToInt(edit4.Text);
  9.   Result := (((i*j*k)/10)/1000) + 6.77;
  10. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: help editchange
« Reply #18 on: January 20, 2019, 06:08:20 pm »
how to code this formula in pascal  edit2.text*edit3.text*edit4.text divide by 10 then divide 1000 then plus 6.77

Code: Pascal  [Select][+][-]
  1. function Calculate: Double;
  2. var
  3.   i, j, k: Integer; {or Single or whatever it is}
  4.  
  5. begin
  6.   i ;= StrToInt(edit2.Text);
  7.   j ;= StrToInt(edit3.Text);
  8.   k ;= StrToInt(edit4.Text);
  9.   Result := (((i*j*k)/10)/1000) + 6.77;
  10. end;

Surely they should be StrToFloat where the figures quoted are real?   and there's no point in /10 then /1000    -   brackets are irrelevant as well.

Result := i*j*k/10000 + 6.77; 

or even

Result := 6.77 + i*j*k/10000;

both will return 7.029727 using the figures provided.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

theflash09

  • New member
  • *
  • Posts: 9
Re: help editchange
« Reply #19 on: January 20, 2019, 06:18:26 pm »
how to code this formula in pascal  edit2.text*edit3.text*edit4.text divide by 10 then divide 1000 then plus 6.77

Code: Pascal  [Select][+][-]
  1. function Calculate: Double;
  2. var
  3.   i, j, k: Integer; {or Single or whatever it is}
  4.  
  5. begin
  6.   i ;= StrToInt(edit2.Text);
  7.   j ;= StrToInt(edit3.Text);
  8.   k ;= StrToInt(edit4.Text);
  9.   Result := (((i*j*k)/10)/1000) + 6.77;
  10. end;

Surely they should be StrToFloat where the figures quoted are real?   and there's no point in /10 then /1000    -   brackets are irrelevant as well.

Result := i*j*k/10000 + 6.77; 

or even

Result := 6.77 + i*j*k/10000;

both will return 7.029727 using the figures provided.
how to round off to make it 7.03

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: help editchange
« Reply #20 on: January 20, 2019, 06:30:51 pm »
how to code this formula in pascal  edit2.text*edit3.text*edit4.text divide by 10 then divide 1000 then plus 6.77

Code: Pascal  [Select][+][-]
  1. function Calculate: Double;
  2. var
  3.   i, j, k: Integer; {or Single or whatever it is}
  4.  
  5. begin
  6.   i ;= StrToInt(edit2.Text);
  7.   j ;= StrToInt(edit3.Text);
  8.   k ;= StrToInt(edit4.Text);
  9.   Result := (((i*j*k)/10)/1000) + 6.77;
  10. end;

Surely they should be StrToFloat where the figures quoted are real?   and there's no point in /10 then /1000    -   brackets are irrelevant as well.

Result := i*j*k/10000 + 6.77; 

or even

Result := 6.77 + i*j*k/10000;

both will return 7.029727 using the figures provided.
how to round off to make it 7.03
use roundto in math unit.
Code: Pascal  [Select][+][-]
  1.   result := RoundTo(Result, -2)
  2.  

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: help editchange
« Reply #21 on: January 20, 2019, 06:31:42 pm »
@J-G

Yes, you're right those brackets are useless. But I think @lucamar want to make the formula easier to understand for OP. Brackets are cheap. We can use lots of brackets and it won't have any performance penalty.

@theflash09

:-X Don't use @lucamar's code. It is buggy, it even won't compile.

Here, try this:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Edit1: TEdit;
  16.     Edit2: TEdit;
  17.     Edit3: TEdit;
  18.     Edit4: TEdit;
  19.     Label1: TLabel;
  20.     Label2: TLabel;
  21.     Label3: TLabel;
  22.     procedure Edit1EditingDone(Sender: TObject);
  23.     procedure Edit2EditingDone(Sender: TObject);
  24.     procedure Edit3EditingDone(Sender: TObject);
  25.     procedure Edit4EditingDone(Sender: TObject);
  26.   private
  27.     procedure Calculate;
  28.   end;
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. implementation
  34.  
  35. {$R *.lfm}
  36.  
  37. { TForm1 }
  38.  
  39. procedure TForm1.Edit1EditingDone(Sender: TObject);
  40. const
  41.   num1 = 275;
  42.   num2 = 3.18;
  43.   num3 = 2.97;
  44.   num4 = 6.56;
  45. begin
  46.   if Edit1.Text = num1.ToString then
  47.   begin
  48.     Edit2.Text := num2.ToString;
  49.     Edit3.Text := num3.ToString;
  50.     Edit4.Text := num4.ToString;
  51.     Calculate;
  52.   end;
  53. end;
  54.  
  55. procedure TForm1.Edit2EditingDone(Sender: TObject);
  56. begin
  57.   Calculate;
  58. end;
  59.  
  60. procedure TForm1.Edit3EditingDone(Sender: TObject);
  61. begin
  62.   Calculate;
  63. end;
  64.  
  65. procedure TForm1.Edit4EditingDone(Sender: TObject);
  66. begin
  67.   Calculate;
  68. end;
  69.  
  70. procedure TForm1.Calculate;
  71. var
  72.   hasError: Boolean;
  73.   Val1, Val2, Val3: Single;
  74. begin
  75.  
  76.   hasError := False;
  77.   if not(TryStrToFloat(Edit2.Text, Val1)) then hasError := True;
  78.   if not(TryStrToFloat(Edit3.Text, Val2)) then hasError := True;
  79.   if not(TryStrToFloat(Edit4.Text, Val3)) then hasError := True;
  80.  
  81.   if hasError then
  82.     Label3.Caption := '<error>'
  83.   else
  84.     Label3.Caption := FormatFloat('0.##', Val1 * Val2 * Val3 / 10 / 1000 + 6.77);
  85.  
  86. end;
  87.  
  88. end.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: help editchange
« Reply #22 on: January 20, 2019, 07:01:31 pm »
Surely they should be StrToFloat where the figures quoted are real?   and there's no point in /10 then /1000    -   brackets are irrelevant as well.
[...]

Yes, but he didn't say what type should be used. And I followed his formula step-by-step i.e. a direct translation. Reduction can always be done later--as you did.

:-X Don't use @lucamar's code. It is buggy, it even won't compile.

Yeah; damned shift key works only when it wants. And I saw what I wanted to be there instead of what it really was :-[
« Last Edit: January 20, 2019, 07:07:06 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018