* * *

Author Topic: Math Problem  (Read 1372 times)

A

  • Guest
Math Problem
« on: January 13, 2018, 09:14:26 pm »
Hello,

I'm nearing a solution with regards to numbering my tabs. This is how the tabs should look like:
Web Browser - Untitled | HTML Viewer - Untitled | Web Browser - Untitled 1 | HTML Viewer - Untitled  1| Web Browser - Untitled 2 | HTML Viewer - Untitled 2 |

Unfortunately, the untitled number goes up like this: 1, 3, 5, 7

It's clear why, when you look at this code, however, I'm struggling mathematically to find a solution for the pairs of tabs to go up like: 1, 2, 3, 4.

This probably isn't as complex as it sounds, however I'm not very good at math (as I'm still learning it is school - I'm 13).

Does anybody know of a solution?

If you want to test it in a project, just create a form with a Button and a PageControl and copy/paste the following code:

Code: Pascal  [Select]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.    SheetBrowser := TTabSheet.Create (PageControl1);
  4.    SheetBrowser.PageControl := PageControl1;
  5.    SheetBrowser.Caption := 'Web Browser - Untitled';
  6.  
  7.    SheetHTML := TTabSheet.Create (PageControl1);
  8.    SheetHTML.PageControl := PageControl1;
  9.    SheetHTML.Caption := 'HTML Viewer - Untitled';
  10. end;
  11.  
  12. procedure TForm1.Button1Click(Sender: TObject);
  13. begin
  14.   Ind := -1;
  15.  
  16.   Comp := FindComponent('SheetHTML');
  17.      if Assigned(Comp) then
  18.        if Comp is TTabSheet then
  19.          Index := TTabSheet(Comp).PageIndex;
  20.  
  21.      SheetBrowser := TTabSheet.Create(PageControl1);
  22.      SheetBrowser.PageControl := PageControl1;
  23.      SheetBrowser.Caption := 'Web Browser - Untitled '+IntToStr(PageControl1.PageCount -2); // This is where the math problem is - the -2 stops the first page being named "Web Browser - Untitled 3 (due to the existence of the first two tabs)
  24.  
  25.      SheetHTML := TTabSheet.Create(PageControl1);
  26.      SheetHTML.PageControl := PageControl1;
  27.      SheetHTML.Caption := 'HTML Viewer - Unitled '+IntToStr(PageControl1.PageCount -3); //  Subtract 3 to account for the previous tab being added - this makes it the same number - Untitled 1, Untitled 1
  28. end;

The variable declarations:

Code: Pascal  [Select]
  1. var
  2.   Form1: TForm1;
  3.   SheetBrowser: TTabSheet;
  4.   SheetHTML: TTabSheet;
  5.   Ind: Integer;
  6.   Comp: TComponent;

Thanks for the help!

Blaazen

  • Hero Member
  • *****
  • Posts: 2570
  • POKE 54296,15
    • Eye-Candy Controls
Re: Math Problem
« Reply #1 on: January 13, 2018, 09:45:57 pm »
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var i: Integer;
  3. begin
  4.   Ind := -1;
  5.  
  6.   Comp := FindComponent('SheetHTML');
  7.      if Assigned(Comp) then
  8.        if Comp is TTabSheet then
  9.          Index := TTabSheet(Comp).PageIndex;
  10.  
  11.      i:=PageControl1.PageCount div 2;
  12.  
  13.      SheetBrowser := TTabSheet.Create(PageControl1);
  14.      SheetBrowser.PageControl := PageControl1;
  15.      SheetBrowser.Caption := 'Web Browser - Untitled '+IntToStr(i); // This is where the math problem is - the -2 stops the first page being named "Web Browser - Untitled 3 (due to the existence of the first two tabs)
  16.  
  17.      SheetHTML := TTabSheet.Create(PageControl1);
  18.      SheetHTML.PageControl := PageControl1;
  19.      SheetHTML.Caption := 'HTML Viewer - Unitled '+IntToStr(i); //  Subtract 3 to account for the previous tab being added - this makes it the same number - Untitled 1, Untitled 1
  20. end;
