有一个字符串 '200000000000000000000000000001' 为30位我想使它加1 得到 '200000000000000000000000000002' .使它减1 得到 '200000000000000000000000000002' .
用strtoint 位数数不够,请问该如何实现!!!
用strtoint 位数数不够,请问该如何实现!!!
解决方案 »
- OPC--如何枚举OPC服务器上的所有标签?
- SendMessage(z, EM_ReplaceSel, 0, Integer(pchar('?')));为什么目标不能显示问号?
- 请教日志问题
- 求保存数据时出现 "Mismatch in datapacket" 错误,解决办法
- 若Stringlist对象sl有100条,既sl.count=100,请问如何将sl在50条以后的记录删除,使其sl.count=50
- 如何解决这样的dll释放问题,这个问题已经困扰好几天了,急!
- sql储存问题
- 整样变掉窗口标题的字体?
- 这种sql语句该怎样写. 在线
- Paradox数据库损坏“Corrupt file-other than Header"
- delphi 6 xml mapper 怎么把数据截断了啊?
- 三层架构中服务器端网络断开造成客户端死机的问题
v, k :Integer;k := 1; //初始要加的数
for i:= length(s) downto 1
begin
v := StrToInt(s[i]) + k;
if v > 10 then
begin
k = v div 10;
s[i] := IntToStr(v mod 10);
end;
end;减法可以参考写吧
v, k :Integer;k := 1; //初始要加的数
for i:= length(s) downto 1
begin
v := StrToInt(s[i]) + k;
if v > 10 then
begin
k = v div 10;
s[i] := IntToStr(v mod 10);
end else
begin
s[i] := IntToStr(v);
break; //忘了退出循环了
end;
end;减法可以参考写吧
v, k :Integer;k := 1; //初始要加的数
for i:= length(s) downto 1
begin
v := StrToInt(s[i]) - k;
if v < 0 then
begin
k = 1;
s[i] := IntToStr(v + 10);
end else
begin
s[i] := IntToStr(v);
break; //忘了退出循环了
end;
end;
int64也放不下这么大的数自己写函数吧
function StringAdd(const Value, Minus: string): string;
var
Digit: string;
LenV, LenM, MaxDigits, I, DigitV, DigitM: Integer;
R: Byte; // Remainder
CV, CM: Char; // char from string A, string B
V, M: string;
begin
V := Value;
M := Minus;
if (V = '') or (V = '0') then
begin
Result := M;
Exit;
end;
if (M = '') or (M = '0') then
begin
Result := V;
Exit;
end;
if V[1]='-' then
begin
Delete(V, 1, 1);
result := StringMultiply(StringSubtract(V, M), '-1');
exit;
end;
if M[1]='-' then
begin
Delete(M, 1, 1);
result := StringSubtract(V, M);
exit;
end;
R := 0;
LenV := Length(V);
LenM := Length(M);
MaxDigits := Max(LenV, LenM);
Result := '';
for I := 0 to MaxDigits - 1 do
begin
DigitV := LenV - I;
DigitM := LenM - I;
if I < LenV then CV := V[DigitV] else CV := '0';
if I < LenM then CM := M[DigitM] else CM := '0';
Digit := AddChars(CV, CM, R);
Result := Digit + Result;
end;
if R > 0 then
Result := '1' + Result;
end;
StringSubtract
AddChars本人刚学习 delphi ,不是太熟悉,请高手帮忙给个程序好吗??
begin
if BlankArgument(A) or BlankArgument(B) then
Result := '0'
else if CanUseShort(A, B, 9) then
Result := InternalShortMultiply(A,B)
else
Result := InternalLongMultiply(A,B);
end;
var
LLeft, LRight: string;
NegCount: Integer;
begin
if (Left='') or (Left='0') or (Right='') or (Right='0') then
begin
result := '0';
exit;
end;
NegCount := 0; if Left[1] = '-' then
begin
Inc(NegCount);
LLeft := Copy(Left, 2, Length(Left) - 1);
end
else
LLeft := Left; if Right[1] = '-' then
begin
Inc(NegCount);
LRight := Copy(Right, 2, Length(Right) - 1);
end
else
LRight := Right; Result := InternalMultiply(LLeft, LRight); if (NegCount mod 2) <> 0 then
if (result <>'0') and (result<>'') then
result := '-'+result;end;
function SubtractChars(V, M: Char; var R: Byte): string;
var
Value: Byte;
//I: Integer;
begin
Value := Byte((Ord(V)-48) - ((Ord(M)-48) + R));
if Value > 9 then // Byte is unsigned: values will be between 246-255
begin
Result := Char(Byte(10+Value) + 48);
R := Byte(1);
end else
begin
Result := Char(Byte(Value + 48));
R := Byte(0);
end;
end;
var
Digit, V, M: string;
LenV, LenM, MaxDigits, I, DigitV, DigitM: Integer;
R: Byte; // Remainder
CV, CM: Char; // char from string A, string B
rs: string;
begin
V := Value;
M := Minus; if (V = '') or (M = '') then
begin
if V = '' then
begin
Result := StringMultiply(M, '-1');
end
else
begin
Result := V;
end;
exit;
end;
if (V = '0') or (M = '0') then
begin
if M = '0' then
Result := V
else
Result := StringMultiply(M, '-1');
Exit;
end; if V[1]='-' then
begin
Delete(V, 1, 1);
result := StringMultiply(StringAdd(V, M), '-1');
exit;
end;
if M[1]='-' then
begin
Delete(M, 1, 1);
result := StringAdd(V, M);
exit;
end; if CompareDigits(Value, Minus) >= 0 then
begin
V := Value;
M := Minus;
rs := '';
end else
begin
M := Value;
V := Minus;
rs := '-';
end; R := 0;
LenV := Length(V);
LenM := Length(M);
MaxDigits := Max(LenV, LenM);
Result := '';
for I := 0 to MaxDigits - 1 do
begin
DigitV := LenV - I;
DigitM := LenM - I;
if I < LenV then
CV := V[DigitV]
else
CV := '0';
if I < LenM then
CM := M[DigitM]
else
CM := '0';
Digit := SubtractChars(CV, CM, R);
Result := Digit + Result;
end;
result := LeftTrim(result);
result := rs + result;
if Result = '' then
Result := '0';
end;
var
Value: Byte;
begin
Value := Byte((Ord(V)-48) + (Ord(M)-48) + R);
if Value > 9 then
begin
Result := Char(Byte(Value - 10) + 48);
R := Byte(1);
end else
begin
Result := Char(Byte(Value + 48));
R := Byte(0);
end;
if Result = '' then Result := '0';
end;
试试看,仅针对1位数相加减var
v, k :Integer;k := 1; //初始要加的数
for i:= length(s) downto 1
begin
v := StrToInt(s[i]) + k;
if v > 10 then
begin
k = v div 10;
s[i] := IntToStr(v mod 10);
end else
begin
s[i] := IntToStr(v);
break; //忘了退出循环了
end;
end;有问题哈, s[i] := IntToStr(v mod 10);
语法错误, s[i] := IntToStr(v mod 10)[1];
还有如果是999+1,你看看你的程序运行结果是什么。
如果真要我写函数,我会返回一个新的字符串的,那999+1就会考虑在里面了
其实,加,减,乘,我都有思路(包括多位数加减乘多位数),但是除法一直找不到好的算法