1 / 24

演算、整数型と浮動小数点型

第3回[平成16年4月27日(火)]:PN04ー03 . ppt. 演算、整数型と浮動小数点型. 今日の内容. 1 復習 2 加減・乗除演算子 3 2項、単項演算子 4 浮動小数点型 5 浮動小数点型の入出力 6 今日の課題. ppt の印刷を 忘れずに持参 して下さい!. 皆さんの質問から. 変数名の中にキーワードが 含まれていても良いか? → OKです。例えば、 if はキーワードですが、 ta if u  は変数名としてOKです。 scanf("%d , %d",&a,&b); という文で、 , の意味がよく分からない。

greg
Download Presentation

演算、整数型と浮動小数点型

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. 第3回[平成16年4月27日(火)]:PN04ー03.ppt第3回[平成16年4月27日(火)]:PN04ー03.ppt 演算、整数型と浮動小数点型 今日の内容 1 復習 2 加減・乗除演算子 3 2項、単項演算子 4 浮動小数点型 5 浮動小数点型の入出力 6 今日の課題

  2. pptの印刷を 忘れずに持参して下さい! 皆さんの質問から • 変数名の中にキーワードが含まれていても良いか? → OKです。例えば、ifはキーワードですが、 taifu は変数名としてOKです。 • scanf("%d,%d",&a,&b); という文で、,の意味がよく分からない。 → キーボードからの入力で、数値の直後に , を区切りとして入れるようにするか否かの指定です。 ,有りでの入力 ,無し 12, 13 12 13

  3. 出力文 入力文 変数宣言 式と代入文 書式指定(見出し) サンプルプログラム(変数、代入) 復習 List1-12:二つの整数の入力と和の計算と表示 #include <stdio.h> int main(void){ int n1, n2; int wa; puts("two integers ? "); printf("整数1:"); scanf("%d",&n1); printf("整数2:"); scanf("%d",&n2); wa = n1 + n2; printf("それらの和は%dです。\n", wa); return(0); }

  4. サンプルプログラム(演算) List2-1:二つの整数値の和・差・積・商・剰余の表示 int vx, vy; puts("以下に二つの整数を入力せよ"); scanf("%d%d", &vx, &vy); printf("vx + vy = %d\n", vx + vy); printf("vx - vy = %d\n", vx - vy); printf("vx * vy = %d\n", vx * vy); printf("vx / vy = %d\n", vx / vy); printf("vx %% vy = %d\n",vx % vy); return(0);

  5. 式の表現(加減、乗除演算子) 演算記号 足算:+ 引算:ー 掛算:* 割算:/ 剰余:% 複雑な式:カッコ( ) 4*3+2-5/2+3*2 → (4*(3+2)-(5/2+3))*2→ 4+-3  → 10/3+2 → 10/3/2 → 10/3*2 → 10%4*-2→ 計算の順序 ( ) 符号のー */% +ー 同順位の場合、左から実行される

  6. 商と余りの計算について (整数型)/(整数型) 整数化(切り捨て) 12/6 → , 13/5 → , (-5)/2 → 余りの計算(剰余演算子 % ) 12%6 → , 13%5 → , 3%7 → 9 % 4→-9 % 4→ 9 % -4 →–9 % -4→

  7. 書式付きの出力文(複数の値) printf("1日は%d秒\n",24*60*60); 文字列はそのまま 書式を指定 変換指定子による変換 見出し付きの出力となる 1日は86400秒Ã • ◎複数の値を見出し付きで出力するには? • 例)変数 n1,n2,n3 の値を出力する

  8. 書式付きの出力文(複数の値) scanf("%d %d %d",&n1,&n2,&n3); それぞれ対応 12 31 15 それぞれ対応 printf( "n1=%d, n2=%d, n3=%d",n1,n2,n3); 出力結果 n1=12, n2=31, n3=15 printf("n1=%d, n2=%d",n1 ); printf("n1=%d, n2= ",n1,n2);

  9. 演習(商と剰余) • n個のリンゴがある。これを一人につき4個づつ配ると、何人に配ることができるか。また、何個残るか? プログラムを完成させよ。 int n, ninzuu, nokori; printf("リンゴの数は:"); scanf( ); ninzuu = ; nokori = ; printf(" ", );

  10. 演習(商と剰余) • 一箱に5個のリンゴが入る箱がある。今、n個のリンゴがある場合、何箱必要か? int main(void) { int hako, n; printf("リンゴの数は:"); scanf( ); = ; printf(" ", ); return(0); }

  11. 演算子の分類(オペランド数による) 加減演算子 足算:+  引算:ー 単純代入演算子 代入:= 乗除演算子 掛算:*  割算:/  剰余:% 2項演算子:(オペランド)(演算子)(オペランド) 単項+-演算子 符号不変:+符号反転:ー (演算子)(オペランド)

  12. サンプルプログラム List2-6[変形]:整数と浮動小数点数 整数化 浮動小数点型 int nx; double dx; float fx; nx = 9.99; dx = 9.99; printf("int型 nx=%d\n", nx); printf(" nx/2=%d\n", nx/2); printf("double型 dx=%f\n", dx); printf(" dx/2.0=%f\n", dx/2.0); scanf("%f%lf", &fx, &dx); printf("fx=%f, dx=%f\n", fx, dx);

  13. 整数と浮動小数点数 • 整数[変換指定子:%d] • 誤差なく内部表現される 10進数 → 2進数に変換 • 表現範囲が狭い(せいぜい10桁) • 小数点以下は無い ( … -2 -1 0 1 2 …) short int : 2バイト(16ビット) -32,768 ~ 32,767 int, long int : 4バイト(32ビット)-2,147,483,648~2,147,483,647 unsigned int : 4バイト(32ビット)0 ~ 4,294,967,295

  14. 整数と浮動小数点数 ・浮動小数点数[変換指定子:%f,%lf,%Lf] • 指数と仮数による内部表現:123.4→0.1234×103 • 表現範囲が広いが、誤差を伴う(有効桁数は数桁) • 小数点がある: 1.23 12. .56 1.2e2 5E15 float : 4バイト 1.2×10-38~ 3.4×1038 [有効数字7桁] double : 8バイト2.2×10-308~ 1.7×10308[有効数字15桁] long double : 10バイト3.4×10-4932~ 1.1×104932[有効数字18桁] [入力:  、出力:  ] [入力:   、出力:  ] [入力:   、出力:   ] %f%f %lf %f %Lf%Lf

  15. 演習(整数型と浮動小数点型) 問. 数値として適当か。適当ならば、整数か浮動小数点数か述べよ。 (1) -666 (2) -666. (3) -666.0 (4) -.666e3 (5) 483.500 (6) 16667E+6 (7)-3e6 (8) .09E0.5

  16. 変換指定子(%f) 浮動小数点数を数字(数文字)として出力 printf("%f",1.5*2+1.8*4); 文字(%f)の代わりに数値を数字にして出力 浮動小数点値を与える計算式 人間が読める数文字 (小数点あり) として表示する 計算結果 → 10.2 パソコンのメモリ内では(0.102×102)10の浮動小数点数で表現されている %f による変換

  17. 演習 • 次のプログラムの誤りを正せ #include <stdio.h> int main(void){ double dx; printf("dxの値は?"); scanf("%f",dx); printf("%dの2倍は%f\n",dx,2.0*dx); return(0); }

  18. 演習 • 画面に、半径2.5の円の面積を表示するプログラムを完成せよ(値だけでよい)。 #include <stdio.h> int main(void) { printf(); return(0); }

  19. 演習 • 半径rを入力し、その円周の長さを見出し付きで表示するプログラムを作成せよ。 #include <stdio.h> int main(void){ return(0); }

  20. 変換指定子(%f,%lf,%Lf) 浮動小数点数の入力 float fx; double dx; long double ldx; scanf("%f%lf %Lf", &fx, &dx, &ldx); printf("fx=%f, dx=%f, ldx=%Lf",fx, dx, ldx); 処理システムに依存するので注意すること

  21. 演習(数値の入力) 問.次の様に変数が宣言されたとき、各変数へ入力する為のscanf関数を書け int a,b,c; float x,y,z; double p,q,r; (1)a (2) x (3) q (4) a b c (5) a x y (6) p q x (7) c z r

  22. 演習(数値の見出し付き出力) 問.次の様に変数を見出し付きで出力する為のprintf関数を書け int a,b; float x,y; double p,q; (1)ab (2) a x y (3) p q x (4) b y q

  23. 今日の課題 入出力はすべて浮動小数点型とする • 箱の縦、横、高さを入力し、その箱の体積を出力するプログラムを作成せよ double length, width, height, volume; printf("縦、横、高さを入力せよ => "); scanf(" ",); volume = ; printf("体積は    です。\n",); 提出用紙に質問・意見・感想・提案等も書いて下さい

  24. P入門(第3回)は終了です。次回(第4回)は5月11日P入門(第3回)は終了です。次回(第4回)は5月11日 続いて、P演習に入ります。では、しばらく休憩します。

More Related