この教科書ではPascal言語を使用していますが、授業ではC言語を使います。ここでは教科書中のPascal言語で記述してある擬似コード等を、C言語に変換してありますので参考にしてください。第3章 3.2の説明まで、および第4章は左側にC言語、右側にPascal言語が並列記述してありますが、アルゴリズムやプログラムはC言語だけになっています。
C言語 Pascal言語 < 第2章 2.2(P.29) > = := 代入(転送) < 第3章 3.2(P.49) >![]()
○擬似コード C言語 Pascal 逐次構造 ; ; ; ; ; ; (ブロック { ; ; ;} begin ; ; ; end) 選択構造 1. if (条件) if 条件 { then 文 真の時の処理1; (セミコロン) else 文 処理2; : } else { 偽の時の処理1; 処理2; : } 2. switch (式) case x of { case 値1: (コロン) 値1:値1の場合の処理; 値1の場合の処理; break; case 値2: 値2:値2の場合の処理 値2の場合の処理; break; end default: どれでもない場合の処理; break; } 反復構造 1. while (繰り返し条件) while 条件 do 繰り返し処理 { 繰り返し処理1; : } 2. do { repeat 繰り返し処理 繰り返し処理; } while (繰り返し条件); until 終了条件 3. for (i=初期値 ; i<=最終値 ; ++i) for 初期値 to 最終値 { do 繰り返し処理 繰り返し処理1; for 初期値 downto 最終値 : do 繰り返し処理 } ++i は i=i+1 とほぼ同じ ○演算子 + - * / 算術演算 + - * / % 剰余 ? = 代入 := == != >= <= > < 比較 = ? ? ? ? && || ! 論理演算 and or not ++ (a = a+1) インクリメント演算子 -- (a = a-1) デクリメント演算子 (b = ++a; …aに1を加えてから、その値をbに代入する) (b = a++; …aをbに代入してから、aに1を加える) ○コメント注釈 /* コメント */ {コメント} < 第3章 3.2 【演習3.12】(P.57) > { S1; while (P2) { S2; do { S3; S4; S5; } while (P5) ; } if (P6) { do { S7; } while (P7) ; } else { if (P8) { S8; } else { S9; } } } < 第3章 3.2【演習3.14】(P57) > { S1; if (P2) { while (P3) { S3; } if (P4) { S4; S5; } } do { S6; S7; if (P8) { S8; } else { S9; } }while (P9) ; } < 第3章 3.3 【例題3.2】(P.58) > { 1. データの入力 ; 2. 処理する ; 3. 結果の出力(表示) ; } (P.60) /* 1. データの入力 */ { 1.1 基底打席数を読む ; 1.2 全選手の打撃成績を読む ; } /* 1.2 全選手の打撃成績を読む */ while(選手がまだ残っている) { 1.2.1 次の選手の打撃成績を読む; } /* 2. 処理 */ { 2.1 打席数が規定打席数に満たない選手を除く ; 2.2 残った選手について、打率を計算する ; 2.3 打率が大きい順に上位10人を求める ; } (P.61) /* 3. 結果の出力 */ { 3.1 見出しを印刷する ; 3.2 for(i=1 ; i<=10 ; i++) { 3.2.1 打撃順位iの選手の選手名と打撃成績を表示する ; } } { /* 1. データの入力 */ { 1.1 基底打席数を読む ; while(選手がまだ残っている){ 1.2.1 次の選手の打撃成績を読む; } /* 2. 処理 */ { 2.1 打席数が規定打席数に満たない選手を除く ; 2.2 残った選手について、打率を計算する ; 2.3 打率が大きい順に上位10人を求める ; } /* 3. 結果の出力 */ { 3.1 見出しを印刷する ; 3.2 for(i=1 ; i<=10 ; i++) { 3.2.1 打撃順位iの選手の選手名と打撃成績を印刷する ; } } } < 第3章 3.4 【例題3.3】(P.62) > (P.64) /* sort */ { for( i=1 ; i<=n-1 ; i=i+1) { 1. 最大値を求めて入れ換える ; } } (P.64) /* 1. 最大値を求めて入れ換える*/ { 1.1 最大値を求める ; 1.2 値を入れ換える ; } (P.65) /* 1.1 最大値を求める */ { max=a[i] ; for( j=i+1 ; j<=n ; j=j+1) { 1.1.1 最大値と比較して必要ならば最大値を置き換える ; } } /* 1.1.1 最大値と比較して必要ならば最大値を置き換える */ { if(a[j]>max) { max=a[j] ; } } (P.67) /* 1.1.1' 最大値と比較して必要ならば最大値を置き換える */ { if(a[j]>max) { max=a[j] ; jmax=j ; } } (P.68) 修正版 /* 1.1' 最大値を求める */ { max=a[i] ; jmax=i ; for(j=i+1 ; j<=n ; j=j+1) { 1.1.1' 最大値と比較して必要ならば最大値を置き換える ; } } /* 1.2 値を入れ換える */ { a[jmax]=a[i] ; a[i]=max ; } (P.68) プログラムの形・・省略(課題になっている) < 第3章 3.6 【例題3.4】(P.78) > アルゴリズム1( for 文使用) { result=0; for( i=1; i<=n; ++i){ if( table[i] == data){ result = i; } } } アルゴリズム1(while 文使用) { result=0; i=1; while( i<=n ){ if( table[i] == data ){ result=i; } i=i+1; } } (P.79)アルゴリズム2 (goto使用) /* (for 文使用) */ { result=0; for( i=1; i<=n; ++i){ if( table[i] == data){ result=i; goto found; } } found: /* (コロン) */ } (P.79)アルゴリズム2 (goto使用) /* (while 文使用) */ { result=0; i=1; while( i<=n ){ if( table[i] == data ){ result=i; goto found; } i=i+1; } found: /* (コロン) */ } アルゴリズム2' (break使用) /* (for 文使用) */ { result=0; for( i=1; i<=n; ++i){ if( table[i] == data ){ result=i; brak; } } } アルゴリズム2' (break使用) /* (while 文使用) */ { result=0; i=1; while( i<=n ){ if( table[i] == data ){ result=i; break; } i=i+1; } } (P.80)アルゴリズム3 (「スイッチ」使用) /* (for 文使用) */ { result=0; found=0; for( i=1; i<=n && found == 0; ++i){ if( table[i] == data ){ result=i; found=1; } } } (P.80)アルゴリズム3 (「スイッチ」使用) /* (while 文使用) */ { result=0; found=0; i=1; while( i<=n && found==0 ){ if( table[i] == data ){ result=i; found=1; } i=i+1; } } (P.81)アルゴリズム4 (「番兵」使用) /* (for 文使用) */ { table[n+1]=data; for( i=1; table[i] != data; ++i) ; } if( i<=n ){ result=i; }else{ result=0; } } (P.81)アルゴリズム4 (「番兵」使用) /* (while 文使用) */ { table[n+1]=data; i=1; while( table[i] != data ){ i=i+1; } if( i<=n ){ result=i; }else{ result=0; } } < 第4章 4.1 【演習4.8】(P.90) > /* (for 文使用) */ { for( x=a; x<=b; x+= delta){ xとsqrt(x)の値を印刷する; x=x+delta; } } /* (while 文使用) */ { x=a; while(x<=b){ xとsqrt(x)の値を印刷する; x=x+delta; } } < 第4章 4.1 【演習4.9】(P.90) > { n=floor((b-a)/delta+0.1) for(i=0; i <=n; ++i){ x=a+delta*i; xとsqrt(x)の値を印刷する; } } < 第4章 4.1 E.データの宣言(P.93) > C言語 Pascal (1) int x,y,z; var x,y,z:integer; (2) float height, depth, width; var height, depth, width:real double height, depth, width; (3) char chr; val chr:char; char mojiretu[n+1] (終端0) word:packed array[1..n] of char; 表現が難しい(ポインタを使う) int result, i, n; result, i, n:integer; なし found:Boolean; < 第4章 4.2 配列とレコード(P.93) > (P.94) float a[20]; あるいは double a[20] var a:array[1..20] of real; int b[50][11]; 添字は必ず0から始まる var b:array[1..50,0..10] of integer なし const lbound=-10; ubound=26; var vec:array[lbound..ubound] of integer (P.95) 構造体(struct) レコード(record) struct dageki{ char namae[14]; record namae:array[1..14] of char; char team; team:char; int daseki, dasu, anda; daseki, dasu, anda:integer } end struct dageki{ var dageki:array[1..100] of char namae[14]; record namae:array[1..14] of char; char team; team:char; int daseki, dasu, anda;} daseki, dasu, anda:integer struct dageki senshu[100]; end < 第5章 5.1 【演習5.1】(P.102) > #include <stdio.h> #include <math.h> #define N 100 double sum(x, n) /* 手続きの宣言 */ double *x; int n; { int i; double s; s=0.0; for(i=1; i<=n; i++) { s=s+x[i]; } return s; } main() { double x[N+1], s; double m; int n, i; データの個数を読む; データx[1],x[2],....,x[n]を読む; s=sum(x, n); /* 手続きの呼び出し */ m=s/n; データの個数n、データx[1],...,x[n],平均値mを印刷する } < 第5章 5.1 (P.109) > #include <stdio.h> #include <math.h> #define N 100 double sum(x, n) double *x; int n; { int i; double s; s=0.0; for(i=1; i<=n; i++) { s=s+x[i]; } return s; } main() { double x[N+1]; x2[N+1] double s, s2, m, v; int n, i; データの個数を読む; データx[1],x[2],....,x[n]を読む; s=sum(x, n); m=s/n; s2=0.0; for(i=1; i<=n; i++) { x2[i]=(x[i]-m)*(x[i]-m); } s2=sum(x2, n); v=s2/n; } n、x[1],...,x[n],m,vを印刷する }