Ruby

『明解C言語 入門編』in multi language

『明解C言語 入門編』in multi language 柴田望洋『明解C言語 入門編』を勉強してまいります。 1. まずは慣れよう 000.整数値 15 と 37 の和を表示する 001.整数値 15 から 37 を引いた値を表示する 002.整数値 15 と 37 の和を親切に表示する 003.2つの変…

ポインタのソート

明解C言語 入門編 > 12. 構造体 > ポインタのソート C #include <stdio.h> #include <string.h> #include <alloc.h>#define NINSU 5typedef struct { char name[20]; int height; float weight; } gstudent;typedef int (*compare)(gstudent*, gstudent*);int compare_height(gstudent* </alloc.h></string.h></stdio.h>…

構造体の動的配列 (malloc)

明解C言語 入門編 > 12. 構造体 > 構造体の動的配列 (malloc) C #include <stdio.h> #include <string.h> #include <alloc.h>#define NINSU 5typedef struct { char name[20]; int height; float weight; } gstudent;typedef int (*compare)(gstudent*, gstudent*);int compare_height(g</alloc.h></string.h></stdio.h>…

5人の学生を 身長・体重で ソート (関数ポインタ)

明解C言語 入門編 > 12. 構造体 > 5人の学生を 身長・体重で ソート (関数ポインタ) C #include <stdio.h> #include <string.h>#define NINSU 5typedef struct { char name[20]; int height; float weight; } gstudent;typedef int (*compare)(gstudent*, gstudent*);int comp</string.h></stdio.h>…

5人の学生を 身長で ソート (構造体)

明解C言語 入門編 > 12. 構造体 > 5人の学生を 身長で ソート (構造体) C #include <stdio.h> #include <string.h>#define NINSU 5typedef struct { char name[20]; int height; float weight; } gstudent;void swap(gstudent* x, gstudent* y) { gstudent tmp = *x; *x = *y;</string.h></stdio.h>…

構造体の受け渡し

明解C言語 入門編 > 12. 構造体 > 構造体の受け渡し C #include <stdio.h> #include <string.h>struct gstudent { char name[20]; int height; float weight; };void hiroko(struct gstudent* std) { (*std).height = 180; (*std).weight = 80; }int main(int argc, char* argv</string.h></stdio.h>…

構造体のメンバの初期化

明解C言語 入門編 > 12. 構造体 > 構造体のメンバの初期化 C #include <stdio.h> #include <string.h>struct gstudent { char name[20]; int height; float weight; };int main(int argc, char* argv[]) { struct gstudent sanaka = {"Sanaka", 175, 60.5}; printf("氏名:%s\n</string.h></stdio.h>…

構造体

明解C言語 入門編 > 12. 構造体 > 構造体 C #include <stdio.h> #include <string.h>struct gstudent { char name[20]; int height; float weight; };int main(int argc, char* argv[]) { struct gstudent sanaka; strcpy(sanaka.name, "Sanaka"); sanaka.height = 175; sanaka</string.h></stdio.h>…

5人の学生を 身長で ソート (配列)

明解C言語 入門編 > 12. 構造体 > 5人の学生を 身長で ソート (配列) C #include <stdio.h> #include <string.h>#define NINSU 5void swap(int* x, int* y) { int tmp = *x; *x = *y; *y = tmp; }void swaps(char sx[], char sy[]) { char tmp[256]; strcpy(tmp, sx); strcpy(</string.h></stdio.h>…

5人の学生の身長を ソート

明解C言語 入門編 > 12. 構造体 > 5人の学生の身長を ソート C #include <stdio.h>#define NINSU 5void swap(int* x, int* y) { int tmp = *x; *x = *y; *y = tmp; }void sort(int data[], int n) { int k = n - 1; while (k >= 0) { int i, j; for (i = 1, j = -1;</stdio.h>…

さまざまな文字列操作

明解C言語 入門編 > 11. 文字列とポインタ > さまざまな文字列操作 C #include <stdio.h> #include <string.h>int main(int argc, char* argv[]) { /* strcat */ char st1[100] = "1234567890"; char st2[100] = "ABCDE"; printf("%s + ", st1); printf("%s = ", st2); strcat(</string.h></stdio.h>…

2つの値を交換する

明解C言語 入門編 > 10. ポインタ > 2つの値を交換する C #include <stdio.h>void swap1(int nx, int ny) { int tmp = nx; nx = ny; ny = tmp; }void swap2(int* nx, int* ny) { int tmp = *nx; *nx = *ny; *ny = tmp; }int main(int argc, char* argv[]) { int na </stdio.h>…

大文字・小文字の変換

明解C言語 入門編 > 9. 文字列の基本 > 大文字・小文字の変換 C #include <stdio.h> #include <ctype.h>void str_toupper(char st[]) { unsigned i = 0; while (st[i]) { st[i] = toupper(st[i]); i++; } }void str_tolower(char st[]) { unsigned i = 0; while (st[i]) { st[</ctype.h></stdio.h>…

文字列の配列を受け渡し

明解C言語 入門編 > 9. 文字列の基本 > 文字列の配列を受け渡し C #include <stdio.h>void put_strary(const char st[][6], int n) { int i; for (i = 0; i < n; i++) { int j = 0; printf("st[%d] = \"", i); while (st[i][j]) putchar(st[i][j++]); puts("\""); } </stdio.h>…

文字列内の数字の出現回数を数える

明解C言語 入門編 > 9. 文字列の基本 > 文字列内の数字の出現回数を数える C #include <stdio.h> int main(int argc, char* argv[]) { int cnt[10] = {0}; char str[100] = "3.1415926535897932846"; unsigned i = 0; while (str[i]) { if (('0' <= str[i]) && (str[</stdio.h>…

