プログラミング言語C K&R著の演習問題を解答していく その2
C言語のバイブルと言われる、K&Rの「プログラミング言語C」を第一章から読み進めて演習問題を記録していく。
- 作者: B.W.カーニハン,D.M.リッチー,石田晴久
- 出版社/メーカー: 共立出版
- 発売日: 1989/06/15
- メディア: 単行本
- 購入: 28人 クリック: 721回
- この商品を含むブログ (202件) を見る
第1章
- 1-10
#include <stdio.h> int main(){ int c; while((c = getchar()) != EOF){ if(c == '\t'){ printf("\\t"); }else if(c == '\b'){ printf("\\b"); }else if(c == '\\'){ printf("\\\\"); }else{ putchar(c); } } }
- 1-11
#include <stdio.h> #define IN 1 #define OUT 0 int main(){ int wordcounter, nowstatus; int c; nowstatus = OUT; wordcounter = 0; while((c = getchar()) != EOF){ if(c == '\t' || c == ' ' || c == '\n'){ nowstatus = OUT; }else if(nowstatus == OUT){ nowstatus = IN; ++wordcounter; } } printf("%d\n", wordcounter); }
- 1-12
#include <stdio.h> #define IN 0 #define OUT 1 int main(){ int nowstatus = OUT; int c; while((c = getchar()) != EOF){ if(c == ' ' || c == '\t' || c == '\n'){ if(nowstatus == IN){ printf("\n"); nowstatus = OUT; } }else{ nowstatus = IN; putchar(c); } } }
- 1-13
#include <stdio.h> #define IN 0 #define OUT 1 int main(){ int wordsize[10]; int c; int i,j,k,l; int wordstatus; wordstatus = OUT; i = 0; for(j = 0; j < 10; j++){ wordsize[j] = 0; } while((c = getchar()) != EOF){ if(c == ' ' || c == '\t' || c == '\n'){ if(wordstatus == IN){ if(i <= 9){ wordsize[i] += 1; }else{ wordsize[9] += 1; } wordstatus = OUT; i = 0; } }else{ wordstatus = IN; i += 1; } } for(k = 0; k < 10; k++){ l = 0; if(wordsize[k] != 0){ printf("%2d:%d",k ,wordsize[k]); while( l < wordsize[k]){ printf("*"); l += 1; } printf("\n"); } } }
- 1-13-2
#include <stdio.h> #define IN 0 #define OUT 1 int main(){ int wordsize[10]; int c; int i,j,k,l,m; int wordstatus; wordstatus = OUT; i = 0; for(j = 0; j < 10; j++){ wordsize[j] = 0; } while((c = getchar()) != EOF){ if(c == ' ' || c == '\t' || c == '\n'){ if(wordstatus == IN){ if(i <= 9){ wordsize[i] += 1; }else{ wordsize[9] += 1; } wordstatus = OUT; i = 0; } }else{ wordstatus = IN; i += 1; } } int output[50]; for(k = 0; k < 50; k++){ output[k] = 0; } for(l = 0; l < 10; l++){ if(wordsize[l] >= 5){ output[l] = 1; } if(wordsize[l] >= 4){ output[10 + l] = 1; } if(wordsize[l] >= 3){ output[20 + l] = 1; } if(wordsize[l] >= 2){ output[30 + l] = 1; } if(wordsize[l] >= 1){ output[40 + l] = 1; } } for(m = 0; m < 50; m++){ if(output[m] == 1){ printf("*"); }else{ printf(" "); } if((m % 10) == 9){ printf("\n"); } } printf("0123456789\n"); }
- 1-14
#include <stdio.h> int main(){ int ascii[128]; int c; int count; int i,j,k; for(i = 0; i < 128; ++i){ ascii[i] = 0; } while((c = getchar()) != EOF){ ascii[c] += 1; } for(j = 0; j< 128; ++j){ if(ascii[j] > 0){ count = ascii[j]; printf("%c:", j); for(k = 0; k < count; k++){ printf("*"); } printf("\n"); } } }
- 1-15
#include <stdio.h> float changeunit(float f); int main(){ float fahr, selsius; int lower, upper, step; lower = 0; upper = 300; step = 20; fahr = lower; printf("fahr-selsius conversion table\n"); while(fahr <= upper){ printf("%3.0f %6.1f\n", fahr, changeunit(fahr)); fahr += step; } } float changeunit(float f){ float v; v = ((5.0/9.0) * (f - 32.0)); return v; }