include stdafx h include math h pragma warningdisable
#include "stdafx. h" #include <math. h> #pragma warning(disable: 4996) int _tmain() { double x; double y; char buf[256]; int i; double start_x; double step_x; FILE* fp; printf( "start_x =" ); fgets( buf, 256, stdin ); sscanf_s( buf, "%lfn", &start_x ); printf( "step_x =" ); fgets( buf, 256, stdin ); sscanf_s( buf, "%lfn", &step_x ); fp = fopen( "d: \data. csv", "w" ); for( i = 0; i < 20; i++ ) { x = start_x + ( i * step_x ); y = ( 9. 8 / 2. 0 ) * x; printf( "x= %f, y= %fn", x, y ); fprintf( fp, "x=, %f, y=, %fn", x, y ); } fprintf( stderr, "file d: \data. csv createdn" ); fclose( fp ); ファイルへの書き出し return 0; } を行っている部分 4 キーボードからの データ読み込みを 行っている部分 計算を行っている部分
#include "stdafx. h" #include <math. h> #pragma warning(disable: 4996) Cプログラムはメイン関数から int _tmain() 実行開始 { double x; double y; 変数 x, y, buf, i, start_x, char buf[256]; step_x, fp をメモリエリア中に確保 int i; double start_x; double step_x; FILE* fp; プログラムは順次実行 printf( "start_x =" ); fgets( buf, 256, stdin ); sscanf_s( buf, "%lfn", &start_x ); printf( "step_x =" ); fgets( buf, 256, stdin ); この部分は繰り返し処理 sscanf_s( buf, "%lfn", &step_x ); fp = fopen( "d: \data. csv", "w" ); (for 文による繰り返し) for( i = 0; i < 20; i++ ) { x = start_x + ( i * step_x ); y = ( 9. 8 / 2. 0 ) * x; printf( "x= %f, y= %fn", x, y ); fprintf( fp, "x=, %f, y=, %fn", x, y ); } fprintf( stderr, "file d: \data. csv createdn" ); fclose( fp ); return 0; } 5
#include "stdafx. h" #include <math. h> Cプログラムはメインの関数から #pragma warning(disable: 4996) int _tmain() 実行開始 { double x; double y; char buf[256]; int i; double start_x; printf でメッセージを表示 double step_x; fgets でキーボードから1行を読み込み FILE* fp; sscanf で数値を読み取って変数に格納 printf( "start_x =" ); fgets( buf, 256, stdin ); sscanf_s( buf, "%lfn", &start_x ); printf でメッセージを表示 fgets でキーボードから1行を読み込み printf( "step_x =" ); sscanf で数値を読み取って変数に格納 fgets( buf, 256, stdin ); sscanf_s( buf, "%lfn", &step_x ); fp = fopen( "d: \data. csv", "w" ); for( i = 0; i < 20; i++ ) { 20回の繰り返し (i = 0, 1, . . . 19) x = start_x + ( i * step_x ); y = ( 9. 8 / 2. 0 ) * x; x の値から printf( "x= %f, y= %fn", x, y ); ( 9. 8 / 2. 0 ) * x fprintf( fp, "x=, %f, y=, %fn", x, y ); を求め,y に書き込む } fprintf( stderr, "file d: \data. csv createdn" ); fclose( fp ); return 0; 6 } → 実行の流れが今日の授業のポイント
#include "stdafx. h" #include <math. h> #pragma warning(disable: 4996) int _tmain() { double x; double y; char buf[256]; int i; double start_x; 整数は, 5, -3, 0 など double step_x; FILE* fp; printf( "start_x =" ); fgets( buf, 256, stdin ); sscanf_s( buf, "%lfn", &start_x ); printf( "step_x =" ); 文字は, 1, 0, 3, -, a など fgets( buf, 256, stdin ); 数字(1, 0, 3 など)も文字の一種 sscanf_s( buf, "%lfn", &step_x ); fp = fopen( "d: \data. csv", "w" ); for( i = 0; i < 20; i++ ) { 浮動小数を扱う double 型 x = start_x + ( i * step_x ); y = sin( x ); 3. 14, -1. 414, 5, 0, -3 など printf( "x= %f, y= %fn", x, y ); (5, 0, -3 などの整数も浮動小数の一種) fprintf( fp, "x=, %f, y=, %fn", x, y ); } fprintf( stderr, "file d: \data. csv createdn" ); ファイルポインタ fclose( fp ); return 0; ファイル操作に使う変数 7 } 変数は4種類使っている 整数を扱う int 型 文字を扱う char 型
#include "stdafx. h" #include <math. h> #pragma warning(disable: 4996) int _tmain() { double x; double y; char buf[256]; int i; double start_x; 整数は, 5, -3, 0 など double step_x; FILE* fp; printf( "start_x =" ); fgets( buf, 256, stdin ); sscanf_s( buf, "%lfn", &start_x ); printf( "step_x =" ); 文字は, 1, 0, 3, -, a など fgets( buf, 256, stdin ); 数字(1, 0, 3 など)も文字の一種 sscanf_s( buf, "%lfn", &step_x ); fp = fopen( "d: \data. csv", "w" ); for( i = 0; i < 20; i++ ) { 浮動小数を扱う double 型 x = start_x + ( i * step_x ); y = sin( x ); 3. 14, -1. 414, 5, 0, -3 など printf( "x= %f, y= %fn", x, y ); (5, 0, -3 などの整数も浮動小数の一種) fprintf( fp, "x=, %f, y=, %fn", x, y ); } fprintf( stderr, "file d: \data. csv createdn" ); double 型の変数については, ファイルポインタ fclose( fp ); sscanf では 「%lf」を,printf , fprintf return 0; ファイル操作に使う変数 では「%f」を, 使う決まりになっている 8 } 変数は4種類使っている 整数を扱う int 型 文字を扱う char 型
「ビルド」による実行ファイルの生成 #include "stdafx. h" #include <math. h> #pragma warning(disable: 4996) int _tmain() { double x; double y; char buf[256]; int i; double start_x; double step_x; FILE* fp; printf( "start_x =" ); fgets( buf, 256, stdin ); sscanf_s( buf, "%lfn", &start_x ); printf( "step_x =" ); fgets( buf, 256, stdin ); sscanf_s( buf, "%lfn", &step_x ); fp = fopen( "d: \data. csv", "w" ); for( i = 0; i < 20; i++ ) { x = start_x + ( i * step_x ); y = ( 9. 8 / 2. 0 ) * x; printf( "x= %f, y= %fn", x, y ); fprintf( fp, "x=, %f, y=, %fn", x, y ); } fprintf( stderr, "file d: \data. csv createdn" ); fclose( fp ); return 0; } C++ソースファイル 「ビルド」により実行ファイル等 が生成される 実行ファイル ビルド Microsoft Visual その他,ビルドの結果 Studio C++ として生成されるファイル 構文エラーがあると, 「ビルド」時にエラーメッセージが表示される (実行ファイルは生成されない) その他のファイル 9
「消去(clean)」の操作 #include "stdafx. h" #include <math. h> #pragma warning(disable: 4996) int _tmain() { double x; double y; char buf[256]; int i; double start_x; double step_x; FILE* fp; printf( "start_x =" ); fgets( buf, 256, stdin ); sscanf_s( buf, "%lfn", &start_x ); printf( "step_x =" ); fgets( buf, 256, stdin ); sscanf_s( buf, "%lfn", &step_x ); fp = fopen( "d: \data. csv", "w" ); for( i = 0; i < 20; i++ ) { x = start_x + ( i * step_x ); y = ( 9. 8 / 2. 0 ) * x; printf( "x= %f, y= %fn", x, y ); fprintf( fp, "x=, %f, y=, %fn", x, y ); } fprintf( stderr, "file d: \data. csv createdn" ); fclose( fp ); return 0; } C++ソースファイル その他のファイル 実行ファイル ビルド Microsoft Visual その他,ビルドの結果 Studio C++ として生成されるファイル 「消去(clean)」の操作により, ビルドの結果が消える (元のソースファイル等は消えない) 10
「自由落下距離」のプログラムを実行すると データファイルが生成される #include "stdafx. h" #include <math. h> #pragma warning(disable: 4996) int _tmain() { double x; double y; char buf[256]; int i; double start_x; double step_x; FILE* fp; printf( "start_x =" ); fgets( buf, 256, stdin ); sscanf_s( buf, "%lfn", &start_x ); printf( "step_x =" ); fgets( buf, 256, stdin ); sscanf_s( buf, "%lfn", &step_x ); fp = fopen( "d: \data. csv", "w" ); for( i = 0; i < 20; i++ ) { x = start_x + ( i * step_x ); y = ( 9. 8 / 2. 0 ) * x; printf( "x= %f, y= %fn", x, y ); fprintf( fp, "x=, %f, y=, %fn", x, y ); } fprintf( stderr, "file d: \data. csv createdn" ); fclose( fp ); return 0; } データファイルに関係 している部分 ビルド 実行ファイル C++ソースファイル Microsoft Visual データファイル Studio C++ その他のファイル 自由落下距離のプログラムを実行すると, データファイル data. csv が生成される 18
#include "stdafx. h" #include <math. h> #pragma warning(disable: 4996) int _tmain() { double x; double y; char buf[256]; int i; double start_x; 前回の授業の例題1 double step_x; と同じ FILE* fp; printf( "start_x =" ); fgets( buf, 256, stdin ); sscanf_s( buf, "%lfn", &start_x ); printf( "step_x =" ); fgets( buf, 256, stdin ); sscanf_s( buf, "%lfn", &step_x ); fp = fopen( "d: \data. csv", "w" ); for( i = 0; i < 20; i++ ) { x = start_x + ( i * step_x ); y = ( 9. 8 / 2. 0 ) * x; printf( "x= %f, y= %fn", x, y ); fprintf( fp, "x=, %f, y=, %fn", x, y ); } fprintf( stderr, "file d: \data. csv createdn" ); fclose( fp ); return 0; 20 }
#include "stdafx. h" #include <math. h> #pragma warning(disable: 4996) int _tmain() ①{ double x; double y; char buf[256]; int i; double start_x; double step_x; FILE* fp; ② printf( "start_x =" ); ③ fgets( buf, 256, stdin ); ④ sscanf_s( buf, "%lfn", &start_x ); ⑤ printf( "step_x =" ); ⑥ fgets( buf, 256, stdin ); ⑦ sscanf_s( buf, "%lfn", &step_x ); ⑧ fp = fopen( "d: \data. csv", "w" ); 20回の繰り返し ⑨ ・・・ 123 for( i = 0; i < 20; i++ ) { (i = 0, 1, . . . 19) ⑩ ・・・ 124 x = start_x + ( i * step_x ); y = ( 9. 8 / 2. 0 ) * x; ⑪ ・・・ 125 printf( "x= %f, y= %fn", x, y ); ⑫ ・・・ 126 fprintf( fp, "x=, %f, y=, %fn", x, y ); ⑬ ・・・ 127 ⑭ ・・・ 128} 129 fprintf( stderr, "file d: \data. csv createdn" ); 130 fclose( fp ); 131 return 0; 132 } 21 Visual Studio C++ の ステップ実行で観察した ステップ実行の順序 → 実際に確認しよう
#include "stdafx. h" #include <math. h> int _tmain() { double x; double y; char buf[256]; int ch; printf("x="); fgets( buf, 256, stdin ); sscanf_s( buf, "%lfn", &x ); if ( x < 0 ) { printf("負なので計算できませんn"); } else { y = sqrt(x); printf("sqrt(%f)=%fn", x, y); } ch = getchar(); return 0; } 条件式 条件が成り立つ 場合に実行され る部分 条件が成り立た ない場合に実行 される部分 31
実行順 (x<0) の場合 #include "stdafx. h" #include <math. h> int _tmain() ①{ double x; double y; char buf[256]; int ch; ② printf("x="); ③ fgets( buf, 256, stdin ); ④ sscanf_s( buf, "%lfn", &x ); ⑤ if ( x < 0 ) { ⑥ printf("負なので計算できませんn"); } ⑦ else { y = sqrt(x); printf("sqrt(%f)=%fn", x, y); } ⑧ ch = getchar(); ⑨ ch = getchar(); ⑩ return 0; ⑪} 33
#include "stdafx. h" #include <math. h> int _tmain() ①{ double x; double y; char buf[256]; int ch; ② printf("x="); ③ fgets( buf, 256, stdin ); ④ sscanf_s( buf, "%lfn", &x ); ⑤ if ( x < 0 ) { printf("負なので計算できませんn"); } else { y = sqrt(x); ⑥ printf("sqrt(%f)=%fn", x, y); ⑦ } ⑧ ch = getchar(); ⑨ ch = getchar(); ⑩ return 0; ⑪} 実行順 (x≧ 0) の場合 34
比較演算の例 「左辺が右辺以上」の意味 if (age >= 20 ){ printf("You may drink alcoholic beverage. "); } else{ printf("You may not drink alcoholic beverage. "); } 41
棒グラフ #include "stdafx. h" #include <math. h> void bar( int len ) { int i; for (i=0; i<len; i++) { printf("*"); } printf("n"); return; } int _tmain() { int len; char buf[256]; int ch; printf( "len =" ); fgets( buf, 256, stdin ); sscanf_s( buf, "%dn", &len ); bar( len ); ch = getchar(); return 0; } 44
棒グラフ #include "stdafx. h" #include <math. h> void bar( int len ) { int i; for (i=0; i<len; i++) { printf("*"); bar関数 } printf("n"); プログラム実行は return; メイン関数から始まる } int _tmain() { int len; char buf[256]; int ch; printf( "len =" ); main関数 fgets( buf, 256, stdin ); sscanf_s( buf, "%dn", &len ); bar( len ); ch = getchar(); return 0; 45 } 複数の関数を含む プログラム
#include "stdafx. h" #include <math. h> 変数は2種類使っている void bar( int len ) { int i; 整数を扱う int 型 for (i=0; i<len; i++) { printf("*"); 整数は, 5, -3, 0 など } printf("n"); return; 文字を扱う char 型 } int _tmain() 文字は, 1, 0, 3, -, a など { 数字(1, 0, 3 など)も文字の一種 int len; char buf[256]; int ch; printf( "len =" ); fgets( buf, 256, stdin ); sscanf_s( buf, "%dn", &len ); bar( len ); ch = getchar(); 変数 len は int 型なので, sscanf では「%d」を使う決まりになっている return 0; 46 }
プログラムの流れ プログラムの実行開始 メイン関数 関数呼び出し bar( len ); #include "stdafx. h" #include <math. h> void bar( int len ) { int i; for (i=0; i<len; i++) { printf("*"); } printf("n"); return; } int _tmain() { int len; int ch; printf( "len=" ); scanf( "%d", &len ); bar( len ); ch = getchar(); return 0; } bar 関数 プログラムの実行終了 return; * printf, scanf の 呼び出しについては 55 図では省略
- Slides: 61