Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
135
Task/Dynamic-variable-names/C/dynamic-variable-names.c
Normal file
135
Task/Dynamic-variable-names/C/dynamic-variable-names.c
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <float.h>
|
||||
|
||||
static inline uint64_t hash(void const *cp, size_t n) {
|
||||
uint64_t h = 0;
|
||||
uint64_t const e = UINT64_C(2718281828459045235); // e * 10**18
|
||||
for(uint8_t const *cb = cp; n > 0; n--) {
|
||||
h = ((h + n) * e) ^ ((*cb++ & 0xFF) * e);
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
static void *dup(void const *cp, size_t n) {
|
||||
void *p = malloc(n);
|
||||
return memcpy(p, cp, n);
|
||||
}
|
||||
|
||||
static void *nodup(void const *cp, size_t n) {
|
||||
return (void*)cp;
|
||||
}
|
||||
|
||||
#define MAPPED struct mapped_s
|
||||
MAPPED {
|
||||
size_t n;
|
||||
char const *s;
|
||||
};
|
||||
#define MAP struct map_s
|
||||
MAP {
|
||||
size_t u;
|
||||
size_t w;
|
||||
size_t m;
|
||||
char a[];
|
||||
};
|
||||
|
||||
static MAP *map_alloc(size_t m, size_t z) {
|
||||
const size_t N = (((m * z) + (sizeof(MAP)-1)) / sizeof(MAP)) + 1;
|
||||
MAP *t = calloc(N, sizeof(MAP));
|
||||
t->m = m;
|
||||
t->w = 3 * m / 4;
|
||||
return t;
|
||||
}
|
||||
|
||||
static void *map(MAP **t, size_t z, char const *s, size_t n, void *(*dup)(void const *, size_t)) {
|
||||
if(!*t) {
|
||||
*t = map_alloc(sizeof(size_t) * CHAR_BIT, z);
|
||||
}
|
||||
MAP *f = *t;
|
||||
uint64_t const h = hash(s, n);
|
||||
size_t x = (h / f->m) % f->m;
|
||||
size_t const k = x + !x;
|
||||
size_t const l = f->m - k;
|
||||
size_t const y = h % f->m;
|
||||
x = y;
|
||||
do {
|
||||
MAPPED *p = (MAPPED *)&f->a[x*z];
|
||||
if((p->n == n) && (memcmp(p->s, s, n) == 0)) {
|
||||
return p;
|
||||
}
|
||||
if((p->n == 0) && dup) {
|
||||
if(f->u == f->w) {
|
||||
*t = map_alloc(2 * f->m, z);
|
||||
for(size_t i = 0; f->u < f->w; i++) {
|
||||
MAPPED *q = (MAPPED *)&f->a[i*z];
|
||||
if(q->n > 0) {
|
||||
(void)map(t, z, q->s, q->n, nodup);
|
||||
}
|
||||
}
|
||||
free(f);
|
||||
return map(t, z, s, n, dup);
|
||||
}
|
||||
f->u++;
|
||||
p->n = n;
|
||||
p->s = dup(s, n);
|
||||
return p;
|
||||
}
|
||||
x = (x >= l) ? ((x + 1) % k) : (x + k);
|
||||
} while(x != y)
|
||||
;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static double mapf(char const *s, double v, void *(*dup)(void const *, size_t)) {
|
||||
static MAP *t = NULL;
|
||||
struct {
|
||||
MAPPED;
|
||||
double v;
|
||||
} *p = map(&t, sizeof(*p), s, strlen(s), dup);
|
||||
if(p) {
|
||||
if(dup) p->v = v;
|
||||
return p->v;
|
||||
}
|
||||
return NAN;
|
||||
}
|
||||
|
||||
static inline double assignf(char const *s, double v) {
|
||||
return mapf(s, v, dup);
|
||||
}
|
||||
|
||||
static inline double valuef(char const *s) {
|
||||
return mapf(s, 0, 0);
|
||||
}
|
||||
|
||||
static char *enter(char const *prompt) {
|
||||
static char buf[256];
|
||||
fputs(prompt, stdout); fflush(stdout);
|
||||
fgets(buf, sizeof(buf), stdin);
|
||||
char *s = buf;
|
||||
for(; *s && !isgraph(*s); s++)
|
||||
;
|
||||
char *t = s + strlen(s);
|
||||
for(; (t > s) && !isgraph(*(t-1)); t--)
|
||||
;
|
||||
*t = '\0';
|
||||
return s;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
for(;;) {
|
||||
char const *name = enter("Enter: ");
|
||||
if(!*name) break;
|
||||
char *val = strchr(name, '=');
|
||||
if(val) {
|
||||
*val++ = '\0';
|
||||
assignf(name, strtod(val, NULL));
|
||||
} else {
|
||||
printf("%s = %F\n", name, valuef(name));
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
(set (intern (read-string "Enter variable name: ")) 123)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
require "io2"
|
||||
|
||||
local name = io.readStr("Enter your variable name: ", 1)
|
||||
local value = io.readStr($"Enter a value for '{name}': ", 1)
|
||||
_G[name] = value -- add variable to global environment and set its value
|
||||
print($"{name} = {value}")
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
$variableName = Read-Host
|
||||
New-Variable $variableName 'Foo'
|
||||
Get-Variable $variableName
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
Rebol [
|
||||
Title: "Dynamic Variable Name"
|
||||
URL: http://rosettacode.org/wiki/Dynamic_variable_names
|
||||
]
|
||||
|
||||
; Here, I ask the user for a name, then convert it to a word and
|
||||
; assign the value "Hello!" to it. To read this phrase, realize that
|
||||
; Rebol collects terms from right to left, so "Hello!" is stored for
|
||||
; future use, then the prompt string "Variable name? " is used as the
|
||||
; argument to ask (prompts user for input). The result of ask is
|
||||
; converted to a word so it can be an identifier, then the 'set' word
|
||||
; accepts the new word and the string ("Hello!") to be assigned.
|
||||
|
||||
set to-word ask "Variable name? " "Hello!"
|
||||
|
|
@ -6,14 +6,20 @@ fn main() {
|
|||
mut name :=""
|
||||
mut vars := map[string]int{}
|
||||
for n < 1 || n > 5 {
|
||||
n = strconv.atoi(os.input("Integer variables (max 5): ")) or {println("Invalid input!") continue}
|
||||
n = strconv.atoi(os.input("Integer variables (max 5): ")) or {
|
||||
println("Invalid input!")
|
||||
continue
|
||||
}
|
||||
}
|
||||
for i <= n {
|
||||
println("OK, enter the variable names and their values, below:")
|
||||
println("Variable ${i}")
|
||||
name = os.input_opt(" Name: ") or {println("Invalid input!") exit(1)}
|
||||
name = os.input_opt(" Name: ") or { panic("Invalid input!") }
|
||||
for {
|
||||
value = strconv.atoi(os.input(" Value: ")) or {println("Must by a number!") continue}
|
||||
value = strconv.atoi(os.input(" Value: ")) or {
|
||||
println("Must be a valid number!")
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
vars[name] += value
|
||||
|
|
@ -21,9 +27,9 @@ fn main() {
|
|||
}
|
||||
println("\n" + "Enter q to quit")
|
||||
for {
|
||||
name = os.input_opt("Which variable do you want to inspect: ") or {println("Invalid input!") exit(1)}
|
||||
if name.to_lower() == "q" {break}
|
||||
if name !in vars {println("Sorry there's no variable of that name, try again!")}
|
||||
else {println("Its value is ${vars[name]}")}
|
||||
name = os.input_opt("Which variable do you want to inspect: ") or { panic("Invalid input!") }
|
||||
if name.to_lower() == "q" { break }
|
||||
if name !in vars { println("Sorry there's no variable of that name, try again!") }
|
||||
else { println("Its value is ${vars[name]}") }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue