Home > Back-end >  How do you write this question?
How do you write this question?

Time:10-12


STR:='1-3, 4, 6, 7-9, 10, 12, 13-15 dec - the 18th and 19th - 21, 22 and 24'//representative numerical range, 1 to 3, 4 to 6, 7 to 9, and so on...

Assuming that there is now a numerical num, how to judge this number belongs to which a range of STR inside? Such as num:=8, supposedly, num should be in the range, 7-9

I want the result is according to the value of num, obtain corresponding interval, get BSTR:='7-9, so, how to write code?

CodePudding user response:

Tstringlist

CodePudding user response:

Custom collection or structure
Or use tstringlist

Then cycle,

CodePudding user response:

The
reference 2 floor response:

define collection or structureOr use tstringlist

Then cycle judgment,
+ 1

CodePudding user response:

Tstringlist assignment after
Then cycle judgment
Or dichotomy

CodePudding user response:

Num>=2 - & gt; Located in the area (num 1) - (num + 1)

CodePudding user response:

Var
Sl: TStringList;
S: a string;
I, num: Integer;
The begin
Sl:=TStringList. Create;

Try
Sl. The Delimiter:=', ';
Num:=8;
Sl. DelimitedText:='1-3, 4, 6, 7-9, 10-12, 13-15 dec - the 18th and 19th - 21, 22 and 24'.
For I:=0 to sl. Do the Count - 1
The begin
S:=sl [I];
If (num & gt; StrToInt [1] (s) - 1) and (num & lt; [3] StrToInt (s) + 1) then
ShowMessage (sl [I]);
end;
The finally
Sl. Free;
end;//try

CodePudding user response:

The
reference 6th floor response:
var
Sl: TStringList;
S: a string;
I, num: Integer;
The begin
Sl:=TStringList. Create;

Try
Sl. The Delimiter:=', ';
Num:=8;
Sl. DelimitedText:='1-3, 4, 6, 7-9, 10, 12, 13-15 16th to 18th and 19th - 21st,...


However, there is an error,

CodePudding user response:

 var 
STR, BSTR: string;
Num, I: Integer;
The begin
STR:='1-3, 4, 6, 7-9, 10, 12, 13-15 dec - the 18th and 19th - 21, 22 and 24'.
Num:=8;

With TStringList. Create the do
Try
CommaText:=StringReplace (STR, '-' and ', '[rfReplaceAll]);
For I:=0 to do the Count - 1
If StrToInt (Strings [I]) & gt; Num then
The begin
BSTR:=Strings [I - 1) + '-' + Strings [I];
ShowMessage (BSTR);
Break
end;
The finally
Free
end;
end;

CodePudding user response:

The
reference 5 floor response:
num>=2 - & gt; Located in the area (num 1) - (num + 1)
change

 
Case Num 3 of mod
Zero: bStr:=IntToStr (Num - 2) + '-' + IntToStr (Num);
1: bStr:=IntToStr (Num) + '-' + IntToStr (Num + 2);
2: bStr:=IntToStr (Num - 1) + '-' + IntToStr (Num + 1);


CodePudding user response:

The above an end

CodePudding user response:

 
Write a function to you, this should be what you want as a result,
Directly into the numerical can determine scope,
The function ShowMSG (num: integer) : string;
Var
STR: string;
The begin
STR:=';
Case num of
1.. 3:
The begin
STR:='1-3;
end;
4.. 6:
The begin
STR:='4-6';
end;
7.. 9:
The begin
STR:='7-9;
end;
10.. 12:
The begin
STR:='10.. 12 ';
end;
The else
The begin
STR:='not found';
end;
end;
Result:=STR;
end;

CodePudding user response:

The
reference 11 floor response:
Delphi (Pascal) code

Write a function to you, this should be what you want as a result,
Directly into the numerical can determine scope,
The function ShowMSG (num: integer) : string;
Var
STR: string;
The begin
STR:=';
Case num of
1.. 3:
The begin
STR:='1-3;
.


 
Procedure TForm1. Button1Click (Sender: TObject);
The begin
Showmessage (ShowMSG (3));
end;

CodePudding user response:

LZ is general
Scope is certain, if the interval number 1, how simple, + 1 and 1 is the scope of the

8 # floor, the answer can be the reference

CodePudding user response:

A: const array [24] 1.. of integer=,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8 (1);
Const al: an array of integer=[1.. 8],4,7,10,13,16,19,22 (1);
Const ah: an array of integer=[1.. 8],6,9,12,15,18,21,24 (3);

The function areaof (n: integer) : integer;
The begin
Areaof:=a, [n].
end;
The function arealow (n: integer) : integer;
The begin
Arealow:=al [n].
end;
The function areahigh (n: integer) : integer;
The begin
Areahigh:=ah [n].
end;
  • Related