k&R

A-Eのような表記を ABCDEに展開するプログラム

c k&R

K&Rに出ていた問題を解いてみた. static int expand(char *dest, const char *src) { int i, j, dest_index; int length; char prev, next; length = strlen(src); dest_index = 0; for (i = 0; i < length; i++) { if (src[i] == '-' && (i != 0 && i != le…

プログラミング言語 C 3.5

c k&r

符号なし整数 nを b進数に変換し、文字列 sに格納する関数 htob(n, s, b)を書け、 という問題。どんどん bで割っていって、余りを sに格納していって、最後に sを 反転させればいいはず。 #include <stdio.h> #include <string.h> #include <assert.h> static void htob(int n, char *s, i</assert.h></string.h></stdio.h>…

プログラミング言語 C 3.4

c k&r

本に掲載されている atoiでは INT_MINがうまく扱えないのでその理由と その問題を修正したものを書けという問題。うまくいかない理由は符号の 処理を最後にしているから、INT_MINを求めるのに -(INT_MAX + 1)となって しまって答えがおかしくなってしまう。…

プログラミング言語 C 3.3

c k&r

a-zなんかを a....zに展開する関数 expand(s1, s2)を書けという問題。s1がソースで s2がデスティネーションです。難しいと思ったが結構すぐできましたね。問題がある かもしれないですね。 #include <stdio.h> #include <string.h> #include <ctype.h> #include <assert.h> static void expand(char</assert.h></ctype.h></string.h></stdio.h>…

プログラミング言語 C 3.2

c k&r

改行文字などの見えない文字を '\n'(2文字)などに変換しながらコピーする escape(src, dest)を作成しろという問題。ただし switchを使うこと。 全部のエスケープ文字をしていないけど、この延長でなんとかなるでしょう。 #include <stdio.h> #include <string.h> #include <assert.h> stat</assert.h></string.h></stdio.h>…

プログラミング言語 C 3.1

c k&r

本に掲載されている二分探索よりも条件分岐が少ないものを書けという 問題。なんか返ってややこしくなる感じがあるので、普通に教科書に書いて いる方を使った方が良さげな気はしますね。 #include <stdio.h> #include <assert.h> static int binary_search(int x, int *a, int </assert.h></stdio.h>…

プログラミング言語 C 2.10

c k&r

三項演算子を使って、tolowerを書けという問題。 #include <stdio.h> #include <assert.h> static int my_lower(int c); static void test_my_lower(void); int main(void) { test_my_lower(); } static void test_my_lower() { assert(my_lower('0') == '0'); assert(my_lower(</assert.h></stdio.h>…

プログラミング言語 C 2.9

c k&r

ある整数 xに対して、 x & (x-1)が最も右の "1"であるビットを削除するという 事実を使って、bitcountの高速版を作れという問題。x & (x-1)がこうなる理由は x & (x-1)をすると、最も右の 1ビット以下のビットが反転するため、それと論理積を とるとすべて 0…

プログラミング言語 C 2.7

c k&r

xのビット位置 pから nビットを反転する関数 invert(x, p, n)を書けと いう問題。print_binaryというのは 2進数で表示する関数です。 #include <stdio.h> #include <assert.h> #include "util.h" static unsigned int invert(unsigned x, int p, int n); static void test_inver</assert.h></stdio.h>…

プログラミング言語 C 2.6

c k&r

位置 pから始まる nビットに yの右端の nビットをセットして、他は ビットはそのままにした xを返す setbits(x, p, n, y)を書け、という問題。 何か問題の日本語おかしい気がするのは、僕の語彙力の問題なんですかね〜? #include <stdio.h> #include <stdlib.h> #include <assert.h> stat</assert.h></stdlib.h></stdio.h>…

プログラミング言語 C 2.5

c k&r

文字列 s2の任意の文字と等しい文字列 s1の最初の文字位置を返す any(s1, s2)を書けという問題。一致しない場合は -1を返す。 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> static int any(char *s1, char *s2); static void test_any(); int main(int argc, ch</assert.h></stdlib.h></string.h></stdio.h>…

プログラミング言語 C 2.4

c k&r

文字列 s2の中の任意の文字に等しい文字を s1から除去するような squeeze(s1, s2)を書きなさい、という問題。しかし Cだと特にテストを 書いてみるって重要な気がしてきた。なんとなくうまくいくときもある 言語なのでそこはしっかりとしないとね。 #include <stdio.h></stdio.h>…

プログラミング言語 C 問 2.3

c k&r

Cを勉強する必要が出てきたので、再勉強。 #include<stdio.h> #include<stdlib.h> #include<ctype.h> #include<assert.h> static int htoi (char *s); static void test_htoi(void); int main(int argc, char **argv) { int answer; if (argc != 2) { fprintf(stderr, "Invalid argument number\n")</assert.h></ctype.h></stdlib.h></stdio.h>…