Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
5
Task/Increment-a-numerical-string/00-META.yaml
Normal file
5
Task/Increment-a-numerical-string/00-META.yaml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
category:
|
||||
- Simple
|
||||
from: http://rosettacode.org/wiki/Increment_a_numerical_string
|
||||
note: Text processing
|
||||
4
Task/Increment-a-numerical-string/00-TASK.txt
Normal file
4
Task/Increment-a-numerical-string/00-TASK.txt
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
;Task:
|
||||
Increment a numerical string.
|
||||
<br><br>
|
||||
|
||||
|
|
@ -0,0 +1 @@
|
|||
V next = String(Int(‘123’) + 1)
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
org 100h
|
||||
jmp demo
|
||||
;;; Increment the number in the $-terminated string under HL.
|
||||
;;; The new string is written back to the original location.
|
||||
;;; It may grow by one byte (e.g. 999 -> 1000).
|
||||
incstr: mvi a,'$' ; End marker
|
||||
lxi d,303Ah ; D='0', E='9'+1
|
||||
lxi b,0 ; Find the end of the string and find the length
|
||||
isrch: cmp m ; Are we there yet?
|
||||
inx b ; If not, try next character
|
||||
inx h
|
||||
jnz isrch
|
||||
dcx h
|
||||
dcx b
|
||||
mov a,b ; Is the string empty?
|
||||
ora c
|
||||
rz ; Then return (do nothing)
|
||||
inx b
|
||||
idigit: dcx b ; Go to previous digit
|
||||
dcx h
|
||||
mov a,b ; Are we at the beginning of the string?
|
||||
ora c
|
||||
jz igrow ; Then the string grows (999 -> 1000)
|
||||
inr m ; Otherwise, increment the digit
|
||||
mov a,e
|
||||
cmp m ; Did we try to increment '9'?
|
||||
rnz ; If not, we're done, return
|
||||
mov m,d ; But if so, this digit is now a 0
|
||||
jmp idigit ; And we should do the next digit
|
||||
igrow: inx h
|
||||
mvi m,'1' ; The string should now be '10000...'
|
||||
inx h ; We know the string is at least one char long
|
||||
mvi a,'$'
|
||||
izero: cmp m ; Are we at the end yet?
|
||||
mov m,d ; In any case, write a zero
|
||||
inx h
|
||||
jnz izero ; If not done, write a zero
|
||||
mov m,a ; Finally, reterminate the string
|
||||
ret
|
||||
;;; Demo code: increment the CP/M command line argument
|
||||
demo: lxi h,80h ; $-terminate the string
|
||||
mov a,m
|
||||
adi 81h ; Length is at 80h, the argument itself at 81h
|
||||
mov l,a
|
||||
mvi m,'$'
|
||||
mvi l,80h ; Skip any spaces
|
||||
mvi a,' '
|
||||
space: inx h
|
||||
cmp m
|
||||
jz space
|
||||
push h ; Store the beginning of the string
|
||||
call incstr ; Increment the number in the string
|
||||
pop d ; Print the string
|
||||
mvi c,9
|
||||
jmp 5
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
cpu 8086
|
||||
bits 16
|
||||
section .text
|
||||
org 100h
|
||||
jmp demo ; Jump towards demo code
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;;; Increment the number in the $-terminated string
|
||||
;;; in [ES:DI]. The string is written back to its original
|
||||
;;; location. It may grow by one byte.
|
||||
incnum: mov al,'$' ; Find string terminator
|
||||
mov bx,di ; Store the beginning of the string
|
||||
mov cx,-1
|
||||
repne scasb
|
||||
dec di ; Move to the terminator
|
||||
cmp bx,di ; If the string is empty, do nothing
|
||||
je .out
|
||||
.digit: cmp bx,di ; Is this the first digit?
|
||||
je .grow ; If so, the string grows
|
||||
dec di ; Go one digit backwards
|
||||
inc byte [es:di] ; Increment the digit
|
||||
cmp byte [es:di],'9'+1 ; Did we increment past 9?
|
||||
jne .out ; If not, we're done
|
||||
mov byte [es:di],'0' ; Otherwise, write a zero
|
||||
jmp .digit ; And increment the next digit
|
||||
.grow: mov al,'1' ; Write an 1 first (we know the string
|
||||
stosb ; is at least one character long)
|
||||
dec al ; Zero
|
||||
.zero: cmp byte [es:di],'$' ; Are we about to overwrite
|
||||
stosb ; the terminator? First, do it anyway;
|
||||
jne .zero ; Keep writing zeroes until $ is reached
|
||||
mov al,'$' ; Finally, write a new terminator
|
||||
stosb
|
||||
.out: ret
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;;; Demo code: increment the number on the MS-DOS
|
||||
;;; command line.
|
||||
demo: mov si,80h ; $-terminate the string
|
||||
lodsb
|
||||
xor bh,bh
|
||||
mov bl,al
|
||||
mov byte [si+bx],'$'
|
||||
mov al,' ' ; Skip past any spaces
|
||||
mov cx,-1
|
||||
mov di,si
|
||||
repe scasb
|
||||
dec di
|
||||
mov dx,di ; Keep start of string in DX
|
||||
call incnum ; Increment the number in the string
|
||||
mov ah,9 ; Print the string
|
||||
int 21h
|
||||
ret
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
/* ARM assembly AARCH64 Raspberry PI 3B */
|
||||
/* program incstring64.s */
|
||||
|
||||
/*******************************************/
|
||||
/* Constantes file */
|
||||
/*******************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly*/
|
||||
.include "../includeConstantesARM64.inc"
|
||||
|
||||
.equ BUFFERSIZE, 100
|
||||
/*******************************************/
|
||||
/* Initialized data */
|
||||
/*******************************************/
|
||||
.data
|
||||
szMessNum: .asciz "Enter number : \n"
|
||||
szCarriageReturn: .asciz "\n"
|
||||
szMessResult: .asciz "Increment number is = @ \n" // message result
|
||||
|
||||
/*******************************************/
|
||||
/* UnInitialized data */
|
||||
/*******************************************/
|
||||
.bss
|
||||
sBuffer: .skip BUFFERSIZE
|
||||
sZoneConv: .skip 24
|
||||
/*******************************************/
|
||||
/* code section */
|
||||
/*******************************************/
|
||||
.text
|
||||
.global main
|
||||
main: // entry of program
|
||||
|
||||
ldr x0,qAdrszMessNum
|
||||
bl affichageMess
|
||||
mov x0,#STDIN // Linux input console
|
||||
ldr x1,qAdrsBuffer // buffer address
|
||||
mov x2,#BUFFERSIZE // buffer size
|
||||
mov x8, #READ // request to read datas
|
||||
svc 0 // call system
|
||||
ldr x1,qAdrsBuffer // buffer address
|
||||
strb wzr,[x1,x0] // store zero at the end of input string (x0
|
||||
//
|
||||
ldr x0,qAdrsBuffer // buffer address
|
||||
bl conversionAtoD // conversion string in number in x0
|
||||
// increment x0
|
||||
add x0,x0,1
|
||||
// conversion register to string
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl conversion10S // call conversion
|
||||
ldr x0,qAdrszMessResult
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl strInsertAtCharInc // insert result at @ character
|
||||
bl affichageMess // display message
|
||||
|
||||
100: // standard end of the program
|
||||
mov x0,0 // return code
|
||||
mov x8,EXIT // request to exit program
|
||||
svc 0 // perform the system call
|
||||
|
||||
qAdrsZoneConv: .quad sZoneConv
|
||||
qAdrszMessNum: .quad szMessNum
|
||||
qAdrsBuffer: .quad sBuffer
|
||||
qAdrszMessResult: .quad szMessResult
|
||||
qAdrszCarriageReturn: .quad szCarriageReturn
|
||||
/********************************************************/
|
||||
/* File Include fonctions */
|
||||
/********************************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly */
|
||||
.include "../includeARM64.inc"
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
report zz_incstring
|
||||
perform test using: '0', '1', '-1', '10000000', '-10000000'.
|
||||
|
||||
form test using iv_string type string.
|
||||
data: lv_int type i,
|
||||
lv_string type string.
|
||||
lv_int = iv_string + 1.
|
||||
lv_string = lv_int.
|
||||
concatenate '"' iv_string '" + 1 = "' lv_string '"' into lv_string.
|
||||
write / lv_string.
|
||||
endform.
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
STRING s := "12345"; FILE f; INT i;
|
||||
associate(f, s); get(f,i);
|
||||
i+:=1;
|
||||
s:=""; reset(f); put(f,i);
|
||||
print((s, new line))
|
||||
|
|
@ -0,0 +1,77 @@
|
|||
begin
|
||||
% returns a string representing the number in s incremented %
|
||||
% As strings are fixed length, the significant length of s must %
|
||||
% be specified in length %
|
||||
% s must contain an unsigned integer %
|
||||
% If the string is invalid or the result would require more %
|
||||
% than 256 characters (the maximum string length), "error" %
|
||||
% is returned %
|
||||
string(256) procedure increment( string(256) value s
|
||||
; integer value length
|
||||
) ;
|
||||
begin
|
||||
logical isValid;
|
||||
integer rPos, sPos, carry;
|
||||
string(256) iValue;
|
||||
string(1) c;
|
||||
isValid := true;
|
||||
iValue := " ";
|
||||
rPos := 256;
|
||||
sPos := length - 1;
|
||||
carry := 1; % ensure the first digit is incremented %
|
||||
while isValid and sPos >= 0 and rPos >= 0 do begin
|
||||
c := s( sPos // 1 );
|
||||
sPos := sPos - 1;
|
||||
if c not = " " then begin
|
||||
isValid := ( c >= "0" and c <= "9" );
|
||||
if isValid then begin
|
||||
integer d;
|
||||
d := ( decode( c ) - decode( "0" ) ) + carry;
|
||||
carry := d div 10;
|
||||
rPos := rPos - 1;
|
||||
iValue( rPos // 1 ) := code( decode( "0" ) + d rem 10 )
|
||||
end if_isValid
|
||||
end if_c_ne_space
|
||||
end while_isValid_and_sPos_ge_0_and_rPOs_ge_0 ;
|
||||
if isValid then begin
|
||||
% the number was incremented successfully %
|
||||
if carry not = 0 then begin
|
||||
% need an extra digit %
|
||||
if rPos <= 0
|
||||
then isValid := false % no room for an extra digit %
|
||||
else begin
|
||||
% have space for an extra digit %
|
||||
rPos := rPos - 1;
|
||||
iValue( rPos // 1 ) := code( decode( "0" ) + carry )
|
||||
end if_rPos_lt_0__
|
||||
end if_carry_gt_0
|
||||
end if_isValid ;
|
||||
if not isValid then begin
|
||||
% s is not a numeric string or the result would be longer than 256 characters %
|
||||
iValue := "error"
|
||||
end
|
||||
else begin
|
||||
% the string could be incremented %
|
||||
string(256) rightJustifiedValue;
|
||||
rightJustifiedValue := iValue;
|
||||
iValue := " ";
|
||||
for iPos := 0 until 255 - rPos do iValue( iPos // 1 ) := rightJustifiedValue( rPos + iPos // 1 )
|
||||
end if_not_isValid_or_carry_ne_0__ ;
|
||||
iValue
|
||||
end increment ;
|
||||
% writes the string s, up to the first blank %
|
||||
procedure writeonToBlank ( string(256) value s ) ;
|
||||
begin
|
||||
integer sPos;
|
||||
sPos := 0;
|
||||
while sPos < 256 and s( sPos // 1 ) not = " " do begin
|
||||
writeon( s_w := 0, s( sPos // 1 ) );
|
||||
sPos := sPos + 1
|
||||
end while_spos_lt_256_and_s_Spos_ne_space
|
||||
end writeonToBlank ;
|
||||
% test increment %
|
||||
write( " 0 + 1: " ); writeonToBlank( increment( "0", 1 ) );
|
||||
write( " 9 + 1: " ); writeonToBlank( increment( "9", 1 ) );
|
||||
write( " 123456789 + 1: " ); writeonToBlank( increment( "123456789", 9 ) );
|
||||
write( "99999999999999999999 + 1: " ); writeonToBlank( increment( "99999999999999999999", 20 ) )
|
||||
end.
|
||||
|
|
@ -0,0 +1 @@
|
|||
⍕1+⍎'12345'
|
||||
|
|
@ -0,0 +1,203 @@
|
|||
/* ARM assembly Raspberry PI */
|
||||
/* program incstring.s */
|
||||
|
||||
/* Constantes */
|
||||
.equ BUFFERSIZE, 100
|
||||
.equ STDIN, 0 @ Linux input console
|
||||
.equ STDOUT, 1 @ Linux output console
|
||||
.equ EXIT, 1 @ Linux syscall
|
||||
.equ READ, 3 @ Linux syscall
|
||||
.equ WRITE, 4 @ Linux syscall
|
||||
/* Initialized data */
|
||||
.data
|
||||
szMessNum: .asciz "Enter number : \n"
|
||||
szCarriageReturn: .asciz "\n"
|
||||
szMessResult: .ascii "Increment number is = " @ message result
|
||||
sMessValeur: .fill 12, 1, ' '
|
||||
.asciz "\n"
|
||||
|
||||
/* UnInitialized data */
|
||||
.bss
|
||||
sBuffer: .skip BUFFERSIZE
|
||||
|
||||
/* code section */
|
||||
.text
|
||||
.global main
|
||||
main: /* entry of program */
|
||||
push {fp,lr} /* saves 2 registers */
|
||||
|
||||
ldr r0,iAdrszMessNum
|
||||
bl affichageMess
|
||||
mov r0,#STDIN @ Linux input console
|
||||
ldr r1,iAdrsBuffer @ buffer address
|
||||
mov r2,#BUFFERSIZE @ buffer size
|
||||
mov r7, #READ @ request to read datas
|
||||
swi 0 @ call system
|
||||
ldr r1,iAdrsBuffer @ buffer address
|
||||
mov r2,#0 @ end of string
|
||||
strb r2,[r1,r0] @ store byte at the end of input string (r0
|
||||
@
|
||||
ldr r0,iAdrsBuffer @ buffer address
|
||||
bl conversionAtoD @ conversion string in number in r0
|
||||
@ increment r0
|
||||
add r0,#1
|
||||
@ conversion register to string
|
||||
ldr r1,iAdrsMessValeur
|
||||
bl conversion10S @ call conversion
|
||||
ldr r0,iAdrszMessResult
|
||||
bl affichageMess @ display message
|
||||
|
||||
100: /* standard end of the program */
|
||||
mov r0, #0 @ return code
|
||||
pop {fp,lr} @restaur 2 registers
|
||||
mov r7, #EXIT @ request to exit program
|
||||
swi 0 @ perform the system call
|
||||
|
||||
iAdrsMessValeur: .int sMessValeur
|
||||
iAdrszMessNum: .int szMessNum
|
||||
iAdrsBuffer: .int sBuffer
|
||||
iAdrszMessResult: .int szMessResult
|
||||
iAdrszCarriageReturn: .int szCarriageReturn
|
||||
/******************************************************************/
|
||||
/* display text with size calculation */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the message */
|
||||
affichageMess:
|
||||
push {fp,lr} /* save registres */
|
||||
push {r0,r1,r2,r7} /* save others registers */
|
||||
mov r2,#0 /* counter length */
|
||||
1: /* loop length calculation */
|
||||
ldrb r1,[r0,r2] /* read octet start position + index */
|
||||
cmp r1,#0 /* if 0 its over */
|
||||
addne r2,r2,#1 /* else add 1 in the length */
|
||||
bne 1b /* and loop */
|
||||
/* so here r2 contains the length of the message */
|
||||
mov r1,r0 /* address message in r1 */
|
||||
mov r0,#STDOUT /* code to write to the standard output Linux */
|
||||
mov r7, #WRITE /* code call system "write" */
|
||||
swi #0 /* call systeme */
|
||||
pop {r0,r1,r2,r7} /* restaur others registers */
|
||||
pop {fp,lr} /* restaur des 2 registres */
|
||||
bx lr /* return */
|
||||
|
||||
/******************************************************************/
|
||||
/* Convert a string to a number stored in a registry */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the area terminated by 0 or 0A */
|
||||
/* r0 returns a number */
|
||||
conversionAtoD:
|
||||
push {fp,lr} @ save 2 registers
|
||||
push {r1-r7} @ save others registers
|
||||
mov r1,#0
|
||||
mov r2,#10 @ factor
|
||||
mov r3,#0 @ counter
|
||||
mov r4,r0 @ save address string -> r4
|
||||
mov r6,#0 @ positive sign by default
|
||||
mov r0,#0 @ initialization to 0
|
||||
1: /* early space elimination loop */
|
||||
ldrb r5,[r4,r3] @ loading in r5 of the byte located at the beginning + the position
|
||||
cmp r5,#0 @ end of string -> end routine
|
||||
beq 100f
|
||||
cmp r5,#0x0A @ end of string -> end routine
|
||||
beq 100f
|
||||
cmp r5,#' ' @ space ?
|
||||
addeq r3,r3,#1 @ yes we loop by moving one byte
|
||||
beq 1b
|
||||
cmp r5,#'-' @ first character is -
|
||||
moveq r6,#1 @ 1 -> r6
|
||||
beq 3f @ then move on to the next position
|
||||
2: /* beginning of digit processing loop */
|
||||
cmp r5,#'0' @ character is not a number
|
||||
blt 3f
|
||||
cmp r5,#'9' @ character is not a number
|
||||
bgt 3f
|
||||
/* character is a number */
|
||||
sub r5,#48
|
||||
umull r0,r1,r2,r0 @ multiply par factor 10
|
||||
cmp r1,#0 @ overflow ?
|
||||
bgt 99f @ overflow error
|
||||
add r0,r5 @ add to r0
|
||||
3:
|
||||
add r3,r3,#1 @ advance to the next position
|
||||
ldrb r5,[r4,r3] @ load byte
|
||||
cmp r5,#0 @ end of string -> end routine
|
||||
beq 4f
|
||||
cmp r5,#0x0A @ end of string -> end routine
|
||||
beq 4f
|
||||
b 2b @ loop
|
||||
4:
|
||||
cmp r6,#1 @ test r6 for sign
|
||||
moveq r1,#-1
|
||||
muleq r0,r1,r0 @ if negatif, multiply par -1
|
||||
b 100f
|
||||
99: /* overflow error */
|
||||
ldr r0,=szMessErrDep
|
||||
bl affichageMess
|
||||
mov r0,#0 @ return zero if error
|
||||
100:
|
||||
pop {r1-r7} @ restaur other registers
|
||||
pop {fp,lr} @ restaur 2 registers
|
||||
bx lr @return procedure
|
||||
/* constante program */
|
||||
szMessErrDep: .asciz "Too large: overflow 32 bits.\n"
|
||||
.align 4
|
||||
|
||||
/***************************************************/
|
||||
/* Converting a register to a signed decimal */
|
||||
/***************************************************/
|
||||
/* r0 contains value and r1 area address */
|
||||
conversion10S:
|
||||
push {r0-r4,lr} @ save registers
|
||||
mov r2,r1 /* debut zone stockage */
|
||||
mov r3,#'+' /* par defaut le signe est + */
|
||||
cmp r0,#0 @ negative number ?
|
||||
movlt r3,#'-' @ yes
|
||||
mvnlt r0,r0 @ number inversion
|
||||
addlt r0,#1
|
||||
mov r4,#10 @ length area
|
||||
1: @ start loop
|
||||
bl divisionpar10
|
||||
add r1,#48 @ digit
|
||||
strb r1,[r2,r4] @ store digit on area
|
||||
sub r4,r4,#1 @ previous position
|
||||
cmp r0,#0 @ stop if quotient = 0
|
||||
bne 1b
|
||||
|
||||
strb r3,[r2,r4] @ store signe
|
||||
subs r4,r4,#1 @ previous position
|
||||
blt 100f @ if r4 < 0 -> end
|
||||
|
||||
mov r1,#' ' @ space
|
||||
2:
|
||||
strb r1,[r2,r4] @store byte space
|
||||
subs r4,r4,#1 @ previous position
|
||||
bge 2b @ loop if r4 > 0
|
||||
100:
|
||||
pop {r0-r4,lr} @ restaur registers
|
||||
bx lr
|
||||
|
||||
|
||||
/***************************************************/
|
||||
/* division par 10 signé */
|
||||
/* Thanks to http://thinkingeek.com/arm-assembler-raspberry-pi/*
|
||||
/* and http://www.hackersdelight.org/ */
|
||||
/***************************************************/
|
||||
/* r0 dividende */
|
||||
/* r0 quotient */
|
||||
/* r1 remainder */
|
||||
divisionpar10:
|
||||
/* r0 contains the argument to be divided by 10 */
|
||||
push {r2-r4} /* save registers */
|
||||
mov r4,r0
|
||||
ldr r3, .Ls_magic_number_10 /* r1 <- magic_number */
|
||||
smull r1, r2, r3, r0 /* r1 <- Lower32Bits(r1*r0). r2 <- Upper32Bits(r1*r0) */
|
||||
mov r2, r2, ASR #2 /* r2 <- r2 >> 2 */
|
||||
mov r1, r0, LSR #31 /* r1 <- r0 >> 31 */
|
||||
add r0, r2, r1 /* r0 <- r2 + r1 */
|
||||
add r2,r0,r0, lsl #2 /* r2 <- r0 * 5 */
|
||||
sub r1,r4,r2, lsl #1 /* r1 <- r4 - (r2 * 2) = r4 - (r0 * 10) */
|
||||
pop {r2-r4}
|
||||
bx lr /* leave function */
|
||||
.align 4
|
||||
.Ls_magic_number_10: .word 0x66666667
|
||||
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
$ awk 'BEGIN{s="42"; s++; print s"("length(s)")" }'
|
||||
43(2)
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
PROC Increment(CHAR ARRAY src,dst)
|
||||
INT val
|
||||
|
||||
val=ValI(src)
|
||||
val==+1
|
||||
StrI(val,dst)
|
||||
RETURN
|
||||
|
||||
PROC Test(CHAR ARRAY src)
|
||||
CHAR ARRAY dst(10)
|
||||
Increment(src,dst)
|
||||
PrintF("%S+1=%S%E",src,dst)
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
Test("0")
|
||||
Test("1")
|
||||
Test("9999")
|
||||
Test("-1")
|
||||
Test("-2")
|
||||
Test("-10000")
|
||||
RETURN
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
function incrementString(str:String):String
|
||||
{
|
||||
return String(Number(str)+1);
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
S : String := "12345";
|
||||
S := Ada.Strings.Fixed.Trim(Source => Integer'Image(Integer'Value(S) + 1), Side => Ada.Strings.Both);
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
o_text(itoa(atoi("2047") + 1));
|
||||
o_byte('\n');
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
string count = '12345';
|
||||
count = String.valueOf(integer.valueOf(count)+1);
|
||||
system.debug('Incremental Value : '+count);
|
||||
|
|
@ -0,0 +1,107 @@
|
|||
use AppleScript version "2.4"
|
||||
use framework "Foundation"
|
||||
use scripting additions
|
||||
|
||||
-- succString :: Bool -> String -> String
|
||||
on succString(blnPruned, s)
|
||||
script go
|
||||
on |λ|(w)
|
||||
try
|
||||
if w contains "." then
|
||||
set v to w as real
|
||||
else
|
||||
set v to w as integer
|
||||
end if
|
||||
{(1 + v) as string}
|
||||
on error
|
||||
if blnPruned then
|
||||
{}
|
||||
else
|
||||
{w}
|
||||
end if
|
||||
end try
|
||||
end |λ|
|
||||
end script
|
||||
unwords(concatMap(go, |words|(s)))
|
||||
end succString
|
||||
|
||||
|
||||
-- TEST ---------------------------------------------------
|
||||
on run
|
||||
script test
|
||||
on |λ|(bln)
|
||||
succString(bln, ¬
|
||||
"41 pine martens in 1491.3 -1.5 mushrooms ≠ 136")
|
||||
end |λ|
|
||||
end script
|
||||
unlines(map(test, {true, false}))
|
||||
end run
|
||||
|
||||
--> 42 1492.3 -0.5 137
|
||||
--> 42 pine martens in 1492.3 -0.5 mushrooms ≠ 137
|
||||
|
||||
-- GENERIC ------------------------------------------------
|
||||
|
||||
-- concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
on concatMap(f, xs)
|
||||
set lng to length of xs
|
||||
set acc to {}
|
||||
tell mReturn(f)
|
||||
repeat with i from 1 to lng
|
||||
set acc to acc & |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
end tell
|
||||
return acc
|
||||
end concatMap
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- unlines :: [String] -> String
|
||||
on unlines(xs)
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, linefeed}
|
||||
set str to xs as text
|
||||
set my text item delimiters to dlm
|
||||
str
|
||||
end unlines
|
||||
|
||||
-- unwords :: [String] -> String
|
||||
on unwords(xs)
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, space}
|
||||
set s to xs as text
|
||||
set my text item delimiters to dlm
|
||||
return s
|
||||
end unwords
|
||||
|
||||
-- words :: String -> [String]
|
||||
on |words|(s)
|
||||
set ca to current application
|
||||
(((ca's NSString's stringWithString:(s))'s ¬
|
||||
componentsSeparatedByCharactersInSet:(ca's ¬
|
||||
NSCharacterSet's whitespaceAndNewlineCharacterSet()))'s ¬
|
||||
filteredArrayUsingPredicate:(ca's ¬
|
||||
NSPredicate's predicateWithFormat:"0 < length")) as list
|
||||
end |words|
|
||||
|
|
@ -0,0 +1 @@
|
|||
("12345" + 1) as text --> "12346"
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
|
||||
use framework "Foundation"
|
||||
|
||||
-- Using the machine's own locale setting. The numerical text must be compatible with this.
|
||||
-- Params: Numerical text or NSString, AppleScript or Objective-C number or text equivalent.
|
||||
on incrementNumericalString:str byAmount:increment
|
||||
set localeID to current application's class "NSLocale"'s currentLocale()'s localeIdentifier()
|
||||
return my incrementNumericalString:str byAmount:increment localeID:localeID
|
||||
end incrementNumericalString:byAmount:
|
||||
|
||||
-- Including the locale ID as an additional parameter.
|
||||
-- Params: As above plus locale ID (text or NSString).
|
||||
on incrementNumericalString:str byAmount:increment localeID:localeID
|
||||
set |⌘| to current application
|
||||
set str to |⌘|'s class "NSString"'s stringWithString:(str)
|
||||
set locale to |⌘|'s class "NSLocale"'s localeWithLocaleIdentifier:(localeID)
|
||||
set decSeparator to locale's objectForKey:(|⌘|'s NSLocaleDecimalSeparator)
|
||||
set regex to |⌘|'s NSRegularExpressionSearch
|
||||
-- Use an NSNumberFormatter to generate the NSDecimalNumber objects for the math,
|
||||
-- as its number/string conversions are more flexible than NSDecimalNumber's own.
|
||||
tell |⌘|'s class "NSNumberFormatter"'s new()
|
||||
its setGeneratesDecimalNumbers:(true)
|
||||
its setLocale:(locale)
|
||||
set symbolRange to str's rangeOfString:("[Ee]| ?[x*] ?10 ?\\^ ?") options:(regex)
|
||||
if (symbolRange's |length|() > 0) then
|
||||
-- Catered-for exponent symbol in the input string. Set the output style to "scientific".
|
||||
its setNumberStyle:(|⌘|'s NSNumberFormatterScientificStyle)
|
||||
its setExponentSymbol:(str's substringWithRange:(symbolRange))
|
||||
else
|
||||
-- Straight numerical text, with or without separators as per the input and locale.
|
||||
its setNumberStyle:(|⌘|'s NSNumberFormatterDecimalStyle)
|
||||
set groupingSeparator to locale's objectForKey:(|⌘|'s NSLocaleGroupingSeparator)
|
||||
its setUsesGroupingSeparator:(str's containsString:(groupingSeparator))
|
||||
its setMinimumFractionDigits:(str's containsString:(decSeparator))
|
||||
end if
|
||||
-- Derive NSDecimalNumbers from the inputs, add together, convert the result back to NSString.
|
||||
set increment to (|⌘|'s class "NSArray"'s arrayWithArray:({increment}))'s firstObject()
|
||||
if ((increment's isKindOfClass:(|⌘|'s class "NSNumber")) as boolean) then ¬
|
||||
set increment to its stringFromNumber:(increment)
|
||||
set sum to (its numberFromString:(str))'s decimalNumberByAdding:(its numberFromString:(increment))
|
||||
set output to its stringFromNumber:(sum)
|
||||
end tell
|
||||
-- Adjustments for AppleScript norms the NSNumberFormatter may omit from scientific notation output:
|
||||
if (symbolRange's |length|() > 0) then
|
||||
-- If no decimal separator in the output mantissa, insert point zero or not to match the input style.
|
||||
if ((output's containsString:(decSeparator)) as boolean) then
|
||||
else if ((str's containsString:(decSeparator)) as boolean) then
|
||||
set output to output's stringByReplacingOccurrencesOfString:("(?<=^-?\\d)") ¬
|
||||
withString:((decSeparator as text) & "0") options:(regex) range:({0, output's |length|()})
|
||||
end if
|
||||
-- If no sign in an E-notation exponent, insert "+" or not ditto.
|
||||
if (((output's rangeOfString:("[Ee][+-]") options:(regex))'s |length|() > 0) as boolean) then
|
||||
else if (((str's rangeOfString:("[Ee][+-]") options:(regex))'s |length|() > 0) as boolean) then
|
||||
set output to output's stringByReplacingOccurrencesOfString:("(?<=[Ee])") ¬
|
||||
withString:("+") options:(regex) range:({0, output's |length|()})
|
||||
end if
|
||||
end if
|
||||
|
||||
return output as text -- Return as AppleScript text.
|
||||
end incrementNumericalString:byAmount:localeID:
|
||||
|
||||
return {¬
|
||||
(my incrementNumericalString:"12345" byAmount:1), ¬
|
||||
(my incrementNumericalString:"999,999,999,999,999" byAmount:5), ¬
|
||||
(my incrementNumericalString:"-1.234" byAmount:10 localeID:"en"), ¬
|
||||
(my incrementNumericalString:"-1,234E+1" byAmount:10 localeID:"fr_FR"), ¬
|
||||
(my incrementNumericalString:"-1.234 x 10^1" byAmount:"10") ¬
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
{"12346", "1,000,000,000,000,004", "8.766", "-2,34E+0", "-2.34 x 10^0"}
|
||||
|
|
@ -0,0 +1 @@
|
|||
S$ = "12345":S$ = STR$ ( VAL (S$) + 1)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
num: "12349"
|
||||
|
||||
print ["The next number is:" (to :integer num)+1]
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
string cadena = "12345.78";
|
||||
cadena = string((real)cadena + 1);
|
||||
write(cadena);
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
str = 12345
|
||||
MsgBox % str += 1
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
Global $x = "12345"
|
||||
$x += 1
|
||||
MsgBox(0,"",$x)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
numberString ::= "1080";
|
||||
incremented ::= numberString (base 10) + 1;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
s$ = "12345"
|
||||
s$ = STR$(VAL(s$) + 1)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
cadena$ = "12345"
|
||||
cadena$ = string(val(cadena$) + 1)
|
||||
#or also
|
||||
cadena$ = string(FromRadix(cadena$,10) + 1)
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
num$ = "567"
|
||||
REPEAT
|
||||
PRINT num$
|
||||
PROCinc$(num$)
|
||||
UNTIL FALSE
|
||||
END
|
||||
|
||||
DEF PROCinc$(RETURN n$)
|
||||
LOCAL A$, I%
|
||||
I% = LEN(n$)
|
||||
REPEAT
|
||||
A$ = CHR$(ASCMID$(n$,I%) + 1)
|
||||
IF A$=":" A$ = "0"
|
||||
MID$(n$,I%,1) = A$
|
||||
I% -= 1
|
||||
UNTIL A$<>"0" OR I%=0
|
||||
IF A$="0" n$ = "1" + n$
|
||||
ENDPROC
|
||||
|
|
@ -0,0 +1 @@
|
|||
1•Repr∘+•BQN "1234"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
set s=12345
|
||||
set /a s+=1
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
s = "1234"
|
||||
s = (int.Parse(s) + 1).ToString()
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(n=35664871829866234762187538073934873121878/6172839450617283945)
|
||||
&!n+1:?n
|
||||
&out$!n
|
||||
|
||||
35664871829866234762193710913385490405823/6172839450617283945
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
#Convert to integer, increment, then back to string
|
||||
p ("100".to_i + 1).to_s #Prints 101
|
||||
|
|
@ -0,0 +1 @@
|
|||
ri?ish
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
// standard C++ string stream operators
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
// inside a function or method...
|
||||
std::string s = "12345";
|
||||
|
||||
int i;
|
||||
std::istringstream(s) >> i;
|
||||
i++;
|
||||
//or:
|
||||
//int i = std::atoi(s.c_str()) + 1;
|
||||
|
||||
std::ostringstream oss;
|
||||
if (oss << i) s = oss.str();
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
#include <string>
|
||||
|
||||
std::string s = "12345";
|
||||
s = std::to_string(1+std::stoi(s));
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
// Boost
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
// inside a function or method...
|
||||
std::string s = "12345";
|
||||
int i = boost::lexical_cast<int>(s) + 1;
|
||||
s = boost::lexical_cast<std::string>(i);
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
// Qt
|
||||
QString num1 = "12345";
|
||||
QString num2 = QString("%1").arg(v1.toInt()+1);
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
// MFC
|
||||
CString s = "12345";
|
||||
int i = _ttoi(s) + 1;
|
||||
int i = _tcstoul(s, NULL, 10) + 1;
|
||||
s.Format("%d", i);
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
#include <string>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
|
||||
void increment_numerical_string(std::string& s)
|
||||
{
|
||||
std::string::reverse_iterator iter = s.rbegin(), end = s.rend();
|
||||
int carry = 1;
|
||||
while (carry && iter != end)
|
||||
{
|
||||
int value = (*iter - '0') + carry;
|
||||
carry = (value / 10);
|
||||
*iter = '0' + (value % 10);
|
||||
++iter;
|
||||
}
|
||||
if (carry)
|
||||
s.insert(0, "1");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string big_number = "123456789012345678901234567899";
|
||||
std::cout << "before increment: " << big_number << "\n";
|
||||
increment_numerical_string(big_number);
|
||||
std::cout << "after increment: " << big_number << "\n";
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
@ num = 5
|
||||
@ num += 1
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
string s = "12345";
|
||||
s = (int.Parse(s) + 1).ToString();
|
||||
// The above functions properly for strings >= Int32.MinValue and
|
||||
// < Int32.MaxValue. ( -2147483648 to 2147483646 )
|
||||
|
||||
// The following will work for any arbitrary-length integer string.
|
||||
// (Assuming that the string fits in memory, leaving enough space
|
||||
// for the temporary BigInteger created, plus the resulting string):
|
||||
using System.Numerics;
|
||||
string bis = "123456789012345678999999999";
|
||||
bis = (BigInteger.Parse(bis) + 1).ToString();
|
||||
// Note that extremely long strings will take a long time to parse
|
||||
// and convert from a BigInteger back into a string.
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* Constraints: input is in the form of (\+|-)?[0-9]+
|
||||
* and without leading zero (0 itself can be as "0" or "+0", but not "-0");
|
||||
* input pointer is realloc'able and may change;
|
||||
* if input has leading + sign, return may or may not keep it.
|
||||
* The constranits conform to sprintf("%+d") and this function's own output.
|
||||
*/
|
||||
char * incr(char *s)
|
||||
{
|
||||
int i, begin, tail, len;
|
||||
int neg = (*s == '-');
|
||||
char tgt = neg ? '0' : '9';
|
||||
|
||||
/* special case: "-1" */
|
||||
if (!strcmp(s, "-1")) {
|
||||
s[0] = '0', s[1] = '\0';
|
||||
return s;
|
||||
}
|
||||
|
||||
len = strlen(s);
|
||||
begin = (*s == '-' || *s == '+') ? 1 : 0;
|
||||
|
||||
/* find out how many digits need to be changed */
|
||||
for (tail = len - 1; tail >= begin && s[tail] == tgt; tail--);
|
||||
|
||||
if (tail < begin && !neg) {
|
||||
/* special case: all 9s, string will grow */
|
||||
if (!begin) s = realloc(s, len + 2);
|
||||
s[0] = '1';
|
||||
for (i = 1; i <= len - begin; i++) s[i] = '0';
|
||||
s[len + 1] = '\0';
|
||||
} else if (tail == begin && neg && s[1] == '1') {
|
||||
/* special case: -1000..., so string will shrink */
|
||||
for (i = 1; i < len - begin; i++) s[i] = '9';
|
||||
s[len - 1] = '\0';
|
||||
} else { /* normal case; change tail to all 0 or 9, change prev digit by 1*/
|
||||
for (i = len - 1; i > tail; i--)
|
||||
s[i] = neg ? '9' : '0';
|
||||
s[tail] += neg ? -1 : 1;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
void string_test(const char *s)
|
||||
{
|
||||
char *ret = malloc(strlen(s));
|
||||
strcpy(ret, s);
|
||||
|
||||
printf("text: %s\n", ret);
|
||||
printf(" ->: %s\n", ret = incr(ret));
|
||||
free(ret);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
string_test("+0");
|
||||
string_test("-1");
|
||||
string_test("-41");
|
||||
string_test("+41");
|
||||
string_test("999");
|
||||
string_test("+999");
|
||||
string_test("109999999999999999999999999999999999999999");
|
||||
string_test("-100000000000000000000000000000000000000000000");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
set(string "1599")
|
||||
math(EXPR string "${string} + 1")
|
||||
message(STATUS "${string}")
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
PROGRAM-ID. increment-num-str.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 str PIC X(5) VALUE "12345".
|
||||
01 num REDEFINES str PIC 9(5).
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
DISPLAY str
|
||||
ADD 1 TO num
|
||||
DISPLAY str
|
||||
|
||||
GOBACK
|
||||
.
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
PROGRAM-ID. increment-num-str.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 num-str PIC 9(5) VALUE 12345.
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
DISPLAY num-str
|
||||
ADD 1 TO num-str
|
||||
DISPLAY num-str
|
||||
|
||||
GOBACK
|
||||
.
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
shared void run() {
|
||||
|
||||
"Increments a numeric string by 1. Returns a float or integer depending on the string.
|
||||
Returns null if the string isn't a number."
|
||||
function inc(String string) =>
|
||||
if(exists integer = parseInteger(string)) then integer + 1
|
||||
else if(exists float = parseFloat(string)) then float + 1.0
|
||||
else null;
|
||||
|
||||
value a = "1";
|
||||
print(a);
|
||||
value b = inc(a);
|
||||
print(b);
|
||||
value c = "1.0";
|
||||
print(c);
|
||||
value d = inc(c);
|
||||
print(d);
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
(str (inc (Integer/parseInt "1234")))
|
||||
|
|
@ -0,0 +1 @@
|
|||
(princ-to-string (1+ (parse-integer "1234")))
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
MODULE Operations;
|
||||
IMPORT StdLog,Args,Strings;
|
||||
|
||||
PROCEDURE IncString(s: ARRAY OF CHAR): LONGINT;
|
||||
VAR
|
||||
resp: LONGINT;
|
||||
done: INTEGER;
|
||||
BEGIN
|
||||
Strings.StringToLInt(s,resp,done);
|
||||
INC(resp);
|
||||
RETURN resp
|
||||
END IncString;
|
||||
|
||||
PROCEDURE DoIncString*;
|
||||
VAR
|
||||
p: Args.Params;
|
||||
BEGIN
|
||||
Args.Get(p);
|
||||
IF p.argc > 0 THEN
|
||||
StdLog.String(p.args[0] + " + 1= ");StdLog.Int(IncString(p.args[0]));StdLog.Ln
|
||||
END
|
||||
END DoIncString;
|
||||
|
||||
END Operations.
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
void main() {
|
||||
import std.string;
|
||||
|
||||
immutable s = "12349".succ;
|
||||
assert(s == "12350");
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
var value : String = "1234";
|
||||
value := IntToStr(StrToInt(value) + 1);
|
||||
PrintLn(value);
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
program IncrementNumericalString;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses SysUtils;
|
||||
|
||||
const
|
||||
STRING_VALUE = '12345';
|
||||
begin
|
||||
WriteLn(Format('"%s" + 1 = %d', [STRING_VALUE, StrToInt(STRING_VALUE) + 1]));
|
||||
|
||||
Readln;
|
||||
end.
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
var str = "42"
|
||||
str = (parse(str) + 1).ToString()
|
||||
|
||||
//Another option:
|
||||
str = (Integer(str) + 1).ToString()
|
||||
|
||||
//And another option using interpolation:
|
||||
str = "\(parse(str) + 1)"
|
||||
|
||||
print(str) //Outputs 45
|
||||
|
|
@ -0,0 +1 @@
|
|||
__makeInt("1234", 10).next().toString(10)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
s string = "12345";
|
||||
s = 1 + s; // Note: s + 1 is a string concatenation.
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
fun incrementGeneric = text by generic T, text numerical
|
||||
return text!(:T!numerical + 1)
|
||||
end
|
||||
fun increment = text by text numerical
|
||||
return incrementGeneric(when(numerical.contains("."), real, int), numerical)
|
||||
end
|
||||
writeLine(incrementGeneric(real, "123.32"))
|
||||
writeLine(incrementGeneric(int, "123"))
|
||||
writeLine(increment("123.32"))
|
||||
writeLine(increment("123"))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
....................
|
||||
s$="12345"
|
||||
s$=STR$(VAL(s$)+1)
|
||||
....................
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
a$ = "12"
|
||||
a$ = number a$ + 1
|
||||
print a$
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(number->string (1+ (string->number "665")))
|
||||
→ "666"
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
String s = "12345";
|
||||
IntLiteral lit1 = new IntLiteral(s);
|
||||
IntLiteral lit2 = 6789;
|
||||
++lit1; // lit1=12346
|
||||
++lit2; // lit2=6790
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
int main()
|
||||
|
||||
i := (int)'123' + 1
|
||||
s := @(i).description
|
||||
|
||||
Log( '%@', s )
|
||||
return 0
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
class
|
||||
APPLICATION
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE}
|
||||
|
||||
make
|
||||
do
|
||||
io.put_string (increment_numerical_string ("7"))
|
||||
io.new_line
|
||||
io.put_string (increment_numerical_string ("99"))
|
||||
end
|
||||
|
||||
increment_numerical_string (s: STRING): STRING
|
||||
-- String 's' incremented by one.
|
||||
do
|
||||
Result := s.to_integer.plus (1).out
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
import extensions;
|
||||
|
||||
public program()
|
||||
{
|
||||
var s := "12345";
|
||||
s := (s.toInt() + 1).toString();
|
||||
|
||||
console.printLine(s)
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
increment1 = fn n -> to_string(String.to_integer(n) + 1) end
|
||||
# Or piped
|
||||
increment2 = fn n -> n |> String.to_integer |> +1 |> to_string end
|
||||
|
||||
increment1.("1")
|
||||
increment2.("100")
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
iex(1)> (List.to_integer('12345') + 1) |> to_char_list
|
||||
'12346'
|
||||
|
|
@ -0,0 +1 @@
|
|||
(1+ (string-to-number "12345"))
|
||||
|
|
@ -0,0 +1 @@
|
|||
integer_to_list(list_to_integer("1336")+1).
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
include get.e
|
||||
|
||||
function val(sequence s)
|
||||
sequence x
|
||||
x = value(s)
|
||||
return x[2]
|
||||
end function
|
||||
|
||||
sequence s
|
||||
|
||||
s = "12345"
|
||||
s = sprintf("%d",{val(s)+1})
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
// Increment a numerical string. Nigel Galloway: April 4th., 2023
|
||||
let inc=int>>(+)1>>string
|
||||
printfn "%s" (inc("1234"))
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
define value = 0, text$ = "12345"
|
||||
|
||||
strint value,text$
|
||||
+1 value
|
||||
intstr text$,value
|
||||
|
||||
print text$
|
||||
pause
|
||||
end
|
||||
|
|
@ -0,0 +1 @@
|
|||
"1234" string>number 1 + number>string
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
fansh> a := "123"
|
||||
123
|
||||
fansh> (a.toInt + 1).toStr
|
||||
124
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
: >string ( d -- addr n )
|
||||
dup >r dabs <# #s r> sign #> ;
|
||||
|
||||
: inc-string ( addr -- )
|
||||
dup count number? not abort" invalid number"
|
||||
1 s>d d+ >string rot place ;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
: inc-string ( addr n -- )
|
||||
over count number? not abort" invalid number"
|
||||
rot s>d d+ >string rot place ;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
s" 123" pad place
|
||||
pad inc-string
|
||||
pad count type
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
s" 123" pad place
|
||||
pad 1 inc-string
|
||||
pad count type
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
CHARACTER(10) :: intstr = "12345", realstr = "1234.5"
|
||||
INTEGER :: i
|
||||
REAL :: r
|
||||
|
||||
READ(intstr, "(I10)") i ! Read numeric string into integer i
|
||||
i = i + 1 ! increment i
|
||||
WRITE(intstr, "(I10)") i ! Write i back to string
|
||||
|
||||
READ(realstr, "(F10.1)") r
|
||||
r = r + 1.0
|
||||
WRITE(realstr, "(F10.1)") r
|
||||
|
|
@ -0,0 +1,97 @@
|
|||
program StrInc;
|
||||
//increments a positive numerical string in different bases.
|
||||
//the string must be preset with a value, length >0 ;
|
||||
{$IFDEF WINDOWS}
|
||||
{$APPTYPE CONSOLE}
|
||||
{$ENDIF}
|
||||
{$IFDEF FPC}
|
||||
{$Mode Delphi} {$Optimization ON,ALL}{$Align 32}
|
||||
uses
|
||||
sysutils;
|
||||
{$ELSE}
|
||||
uses
|
||||
system.SysUtils;
|
||||
{$ENDIF}
|
||||
type
|
||||
myString = AnsiString; // String[32];//
|
||||
|
||||
function IncLoop(ps: pChar;le,Base: NativeInt):NativeInt;inline;
|
||||
//Add 1 and correct carry
|
||||
//returns 0, if no overflow, else 1
|
||||
var
|
||||
dg: nativeInt;
|
||||
Begin
|
||||
dec(le);//ps is 0-based
|
||||
dg := ord(ps[le])+(-ord('0')+1);
|
||||
result := 0;
|
||||
|
||||
repeat
|
||||
IF dg < base then
|
||||
begin
|
||||
ps[le] := chr(dg+ord('0'));
|
||||
EXIT;
|
||||
end;
|
||||
ps[le] := '0';
|
||||
dec(le);
|
||||
dg := ord(ps[le])+(-ord('0')+1);
|
||||
until (le<0);
|
||||
result:= 1;
|
||||
end;
|
||||
|
||||
procedure IncIntStr(base:NativeInt;var s:myString);
|
||||
var
|
||||
le: NativeInt;
|
||||
begin
|
||||
le := length(s);
|
||||
//overflow -> prepend a '1' to string
|
||||
if (IncLoop(pChar(@s[1]),le,base) <>0) then
|
||||
Begin
|
||||
// s := '1'+s;
|
||||
setlength(s,le+1);
|
||||
move(s[1],s[2],le);
|
||||
s[1] := '1';
|
||||
end;
|
||||
end;
|
||||
|
||||
const
|
||||
ONE_BILLION = 1000*1000*1000;
|
||||
strLen = 26;
|
||||
MAX = 1 shl strLen -1;
|
||||
|
||||
var
|
||||
s : myString;
|
||||
i,base : nativeInt;
|
||||
T0: TDateTime;
|
||||
Begin
|
||||
writeln(MAX,' increments in base');
|
||||
For base := 2 to 10 do
|
||||
Begin
|
||||
//s := '0' doesn't work
|
||||
setlength(s,1);
|
||||
s[1]:= '0';
|
||||
{ //Zero pad string
|
||||
setlength(s,strLen);fillchar(s[1],strLen,'0');
|
||||
}
|
||||
T0 := time;
|
||||
For i := 1 to MAX do
|
||||
IncIntStr(Base,s);
|
||||
T0 := (time-T0)*86400;
|
||||
writeln(s:strLen,' base ',base:2,T0:8:3,' s');
|
||||
end;
|
||||
|
||||
writeln;
|
||||
writeln('One billion digits "9"');
|
||||
setlength(s,ONE_BILLION+1);
|
||||
s[1]:= '0';//don't measure setlength in IncIntStr
|
||||
fillchar(s[2],length(s)-1,'9');
|
||||
writeln('first 5 digits ',s[1],s[2],s[3],s[4],s[5]);
|
||||
T0 := time;
|
||||
IncIntStr(10,s);
|
||||
T0 := (time-T0)*86400;
|
||||
writeln(length(s):10,T0:8:3,' s');
|
||||
writeln('first 5 digits ',s[1],s[2],s[3],s[4],s[5]);
|
||||
s:='';
|
||||
{$IFDEF WINDOWS}
|
||||
readln;
|
||||
{$ENDIF}
|
||||
end.
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Function Increment (num As String) As String
|
||||
Dim d As Double = Val(num)
|
||||
Return Str(d + 1.0)
|
||||
End Function
|
||||
|
||||
Dim num(5) As String = {"1", "5", "81", "123.45", "777.77", "1000"}
|
||||
For i As Integer = 0 To 5
|
||||
Print num(i); " + 1", " = "; Increment(num(i))
|
||||
Next
|
||||
|
||||
Print
|
||||
Print "Press any key to exit"
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
a = input["Enter number: "]
|
||||
toString[eval[a] + 1]
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
include "NSLog.incl"
|
||||
|
||||
CFStringRef s = @"12345"
|
||||
NSInteger i
|
||||
|
||||
for i = 1 to 10
|
||||
NSLog( @"%ld", fn StringIntegerValue( s ) + i )
|
||||
next
|
||||
HandleEvents
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
# Using built-in functions
|
||||
Incr := s -> String(Int(s) + 1);
|
||||
|
||||
# Implementing addition
|
||||
# (but here 9...9 + 1 = 0...0 since the string length is fixed)
|
||||
Increment := function(s)
|
||||
local c, n, carry, digits;
|
||||
digits := "0123456789";
|
||||
n := Length(s);
|
||||
carry := true;
|
||||
while n > 0 and carry do
|
||||
c := Position(digits, s[n]) - 1;
|
||||
if carry then
|
||||
c := c + 1;
|
||||
fi;
|
||||
if c > 9 then
|
||||
carry := true;
|
||||
c := c - 10;
|
||||
else
|
||||
carry := false;
|
||||
fi;
|
||||
s[n] := digits[c + 1];
|
||||
n := n - 1;
|
||||
od;
|
||||
end;
|
||||
|
||||
s := "2399";
|
||||
Increment(s);
|
||||
s;
|
||||
# "2400"
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
Public Sub Main()
|
||||
Dim vInput As Variant = "12345"
|
||||
|
||||
Inc vInput
|
||||
Print vInput
|
||||
|
||||
End
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package main
|
||||
import "fmt"
|
||||
import "strconv"
|
||||
func main() {
|
||||
i, _ := strconv.Atoi("1234")
|
||||
fmt.Println(strconv.Itoa(i + 1))
|
||||
}
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// integer
|
||||
is := "1234"
|
||||
fmt.Println("original: ", is)
|
||||
i, err := strconv.Atoi(is)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
// assignment back to original variable shows result is the same type.
|
||||
is = strconv.Itoa(i + 1)
|
||||
fmt.Println("incremented:", is)
|
||||
|
||||
// error checking worthwhile
|
||||
fmt.Println()
|
||||
_, err = strconv.Atoi(" 1234") // whitespace not allowed
|
||||
fmt.Println(err)
|
||||
_, err = strconv.Atoi("12345678901")
|
||||
fmt.Println(err)
|
||||
_, err = strconv.Atoi("_1234")
|
||||
fmt.Println(err)
|
||||
_, err = strconv.ParseFloat("12.D34", 64)
|
||||
fmt.Println(err)
|
||||
|
||||
// float
|
||||
fmt.Println()
|
||||
fs := "12.34"
|
||||
fmt.Println("original: ", fs)
|
||||
f, err := strconv.ParseFloat(fs, 64)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
// various options on FormatFloat produce different formats. All are valid
|
||||
// input to ParseFloat, so result format does not have to match original
|
||||
// format. (Matching original format would take more code.)
|
||||
fs = strconv.FormatFloat(f+1, 'g', -1, 64)
|
||||
fmt.Println("incremented:", fs)
|
||||
fs = strconv.FormatFloat(f+1, 'e', 4, 64)
|
||||
fmt.Println("what format?", fs)
|
||||
|
||||
// complex
|
||||
// strconv package doesn't handle complex types, but fmt does.
|
||||
// (fmt can be used on ints and floats too, but strconv is more efficient.)
|
||||
fmt.Println()
|
||||
cs := "(12+34i)"
|
||||
fmt.Println("original: ", cs)
|
||||
var c complex128
|
||||
_, err = fmt.Sscan(cs, &c)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
cs = fmt.Sprint(c + 1)
|
||||
fmt.Println("incremented:", cs)
|
||||
|
||||
// big integers have their own functions
|
||||
fmt.Println()
|
||||
bs := "170141183460469231731687303715884105728"
|
||||
fmt.Println("original: ", bs)
|
||||
var b, one big.Int
|
||||
_, ok := b.SetString(bs, 10)
|
||||
if !ok {
|
||||
fmt.Println("big.SetString fail")
|
||||
return
|
||||
}
|
||||
one.SetInt64(1)
|
||||
bs = b.Add(&b, &one).String()
|
||||
fmt.Println("incremented:", bs)
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
~)`
|
||||
|
|
@ -0,0 +1 @@
|
|||
"1234" ~)` p
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
println ((("23455" as BigDecimal) + 1) as String)
|
||||
println ((("23455.78" as BigDecimal) + 1) as String)
|
||||
|
|
@ -0,0 +1 @@
|
|||
((show :: Integer -> String) . succ . read) "1234"
|
||||
|
|
@ -0,0 +1 @@
|
|||
((show :: Bool -> String) . succ . read) "False"
|
||||
|
|
@ -0,0 +1 @@
|
|||
(show . (+1) . read) "1234"
|
||||
|
|
@ -0,0 +1 @@
|
|||
(show . succ) (read "1234" :: Int)
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
import Text.Read (readMaybe)
|
||||
import Data.Maybe (mapMaybe)
|
||||
|
||||
succString :: Bool -> String -> String
|
||||
succString pruned s =
|
||||
let succs
|
||||
:: (Num a, Show a)
|
||||
=> a -> Maybe String
|
||||
succs = Just . show . (1 +)
|
||||
go w
|
||||
| elem '.' w = (readMaybe w :: Maybe Double) >>= succs
|
||||
| otherwise = (readMaybe w :: Maybe Integer) >>= succs
|
||||
opt w
|
||||
| pruned = Nothing
|
||||
| otherwise = Just w
|
||||
in unwords $
|
||||
mapMaybe
|
||||
(\w ->
|
||||
case go w of
|
||||
Just s -> Just s
|
||||
_ -> opt w)
|
||||
(words s)
|
||||
|
||||
|
||||
-- TEST ---------------------------------------------------
|
||||
main :: IO ()
|
||||
main =
|
||||
(putStrLn . unlines) $
|
||||
succString <$> [True, False] <*>
|
||||
pure "41.0 pine martens in 1491 -1.5 mushrooms ≠ 136"
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
CHARACTER string = "123 -4567.89"
|
||||
|
||||
READ( Text=string) a, b
|
||||
WRITE(Text=string) a+1, b+1 ! 124 -4566.89
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
I8 *s;
|
||||
|
||||
s = "10";
|
||||
s = Str2I64(s) + 1;
|
||||
Print("%d\n", s);
|
||||
|
||||
s = "-10";
|
||||
s = Str2I64(s) + 1;
|
||||
Print("%d\n", s);
|
||||
|
|
@ -0,0 +1 @@
|
|||
(str (inc (int "123")))
|
||||
|
|
@ -0,0 +1 @@
|
|||
(-> "123" (int) (inc) (str))
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue