Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,2 +1,4 @@
---
category:
- Simple
note: Text processing

View file

@ -1,2 +1,2 @@
$ awk 'BEGIN{s="42";s++;print s"("length(s)")"}'
$ awk 'BEGIN{s="42"; s++; print s"("length(s)")" }'
43(2)

View file

@ -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;
}

View file

@ -0,0 +1,17 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
feature {NONE} -- Initialization
make
do
inc("23")
end
inc(s:STRING)
do
io.put_string (s.to_integer.plus (1).out)
end
end

View file

@ -1 +1,2 @@
toString[parseInt["123"] + 1]
a = input["Enter number: "]
toString[eval[a] + 1]

View file

@ -1,7 +1,7 @@
package main
import (
"big"
"math/big"
"fmt"
"strconv"
)
@ -27,24 +27,24 @@ func main() {
fmt.Println(err)
_, err = strconv.Atoi("_1234")
fmt.Println(err)
_, err = strconv.Atof64("12.D34")
_, err = strconv.ParseFloat("12.D34", 64)
fmt.Println(err)
// float
fmt.Println()
fs := "12.34"
fmt.Println("original: ", fs)
f, err := strconv.Atof64(fs)
f, err := strconv.ParseFloat(fs, 64)
if err != nil {
fmt.Println(err)
return
}
// various options on Ftoa64 produce different formats. All are valid
// input to Atof64, so result format does not have to match original
// format. (Matching original format would take a lot of code.)
fs = strconv.Ftoa64(f+1, 'g', -1)
// 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.Ftoa64(f+1, 'e', 4)
fs = strconv.FormatFloat(f+1, 'e', 4, 64)
fmt.Println("what format?", fs)
// complex

View file

@ -0,0 +1,4 @@
var str = "123";
var str = $string($int(str) + 1);
$print(str);

View file

@ -0,0 +1 @@
(string (++ (int "123")))

View file

@ -1,15 +1,21 @@
count = "3" /*typeless variables are all character strings.*/
count = 3 /*(same as above)*/
count = count + 1 /*variables in a numerical context are treated as numbers.*/
say count
/*REXX program demonstrates a method how to increment a numerical string*/
count = "3" /*REXX variables (and constants) are character strings.*/
count = 3 /*(identical to the above statement.) */
count = count+1 /*strings in a numerical context are treated as numbers*/
say 'sum=' count /*display the value of COUNT to the terminal (screen)*/
/*------------------ The default numeric digits for REXX is 9 digits. */
/*------------------ However, that can be increased with NUMERIC DIGITS.*/
/*────────────────── The default numeric digits for REXX is 9 digits. */
/*────────────────── However, that can be increased with NUMERIC DIGITS.*/
numeric digits 15000 /*let's go ka-razy with fifteen thousand digits. */
count=copies(2,15000) /*stressing REXX's brains with lots of two's, */
/*the above is considered a number in REXX. */
count=count+3 /*make that last digit a "five". */
count=count+3 /*make that last digit of COUNT a "5". */
if 1==0 then /*let's not display this gihugeic number to term,*/
say 'count=' count /*ya most likely don't want to display this thing*/
/* [↓] show the six leftmost and rightmost digs.*/
say 'count=' left(count,6)'···'right(count,6)
/*stick a fork in it, we're done.*/

View file

@ -0,0 +1,3 @@
import String;
public str IncrNumStr(str s) = "<toInt(s) + 1>";