String 帕斯卡:如何比较大的数字
我有一些包含整数的字符串,它们可能比maxInt更大,我需要对它们进行比较,那么最好的方法是什么呢。 以下是我的代码示例:String 帕斯卡:如何比较大的数字,string,performance,comparison,pascal,long-integer,String,Performance,Comparison,Pascal,Long Integer,我有一些包含整数的字符串,它们可能比maxInt更大,我需要对它们进行比较,那么最好的方法是什么呢。 以下是我的代码示例: x := 1; Reset(File1); While Not eof(File1) do Begin Read(File1, num[i]); Inc(i) End; z := i; w := z + 1; j := z + 1; While Not eof(File1) do Begin
x := 1;
Reset(File1);
While Not eof(File1) do
Begin
Read(File1, num[i]);
Inc(i)
End;
z := i;
w := z + 1;
j := z + 1;
While Not eof(File1) do
Begin
Read(File1, num[j]);
Inc(j)
End;
y := j;
If
If j > i Then a := 1 Else If j = i Then
Begin
While z <> x do
Begin
If Ord(num[j]) > Ord(num[i]) Then a := 1 Else If Ord(num[j]) < Ord(num[i]) Then a := 0;
Dec(j);
Dec(i)
End;
End Else a := 0;
If a = 1 Then
Begin
x := z+1;
z := y
End;
x:=1;
重置(文件1);
而不是eof(文件1)做什么
开始
读取(File1,num[i]);
公司(一)
结束;
z:=i;
w:=z+1;
j:=z+1;
而不是eof(文件1)做什么
开始
读取(File1,num[j]);
公司(j)
结束;
y:=j;
如果
如果j>i则a:=1,否则如果j=i则
开始
而z x
开始
如果Ord(num[j])>Ord(num[i]),则a:=1;如果Ord(num[j])
如果您要做的唯一一件事是比较包含可能大于编译器内置例程的数字的字符串,则可以比较字符串本身
首先比较长度,如果相同,从左到右比较字符将是一个好策略
NB。如果字符串包含尾随空格或前导空格、前导零,请在比较之前删除它们。
下面是一个使用stringlist按升序对值(作为字符串)排序的示例(应在Delphi和freepascal中使用):
更新:
如果数字可能为负数,则可通过该比较测试确定:
function TMyStringList.CompareStrings(const S1, S2: string): Integer;
var
i : Integer;
cmpNegative : Boolean;
const
cNeg : array[boolean] of Integer = (1,-1);
begin
// Trimming leading/trailing spaces and leading zeroes might be needed first
Result := 0;
cmpNegative := false;
// Test for negative numbers
if (S1[1] = '-') then begin
if (S2[1] <> '-') then begin
Result := -1;
Exit;
end;
// Both numbers negative, reverse comparison
cmpNegative := true;
end
else
if (S2[1] = '-') then begin
Result := 1;
Exit;
end;
// Compare length, shortest sorts first
if (Length(S1) > Length(S2)) then begin
Result := 1*cNeg[cmpNegative];
Exit;
end;
if (Length(S1) < Length(S2)) then begin
Result := -1*cNeg[cmpNegative];
Exit;
end;
i := 1;
while (i <= Length(S1)) do begin
if (Ord(S1[i]) < Ord(S2[i])) then begin
Result := -1*cNeg[cmpNegative];
Exit;
end
else
if (Ord(S1[i]) > Ord(S2[i])) then begin
Result := 1*cNeg[cmpNegative];
Exit;
end;
Inc(i);
end;
end;
函数TMyStringList.CompareStrings(常量S1,S2:string):整数;
变量
i:整数;
cmpNegative:布尔型;
常数
cNeg:Integer=(1,-1)的数组[boolean];
开始
//可能首先需要修剪前导/尾随空格和前导零
结果:=0;
cmpNegative:=假;
//负数检验
如果(S1[1]='-'),则开始
如果(S2[1]'-'),则开始
结果:=-1;
出口
结束;
//两个数字都是负数,反向比较
cmpNegative:=真;
结束
其他的
如果(S2[1]='-'),则开始
结果:=1;
出口
结束;
//比较长度,首先排序最短
如果(长度(S1)>长度(S2)),则开始
结果:=1*cNeg[CMP阴性];
出口
结束;
如果(长度(S1)<长度(S2)),则开始
结果:=-1*cNeg[CMP阴性];
出口
结束;
i:=1;
while(i)Ord(S2[i])然后开始
结果:=1*cNeg[CMP阴性];
出口
结束;
公司(一);
结束;
结束;
如果需要对这些值进行算术运算,请考虑使用大整数包。请参见
如果您只想比较包含可能大于编译器内置例程的数字的字符串,则可以比较字符串本身 首先比较长度,如果相同,从左到右比较字符将是一个好策略 NB。如果字符串包含尾随空格或前导空格、前导零,请在比较之前删除它们。 下面是一个使用stringlist按升序对值(作为字符串)排序的示例(应在Delphi和freepascal中使用): 更新: 如果数字可能为负数,则可通过该比较测试确定:function TMyStringList.CompareStrings(const S1, S2: string): Integer;
var
i : Integer;
cmpNegative : Boolean;
const
cNeg : array[boolean] of Integer = (1,-1);
begin
// Trimming leading/trailing spaces and leading zeroes might be needed first
Result := 0;
cmpNegative := false;
// Test for negative numbers
if (S1[1] = '-') then begin
if (S2[1] <> '-') then begin
Result := -1;
Exit;
end;
// Both numbers negative, reverse comparison
cmpNegative := true;
end
else
if (S2[1] = '-') then begin
Result := 1;
Exit;
end;
// Compare length, shortest sorts first
if (Length(S1) > Length(S2)) then begin
Result := 1*cNeg[cmpNegative];
Exit;
end;
if (Length(S1) < Length(S2)) then begin
Result := -1*cNeg[cmpNegative];
Exit;
end;
i := 1;
while (i <= Length(S1)) do begin
if (Ord(S1[i]) < Ord(S2[i])) then begin
Result := -1*cNeg[cmpNegative];
Exit;
end
else
if (Ord(S1[i]) > Ord(S2[i])) then begin
Result := 1*cNeg[cmpNegative];
Exit;
end;
Inc(i);
end;
end;
函数TMyStringList.CompareStrings(常量S1,S2:string):整数;
变量
i:整数;
cmpNegative:布尔型;
常数
cNeg:Integer=(1,-1)的数组[boolean];
开始
//可能首先需要修剪前导/尾随空格和前导零
结果:=0;
cmpNegative:=假;
//负数检验
如果(S1[1]='-'),则开始
如果(S2[1]'-'),则开始
结果:=-1;
出口
结束;
//两个数字都是负数,反向比较
cmpNegative:=真;
结束
其他的
如果(S2[1]='-'),则开始
结果:=1;
出口
结束;
//比较长度,首先排序最短
如果(长度(S1)>长度(S2)),则开始
结果:=1*cNeg[CMP阴性];
出口
结束;
如果(长度(S1)<长度(S2)),则开始
结果:=-1*cNeg[CMP阴性];
出口
结束;
i:=1;
while(i)Ord(S2[i])然后开始
结果:=1*cNeg[CMP阴性];
出口
结束;
公司(一);
结束;
结束;
如果需要对这些值进行算术运算,请考虑使用大整数包。请看
尽管我投了赞成票,我还是想到了负数和负号。@LURD已经链接到了一个列表。我想推荐我的:文档。@MarkSetchell,谢谢,还增加了一个负数测试。尽管我投了赞成票,但我只想到了负数和负号。@LURD已经链接到了一个列表。我想推荐我的:文档。@MarkSetchell,谢谢,还添加了负数测试。function TMyStringList.CompareStrings(const S1, S2: string): Integer;
var
i : Integer;
cmpNegative : Boolean;
const
cNeg : array[boolean] of Integer = (1,-1);
begin
// Trimming leading/trailing spaces and leading zeroes might be needed first
Result := 0;
cmpNegative := false;
// Test for negative numbers
if (S1[1] = '-') then begin
if (S2[1] <> '-') then begin
Result := -1;
Exit;
end;
// Both numbers negative, reverse comparison
cmpNegative := true;
end
else
if (S2[1] = '-') then begin
Result := 1;
Exit;
end;
// Compare length, shortest sorts first
if (Length(S1) > Length(S2)) then begin
Result := 1*cNeg[cmpNegative];
Exit;
end;
if (Length(S1) < Length(S2)) then begin
Result := -1*cNeg[cmpNegative];
Exit;
end;
i := 1;
while (i <= Length(S1)) do begin
if (Ord(S1[i]) < Ord(S2[i])) then begin
Result := -1*cNeg[cmpNegative];
Exit;
end
else
if (Ord(S1[i]) > Ord(S2[i])) then begin
Result := 1*cNeg[cmpNegative];
Exit;
end;
Inc(i);
end;
end;