2012年3月26日月曜日

開発環境

『実践プログラミング 第3版』 (Steve Oualline (著)、 望月 康司 (監修) (翻訳)、 谷口 功 (翻訳)、 オライリー・ジャパン、1998年、ISBN978-4900900646) の6章(条件文と制御文)6.10(プログラミング実習)実習6-2を解いてみる。

6-1.

else ifはまだ出てきてないのでif文のみでコードを書いてみる。

コード(TextWrangler)

#include <stdio.h>
int main(){
  float score;
  char line[1000000];
  char ch;
  printf("正解%を入力: ");
  fgets(line,sizeof(line),stdin);
  sscanf(line,"%f",&score);
  if(0 <= score && score <=60) ch = 'F';
  if(61 <= score && score <= 70) ch = 'D';
  if(71 <= score && score <= 80) ch = 'C';
  if(81 <= score && score <= 90) ch = 'B';
  if(91 <= score && score <= 100) ch = 'A';
  if(score < 0 || 100 < score) ch = '?';
  printf("成績: %c\n",ch);
  return (0);
}

入出力結果(Terminal)

$ cc -g -o c_program c_program.c
$ ./c_program
正解%を入力: 50
成績: F
$ ./c_program
正解%を入力: 70
成績: D
$ ./c_program
正解%を入力: 71
成績: C
$ ./c_program
正解%を入力: 81
成績: B
$ ./c_program
正解%を入力: 100
成績: A
$ ./c_program
正解%を入力: 101
成績: ?
$ ./c_program
正解%を入力: -5
成績: ?
$

0 コメント:

コメントを投稿