1 / 11

情報技術 ~プログラミング演習( C 言語)~

情報技術 ~プログラミング演習( C 言語)~. 2011 年 1 月 13 日 笠井俊信. 今後の予定. 1 月 20 日 通常講義 1 月 27 日 試験 1 月 20 日に試験当日の形式や流れなどについて説明します. 演習問題1(復習). #include <stdio.h> void main(void) { int a, kai; scanf(“%d”, &a); if(a>=0){ kai=a; } else{ kai=a*(-1); }

adeola
Télécharger la présentation

情報技術 ~プログラミング演習( C 言語)~

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 情報技術~プログラミング演習(C言語)~ 2011年1月13日 笠井俊信

  2. 今後の予定 • 1月20日 通常講義 • 1月27日 試験 • 1月20日に試験当日の形式や流れなどについて説明します

  3. 演習問題1(復習) #include <stdio.h> void main(void) { int a, kai; scanf(“%d”, &a); if(a>=0){ kai=a; } else{ kai=a*(-1); } printf(“%d\n”, kai); } • 数値を入力する • (条件分岐):絶対値を求める • もし数値が正または零ならば • その数値が絶対値 • そうでないなら数値と-1の積が絶対値 • 絶対値を出力

  4. 演習問題2(復習) #include <stdio.h> void main(void) { int a, count; scanf("%d", &a); for(count=a;count>=0;count--){ printf("%d\n",count); } printf("カウントダウン終了\n"); } • 数値を入力 • カウンタに入力した数値を設定 • (繰り返し)カウンタの値が正または零である限り • カウンタの値を出力 • カウンタの値を1だけ減らす • カウントダウン終了のメッセージを出力

  5. 演習問題2’(復習) #include <stdio.h> void main(void) { int count; scanf("%d", &count); while (count>=0){ printf("%d\n",count); count--; } printf("カウントダウン終了\n"); } • 数値を入力 • カウンタに入力した数値を設定 • (繰り返し)カウンタの値が正または零である限り • カウンタの値を出力 • カウンタの値を1だけ減らす • カウントダウン終了のメッセージを出力

  6. 演習問題1 問題:  10のべき乗を100~105まで求めて表示せよ。 発展:  10のべき乗を100~10nまで求めて表示せよ。 ※nは入力させる 出力イメージ 10^0 = 1 10^1 = 10 10^2 = 100 10^3 = 1000 10^4 = 10000 10^5 = 100000

  7. 演習問題1(Cプログラム) #include <stdio.h> void main(void) { int pow, i; pow=1; for (i=0;i<=5;i++){ printf("10^%d=%d\n", i, pow); pow = pow * 10; } } • カウンタiの値を0に設定 • 変数POWの値を1に設定 • (繰り返し)iの値が5以下である限り • POWを10のi乗として出力 • POWにPOWと10の積を代入 • iを1増やす i=0; while(i<=5){ printf("10^%d=%d\n", i, pow); pow = pow * 10; i++; }

  8. 演習問題(段階的に) • 段階1:入力された西暦年がうるう年か? • "2000"と入力すると"うるう年"と出力 • 段階2:1900年から入力された西暦年の前年までにうるう年が何回あったか? • "2000"と入力すると"24回"と出力 • 段階3:入力された年月日が何曜日か? • 1900年以降回答可能,1900年1月1日は月曜日 • "2006"年"7"月"11"日と入力すると"火曜日"と出力

  9. 演習問題(段階1) • 入力された西暦年がうるう年か? • うるう年の条件 • 4で割り切れる年は基本的にうるう年 • 100で割り切れる年はうるう年ではない • 400で割り切れる年はうるう年 「余り」演算子: % 例: x%4 比較演算子: ==(等しい?) !=(等しくない?) 例: x%4 == 0

  10. 演習問題(段階1) アルゴリズム例 • 数値を入力(year) • yearは4で割り切れるか? • 割り切れるならば,100で割り切れるか? • 割り切れるならば400で割り切れるか? • 割り切れるならうるう年 • 割り切れないならうるう年ではない • 割り切れないならばうるう年 • 割り切れないならばうるう年ではない

  11. 演習問題(段階1) int year; printf("何年? "); scanf("%d", &year); if(year % 4 != 0) printf(“%d年はうるう年ではない", year); else if(year % 100 == 0) if(year % 400 == 0) printf("%d年はうるう年", year); else printf("%d年はうるう年ではない", year); else printf(“%d年はうるう年", year);

More Related