メモ帳と開発者コマンドプロンプト for vs2015を使用する方法€¦ ·...
TRANSCRIPT
プログラミング応用演習~sinx,sin2x,sin3xの値(x=0~2π)を出力~
~sinx,sin2x,sin3xのグラフ(x=0~2π)をgnuplotで可視化~
“メモ帳”と“開発者コマンドプロンプトfor VS2015”を使用する方法
“メモ帳”と“開発者コマンドプロンプト for VS2015”を使用する方法①
スタートボタン→メモ帳を開いて、ソースコードを入力。1
#include<stdio.h>#include<math.h>
#define PI 3.14159
int main(void){
int i, imax = 360;double x, y1, y2, y3;
sin1x2x3x_1.c
for (i=0; i <= imax; i++) {
x = i * PI / 180;y1 = sin(x);y2 = sin(2 * x);y3 = sin(3 * x);
printf("%f %f %f %f ¥n",x ,y1 ,y2 ,y3 );}
return 0;}
ファイル→名前を付けて保存→自分のUSB内のフォルダを選択し、
ファイル名を“sin1x2x3x_1.c”ファイルの種類を“すべてのファイル”として保存。
2
“メモ帳”と“開発者コマンドプロンプト for VS2015”を使用する方法②
スタートボタン→Visual Studio 2015→開発者コマンドプロンプト for VS2015 を開く。
3
以下のコマンドを入力していく
e:(カレントドライブを変更)
cd TA¥programing¥case1_memo(カレントディレクトリを変更)
cl sin1x2x3x_1.c(cファイルをコンパイル)
sin1x2x3x_1(sin1x2x3x_1.exeを実行)
4
“メモ帳”と“開発者コマンドプロンプト for VS2015”を使用する方法③
実行結果が表示される。
1列目にx、2列目にsinx、3列目にsin2x、4列目にsin3xの値が出力される。
コマンドプロンプトは閉じずに、⑥へ。
5
“メモ帳”と“開発者コマンドプロンプト for VS2015”を使用する方法④
“sin1x2x3x_1.c”をメモ帳で開き、以下の赤線部を追加する。6
#include<stdio.h>#include<math.h>
#define PI 3.14159
int main(void){
int i, imax = 360;double x, y1, y2, y3;
FILE *data;char *data_file;
data_file="sin1x2x3x.d";data=fopen(data_file,"w");
sin1x2x3x_2.c
for (i=0; i <= imax; i++) {
x = i * PI / 180;y1 = sin(x);y2 = sin(2 * x);y3 = sin(3 * x);
fprintf(data,“%f %f %f %f ¥n",x ,y1 ,y2 ,y3 );}
fclose(data);
return 0;}
「,」も忘れずに
“メモ帳”と“開発者コマンドプロンプト for VS2015”を使用する方法⑤
ファイル→名前を付けて保存→自分のUSB内のフォルダを選択し、
ファイル名を“sin1x2x3x_2.c”ファイルの種類を“すべてのファイル”として保存。
7
コマンドプロンプトで、以下のコマンドを入力していく。
cl sin1x2x3x_2.c(cファイルをコンパイル)
sin1x2x3x_2(sin1x2x3x_2.exeを実行)
8
usbのフォルダ内に“sin1x2x3x.d”が作成されていることを確認する。
9
スタートボタン→メモ帳を開いて、gnuplotのコマンドを入力。
set terminal windowsset xrange [-0.2:6.5]set yrange [-1.5:1.5]set xlabel "x[rad]"set ylabel "sinx,sin2x,sin3x"set gridplot 'sin1x2x3x.d' using 1:2 with linesreplot 'sin1x2x3x.d' using 1:3 with linesreplot 'sin1x2x3x.d' using 1:4 with linespause -1 'Hit any key to close plot windows.'set terminal pushset terminal pngset output 'sin1x2x3x.png'replotpause -1 'Hit any key to quit.'
sin1x2x3x.gp
“メモ帳”と“開発者コマンドプロンプト for VS2015”を使用する方法⑥
ファイル→名前を付けて保存→自分のUSB内のフォルダを選択し、
ファイル名を“sin1x2x3x.gp”ファイルの種類を“すべてのファイル”として保存。
11
10
“メモ帳”と“開発者コマンドプロンプト for VS2015”を使用する方法⑦
スタートボタン→gnuplotを開いて以下のコマンドを入力する。
gnuplot > cd ‘e:TA¥programing¥case1_memo’ (ディレクトリの移動)gnuplot > load ‘sin1x2x3x.gp’ (gpファイルを読み込む)
12
OKを2回押す。13
“CPad for Borland C++Compiler”を使用する方法
“CPad for Borland C++Compiler”を使用する方法①
スタートボタン→CPad for Borland C++Compilerを開いて、ソースコードを入力。1
#include<stdio.h>#include<math.h>
#define PI 3.14159
int main(void){
int i, imax = 360;double x, y1, y2, y3;
sin1x2x3x_1.c
for (i=0; i <= imax; i++) {
x = i * PI / 180;y1 = sin(x);y2 = sin(2 * x);y3 = sin(3 * x);
printf("%f %f %f %f ¥n",x ,y1 ,y2 ,y3 );}
return 0;}
“CPad for Borland C++Compiler”を使用する方法②
ファイル→名前を付けて保存→自分のUSB内のフォルダを選択し、
ファイル名を“sin1x2x3x_1.c”として保存。
2
実行→コンパイル&実行3
出てきたウィンドウ上に実行結果が表示される。4
“CPad for Borland C++Compiler”を使用する方法③
CPad上で、“sin1x2x3x_1.c”を開き、以下の赤線部を追加する。5
#include<stdio.h>#include<math.h>
#define PI 3.14159
int main(void){
int i, imax = 360;double x, y1, y2, y3;
FILE *data;char *data_file;
data_file="sin1x2x3x.d";data=fopen(data_file,"w");
sin1x2x3x_2.c
for (i=0; i <= imax; i++) {
x = i * PI / 180;y1 = sin(x);y2 = sin(2 * x);y3 = sin(3 * x);
fprintf(data,"%f %f %f %f ¥n",x ,y1 ,y2 ,y3 );}
fclose(data);
return 0;}
「,」も忘れずに
“CPad for Borland C++Compiler”を使用する方法④
実行→コンパイル&実行7
出てきたウィンドウ上に実行結果が表示される。8
ファイル→名前を付けて保存→自分のUSB内のフォルダを選択し、
ファイル名を“sin1x2x3x_2.c”として保存。
6
usbのフォルダ内に“sin1x2x3x.d”が作成されていることを確認する。
9
“CPad for Borland C++Compiler”を使用する方法⑤
set terminal windowsset xrange [-0.2:6.5]set yrange [-1.5:1.5]set xlabel "x[rad]"set ylabel "sinx,sin2x,sin3x"set gridplot 'sin1x2x3x.d' using 1:2 with linesreplot 'sin1x2x3x.d' using 1:3 with linesreplot 'sin1x2x3x.d' using 1:4 with linespause -1 'Hit any key to close plot windows.'set terminal pushset terminal pngset output 'sin1x2x3x.png'replotpause -1 'Hit any key to quit.'
sin1x2x3x.gp
ファイル→新規作成→左のように入力する。
10
ファイル→名前を付けて保存→自分のUSB内のフォルダを選択し、
ファイル名を“sin1x2x3x.gp”ファイルの種類を“すべてのファイル”として保存。
11
スタートボタン→gnuplotを開いて以下のコマンドを入力する。
gnuplot > cd ‘e:TA¥programing¥case1_cpad’ (ディレクトリの移動)gnuplot > load ‘sin1x2x3x.gp’ (gpファイルを読み込む)
12
“CPad for Borland C++Compiler”を使用する方法⑥
OKを2回押す。13