#include "stdio.h"
#include <math.h>
#pragma warning(disable:4996)
int main()
{
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, "%lf¥n", &start_x );
printf( "step_x =" );
fgets( buf, 256, stdin );
sscanf_s( buf, "%lf¥n", &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 * x;
printf( "x= %f, y= %f¥n", x, y );
fprintf( fp, "x=, %f, y=, %f¥n", x, y );
}
fprintf( stderr, "file d:¥¥data.csv created¥n" );
fclose( fp );
return 0;
}
•7
Cプログラムはメイン関数から
実行開始
プログラムは順次実行
変数 x, y, buf, i, start_x,
step_x, fp をメモリエリア中に確保
printf でメッセージを表示
fgets でキーボードから1行を読み込み
sscanf で数値を読み取って変数に格納
printf でメッセージを表示
fgets でキーボードから1行を読み込み
sscanf で数値を読み取って変数に格納
20回の繰り返し (i = 0, 1, ... 19)
x の値から
( 9.8 / 2.0 ) * x * x
を求め,y に書き込む