index int64 0 1.54k | before stringlengths 30 10.1k | after stringlengths 51 30.2k |
|---|---|---|
0 | int main() { puts(getenv("HOME")); return 0; } | fn main() -> i32 { unsafe { puts(getenv(b"HOME\0" as *const u8 as *const i8)); } return 0; } |
1 | int gcd(int m, int n) { int t; while (m) { t = m; m = n % m; n = t; } return n; } | pub extern "C" fn gcd(mut m: i32, mut n: i32) -> i32 { let mut t: i32 = 0; while m != 0 { t = m; m = n % m; n = t; } return n; } |
2 | void reset() { int i; msg = 0; pool_ptr = pos = 0; for (i = 0; i < POOL_SIZE; i++) { pool[i].op = OP_NONE; pool[i].left = pool[i].right = 0; } for (i = 0; i < N_DIGITS; i++) digits[i].used = 0; } | pub extern "C" fn reset() { let mut i: i32 = 0; unsafe { msg = 0 as *const i8; pos = 0; pool_ptr = pos; } i = 0; unsafe { while i < 8 { pool[i as usize].op = OP_NONE as i32; pool[i as usize].right = 0 as expr; pool[i as usize].left ... |
3 | void get_input() { int i; reinput: reset(); printf("\nAvailable digits are:"); for (i = 0; i < N_DIGITS; i++) printf(" %d", digits[i].val); printf(". Type an expression and I'll check it for you, or make new numbers.\n" "Your choice? [Expr/n/q] "); while (1) { for (i = 0; i < MAX_INPUT; i++) str[i] = '\n... | pub extern "C" fn get_input() { let mut i: i32 = 0; unsafe { loop { reset(); print!("\nAvailable digits are:"); i = 0; while i < 4 { print!(" {}", digits[i as usize].val); i += 1; i; } ... |
4 | expr parse() { int i; expr ret = get_expr(); if (next_tok() != '\0') bail("Trailing garbage"); for (i = 0; i < N_DIGITS; i++) if (!digits[i].used) bail("Not all digits are used"); return ret; } | pub extern "C" fn parse() -> expr { let mut i: i32 = 0; let mut ret: expr = get_expr(); if next_tok() != '\0' as i32 { bail(b"Trailing garbage\0" as *const u8 as *const i8); } i = 0; unsafe { while i < 4 { if digits[i as usize].used == 0 { bail(b"Not a... |
5 | void gen_digits() { int i; for (i = 0; i < N_DIGITS; i++) digits[i].val = 1 + rand() % 9; } | pub extern "C" fn gen_digits() { let mut i: i32 = 0; i = 0; unsafe { while i < 4 { digits[i as usize].val = 1 + rand() % 9; i += 1; i; } } } |
6 | expr new_expr() { if (pool_ptr < POOL_SIZE) return pool + pool_ptr++; return 0; } | pub extern "C" fn new_expr() -> expr { unsafe { if pool_ptr < 8 { let fresh0 = pool_ptr; pool_ptr = pool_ptr + 1; return pool.as_mut_ptr().offset(fresh0 as isize); } } return 0 as expr; } |
7 | expr get_digit() { int i, c = next_tok(); expr ret; if (c >= '0' && c <= '9') { take(); ret = new_expr(); ret->op = OP_NUM; ret->val = c - '0'; for (i = 0; i < N_DIGITS; i++) if (digits[i].val == ret->val && !digits[i].used) { digits[i].used = 1; return ret; } bail("Invalid digit"); } ret... | pub extern "C" fn get_digit() -> expr { let mut i: i32 = 0; let mut c: i32 = next_tok(); let mut ret: expr = 0 as *mut expr_t; unsafe { if c >= '0' as i32 && c <= '9' as i32 { take(); ret = new_expr(); (*ret).op = OP_NUM as i32; (*ret).val = c - '0... |
8 | int take() { if (str[pos] != '\0') return ++pos; return 0; } | pub extern "C" fn take() -> i32 { unsafe { if str[pos as usize] as i32 != '\0' as i32 { pos += 1; return pos; } } return 0; } |
9 | int next_tok() { while (isspace(str[pos])) pos++; return str[pos]; } | pub extern "C" fn next_tok() -> i32 { unsafe { while *(*__ctype_b_loc()).offset(str[pos as usize] as i32 as isize) as i32 & _ISspace as i32 != 0 { pos += 1; pos; } return str[pos as usize] as i32; } } |
10 | int main() { frac_t f; srand(time(0)); gen_digits(); while(1) { get_input(); setjmp(ctx); /* if parse error, jump back here with err msg set */ if (msg) { /* after error jump; announce, reset, redo */ printf("%s at '%.*s'\n", msg, pos, str); continue; } eval_tree(parse(), &f); if (f.denom... | fn main() -> i32 { let mut f: frac_t = frac_t { denom: 0, num: 0 }; unsafe { srand(rust_time(None) as u32); } gen_digits(); unsafe { loop { get_input(); _setjmp(ctx.as_mut_ptr()); if !msg.is_null() { print!( "{0:... |
11 | expr get_term() { int c; expr l, r, ret; ret = get_fact(); while((c = next_tok()) == '*' || c == '/') { if (!take()) bail("Unexpected end of input"); r = get_fact(); l = ret; ret = new_expr(); ret->op = (c == '*') ? OP_MUL : OP_DIV; ret->left = l; ret->right = r; } return ret; } | pub extern "C" fn get_term() -> expr { let mut c: i32 = 0; let mut l: expr = 0 as *mut expr_t; let mut r: expr = 0 as *mut expr_t; let mut ret: expr = 0 as *mut expr_t; ret = get_fact(); loop { c = next_tok(); if !(c == '*' as i32 || c == '/' as i32) { break; ... |
12 | expr get_fact() { int c; expr l = get_digit(); if (l) return l; if ((c = next_tok()) == '(') { take(); l = get_expr(); if (next_tok() != ')') bail("Unbalanced parens"); take(); return l; } return 0; } | pub extern "C" fn get_fact() -> expr { let mut c: i32 = 0; let mut l: expr = get_digit(); if !l.is_null() { return l; } c = next_tok(); if c == '(' as i32 { take(); l = get_expr(); if next_tok() != ')' as i32 { bail(b"Unbalanced parens\0" as *const u8 ... |
13 | void bail(const char *s) { msg = s; longjmp(ctx, 1); } | pub extern "C" fn bail(mut s: *const i8) { unsafe { msg = s; longjmp(ctx.as_mut_ptr(), 1); } } |
14 | void eval_tree(expr e, frac res) { frac_t l, r; int t; if (e->op == OP_NUM) { res->num = e->val; res->denom = 1; return; } eval_tree(e->left, &l); eval_tree(e->right, &r); switch(e->op) { case OP_ADD: res->num = l.num * r.denom + l.denom * r.num; res->denom = l.denom * r.denom; break; case OP_S... | pub extern "C" fn eval_tree(mut e: expr, mut res: frac) { let mut l: frac_t = frac_t { denom: 0, num: 0 }; let mut r: frac_t = frac_t { denom: 0, num: 0 }; let mut t: i32 = 0; if (*e).op == OP_NUM as i32 { (*res).num = (*e).val; (*res).denom = 1; return; } eval_tree((*e).... |
15 | expr get_expr() { int c; expr l, r, ret; if (!(ret = get_term())) bail("Expected term"); while ((c = next_tok()) == '+' || c == '-') { if (!take()) bail("Unexpected end of input"); if (!(r = get_term())) bail("Expected term"); l = ret; ret = new_expr(); ret->op = (c == '+') ? OP_ADD : OP_SUB; ret->lef... | pub extern "C" fn get_expr() -> expr { let mut c: i32 = 0; let mut l: expr = 0 as *mut expr_t; let mut r: expr = 0 as *mut expr_t; let mut ret: expr = 0 as *mut expr_t; ret = get_term(); if ret.is_null() { bail(b"Expected term\0" as *const u8 as *const i8); } loop { c = n... |
16 | int main() { int i; pthread_t a[3]; threadfunc p[3] = {t_enjoy, t_rosetta, t_code}; for(i=0;i<3;i++) { pthread_create(&a[i], NULL, p[i], NULL); } sleep(1); bang = 1; pthread_cond_broadcast(&cond); for(i=0;i<3;i++) { pthread_join(a[i], NULL); } } | fn main() -> i32 { let mut i: i32 = 0; let mut a: [u64; 3] = [0; 3]; unsafe { let mut p: [threadfunc; 3] = [ Some(t_enjoy as unsafe extern "C" fn(*mut libc::c_void) -> *mut libc::c_void), Some(t_rosetta as unsafe extern "C" fn(*mut libc::c_void) -> *mut libc::c_void), ... |
17 | int main() { char str[] = "This is a top secret text message!"; printf("Original: %s\n", str); caesar(str); printf("Encrypted: %s\n", str); decaesar(str); printf("Decrypted: %s\n", str); return 0; } | fn main() -> i32 { unsafe { let mut str: [i8; 35] = *::core::mem::transmute::<&[u8; 35], &mut [i8; 35]>( b"This is a top secret text message!\0", ); print!( "Original: {}\n", build_str_from_raw_ptr(str.as_mut_ptr() as *mut u8) ); rot(13, st... |
18 | void rot(int c, char *str) { int l = strlen(str); const char *alpha[2] = { "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; int i; for (i = 0; i < l; i++) { if (!isalpha(str[i])) continue; if (isupper(str[i])) str[i] = alpha[1][((int)(tolower(str[i]) - 'a') + c) % 26]... | pub extern "C" fn rot(mut c: i32, mut str: *mut i8) { unsafe { let mut l: i32 = strlen(str) as i32; let mut alpha: [*const i8; 2] = [ b"abcdefghijklmnopqrstuvwxyz\0" as *const u8 as *const i8, b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\0" as *const u8 as *const i8, ]; let m... |
19 | void chao(const char *in, char *out, cmode mode, bool show_steps) { int i, j, index; char store; size_t len = strlen(in); char left[27], right[27], temp[27]; strcpy(left, l_alphabet); strcpy(right, r_alphabet); temp[26] = '\0'; for (i = 0; i < len; ++i ) { if (show_steps) print... | pub extern "C" fn chao(mut in_0: *const i8, mut out: *mut i8, mut mode: u32, mut show_steps: i32) { unsafe { let mut i: i32 = 0; let mut j: i32 = 0; let mut index: i32 = 0; let mut store: i8 = 0; let mut len: u64 = strlen(in_0); let mut left: [i8; 27] = [0; 27]; ... |
20 | int main() { const char *plain_text = "WELLDONEISBETTERTHANWELLSAID"; char *cipher_text = malloc(strlen(plain_text) + 1); char *plain_text2 = malloc(strlen(plain_text) + 1); printf("The original plaintext is : %s\n", plain_text); printf("\nThe left and right alphabets after each permutation" ... | fn main() -> i32 { unsafe { let mut plain_text: *const i8 = b"WELLDONEISBETTERTHANWELLSAID\0" as *const u8 as *const i8; let mut cipher_text: *mut i8 = malloc((strlen(plain_text)).wrapping_add(1)) as *mut i8; let mut plain_text2: *mut i8 = malloc((strlen(plain_text)).wrapping_add(1)) as *mut... |
21 | int main() { int a, b; scanf("%d %d", &a, &b); if (a < b) printf("%d is less than %d\n", a, b); if (a == b) printf("%d is equal to %d\n", a, b); if (a > b) printf("%d is greater than %d\n", a, b); return 0; } | fn main() -> i32 { let mut a: i32 = 0; let mut b: i32 = 0; unsafe { scanf( b"%d %d\0" as *const u8 as *const i8, &mut a as *mut i32, &mut b as *mut i32, ); } if a < b { print!("{} is less than {}\n", a, b); } if a == b { pri... |
22 | int main(void) { oldColor = 1; newColor = oldColor ? 0 : 1; readPortableBitMap(stdin); floodFill(height/2,width/2); writePortableBitMap(stdout); return EXIT_SUCCESS; } | fn main() -> i32 { unsafe { oldColor = 1; newColor = (if oldColor as i32 != 0 { 0 } else { 1 }) as u8; readPortableBitMap(stdin); floodFill(height / 2, width / 2); writePortableBitMap(stdout); } return 0; } |
23 | void skipCommentLines(FILE* file) { int c; int comment = '#'; while ((c = fgetc(file)) == comment) skipLine(file); ungetc(c,file); } | pub extern "C" fn skipCommentLines(mut file: *mut FILE) { unsafe { let mut c: i32 = 0; let mut comment: i32 = '#' as i32; loop { c = fgetc(file); if !(c == comment) { break; } skipLine(file); } ungetc(c, file); ... |
24 | void floodFill(int i, int j) { if ( 0 <= i && i < height && 0 <= j && j < width && bitmap[i][j] == oldColor ) { bitmap[i][j] = newColor; floodFill(i-1,j); floodFill(i+1,j); floodFill(i,j-1); floodFill(i,j+1); } } | pub extern "C" fn floodFill(mut i: i32, mut j: i32) { unsafe { if 0 <= i && i < height && 0 <= j && j < width && bitmap[i as usize][j as usize] as i32 == oldColor as i32 { bitmap[i as usize][j as usize] = newColor; floodFill(i -... |
25 | void skipLine(FILE* file) { while(!ferror(file) && !feof(file) && fgetc(file) != '\n') ; } | pub extern "C" fn skipLine(mut file: *mut FILE) { unsafe { while ferror(file) == 0 && feof(file) == 0 && fgetc(file) != '\n' as i32 {} } } |
26 | void writePortableBitMap(FILE* file) { int i,j; fprintf(file,"P1\n"); fprintf(file,"%d %d\n", width, height); for ( i = 0; i < height; i++ ) { for ( j = 0; j < width; j++ ) fprintf(file,"%1d", bitmap[i][j]); fprintf(file,"\n"); } } | pub extern "C" fn writePortableBitMap(mut file: *mut FILE) { unsafe { let mut i: i32 = 0; let mut j: i32 = 0; fprintf(file, b"P1\n\0" as *const u8 as *const i8); fprintf(file, b"%d %d\n\0" as *const u8 as *const i8, width, height); i = 0; while i < height { ... |
27 | void iter_string(const char * str, int d) { long long len; while (*str != '\0') { switch(*(str++)) { case 'X': if (d) iter_string("XHXVX", d - 1); else{ clen ++; h_rgb(x/scale, y/scale); x += dx; y -= dy; } continue; case 'V': len = 1LLU << d; while (len--) { clen ++; h... | pub extern "C" fn iter_string(mut str: *const i8, mut d: i32) { unsafe { let mut len: i64 = 0; while *str as i32 != '\0' as i32 { let fresh0 = str; str = str.offset(1); match *fresh0 as i32 { 88 => { if d != 0 { ... |
28 | void sierp(long leng, int depth) { long i; long h = leng + 20, w = leng + 20; /* allocate pixel buffer */ rgb *buf = malloc(sizeof(rgb) * w * h); pix = malloc(sizeof(rgb *) * h); for (i = 0; i < h; i++) pix[i] = buf + w * i; memset(buf, 0, sizeof(rgb) * w * h); /* init coords; scale up to desired; ... | pub extern "C" fn sierp(mut leng: i64, mut depth: i32) { unsafe { let mut i: i64 = 0; let mut h: i64 = leng + 20; let mut w: i64 = leng + 20; let mut buf: *mut rgb = malloc( (::core::mem::size_of::<rgb>() as u64) .wrapping_mul(w as u64) .wr... |
29 | void sc_up() { scale *= 2; x *= 2; y *= 2; cscale *= 3; } | pub extern "C" fn sc_up() { unsafe { scale *= 2; x *= 2; y *= 2; cscale *= 3; } } |
30 | int main(int c, char ** v) { int size, depth; depth = (c > 1) ? atoi(v[1]) : 10; size = 1 << depth; fprintf(stderr, "size: %d depth: %d\n", size, depth); sierp(size, depth + 2); return 0; } | fn main(mut c: i32, mut v: *mut *mut i8) -> i32 { unsafe { let mut size: i32 = 0; let mut depth: i32 = 0; depth = if c > 1 { atoi(*v.offset(1 as isize)) } else { 10 }; size = 1 << depth; fprintf( stderr, b"size: ... |
31 | void h_rgb(long long x, long long y) { rgb *p = &pix[y][x]; # define SAT 1 double h = 6.0 * clen / cscale; double VAL = 1; double c = SAT * VAL; double X = c * (1 - fabs(fmod(h, 2) - 1)); switch((int)h) { case 0: p->r += c; p->g += X; return; case 1: p->r += X; p->g += c; return; case 2: p->g += c; p->b +=... | pub extern "C" fn h_rgb(mut x_0: i64, mut y_0: i64) { unsafe { let mut p: *mut rgb = &mut *(*pix.offset(y_0 as isize)).offset(x_0 as isize) as *mut rgb; let mut h: f64 = 6.0f64 * clen as f64 / cscale as f64; let mut VAL: f64 = 1 as f64; let mut c: f64 = 1 as f64 * VAL; let mu... |
32 | int main(int argc, char **argv) { if (argc != 2) { printf("Usage: benford <file>\n"); return EXIT_FAILURE; } float *actual = get_actual_distribution(argv[1]); float *expected = benford_distribution(); puts("digit\tactual\texpected"); for (int i = 0; i < 9; i++) ... | fn main(mut argc: i32, mut argv: *mut *mut i8) -> i32 { unsafe { if argc != 2 { print!("Usage: benford <file>\n"); return 1; } let mut actual: *mut libc::c_float = get_actual_distribution(*argv.offset(1 as isize)); let mut expected: *mut libc::c_float = benfor... |
33 | int main(void) { int i; printf("Set [0,25]:\n"); for (i = 0; i < 26; i++) { char s[5]; printf("%s ", addSuffix(i, s, 5)); } putchar('\n'); printf("Set [250,265]:\n"); for (i = 250; i < 266; i++) { char s[6]; printf("%s ", addSuffix(i, s, 6)); ... | fn main() -> i32 { let mut i: i32 = 0; print!("Set [0,25]:\n"); i = 0; unsafe { while i < 26 { let mut s: [i8; 5] = [0; 5]; print!( "{} ", build_str_from_raw_ptr(addSuffix(i, s.as_mut_ptr(), 5) as *mut u8) ); i += 1;... |
34 | int main() { range_t test1[] = { {1.1, 2.2} }; range_t test2[] = { {6.1, 7.2}, {7.2, 8.3} }; range_t test3[] = { {4, 3}, {2, 1} }; range_t test4[] = { {4, 3}, {2, 1}, {-1, -2}, {3.9, 10} }; range_t test5[] = { {1, 3}, {-6, -1}, {-4, -5}, {8, 2}, {-6, -6} }; test_consolidate_ranges(test1, LENGTHO... | fn main() -> i32 { let mut test1: [range_t; 1] = [{ let mut init = range_tag { low: 1.1f64, high: 2.2f64, }; init }]; let mut test2: [range_t; 2] = [ { let mut init = range_tag { low: 6.1f64, high: 7.2f64, ... |
35 | size_t consolidate_ranges(range_t* ranges, size_t count) { normalize_ranges(ranges, count); size_t out_index = 0; for (size_t i = 0; i < count; ) { size_t j = i; while (++j < count && ranges[j].low <= ranges[i].high) { if (ranges[i].high < ranges[j].high) ranges[i... | pub extern "C" fn consolidate_ranges(mut ranges: *mut range_t, mut count: u64) -> u64 { unsafe { normalize_ranges(ranges, count); let mut out_index: u64 = 0; let mut i: u64 = 0; while i < count { let mut j: u64 = i; loop { j = j.wrapping_add(1)... |
36 | void test_consolidate_ranges(range_t* ranges, size_t count) { print_ranges(ranges, count); printf(" -> "); count = consolidate_ranges(ranges, count); print_ranges(ranges, count); printf("\n"); } | pub extern "C" fn test_consolidate_ranges(mut ranges: *mut range_t, mut count: u64) { unsafe { print_ranges(ranges, count); print!(" -> "); count = consolidate_ranges(ranges, count); print_ranges(ranges, count); print!("\n"); } } |
37 | void normalize_ranges(range_t* ranges, size_t count) { for (size_t i = 0; i < count; ++i) normalize_range(&ranges[i]); qsort(ranges, count, sizeof(range_t), range_compare); } | pub extern "C" fn normalize_ranges(mut ranges: *mut range_t, mut count: u64) { unsafe { let mut i: u64 = 0; while i < count { normalize_range(&mut *ranges.offset(i as isize)); i = i.wrapping_add(1); i; } qsort( ranges as *mut libc::c_vo... |
38 | void normalize_range(range_t* range) { if (range->high < range->low) { double tmp = range->low; range->low = range->high; range->high = tmp; } } | pub extern "C" fn normalize_range(mut range: *mut range_t) { unsafe { if (*range).high < (*range).low { let mut tmp: f64 = (*range).low; (*range).low = (*range).high; (*range).high = tmp; } } } |
39 | int range_compare(const void* p1, const void* p2) { const range_t* r1 = p1; const range_t* r2 = p2; if (r1->low < r2->low) return -1; if (r1->low > r2->low) return 1; if (r1->high < r2->high) return -1; if (r1->high > r2->high) return 1; return 0; } | pub extern "C" fn range_compare(mut p1: *const libc::c_void, mut p2: *const libc::c_void) -> i32 { unsafe { let mut r1: *const range_t = p1 as *const range_t; let mut r2: *const range_t = p2 as *const range_t; if (*r1).low < (*r2).low { return -1; } if (*r1).low >... |
40 | void print_ranges(const range_t* ranges, size_t count) { if (count == 0) return; print_range(&ranges[0]); for (size_t i = 1; i < count; ++i) { printf(", "); print_range(&ranges[i]); } } | pub extern "C" fn print_ranges(mut ranges: *const range_t, mut count: u64) { unsafe { if count == 0 { return; } print_range(&*ranges.offset(0 as isize)); let mut i: u64 = 1; while i < count { print!(", "); print_range(&*ranges.offset(i as i... |
41 | void print_range(const range_t* range) { printf("[%g, %g]", range->low, range->high); } | pub extern "C" fn print_range(mut range: *const range_t) { print!("[{}, {}]", (*range).low, (*range).high); } |
42 | int main(void) { int us_coins[] = { 100, 50, 25, 10, 5, 1, 0 }; int eu_coins[] = { 200, 100, 50, 20, 10, 5, 2, 1, 0 }; show(count( 100, us_coins + 2)); show(count( 1000, us_coins)); show(count( 1000 * 100, us_coins)); show(count( 10000 * 100, us_coins)); show(count(100000 * 100, us_coins)); putchar('\... | fn main() -> i32 { let mut us_coins: [i32; 7] = [100, 50, 25, 10, 5, 1, 0]; let mut eu_coins: [i32; 9] = [200, 100, 50, 20, 10, 5, 2, 1, 0]; unsafe { show(count(100, us_coins.as_mut_ptr().offset(2 as isize))); } show(count(1000, us_coins.as_mut_ptr())); show(count(1000 * 100, us_coins.as... |
43 | int count2(int sum, int *coins) { if (!*coins || sum < 0) return 0; if (!sum) return 1; return count2(sum - *coins, coins) + count2(sum, coins + 1); } | pub extern "C" fn count2(mut sum: i32, mut coins: *mut i32) -> i32 { unsafe { if *coins == 0 || sum < 0 { return 0; } if sum == 0 { return 1; } return count2(sum - *coins, coins) + count2(sum, coins.offset(1 as isize)); } } |
44 | i128 count(int sum, int *coins) { int n, i, k; for (n = 0; coins[n]; n++); i128 **v = malloc(sizeof(int*) * n); int *idx = malloc(sizeof(int) * n); for (i = 0; i < n; i++) { idx[i] = coins[i]; // each v[i] is a cyclic buffer v[i] = calloc(sizeof(i128), coins[i]); } v[0][coins[0] - 1] = (i128) {{1, 0}... | pub extern "C" fn count(mut sum: i32, mut coins: *mut i32) -> i128_0 { unsafe { let mut n: i32 = 0; let mut i: i32 = 0; let mut k: i32 = 0; n = 0; while *coins.offset(n as isize) != 0 { n += 1; n; } let mut v: *mut *mut i128_0 = ... |
45 | void show(i128 v) { uint32_t x[4] = {v.x[0], v.x[0] >> 32, v.x[1], v.x[1] >> 32}; int i, j = 0, len = 4; char buf[100]; do { uint64_t c = 0; for (i = len; i--; ) { c = (c << 32) + x[i]; x[i] = c / 10, c %= 10; } buf[j++] = c + '0'; for (len = 4; !x[len - 1]; len--); } while (len); while (j--) ... | pub extern "C" fn show(mut v: i128_0) { let mut x: [u32; 4] = [ v.x[0 as usize] as u32, (v.x[0 as usize] >> 32i32) as u32, v.x[1 as usize] as u32, (v.x[1 as usize] >> 32i32) as u32, ]; let mut i: i32 = 0; let mut j: i32 = 0; let mut len: i32 = 4; let mut buf: [i8;... |
46 | int main(void) { int i; for (i = 2; i < 100; i++) if (semiprime(i)) printf(" %d", i); putchar('\n'); return 0; } | fn main() -> i32 { let mut i: i32 = 0; i = 2; while i < 100 { if semiprime(i) != 0 { print!(" {}", i); } i += 1; i; } print!("{}", '\n' as i32); return 0; } |
47 | int semiprime(int n) { int p, f = 0; for (p = 2; f < 2 && p*p <= n; p++) while (0 == n % p) n /= p, f++; return f + (n > 1) == 2; } | pub extern "C" fn semiprime(mut n: i32) -> i32 { let mut p: i32 = 0; let mut f: i32 = 0; p = 2; while f < 2 && p * p <= n { while 0 == n % p { n /= p; f += 1; f; } p += 1; p; } return (f + (n > 1i32) as i32 == 2) as i32; } |
48 | int main() { { unsigned long long n = 1; for (int i = 0; i < 30; i++) { // __builtin_popcount() for unsigned int // __builtin_popcountl() for unsigned long // __builtin_popcountll() for unsigned long long printf("%d ", __builtin_popcountll(n)); n *= 3; } printf("\n"); }... | fn main() -> i32 { let mut n: u64 = 1; let mut i: i32 = 0; while i < 30 { print!("{} ", n.count_ones() as i32); n = n.wrapping_mul(3); i += 1; i; } print!("\n"); let mut od: [i32; 30] = [0; 30]; let mut ne: i32 = 0; let mut no: i32 = 0; print!("evil :... |
49 | double InvokeComposed( Class2Func f1, Class2Func f2, double val ) { return f1(f2(val)); } | pub extern "C" fn InvokeComposed(mut f1: Class2Func, mut f2: Class2Func, mut val: f64) -> f64 { unsafe { return f1.expect("non-null function pointer")(f2.expect("non-null function pointer")(val)); } } |
50 | Class2Func WhichFunc( int idx) { return (idx < 4) ? &functionA : &functionB; } | pub extern "C" fn WhichFunc(mut idx: i32) -> Class2Func { return if idx < 4 { Some(functionA as unsafe extern "C" fn(f64) -> f64) } else { Some(functionB as unsafe extern "C" fn(f64) -> f64) }; } |
51 | int main(int argc, char *argv[]) { int ix; Composition c; printf("Function1(functionA, 3.0) = %f\n", Function1(WhichFunc(0), 3.0)); for (ix=0; ix<4; ix++) { c = Compose(funcListA[ix], funcListB[ix]); printf("Compostion %d(0.9) = %f\n", ix, CallComposed(c, 0.9)); } return 0; } | fn main(mut argc: i32, mut argv: *mut *mut i8) -> i32 { unsafe { let mut ix: i32 = 0; let mut c: Composition = 0 as *mut sComposition; print!( "Function1(functionA, 3.0) = {}\n", Function1(WhichFunc(0), 3.0f64) ); ix = 0; while ix < 4 { ... |
52 | double Function1( Class2Func f2, double val ) { return f2(val); } | pub extern "C" fn Function1(mut f2: Class2Func, mut val: f64) -> f64 { unsafe { return f2.expect("non-null function pointer")(val); } } |
53 | double functionB(double v) { return exp(log(v)/3); } | pub extern "C" fn functionB(mut v: f64) -> f64 { unsafe { return exp(log(v) / 3 as f64); } } |
54 | Composition Compose( Class2Func f1, Class2Func f2) { Composition comp = malloc(sizeof(struct sComposition)); comp->f1 = f1; comp->f2 = f2; return comp; } | pub extern "C" fn Compose(mut f1: Class2Func, mut f2: Class2Func) -> Composition { unsafe { let mut comp: Composition = malloc(::core::mem::size_of::<sComposition>() as u64) as Composition; (*comp).f1 = f1; (*comp).f2 = f2; return comp; } } |
55 | double functionA( double v) { return v*v*v; } | pub extern "C" fn functionA(mut v: f64) -> f64 { return v * v * v; } |
56 | double CallComposed( Composition comp, double val ) { return comp->f1( comp->f2(val) ); } | pub extern "C" fn CallComposed(mut comp: Composition, mut val: f64) -> f64 { return ((*comp).f1).expect("non-null function pointer")(((*comp).f2) .expect("non-null function pointer")( val )); } |
57 | void testcase(const char *pszTest) { unsigned char abyAddr[16]; int bIsIPv6; int nPort; int bSuccess; printf("Test case '%s'\n", pszTest); const char *pszTextCursor = pszTest; bSuccess = ParseIPv4OrIPv6(&pszTextCursor, abyAddr, &nPort, &bIsIPv6); if (!bSuccess) { printf("par... | pub extern "C" fn testcase(mut pszTest: *const i8) { unsafe { let mut abyAddr: [u8; 16] = [0; 16]; let mut bIsIPv6: i32 = 0; let mut nPort: i32 = 0; let mut bSuccess: i32 = 0; print!("Test case {}\n", build_str_from_raw_ptr(pszTest as *mut u8)); let mut pszTextCursor:... |
58 | unsigned short htons(unsigned short us) { return (((unsigned char *)&us)[0] << 8) + ((unsigned char *)&us)[1]; } | pub extern "C" fn htons(mut us: u16) -> u16 { unsafe { return (((*(&mut us as *mut u16 as *mut u8).offset(0 as isize) as i32) << 8i32) + *(&mut us as *mut u16 as *mut u8).offset(1 as isize) as i32) as u16; } } |
59 | int ParseIPv4OrIPv6_2(const char *pszText, unsigned char *abyAddr, int *pnPort, int *pbIsIPv6) { const char *pszTextLocal = pszText; return ParseIPv4OrIPv6(&pszTextLocal, abyAddr, pnPort, pbIsIPv6); } | pub extern "C" fn ParseIPv4OrIPv6_2( mut pszText: *const i8, mut abyAddr: *mut u8, mut pnPort: *mut i32, mut pbIsIPv6: *mut i32, ) -> i32 { unsafe { let mut pszTextLocal: *const i8 = pszText; return ParseIPv4OrIPv6(&mut pszTextLocal, abyAddr, pnPort, pbIsIPv6); } } |
60 | void dumpbin(unsigned char *pbyBin, int nLen) { int i; for (i = 0; i < nLen; ++i) { printf("%02x", pbyBin[i]); } } | pub extern "C" fn dumpbin(mut pbyBin: *mut u8, mut nLen: i32) { unsafe { let mut i: i32 = 0; i = 0; while i < nLen { print!("{:02x}", *pbyBin.offset(i as isize) as i32); i += 1; i; } } } |
61 | static unsigned int _parseHex(const char **pchCursor) { unsigned int nVal = 0; char chNow; while (chNow = **pchCursor & 0x5f, //(collapses case, but mutilates digits) (chNow >= ('0' & 0x5f) && chNow <= ('9' & 0x5f)) || (chNow >= 'A' && chNow <= 'F')) { unsigned char nyb... | extern "C" fn _parseHex(mut pchCursor: *mut *const i8) -> u32 { unsafe { let mut nVal: u32 = 0; let mut chNow: i8 = 0; loop { chNow = (**pchCursor as i32 & 0x5fi32) as i8; if !(chNow as i32 >= '0' as i32 & 0x5f && chNow as i32 <= '9' as i32 & 0x5f || c... |
62 | int ParseIPv4OrIPv6(const char **ppszText, unsigned char *abyAddr, int *pnPort, int *pbIsIPv6) { unsigned char *abyAddrLocal; unsigned char abyDummyAddr[16]; //find first colon, dot, and open bracket const char *pchColon = strchr(*ppszText, ':'); const char *pchDot = strchr(*pps... | pub extern "C" fn ParseIPv4OrIPv6( mut ppszText: *mut *const i8, mut abyAddr: *mut u8, mut pnPort: *mut i32, mut pbIsIPv6: *mut i32, ) -> i32 { unsafe { let mut abyAddrLocal: *mut u8 = 0 as *mut u8; let mut abyDummyAddr: [u8; 16] = [0; 16]; let mut pchColon: *const i8 = strch... |
63 | static unsigned int _parseDecimal(const char **pchCursor) { unsigned int nVal = 0; char chNow; while (chNow = **pchCursor, chNow >= '0' && chNow <= '9') { //shift digit in nVal *= 10; nVal += chNow - '0'; ++*pchCursor; } return nVal; } | extern "C" fn _parseDecimal(mut pchCursor: *mut *const i8) -> u32 { unsafe { let mut nVal: u32 = 0; let mut chNow: i8 = 0; loop { chNow = **pchCursor; if !(chNow as i32 >= '0' as i32 && chNow as i32 <= '9' as i32) { break; } nVa... |
64 | int main(int argc, char *argv[]) { //The "localhost" IPv4 address testcase("127.0.0.1"); //The "localhost" IPv4 address, with a specified port (80) testcase("127.0.0.1:80"); //The "localhost" IPv6 address testcase("::1"); //The "localhost" IPv6 address, with a specified port (80) testc... | fn main(mut argc: i32, mut argv: *mut *mut i8) -> i32 { unsafe { testcase(b"127.0.0.1\0" as *const u8 as *const i8); testcase(b"127.0.0.1:80\0" as *const u8 as *const i8); testcase(b"::1\0" as *const u8 as *const i8); testcase(b"[::1]:80\0" as *const u8 as *const i8); testcas... |
65 | void example() { printf("Example\n"); } | pub extern "C" fn example() { print!("Example\n"); } |
66 | int main(int argc, char *argv[]) { repeat(example, 4); return 0; } | fn main(mut argc: i32, mut argv: *mut *mut i8) -> i32 { unsafe { repeat( ::core::mem::transmute::< Option<unsafe extern "C" fn() -> ()>, Option<unsafe extern "C" fn() -> ()>, >(Some(::core::mem::transmute::< unsafe extern "C" fn() -> ()... |
67 | void repeat(void (*f)(void), unsigned int n) { while (n-->0) (*f)(); //or just f() } | pub extern "C" fn repeat(mut f: Option<unsafe extern "C" fn() -> ()>, mut n: u32) { unsafe { loop { let fresh0 = n; n = n.wrapping_sub(1); if !(fresh0 > 0) { break; }; (Some(f.expect("non-null function pointer"))).expect("non-null f... |
68 | int vc_cmp(const void *a, const void *b) { return ((const vcount*)b)->c - ((const vcount*)a)->c; } | pub extern "C" fn vc_cmp(mut a: *const libc::c_void, mut b: *const libc::c_void) -> i32 { unsafe { return (*(b as *const vcount)).c - (*(a as *const vcount)).c; } } |
69 | int cmp_dbl(const void *a, const void *b) { double x = *(const double*)a - *(const double*)b; return x < 0 ? -1 : x > 0; } | pub extern "C" fn cmp_dbl(mut a: *const libc::c_void, mut b: *const libc::c_void) -> i32 { unsafe { let mut x: f64 = *(a as *const f64) - *(b as *const f64); return if x < 0 as f64 { -1 } else { (x > 0 as f64) as i32 }; } } |
70 | int get_mode(double* x, int len, vcount **list) { int i, j; vcount *vc; /* sort values */ qsort(x, len, sizeof(double), cmp_dbl); /* count occurence of each value */ for (i = 0, j = 1; i < len - 1; i++, j += (x[i] != x[i + 1])); *list = vc = malloc(sizeof(vcount) * j); vc[0].v = x[0]; vc[0].c = 1; /* ... | pub extern "C" fn get_mode(mut x: *mut f64, mut len_0: i32, mut list: *mut *mut vcount) -> i32 { unsafe { let mut i: i32 = 0; let mut j: i32 = 0; let mut vc: *mut vcount = 0 as *mut vcount; qsort( x as *mut libc::c_void, len_0 as u64, ::core::mem::... |
71 | int main() { double values[] = { 1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 12, 12, 17 }; # define len sizeof(values)/sizeof(double) vcount *vc; int i, n_modes = get_mode(values, len, &vc); printf("got %d modes:\n", n_modes); for (i = 0; i < n_modes; i++) printf("\tvalue = %g, count = %d\n", vc[i].v, vc[i].c); free(... | fn main() -> i32 { unsafe { let mut values: [f64; 13] = [ 1 as f64, 3 as f64, 6 as f64, 6 as f64, 6 as f64, 6 as f64, 7 as f64, 7 as f64, 12 as f64, 12 as f64, 12 as f64, 12 as f64, 17 as f64, ]; let mut vc: *mut vcount = 0 as *mut vcount; let mut i: i32 = 0; ... |
72 | int main() { printf("Pi is %f\n", pi(3e-4)); /* set to 1e-4 for some fun */ return 0; } | fn main() -> i32 { print!("Pi is {}\n", pi(3e-4f64)); return 0; } |
73 | double pi(double tolerance) { double x, y, val, error; unsigned long sampled = 0, hit = 0, i; do { /* don't check error every turn, make loop tight */ for (i = 1000000; i; i--, sampled++) { x = rand() / (RAND_MAX + 1.0); y = rand() / (RAND_MAX + 1.0); if (x * x + y * y < 1) hit ++; } val = (doub... | pub extern "C" fn pi(mut tolerance: f64) -> f64 { let mut x: f64 = 0.; let mut y: f64 = 0.; let mut val: f64 = 0.; let mut error: f64 = 0.; let mut sampled: u64 = 0; let mut hit: u64 = 0; let mut i: u64 = 0; unsafe { loop { i = 1000000; while i != 0 { ... |
74 | sma_result_t sma(enum Action action, ...) { va_list vl; sma_result_t r; sma_obj_t *o; double v; va_start(vl, action); switch(action) { case SMA_NEW: // args: int period r.handle = malloc(sizeof(sma_obj_t)); r.handle->sma = 0.0; r.handle->period = va_arg(vl, int); r.handle->values = mallo... | pub unsafe extern "C" fn sma(mut action: u32, mut args: ...) -> sma_result_t { let mut vl: ::core::ffi::VaListImpl; let mut r: sma_result_t = sma_result { handle: 0 as *mut sma_obj_t, }; let mut o: *mut sma_obj_t = 0 as *mut sma_obj_t; let mut v_0: f64 = 0.; vl = args.clone(); match ... |
75 | int main() { int i; sma_obj_t *h3 = sma(SMA_NEW, 3).handle; sma_obj_t *h5 = sma(SMA_NEW, 5).handle; for(i=0; i < sizeof(v)/sizeof(double) ; i++) { printf("next number %lf, SMA_3 = %lf, SMA_5 = %lf\n", v[i], sma(SMA_ADD, h3, v[i]).sma, sma(SMA_ADD, h5, v[i]).sma); } sma(SMA_FREE, h3); sma(SMA... | fn main() -> i32 { unsafe { let mut i: i32 = 0; let mut h3: *mut sma_obj_t = (sma(SMA_NEW, 3)).handle; let mut h5: *mut sma_obj_t = (sma(SMA_NEW, 5)).handle; i = 0; while (i as u64) < (::core::mem::size_of::<[f64; 10]>() as u64) .wrapping_div(::cor... |
76 | double mapRange(double a1,double a2,double b1,double b2,double s) { return b1 + (s-a1)*(b2-b1)/(a2-a1); } | pub extern "C" fn mapRange(mut a1: f64, mut a2: f64, mut b1: f64, mut b2: f64, mut s: f64) -> f64 { return b1 + (s - a1) * (b2 - b1) / (a2 - a1); } |
77 | int main() { int i; puts("Mapping [0,10] to [-1,0] at intervals of 1:"); for(i=0;i<=10;i++) { printf("f(%d) = %g\n",i,mapRange(0,10,-1,0,i)); } return 0; } | fn main() -> i32 { let mut i: i32 = 0; unsafe { puts(b"Mapping [0,10] to [-1,0] at intervals of 1:\0" as *const u8 as *const i8); } i = 0; while i <= 10 { print!( "f({}) = {}\n", i, mapRange(0 as f64, 10 as f64, -1i32 as f64, 0 as f64, i as f64) ... |
78 | int do_tick(int d) { game.have_moved = 0; do_gravity(d); do_merge(d); do_gravity(d); return game.have_moved; } | pub extern "C" fn do_tick(mut d: i32) -> i32 { unsafe { game.have_moved = 0; } do_gravity(d); do_merge(d); do_gravity(d); unsafe { return game.have_moved; } } |
79 | void do_newblock(void) { if (game.blocks_in_play >= 16) return; int bn = rand() % (16 - game.blocks_in_play); int pn = 0; for (int x = 0; x < 4; ++x) { for (int y = 0; y < 4; ++y) { if (game.grid[x][y]) continue; if (pn == bn){ game.g... | pub extern "C" fn do_newblock() { unsafe { if game.blocks_in_play >= 16 { return; } let mut bn: i32 = rand() % (16 - game.blocks_in_play); let mut pn: i32 = 0; let mut x: i32 = 0; while x < 4 { let mut y: i32 = 0; while y < 4 { ... |
80 | void do_gravity(int d) { #define GRAVITATE_DIRECTION(_v1, _v2, _xs, _xc, _xi, _ys, _yc, _yi, _x, _y) \ do { \ int break_cond = 0; \ while (!break_cond) { ... | pub extern "C" fn do_gravity(mut d: i32) { unsafe { match d { 4 => { let mut break_cond: i32 = 0; while break_cond == 0 { break_cond = 1; let mut x: i32 = 0; while x < 3 { let mut ... |
81 | void do_merge(int d) { /* These macros look pretty scary, but mainly demonstrate some space saving */ #define MERGE_DIRECTION(_v1, _v2, _xs, _xc, _xi, _ys, _yc, _yi, _x, _y) \ do { \ for (int _v1 = _xs; _v1 _xc; _v1 += _xi) { ... | pub extern "C" fn do_merge(mut d: i32) { unsafe { game.score_last_move = 0; match d { 4 => { let mut x: i32 = 0; while x < 3 { let mut y: i32 = 0; while y < 4 { if game.grid[x as usize][y as u... |
82 | int main(void) { /* Initialize terminal settings */ tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); srand(time(NULL)); memset(&game, 0, sizeof(game)); do_newblock(); do_newblock(); do_draw(); while (1... | fn main() -> i32 { let mut current_block: u64; unsafe { tcgetattr(0, &mut oldt); newt = oldt; newt.c_lflag &= !(0o2 | 0o10i32) as u32; tcsetattr(0, 0, &mut newt); srand(rust_time(None) as u32); memset( &mut game as *mut gamestate_struct__ as *mut libc:... |
83 | int do_check_end_condition(void) { int ret = -1; for (int x = 0; x < 4; ++x) { for (int y = 0; y < 4; ++y) { if (values[game.grid[x][y]] == 2048) return 1; if (!game.grid[x][y] || ((x + 1 < 4) && (game.grid[x][y] == game.grid[x + 1][y])) || ... | pub extern "C" fn do_check_end_condition() -> i32 { let mut ret: i32 = -1; let mut x: i32 = 0; unsafe { while x < 4 { let mut y: i32 = 0; while y < 4 { if values[game.grid[x as usize][y as usize] as usize] == 2048 { return 1; ... |
84 | void do_draw(void) { printf("\033[2J\033[HScore: %ld", game.total_score); if (game.score_last_move) printf(" (+%ld)", game.score_last_move); printf("\n"); for (int i = 0; i < 25; ++i) printf("-"); printf("\n"); for (int y = 0; y < 4; ++y) { printf("|"); for (i... | pub extern "C" fn do_draw() { unsafe { print!("\x1B[2J\x1B[HScore: {}", game.total_score); if game.score_last_move != 0 { print!(" (+{})", game.score_last_move); } } print!("\n"); let mut i: i32 = 0; while i < 25 { print!("-"); i += 1; i; ... |
85 | int ord(char v) { return v-'0'; } | pub extern "C" fn ord(mut v: i8) -> i32 { return v as i32 - '0' as i32; } |
86 | void printTable( Table tbl, FILE *fout, const char *colFmts[]) { int row, col; for (row=0; row<tbl->n_rows; row++) { fprintf(fout, " "); for(col=0; col<tbl->n_cols; col++) { fprintf(fout, colFmts[col], tbl->rows[row][col]); } fprintf(fout, "\n"); } fprintf(fout, "\n"); } | pub extern "C" fn printTable(mut tbl: Table, mut fout: *mut FILE, mut colFmts: *mut *const i8) { unsafe { let mut row: i32 = 0; let mut col: i32 = 0; row = 0; while row < (*tbl).n_rows { fprintf(fout, b" \0" as *const u8 as *const i8); col = 0; w... |
87 | int CmprRows( const void *aa, const void *bb) { String *rA = *(String *const *)aa; String *rB = *(String *const *)bb; int sortCol = sortSpec.column; String left = sortSpec.reversed ? rB[sortCol] : rA[sortCol]; String right = sortSpec.reversed ? rA[sortCol] : rB[sortCol]; return sortSpec.compare( lef... | pub extern "C" fn CmprRows(mut aa: *const libc::c_void, mut bb: *const libc::c_void) -> i32 { unsafe { let mut rA: *mut String_0 = *(aa as *const *mut String_0); let mut rB: *mut String_0 = *(bb as *const *mut String_0); let mut sortCol: i32 = sortSpec.column; let mut left: String_0 ... |
88 | int sortTable(Table tbl, const char* argSpec,... ) { va_list vl; const char *p; int c; sortSpec.compare = &strcmp; sortSpec.column = 0; sortSpec.reversed = 0; va_start(vl, argSpec); if (argSpec) for (p=argSpec; *p; p++) { switch (*p) { case 'o': sortSpec.com... | pub unsafe extern "C" fn sortTable(mut tbl: Table, mut argSpec: *const i8, mut args: ...) -> i32 { let mut vl: ::core::ffi::VaListImpl; let mut p: *const i8 = 0 as *const i8; let mut c: i32 = 0; sortSpec.compare = Some(strcmp as unsafe extern "C" fn(*const i8, *const i8) -> i32); sortSpec.column = 0... |
89 | int cmprStrgs(String s1, String s2) { const char *p1 = s1; const char *p2 = s2; const char *mrk1, *mrk2; while ((tolower(*p1) == tolower(*p2)) && *p1) { p1++; p2++; } if (isdigit(*p1) && isdigit(*p2)) { long v1, v2; if ((*p1 == '0') ||(*p2 == '0')) { while (p1... | pub extern "C" fn cmprStrgs(mut s1: String_0, mut s2: String_0) -> i32 { unsafe { let mut p1: *const i8 = s1; let mut p2: *const i8 = s2; let mut mrk1: *const i8 = 0 as *const i8; let mut mrk2: *const i8 = 0 as *const i8; while tolower(*p1 as i32) == tolower(*p2 as i32) && *p... |
90 | int main() { const char *colFmts[] = {" %-5.5s"," %-5.5s"," %-9.9s"}; String r1[] = { "a101", "red", "Java" }; String r2[] = { "ab40", "gren", "Smalltalk" }; String r3[] = { "ab9", "blue", "Fortran" }; String r4[] = { "ab09", "ylow", "Python" }; String r5[] = { "ab1a", "blak", "Factor" }; String ... | fn main() -> i32 { let mut colFmts: [*const i8; 3] = [ b" %-5.5s\0" as *const u8 as *const i8, b" %-5.5s\0" as *const u8 as *const i8, b" %-9.9s\0" as *const u8 as *const i8, ]; let mut r1: [String_0; 3] = [ b"a101\0" as *const u8 as *const i8, b"red\0" as *const u8 a... |
91 | int main(int argc, char** argv) { const char* filename = "sierpinski_arrowhead.svg"; if (argc == 2) filename = argv[1]; FILE* out = fopen(filename, "w"); if (!out) { perror(filename); return EXIT_FAILURE; } write_sierpinski_arrowhead(out, 600, 8); fclose(out); ret... | fn main(mut argc: i32, mut argv: *mut *mut i8) -> i32 { unsafe { let mut filename: *const i8 = b"sierpinski_arrowhead.svg\0" as *const u8 as *const i8; if argc == 2 { filename = *argv.offset(1 as isize); } let mut out: *mut FILE = fopen(filename, b"w\0" as *const u8 as *c... |
92 | void write_sierpinski_arrowhead(FILE* out, int size, int order) { const double margin = 20.0; const double side = size - 2.0 * margin; cursor_t cursor; cursor.angle = 0; cursor.x = margin; cursor.y = 0.5 * size + 0.25 * sqrt(3) * side; if ((order & 1) != 0) turn(&cursor, -60); fp... | pub extern "C" fn write_sierpinski_arrowhead(mut out: *mut FILE, mut size: i32, mut order: i32) { unsafe { let margin: f64 = 20.0f64; let side: f64 = size as f64 - 2.0f64 * margin; let mut cursor: cursor_t = cursor_t { x: 0., y: 0., angle: 0, }; ... |
93 | void curve(FILE* out, int order, double length, cursor_t* cursor, int angle) { if (order == 0) { draw_line(out, cursor, length); } else { curve(out, order - 1, length/2, cursor, -angle); turn(cursor, angle); curve(out, order - 1, length/2, cursor, angle); turn(cursor, ang... | pub extern "C" fn curve( mut out: *mut FILE, mut order: i32, mut length: f64, mut cursor: *mut cursor_t, mut angle: i32, ) { unsafe { if order == 0 { draw_line(out, cursor, length); } else { curve(out, order - 1, length / 2 as f64, cursor, -angle); ... |
94 | void turn(cursor_t* cursor, int angle) { cursor->angle = (cursor->angle + angle) % 360; } | pub extern "C" fn turn(mut cursor: *mut cursor_t, mut angle: i32) { unsafe { (*cursor).angle = ((*cursor).angle + angle) % 360; } } |
95 | void draw_line(FILE* out, cursor_t* cursor, double length) { double theta = (M_PI * cursor->angle)/180.0; cursor->x += length * cos(theta); cursor->y += length * sin(theta); fprintf(out, "L%g,%g\n", cursor->x, cursor->y); } | pub extern "C" fn draw_line(mut out: *mut FILE, mut cursor: *mut cursor_t, mut length: f64) { unsafe { let mut theta: f64 = 3.14159265358979323846f64 * (*cursor).angle as f64 / 180.0f64; (*cursor).x += length * cos(theta); (*cursor).y += length * sin(theta); fprintf( out,... |
96 | int main(void) { int p1; for (p1 = 2; p1 < 62; ++p1) carmichael3(p1); return 0; } | fn main() -> i32 { let mut p1: i32 = 0; p1 = 2; while p1 < 62 { carmichael3(p1); p1 += 1; p1; } return 0; } |
97 | int is_prime(unsigned int n) { if (n <= 3) { return n > 1; } else if (!(n % 2) || !(n % 3)) { return 0; } else { unsigned int i; for (i = 5; i*i <= n; i += 6) if (!(n % i) || !(n % (i + 2))) return 0; return 1; } } | pub extern "C" fn is_prime(mut n: u32) -> i32 { if n <= 3 { return (n > 1) as i32; } else if n.wrapping_rem(2) == 0 || n.wrapping_rem(3) == 0 { return 0; } else { let mut i: u32 = 0; i = 5; while i.wrapping_mul(i) <= n { if n.wrapping_rem(i) == 0 || n.wrap... |
98 | void carmichael3(int p1) { if (!is_prime(p1)) return; int h3, d, p2, p3; for (h3 = 1; h3 < p1; ++h3) { for (d = 1; d < h3 + p1; ++d) { if ((h3 + p1)*(p1 - 1) % d == 0 && mod(-p1 * p1, h3) == d % h3) { p2 = 1 + ((p1 - 1) * (h3 + p1)/d); if (!is_prime(p2))... | pub extern "C" fn carmichael3(mut p1: i32) { if is_prime(p1 as u32) == 0 { return; } let mut h3: i32 = 0; let mut d: i32 = 0; let mut p2: i32 = 0; let mut p3: i32 = 0; h3 = 1; while h3 < p1 { d = 1; while d < h3 + p1 { if (h3 + p1) * (p1 - 1) % d == 0 ... |
99 | int myopenimage(const char *in) { static int handle=0; fprintf(stderr, "internal openimage opens %s...\n", in); return handle++; } | pub extern "C" fn myopenimage(mut in_0: *const i8) -> i32 { unsafe { static mut handle: i32 = 0; fprintf( stderr, b"internal openimage opens %s...\n\0" as *const u8 as *const i8, in_0, ); let fresh0 = handle; handle = handle + 1; re... |
End of preview. Expand in Data Studio
README.md exists but content is empty.
- Downloads last month
- 18