Lazarus 1.9.0 r58554 FPC 3.1.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.1, Plasma 5.13.2
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.11

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

taazz

  • Hero Member
  • *****
  • Posts: 5149
Re: Math Problem
« Reply #2 on: January 13, 2018, 09:50:02 pm »
Code: Pascal  [Select]
  1.       i:= (PageControl1.PageCount div 2)+1;
  2.  
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

A

  • Guest
Re: Math Problem
« Reply #3 on: January 13, 2018, 09:54:59 pm »
Thanks!

I didn't know about 'div' - I tried using '/' to fiddle about with it, but I got errors.

I'll read the documentation and find out when I should use 'div' and when I should use '/'.

Regardless, all working now.

Thanks!

taazz

  • Hero Member
  • *****
  • Posts: 5149
Re: Math Problem
« Reply #4 on: January 13, 2018, 09:56:47 pm »
Thanks!

I didn't know about 'div' - I tried using '/' to fiddle about with it, but I got errors.

I'll read the documentation and find out when I should use 'div' and when I should use '/'.

Regardless, all working now.

Thanks!
Code: Pascal  [Select]
  1. function Div(const a,b:integer):integer;
  2. begin
  3.   result := trunc(a/b);//truncate the decimal places and return an integer.
  4. end;
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

jamie

  • Hero Member
  • *****
  • Posts: 773
Re: Math Problem
« Reply #5 on: January 14, 2018, 01:37:43 am »
 There is a bit test trick..

  If (SomeIndexNumber and 1) <> 0 Then We Have an ODD number...

  Then if you want to ignore the odd number and only use the divisible in groups
of two..
  SomeIndexNumber :=  SomeIndexNumber and (MaxInteger-1);

 That will mask off the first bit leaving you with the remaining index value which
 will do things like 2,4,6,8 and up etc..

Thaddy

  • Hero Member
  • *****
  • Posts: 6350
Re: Math Problem
« Reply #6 on: January 14, 2018, 08:17:56 am »
Jamie we have Odd() for that: https://www.freepascal.org/docs-html/rtl/system/odd.html
Code: Pascal  [Select]
  1. if Odd(SomeIndexNumber) then; // we have an odd number ;)
  2. if not Odd(SomeIndexNumber) then; // we have an even number ;)
« Last Edit: January 14, 2018, 08:25:31 am by Thaddy »
I am enjoining wine, not whine....

mangakissa

  • Hero Member
  • *****
  • Posts: 809
Re: Math Problem
« Reply #7 on: January 14, 2018, 12:19:07 pm »
Thanks!

I didn't know about 'div' - I tried using '/' to fiddle about with it, but I got errors.

I'll read the documentation and find out when I should use 'div' and when I should use '/'.
'div' will be used by integers and '/' by floats
Lazarus 1.64 (32b) / FPC 3.0
Windows 10

Thaddy

  • Hero Member
  • *****
  • Posts: 6350
Re: Math Problem
« Reply #8 on: January 14, 2018, 12:22:36 pm »
Thanks!

I didn't know about 'div' - I tried using '/' to fiddle about with it, but I got errors.

I'll read the documentation and find out when I should use 'div' and when I should use '/'.
'div' will be used by integers and '/' by floats

Also ignore the div function above: it is wrong. div is a compiler intrinsic for integer divide and part of the language. It generates integer divide code on CPU instruction level (processor) level. Things like (trunc()) are much slower than native instructions like idiv and family. The above example is close to trolling if it isn't trolling.
« Last Edit: January 14, 2018, 12:24:52 pm by Thaddy »
I am enjoining wine, not whine....

mangakissa

  • Hero Member
  • *****
  • Posts: 809
Re: Math Problem
« Reply #9 on: January 14, 2018, 12:25:40 pm »
+1
Lazarus 1.64 (32b) / FPC 3.0
Windows 10

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus