PowerPoint プレゼンテーション - 物理学情報処理演習

Similar documents
PowerPoint プレゼンテーション - 物理学情報処理演習

PowerPoint プレゼンテーション - 物理学情報処理演習

PowerPoint プレゼンテーション - 物理学情報処理演習

PowerPoint プレゼンテーション - 物理学情報処理演習

第1回 プログラミング演習3 センサーアプリケーション

Prog1_10th

02: 変数と標準入出力

Cプログラミング1(再) 第2回

02: 変数と標準入出力

02: 変数と標準入出力

Microsoft PowerPoint - 計算機言語 第7回.ppt

02: 変数と標準入出力

PowerPoint Presentation

joho07-1.ppt

PowerPoint プレゼンテーション

Microsoft Word - no202.docx

program7app.ppt

講習No.12

プログラミング方法論 II 第 14,15 回 ( 担当 : 鈴木伸夫 ) 問題 17. x 座標と y 座標をメンバに持つ構造体 Point を作成せよ 但し座標 は double 型とする typedef struct{ (a) x; (b) y; } Point; 問題 18. 問題 17 の

Microsoft PowerPoint - 5Chap15.ppt

memo

Microsoft Word - 3new.doc

プログラミング実習I

02: 変数と標準入出力

PowerPoint プレゼンテーション

CプログラミングI

Microsoft PowerPoint - kougi7.ppt

Microsoft PowerPoint - 11.pptx

Microsoft PowerPoint - kougi6.ppt

講習No.9

Microsoft PowerPoint - prog04.ppt

Microsoft Word - Cプログラミング演習(12)

プログラミング基礎

02: 変数と標準入出力

情報処理Ⅰ演習

Microsoft PowerPoint - 09.pptx

memo

PowerPoint プレゼンテーション

プログラミング実習I

Microsoft PowerPoint - prog06.ppt

02: 変数と標準入出力

Microsoft Word - Cプログラミング演習(1)_2012

C プログラミング演習 1( 再 ) 2 講義では C プログラミングの基本を学び 演習では やや実践的なプログラミングを通して学ぶ

バイオプログラミング第 1 榊原康文 佐藤健吾 慶應義塾大学理工学部生命情報学科

情報処理演習 B8クラス

Taro-ファイル処理(公開版).jtd

char int float double の変数型はそれぞれ 文字あるいは小さな整数 整数 実数 より精度の高い ( 数値のより大きい より小さい ) 実数 を扱う時に用いる 備考 : 基本型の説明に示した 浮動小数点 とは数値を指数表現で表す方法である 例えば は指数表現で 3 書く

1. 入力した文字列を得る 1.1. 関数 scanf() を使う まずは関数 scanf() を使ったプログラムを作ってみましょう 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: #include<stdio.h> #define SIZE 128 main(

C プログラミング 1( 再 ) 第 4 回 講義では C プログラミングの基本を学び 演習では やや実践的なプログラミングを通して学ぶ 1

PowerPoint Presentation

Microsoft PowerPoint - bp02.ppt

memo

プログラミング及び演習 第1回 講義概容・実行制御

Microsoft PowerPoint ppt

PowerPoint プレゼンテーション

講習No.1

コマンドラインから受け取った文字列の大文字と小文字を変換するプログラムを作成せよ 入力は 1 バイトの表示文字とし アルファベット文字以外は変換しない 1. #include <stdio.h> 2. #include <ctype.h> /*troupper,islower,isupper,tol

書式に示すように表示したい文字列をダブルクォーテーション (") の間に書けば良い ダブルクォーテーションで囲まれた文字列は 文字列リテラル と呼ばれる プログラム中では以下のように用いる プログラム例 1 printf(" 情報処理基礎 "); printf("c 言語の練習 "); printf

slide5.pptx

/*Source.cpp*/ #include<stdio.h> //printf はここでインクルードして初めて使えるようになる // ここで関数 average を定義 3 つの整数の平均値を返す double 型の関数です double average(int a,int b,int c){

ゲームエンジンの構成要素

02: 変数と標準入出力

< F2D837C E95CF CF68A4A94C5816A2E6A>

第9回 配列(array)型の変数

Microsoft PowerPoint - 13th.ppt [互換モード]

gengo1-11

Microsoft PowerPoint - lec10.ppt

デジタル表現論・第6回

Microsoft Word - no15.docx

Microsoft PowerPoint - prog07.ppt

基礎プログラミング2015

Microsoft Word - Training10_プリプロセッサ.docx

Microsoft Word - Cプログラミング演習(11)

02: 変数と標準入出力

プログラミング演習3 - Cプログラミング -

今までの復習 プログラムで最低限必要なもの 入力 ( キーボードから ファイルから ) 出力 ( 画面へ ファイルへ ) 条件分岐 : 条件の成立 不成立により 異なる動作をする 繰り返し : 一定の回数の繰返し 条件成立の間の繰返し 関数の定義 関数の呼び出し C ではそれ以外に ポインタ データ

Microsoft Word - Cプログラミング演習(8)

Microsoft PowerPoint - 10.ppt [互換モード]

gengo1-12

関数の呼び出し ( 選択ソート ) 選択ソートのプログラム (findminvalue, findandreplace ができているとする ) #include <stdiu.h> #define InFile "data.txt" #define OutFile "surted.txt" #def

ポインタ変数

kiso2-09.key

プログラミング基礎

Microsoft PowerPoint - exp2-02_intro.ppt [互換モード]

Prog1_12th

ポインタ変数

1. 入力した正の整数を降順に並べ換えて出力するプログラムを作成せよ プログラムは個別にコンパイルし make コマンドで実行すること 入力データは 50 以下とし 以下の数が混在しているとする 16 進数 : 先頭 1 文字が x または X( エックスの小文字か大文字 ) 8 進数 : 先頭 1

2006年10月5日(木)実施

Microsoft Word - no12.doc

Microsoft PowerPoint - prog03.ppt

Microsoft PowerPoint - kougi9.ppt

問 2 ( 型変換 ) 次のプログラムを実行しても正しい結果が得られない 何が間違いかを指摘し 正しく修正せよ ただし int サイズが 2 バイト long サイズが 4 バイトの処理系での演算を仮定する #include <stdio.h> int main( void ) { int a =

基礎プログラミング2015

新版明解C言語 実践編

ファイル入出力

PowerPoint プレゼンテーション

Prog1_6th

計算機プログラミング

Prog1_6th

Transcription:

物理学情報処理演習 8. C 言語 5 文字列 ポインタ 2016 年 6 月 7 日 ver20160607_2 本日の推奨作業 directory lesson08 8.1 文字列 8.2 ポインタ 参考文献 やさしい C++ 第 4 版高橋麻奈 ( 著 ) ソフトバンククリエイティブ プログラミング言語 C++ 第 4 版ビャーネ ストラウストラップ, Bjarne Stroustrup, 柴田望洋 Numerical Recipes: The Art of Scientific Computing, Third Edition in C++ 身内賢太朗レポート提出 :fsci-phys-jouhou@edu.kobe-u.ac.jp 課題提出期限 2016 年 6 月 14 日 13:00

8.1 文字列 8.1.1 文字列処理 ある変数に対するアドレス ( メモリ上の位置 ) をもつ変数 N 個の文字からなる文字列は N+1 個の大きさの文字配列 末尾の文字は NULL 文字 ( 0 ) 例 char c[16]; #include <iostream> using namespace std; int main(){ // comment can be writen starting with "//" char c[16]="hello World!"; cout <<c << endl; hello_2.cxx 配列 c の中身 H e l l o W o r l d! 0 演習 8.1.1( 提出不要 ) hello_2e.cxx hello_2ee.cxx hello_2eee.cxx を debug してみよう エラーメッセージをよく確認すること

8.1.2 文字列処理 標準ライブラリの <string.h> に定義してある文字列処理ルーチン char* strcpy(char* s, const char* ct) char* strncpy(char* s, const char* ct, int n) char* strcat(char* s, const char* ct) char* strncat(char* s, const char* ct, int n) int strcmp(const char* cs, const char* ct) size_t strlen(const char* cs) NULL 文字を含めて文字列 ct を文字列 s にコピーし s を返す 文字列 ct 内から n 文字を文字列 s にコピーし s を返す ct が n 文字より少ないときは NULL 文字をつめる 文字列 ct を文字列 s の終わりに連結し s を返す 文字列 ct 内から最大 n 文字を文字列 s の終わりに連結し s を返す 文字列 cs と文字列 ct を比較する 一致していれば 0 を返す 一致していなければ 相違が発見された文字どうしの数値上の差を返す 文字列 cs の長さを返す 演習 8.1.2 ( 提出不要 ) hello_3.cxx を実行してみよう 内容を確認して 上記機能を試してみよう

8.1.2 文字列処理 標準ライブラリの <stdio.h> の関数 sprintf も使える int printf (const char *format,... ); int sprintf (char *str, const char *format,... ); 第一引数に書式を書き 第二引数以降に実際に出力される変数を書く 出力は標準出力 第一引数に代入されるべき文字 第二引数に書式を書き 第三引数以降に実際に出力される変数を書く #include <iostream> #include<string.h> #include<stdio.h> using namespace std; int main(){ // comment can be writen starting with "//" int i; char c1[16]="hello "; char c2[16]="world!"; char c3[16]; char c4[64]; cout <<c1 << endl; strcat(c1,c2); cout <<c1 << endl; for(i=0;i<10;i++){ sprintf(c3,"%d",i); //substitute i to c3 as a character. sprintf(c4,"%s%s_%d.dat",c1,c2,i); //sprintf can have more than one paramters printf("%d t%s",i,c3); //same as cout << i << " t" << c3 << endl; hello_4.cxx < 書式の例 > %d 10 進数 %lf double %e e 指数表示 %s 文字列 フィールド幅 % とdなどの間に数字を入れる %3dで3 桁で整数をs 桁で表示 %2.1fで全体で2 桁 小数点以下 1 桁 %2.1eで全体で2 桁 小数点以下 1 桁 %c 1 文字 < エスケープシーケンスの例 > n 改行 t タブ r キャリッジリターン 演習 8.1.3 ( 提出不要 ) hello_4.cxx, hello_5.cxx を実行してみよう 内容を確認して 上記機能を試してみよう

8.1.3 多次元配列任意の型の配列を多次元化することが可能 #include <iostream> #include <stdlib.h> fact_13.cxx #include <string.h> #include <math.h> using namespace std; int fact(int n){ //calculated the factorial int i,ans; ans=1; for(i=1;i<=n;i++){ ans=ans*i; return ans; int main(int argc, char *argv[] ) { int n,max,i; int ans[2][20]; if(argc>1){ max=(int)atof(argv[1]); else{ // for default max = 10; for(i=0;i<max;i++){ ans[0][i]=i; ans[1][i]=fact(i); for(i=0;i<max;i++){ cout <<ans[0][i]<<"!="<<ans[1][i] << endl; 演習 8.1.4 ( 提出不要 ) 階乗計算のプログラムを多元配列を用いて書いたサンプルコード fact_13.cxx を実行してみよう 出力 3 列目に 1 列目の数の 2 乗を出力するように変更を加えてみよう

複数の文字列も多次元配列 char array[a_size][str_size] が使える 例 char array[4][8] = { First, Second, Third, Last ; array[0] value F i r s t 0 array[0][0] array[0][1] array[0][2] array[0][3] array[0][4] array[0][5] array[1] array[2] array[3] S e c o n d 0 T h i r d 0 L a s t 0 演習 8.1.5 ( 提出不要 ) hello_3.cxx を二次元配列を用いて書いてみよう

出力 ofstream #include <fstream> が必要 これまで cout,cerr で出力していたが 出力先をファイルとすることも可能 #include <iostream> #include <fstream> #include<string.h> #include<stdio.h> using namespace std; int main(){ int i; char fout[16]="test.out"; char c1[16]="hello World!"; ofstream ofs(fout); ofs << c1<<endl; ofstest_1.cxx 演習 8.1.6 ( 提出不要 ) ofstest_1.cxx を書き換えて 出力ファイル名を変更してみよう

8.2 ポインタ 8.2.1 ポインタ ある変数に対するアドレス ( メモリ上の位置 ) をもつ変数 * を付けて定義する 例 int *p_int; //*p_int というパラメータが int p_int がポインタ変数 ポインタ演算子 * は ポインタの指し示している変数の値を得る演算子 アドレス演算子 & は 変数のアドレスを得る演算子 #include <iostream> #include<string.h> pointer_1.cxx #include<stdio.h> using namespace std; int main(){ int *p_int; int i=1; *p_int=2; cout << "i: "<<i<<endl; //integer cout << "*p_int: "<<*p_int<<endl; // integer cout << "p_int: "<<p_int<<endl; // pointer of integer cout << &(*p_int): <<&(*p_int)<<endl; // pointer of integer 演習 8.2.1 ( 提出不要 ) pointer_1 を実行 何が起きているか理解しよう double 型のポインタ変数を入れてみよう

8.2.2 配列とポインタ 配列を示す変数には 配列の先頭アドレスが入っている 配列を示す変数は ポインタとしても使用できる 例 1: 次のようにすると moji1, moji2 は同じ文字となる char array[128], moji1, moji2; moji1 = array[0]; /* 0 番目の文字 */ moji2 = *array; /* 配列の先頭の文字 */ 例 2: 次のようにすると moji1, moji2 は同じ文字となる char array[128], moji1, moji2; int i; moji1 = array[i]; /* i 番目の文字 */ moji2 = *(array+i); /* 配列の先頭からi 番目の文字 */

配列とポインタ 配列とポインタの違い ポインタを宣言したときは アドレスの値を入れる メモリ領域 が確保される 配列を示す変数は ポインタとしても使用できる 配列全体を格納する メモリ領域 が確保されると共に アドレスの値を入れる メモリ領域 が確保され 配列の 先頭アドレス がセットされる

多次元配列 ポインタの配列複数の文字列をつくるには ( 文字の ) 多次元配列 char array[a_size][str_size] ( 文字への ) ポインタからなる配列 char *array[a_size] ( 文字への ) ポインタへのポインタ char **array が使用できる

8.2.3 ポインタと関数 これまで : 関数への値渡し 関数へ値を渡す 渡した変数は変更されない 演習 8.2.2 ( 提出不要 ) hist_1 < grade.dat として実行 上記を確認しよう また 結果をパイプリダイレクションで grade_hist.dat に書き出して gnuplot で描画してみよう ##include <iostream> #include <fstream> #include <stdlib.h> #include <string.h> #include <math.h> using namespace std; #define HIST_MIN 0 #define HIST_MAX 50 #define HIST_BIN 10 int output_hist(int value){ cout <<value<<" t"; value=0; cout <<value<< t ; // 関数内の変数 valueは変更される int main(int argc, char *argv[] ) { int i,num; double hist[3][hist_bin]; double hist_step=(double)(hist_max-hist_min)/hist_bin; double grade; for(i=0;i<hist_bin;i++){ hist[0][i]=hist_step*i;//hist[0] for lower bound hist[1][i]=hist_step*(i+1);// hist[1] for upper bound hist[2][i]=0; //hist[2] for contents num=0; while(cin>>grade){ hist[2][(int)(grade/hist_step)]++; // filling the histgram // cout << grade<< t <<hist[0][(int)(grade/hist_step)]<< t << hist[1][(int)(grade/hist_step)]<<" t"<<hist[2][(int)(grade/hist_step)]<<endl; // check for histgram filling num++; for(i=0;i<hist_bin;i++){ cout<<hist[0][i]<<" t"<<hist[1][i]<<" t"<<hist[2][i]<<" t"; output_hist(hist[2][i]); cout<<hist[2][i]<<endl; hist_1.cxx // main の変数 hist[2][i] は変更されない

using namespace std; #define HIST_MIN 0 hist_2.cxx #define HIST_MAX 50 #define HIST_BIN 10 int hist_init(double hist[3][hist_bin],double *hist_step){ int i; *hist_step=(double)(hist_max-hist_min)/hist_bin; for(i=0;i<hist_bin;i++){ hist[0][i]=(*hist_step)*i; hist[1][i]=(*hist_step)*(i+1); hist[2][i]=0; int hist_fill(double value,double hist[3][hist_bin],double hist_step){ hist_step=(double)(hist_max-hist_min)/hist_bin; hist[2][(int)(value/hist_step)]++; int hist_output(double hist[3][hist_bin]){ int i; for(i=0;i<hist_bin;i++){ cout<<hist[0][i]<<" t"<<hist[1][i]<<" t"<<hist[2][i]<<endl; int main(int argc, char *argv[] ) { int i,num; double hist[3][hist_bin]; double hist_step; double grade; hist_init(hist,&hist_step); num=0; while(cin>>grade){ hist_fill(grade,hist,hist_step); num++; hist_output(hist); 新 : アドレス渡し 呼び出した側の引数としてアドレスを渡す 関数の処理によって変数の値を変えることができる 呼び出し側の引数のアドレス (&hist_step) が 呼び出された側の引数であるポインタ変数 (hist_step) の値 (*hist_step) となる 呼び出された側で hist_step の指し示す変数の値を変更しているので 呼び出し側の変数 hist_step も変化する 演習 8.2.3 ( 提出不要 ) hist_2 < grade.dat として実行 上記を確認しよう また 結果をパイプリダイレクションで grade_hist.dat に書き出して gnuplot で描画してみよう ( 必要に応じて show_hist.plt を使うこと )

レポート提出 :fsci-phys-jouhou@edu.kobe-u.ac.jp 提出期限 2016 年 6 月 14 日 13:00 ソースコードファイル名 :2016_jouhou_08_ 学籍番号の下 4 桁.cxx 出力ファイル名 :2016_jouhou_08_ 学籍番号の下 4 桁.txt 出力画像ファイル名 :2016_jouhou_08_ 学籍番号の下 4 桁.pdf 課題 8: 課題 7 のソースなどを基にして 以下の様なコードを作成せよ 提出はソースコード gnuplot の出力ファイル 作業 directory で ls した出力を txt としたファイルの 3 点とする ( プログラム仕様 ) 1 平均値 μ=0 標準偏差 σ の規格化された正規分布を計算 出力 1 列目に X(-10 から 10) 2 列目に確率をファイルに出力する 2 プログラム内で標準偏差 σ を 0 から 5 まで 0.1 刻みで変更し それぞれの σ に対応する出力ファイルを gaus_?.?.dat( 例 :σ=2.5 の出力は gaus_2.5.dat) として プログラムから自動的に生成されるようにする (hello_5.cxx, oufstest_1.cxx 参照 全部で 50 ファイルできるはずである ls の出力をテキストファイルに落として提出のこと ) 3 2 で作成したデータのうちで σ=1.0,3.0,5.0 について同じ plot に描画せよ ( 必要であれば show.plt を使用せよ )