プログラミング演習(後半)

プログラミング演習
初級編
2004年6月17日(木)
奈良先端科学技術大学院大学
情報科学研究科
猿渡 洋 斎藤 将人 新保 仁
変数を使う(1)
#include <stdio.h>
main()
型宣言
{
代入演算
int i;
i = 2004;
printf("This year is %d\n", i);
}
変数を使う(2)
#include <stdio.h>
main()
同じ型はコンマで区切って同時に宣言可能
{
int i, j;
代入演算
i = 2004;
printf("This year is %d\n", i);
j = i + 1;
printf("Next year is %d\n", j);
}
データを読み込む
#include <stdio.h>
main()
{
int i, j;
printf("What year is this?\n"); 変数 i の値キーボード
から得る
scanf("%d", &i);
(詳細は後ほど)
printf("This year is %d\n", i);
j = i + 1;
printf("Next year is %d\n", j);
}
式と算術演算子

算術演算子
 和差積:
a + b a – b
a * b
 商:
a / b
 整数演算の商は小数点以下切り下げ
 剰余:
a % b

単項演算子
 インクリメント(1増加): a++ ++a
 デクリメント(1減少): a-- --a
括弧:()

演習問題
演習1-1 キーボードから整数を2個,順に読み込んで, それらの
四則演算と剰余を出力するプログラムを作成せよ.
演習1-2 センチメートルとインチの相互変換をするプログラムを
作成せよ.
なお,1インチは2.54cmである.
演算とデータ型(1)
#include <stdio.h>
main()
{
出力結果は
int x;
どうなる?
int y;
x = 8;
printf("diameter is %d\n", x);
y = x * 3.1415926;
printf("circumference is %d\n", y);
}
演算とデータ型(2)
#include <stdio.h>
main()
C言語では変数の型に
{
注意が必要
double x;
double y;
x = 8.0;
printf("diameter is %f\n", x);
y = x * 3.1415926;
printf("circumference is %f\n", y);
}
ここが%dの
ままだと…?
演算とデータ型(3)
#include <stdio.h>
main()
{
int
x;
double y;
x = 8;
printf("diameter is %d\n", x); 異なる型との演算時には
型変換が行われる
y = x * 3.1415926;
printf("circumference is %f\n", y);
}
書式付き入出力
整数(10進数)
不動小数点数
文字(一文字)
文字列

%d
%f
%c
%s

出力形式、桁数などを細かく指定可能



(詳細は配布資料を参照)
int, char
double, float
scanf でデータを読み込む
#include <stdio.h>
main()
{
double x;
double y;
printf("Enter diameter:\n");
scanf("%f", &x);
引数の前に「&」
printf("diameter is %f\n", x);
y = x * 3.1415926;
printf("circumference is %f\n", y);
}
if 文(1):条件分岐
#include <stdio.h>
main()
{
int i;
printf("Type any number between 1 and 10.\n");
scanf("%d", &i);
条件式が真のときのみ
直後の1文を実行
if (i < 1)
printf("%d is less than 1\n", i);
printf("end of program\n");
}
if 文(2):ブロック
#include <stdio.h>
{…} で複数の文をひとまとめ
main()
(ブロック)として扱える.
{
int i;
printf("Type any number between 1 and 10.\n");
scanf("%d", &i);
if (i < 1)
{
printf("%d is less than 1\n", i);
printf("It’s too small!\n");
}
printf("end of program\n"); 条件式が真のとき、{…} 内
}
の文が順に実行される
if 文(3):else
#include <stdio.h>
main()
{
int i;
printf("Type any number between 1 and 10.\n");
scanf("%d", &i);
if (i < 1) {
printf("%d is less than 1\n", i);
} else {
printf("%d is greater than or equal to 1\n", i);
}
printf("end of program\n");
}
条件式が偽のときはこちらを実行
if 文(4):else if
#include <stdio.h>
main()
{
int i;
printf("Type any number between 1 and 10.\n");
scanf("%d", &i);
if (i < 1) {
printf("%d is less than 1\n", i);
} else if (i > 10) {
printf("%d is greater than 10\n", i);
} else {
printf("%d is between 1 and 10\n", i);
}
}
if 文(5):条件式
例
関係演算子
大小
<
数値の等値性
==
(y % 4) == 0
非等値性
!=
(y % 4) != 0
&&
例
(i >= 1) && (i <= 10)
論理演算子
論理積(かつ)
論理積(または) ||
論理否定

!
>
<=
>=
i > (j + 1)
(i == 2) || (i == 4)
!((i < 1) || (i > 10))
演算子の優先順位に注意(詳細は資料を参照)
演習問題
演習1-4
整数を入力させ, その整数の符号が正か,負か,あ
るいは0かを判定するプログラムを作成せよ.
演習1-5
タクシーの乗車距離から運賃を算出するプログラム
を作成せよ. 料金表は次のとおり.
初乗り
2kmまで
以後加算
300mまでごとに
630円
80円
if 文:補足

if (式) 文1 else 文2
式が真の場合には文1を実行.
 偽の場合文2を実行.
式の真偽,とは?


#include <stdio.h>
main()
{
if (0) print("0\n");
if (1) printf("1\n");
if (2) printf("2\n");
}
条件式の真偽


式が真,とは "式の値が 0 でない" こと.
したがって,たとえば == は厳密に言うと,
 両辺の値が等しいときには 1,そうでないときに 0 を返す
演算子
#include <stdio.h>
main()
{
int a;
a = (2 == 1 + 1)
printf("2==1+1? %d\n", a);
printf("3==1+1? %d\n", 3 == 1 + 1);
}