1 / 16

物理情報処理基礎実習 II 第8回

http://www.phys.ynu.ac.jp/labs/cosmic/shibata/jisshu/. 物理情報処理基礎実習 II 第8回. 制御構造 ~ continue 文・ break 文・ goto 文~ + α. (原 shara@icrr.u-tokyo.ac.jp ) 柴田 shibata@phys.ynu.ac.jp 石川 isikawak@sit.shonan-it.ac.jp. § 制御構造. プログラムを条件によって、 処理を繰り返したり、分岐させたりする。 while (繰り返し) if (分岐) for (繰り返し)

Télécharger la présentation

物理情報処理基礎実習 II 第8回

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. http://www.phys.ynu.ac.jp/labs/cosmic/shibata/jisshu/ 物理情報処理基礎実習II第8回 制御構造 ~continue文・break文・goto文~ +α (原 shara@icrr.u-tokyo.ac.jp) 柴田 shibata@phys.ynu.ac.jp 石川 isikawak@sit.shonan-it.ac.jp

  2. §制御構造 • プログラムを条件によって、処理を繰り返したり、分岐させたりする。 • while(繰り返し) • if(分岐) • for(繰り返し) • do while(繰り返し) • switch(分岐) • goto(分岐) ※ continueと、breakは、上記と併せて使用。

  3. §課題6 解答例 –プログラム– aplsrvky01% cat exercise6.c #include <stdio.h> main() { int i, n, count; printf("Input the integer number : "); scanf("%d",&n); if(n<2){ /* 2 より小さければ素数ではない。 */ printf(" Number %d is not a prime number!\n",n); }else{ count = 0; for(i=2;i<n;i++){ if(n%i == 0){ /* 余り0なら約数あり,すなわち素数ではない。 */ count ++; /* if(n%i==0) count++; と書いても同じ*/ } } if(count == 0){ printf(" Number %d is a prime number!\n",n); }else{ printf(" Number %d is not a prime number!\n",n); } } }

  4. §課題6 解答例 –実行– aplsrvky01% cc exercise6.c -o exercise6 aplsrvky01% ./exercise6 Input the integer number : -1 Number -1 is not a prime number! aplsrvky01% ./exercise6 Input the integer number : 3 Number 3 is a prime number! aplsrvky01% ./exercise6 Input the integer number : 8 Number 8 is not a prime number! aplsrvky01% ./exercise6 Input the integer number : 65537 Number 65537 is a prime number!

  5. §課題6 別解 –プログラム1– aplsrvky01% cat exercise6.c #include <stdio.h> main() { int i, n, count; printf("Input the integer number : "); scanf("%d",&n); if(n==2){ /* 2 は素数である。 */ printf(" Number %d is not a prime number!\n",n); }else if(n<2 || n%2==0){ /* 偶数は素数ではない。 */ printf(" Number %d is not a prime number!\n",n); }else{ for(i=3,count=0;i<n;i+=2){ if(n%i==0) count++; /* 余り0なら約数あり,すなわち素数ではない。 */ } if(count == 0){ printf(" Number %d is a prime number!\n",n); }else{ printf(" Number %d is not a prime number!\n",n); } } } ※奇数のみを調べることで計算数を減らしている。

  6. §課題6 別解 –プログラム2– aplsrvky01% cat exercise6another.c #include <stdio.h> main() { int i, n; printf("Input the integer number : "); scanf("%d",&n); if(n==2){ /* 2 は素数である。 */ printf(" Number %d is not a prime number!\n",n); }else if(n<2 || n%2==0){ /* 偶数は素数ではない。 */ printf(" Number %d is not a prime number!\n",n); }else{ for(i=3;n%i!=0;i+=2){ /* 余りが0(約数あり)ならばループ終了 */ ; /* ; のみは何もしないという実行文 */ } if(i<n){ printf(" Number %d is not a prime number!\n",n); }else{ printf(" Number %d is a prime number!\n",n); } } } ※条件式を剰余計算にしてしまうことで計算数を減らしている。

  7. §課題7 フローチャート 真→青分岐 偽→赤分岐 • 課題7 四則算術計算ソフト 変数 int a, b char c 入力 a,c,b c 不一致 ’+’ ’ー’ ’*’ ’/’ a+b a-b a*b b!=0 Err:Op a/b Err:Inp

  8. §continue 文 • 繰り返し処理を途中で中断して次の繰り返しに移りたい時に使用する。 • while、do while、for文内のみで使用出来る。 • while、do while文内では…continue文が存在するループの先頭に強制的に移行する(条件式判断に飛ぶ)。 • for文では…continue文が存在するループの処理2を実行した後、やはり条件式判断からループを実行する。

  9. §break 文 • 繰り返し処理等を途中で中断し、終了する。 • while、do while、for、switch文内でのみ使用。 • break文が存在するもっとも内側の制御構造({ }、ループ等)から強制的に抜け、次の文に移行する。※当然、ある条件を満たした場合にのみ使わないと、  この2つの図の様にループなどに意味が無くなる。 continue break 他の処理 他の処理

  10. §continue 文 & break 文 • 教科書p87 ex223.c • #include <stdio.h> • main() • { • int i; • for (i = 0; i < 10; i++) { • if (i < 5) {※continueもbreakもif文には関係しない! • continue;※ i<5の時はループの最初に戻る! • } else if (i > 8) { • break;※ i>8の場合はループから抜ける! • } • printf("%d\n", i); • } • }

  11. §goto 文 -I- • 対応するラベルの位置へ、強制的に移行する。 • 制御構造が入れ子構造になっている場合(while文やfor文等の中に、 更にwhile文やfor文を含むような場合)、その内側から、一気に外に抜けることが出来る。 • プログラムの可読性を著しく乱すため、上記用法以外では可能な限り使うのを避ける。 • 文法 goto ラベル1; ラベル1:※←「;」ではなく「:」に注意!

  12. §goto 文 -II- 教科書p89 ex224.c #include <stdio.h> main() { int i, j; for (i = 0; i < 10; i++) { for (j = 0; j < 10; j++) { if (i < j) { break; } else if (i * j > 35) { gotoend; } printf("(%d, %d)", j, i); } printf("\n"); } end:※ラベル、「:」に注意! printf("\nend\n"); }

  13. §ftpの使い方 • anonymous ftpサーバからファイルを持ってくる。 • aplsrvky01% ncftpftp.phys.ynu.ac.jp・・・         ※↑知能物理科のftpサーバ • ncftp> cd pub/jisshu/primer ※ディレクトリの移動 • ncftp> ls※ファイル一覧の表示 • ncftp> getファイル名    ※ファイルのダウンロード • ncftp> quit※ftpの終了・・・ • Save? (yes/no) no※bookmark保存(してもいいけど) ※試しにex224.cを持ってくる!

  14. §課題8 方程式の根 • X2ーsin(x)=0の解(x=0以外)が0.1≦x≦1.0の間にある。この区間内の関数f(x)=X2ーsin(x)の端点での値を調べて誤差10ー6以内で解を求めたい。 端点のひとつと端点の中点の区間で解の存在範囲を調べる。誤差が10ー6より大きければ、区間を半分にせばめ解の存在範囲を繰り返し調べる。収束後(誤差が10ー6以内)の反復回数と、端点・中点および関数の値を表示するプログラムを作成し、実行せよ。 ただし、端点(0.1、1.0)および誤差10ー6は入力とし、反復の最大回数は10000と限定する。また、レポートには簡単なフローチャートも添付すること。※出力書式はprintf(“x1=%15.10lf¥n”、x1); を参考にせよ。 

  15. f(x)=x2-sin(x)

  16. おまけ • 課題のヒント • 「X1<x<xm(中点)<x2でf(x1)*f(xm)<0なら、解はどこの範囲、f(x1)*f(xm)>0ならどこに」という話。 • 収束判定は、絶対値関数       if(fabs(f(x))<eps) break;などを使うとよい。#include<math.h>をつけて。 • 12月6日(火)は試験!scanfとか、printfとか、%5dだとか、%20.10lfだとか、覚えた?制御(繰り返しや分岐)のwhile文、for文やif文、 switcht文だとかも。 • 来週からは柴田先生にバトンタッチ。配列・文字列とかのお話。

More Related