演習4-6 K&R プログラミング言語C

4.3 の電卓プログラムを修正する。 演習4-6 A-Z の各文字を変数とする。R は最近に印字された値を保存する特別な読込専用変数とする。 main 関数に変数を扱う処理を追加する。 /* 省略 */ case ‘w’: /* write variable */ push(type); break; case ‘r’: /* read variable */ push(type); break; case ‘\n’: recentPrintedValue = pop(); assign_var(recentPrintedValue, ‘R’); /* 最近に印字された値を保存する */ printf("\t%.8g\n", recentPrintedValue); break; default: if (type >= ‘A’ && type <= ‘Z’) { op2 = pop(); if ((int) [...]…

続きを読む

演習4-5 K&R プログラミング言語C

4.3 の電卓プログラムを修正する。 演習4-5 math.h をインクルードする。 #include <math.h> main 関数に各演算のケースを追加する。 /* 省略 */ double op1, op2; /* 省略 */ case ‘s’: /* sin */ push(sin(pop())); break; case ‘c’: /* cos */ push(cos(pop())); break; case ‘t’: /* tan */ push(tan(pop())); break; case ‘r’: /* sqrt */ push(sqrt(pop())); break; case ‘e’: /* exp */ push(exp(pop())); break; case ‘l’: [...]…

続きを読む

演習4-4 K&R プログラミング言語C

4.3 の電卓プログラムを修正する。…

続きを読む

C言語での2進数出力関数

よくわかる! 実践Cプログラミング(日経BPパソコンベストムック)の33ページの2進数出力関数 putBinary の動作について調べてみる。…

続きを読む

4.3 外部変数, 演習4-3 K&R プログラミング言語C

4.3 外部変数 演習4-10まで以下の電卓プログラムを修正していく。 #include <stdio.h> #include <stdlib.h> /* atof() 用 */ #define MAXOP 100 /* 被演算子数、演算子の最大サイズ */ #define NUMBER ’0′ /* 数字があったという記号 */ int getop(char []); void push(double); double pop(void); /* 逆ポーランド電卓プログラム */ int main(int argc, char *argv[]) { int type; double op2; char s[MAXOP]; while ((type = getop(s)) != EOF) { switch (type) { [...]…

続きを読む

4.2 非整数を返す関数, 演習4-2 K&R プログラミング言語C

4.2 非整数を返す関数 atof が返す double の値は return される際に int に変換される。 これは、atoi が int を返す関数であるため。 そこで、コンパイラが警告を出さないように明示的にキャスト(型変換)している。 /* atoi : atof を使って文字列 s を整数に変換する */ int atoi(char s[]) { double atof(char s[]); return (int) atof(s); } 演習4-2 #include <stdio.h> #include <ctype.h> double atof(char str[]); int main(int argc, char *argv[]) { char *str1 = "1000"; char *str2 = [...]…

続きを読む

演習4-1 K&R プログラミング言語C

演習4-1 行の最後尾、探索文字の後ろから探していく。 #include <stdio.h> #include <string.h> #define MAXLINE 1024 int getline(char line[], int max); int strrindex(char source[], char searchfor[]); char pattern[] = "hello"; int main(int argc, char *argv[]) { char line[MAXLINE]; int i; int pos = 0; int found = 0; while (getline(line, MAXLINE) > 0) { if ((pos = strrindex(line, pattern)) >= 0) { [...]…

続きを読む

演習3-6 K&R プログラミング言語C

演習3-6 文字配列に文字を入れる毎に幅 w の値をデクリメントしてゆき、最後に w の数だけスペースを入れる。 #include <stdio.h> #include <string.h> #define MAX 1024 void itoa(int n, char s[], int w); void reverse(char s[]); int main(int argc, char *argv[]) { char str1[MAX]; char str2[MAX]; int n; n = 124; itoa(n, str1, 5); itoa(n, str2, 8); printf("%s%s\n", str1, str2); return 0; } void itoa(int n, char s[], [...]…

続きを読む

演習3-5 K&R プログラミング言語C

演習3-5 #include <stdio.h> #include <string.h> #define MAX 1024 void itob(unsigned n, char s[], int b); void reverse(char s[]); int main(int argc, char *argv[]) { char str1[MAX]; char str2[MAX]; char str3[MAX]; unsigned n; n = 158; itob(n, str1, 2); itob(n, str2, 8); itob(n, str3, 16); printf("itob(158, s, 2) => %s\n", str1); printf("itob(158, s, 8) => %s\n", [...]…

続きを読む

演習3-4 K&R プログラミング言語C

演習3-4 int の有効範囲を調べる。 #include <stdio.h> #include <limits.h> int main(int argc, char *argv[]) { printf("INT_MIN => %12d\n", INT_MIN); printf("INT_MAX => %12d\n", INT_MAX); return 0; } INT_MIN の符号を反転させると INT_MAX を超えてしまう。 $ ./intsize INT_MIN => -2147483648 INT_MAX => 2147483647 値が正の場合と負の場合とで処理を分けて対処する。 #include <stdio.h> #include <limits.h> #include <string.h> #define MAX 1024 void itoa(int n, char s[]); void reverse(char s[]); int [...]…

続きを読む
Page 3 of 41234
↑ページの先頭へ