文字列の走査

明解C言語 入門編 > 9. 文字列の基本 > 文字列の走査 C #include <stdio.h>void put_string(const char str[]) { unsigned i = 0; while (str[i]) putchar(str[i++]); }int main(int argc, char* argv[]) { char str[100] = "ABC"; put_string(str); putchar('\n'); </stdio.h>…

文字列の長さを調べる

明解C言語 入門編 > 9. 文字列の基本 > 文字列の長さを調べる C #include <stdio.h> #include <string.h>unsigned str_length(const char str[]) { unsigned len = 0; while (str[len]) len++; return len; }int main(int argc, char* argv[]) { char str[100] = "ABC"; printf</string.h></stdio.h>…

文字列の配列

明解C言語 入門編 > 9. 文字列の基本 > 文字列の配列 C #include <stdio.h> int main(int argc, char* argv[]) { int i; char cs[][6] = {"Turbo", "NA", "DOHC"}; for (i = 0; i < 3; i++) printf("cs[%d] = \"%s\"\n", i, cs[i]); return 0; }実行結果 R:\>lesson0</stdio.h>…

文字列を読み込み、書式化して表示

明解C言語 入門編 > 9. 文字列の基本 > 文字列を読み込み、書式化して表示 C #include <stdio.h> int main(int argc, char* argv[]) { char str[40]; printf("文字を入力してください:"); scanf("%s", str); printf("%s\n", str); /* そのまま */ printf("%5s\n", s</stdio.h>…

文字列リテラルの大きさを表示する

明解C言語 入門編 > 9. 文字列の基本 > 文字列リテラルの大きさを表示する C #include <stdio.h> int main(int argc, char* argv[]) { printf("sizeof(\"123\") = %u\n", (unsigned)sizeof("123")); printf("sizeof(\"AB\\tC\") = %u\n", (unsigned)sizeof("AB\tC"))</stdio.h>…

標準入力からの入力を標準出力にコピーする

明解C言語 入門編 > 8. いろいろなプログラムを作ってみよう > 標準入力からの入力を標準出力にコピーする C #include <stdio.h>int main(int argc, char* argv[]) { int ch; while ((ch = getchar()) != EOF) putchar(ch); return 0; }実行結果 R:\>lesson067\Proje</stdio.h>…

階乗を求める (再帰)

明解C言語 入門編 > 8. いろいろなプログラムを作ってみよう > 階乗を求める (再帰) C #include <stdio.h>int factorial(int n) { if (n > 0) return (n * factorial(n -1)); return 1; }int main(int argc, char* argv[]) { int n = 3; printf("%dの階乗は%dです。\</stdio.h>…

ビットシフト

明解C言語 入門編 > 7. 基本型 > ビットシフト C #include <stdio.h>int count_bits(unsigned x) { int count = 0; while (x) { if (x & 1u) count++; x >>= 1; } return count; }int int_bits(void) { return count_bits(~0u); }void print_bits(unsigned x) { int </stdio.h>…

論理和・論理積・排他的論理和

明解C言語 入門編 > 7. 基本型 > 論理和・論理積・排他的論理和 C #include <stdio.h>int count_bits(unsigned x) { int count = 0; while (x) { if (x & 1u) count++; x >>= 1; } return count; }int int_bits(void) { return count_bits(~0u); }void print_bits(un</stdio.h>…

ビット構成を表示する

明解C言語 入門編 > 7. 基本型 > ビット構成を表示する C #include <stdio.h>int count_bits(unsigned x) { int count = 0; while (x) { if (x & 1u) count++; x >>= 1; } return count; }int int_bits(void) { return count_bits(~0u); }void print_bits(unsigned x</stdio.h>…

識別子の有効範囲を確認する

明解C言語 入門編 > 6. 関数 > 識別子の有効範囲を確認する C #include <stdio.h>int x = 700;void print_x(void) { printf("%d\n", x); }int main(int argc, char* argv[]) { int x = 800; print_x(); /* 700 */ printf("%d\n", x); /* 800 */ int i; for (i = 1; i </stdio.h>…

多次元配列の受け渡し

明解C言語 入門編 > 6. 関数 > 多次元配列の受け渡し C #include <stdio.h>void mat_add(const int ma[][3], const int mb[][3], int mc[][3]) { int i, j; for (i = 0; i < 2; i++) for (j = 0; j < 3; j++) mc[i][j] = ma[i][j] + mb[i][j]; }int main(int argc, c</stdio.h>…

逐次探索 (番兵)

明解C言語 入門編 > 6. 関数 > 逐次探索 (番兵) C #include <stdio.h>#define NINSU 5 #define FAILED -1int search(int ary[], const int key, const int no) { ary[no] = key; int i = 0; while (1) { if (ary[i] == key) break; i++; } return (i == no ? FAILED </stdio.h>…

逐次探索

明解C言語 入門編 > 6. 関数 > 逐次探索 C #include <stdio.h>#define NINSU 5 #define FAILED -1int search(const int ary[], const int key, const int no) { int i = 0; while (1) { if (i == no ) return FAILED; if (ary[i] == key) return i + 1; i++; } }int </stdio.h>…

受け取った配列への書き込み

明解C言語 入門編 > 6. 関数 > 受け取った配列への書き込み C #include <stdio.h>#define NINSU 5void int_set(int vc[], int no) { int i; for (i = 0; i < no; i++) vc[i] = i; no = 0; }int main(int argc, char* argv[]) { int i = NINSU; int j; int ary[NINSU]</stdio.h>…