開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの無料、light版)
- 言語: C
- コンパイラ: UNIX ccコンパイラ (汎用UNIX)
『実践プログラミング 第3版』 (Steve Oualline (著)、 望月 康司 (監修) (翻訳)、 谷口 功 (翻訳)、 オライリー・ジャパン、1998年、ISBN978-4900900646) II部(単純なプログラミング)の8章(他の制御文)8.5(プログラミング実習)実習8-6を解いてみる。
実習8-6.
コード(TextWrangler)
#include <stdio.h>
#include <string.h>
int main(){
char line[100];
char result[1000];
strcpy(result,"");
int i;
printf("数値を入力: ");
fgets(line,sizeof(line),stdin);
for(i = 0; i < strlen(line); i++){
if(line[i] == '\n'){
break;
}
switch(line[i]){
case '0': strcat(result , "zero ");break;
case '1': strcat(result, "one ");break;
case '2': strcat(result,"two ");break;
case '3': strcat(result, "three ");break;
case '4': strcat(result, "four ");break;
case '5': strcat(result, "five ");break;
case '6': strcat(result, "six ");break;
case '7': strcat(result, "seven ");break;
case '8': strcat(result, "eight ");break;
case '9': strcat(result, "nine ");break;
default : strcat(result, "?");break;
}
}
printf("%s\n",result);
return (0);
}
入出力結果(Terminal)
$ cc -g -o sample sample.c $ ./sample 数値を入力: 895 eight nine five $ ./sample 数値を入力: 1234567890 one two three four five six seven eight nine zero $
0 コメント:
コメントを投稿