新しく発言する  EXIT  インデックスへ
流れ図(フローチャート)を描く

  流れ図(フローチャート)を描く 山中和義 2008/01/09 16:18:14  (修正1回)
  つづき 山中和義 2008/01/09 16:20:23  (修正1回)
   └流れ図でアルゴリズムを検証する。2008.1.1... 山中和義 2008/01/10 12:53:57  (修正4回)
    ├つづき 山中和義 2008/01/10 12:55:58  (修正4回)
    ├つづき2 山中和義 2008/01/10 13:01:14  (修正4回)
    ├つづき3 山中和義 2008/01/11 21:06:32  (修正2回)
    └つづき4 山中和義 2008/01/13 20:10:24  (修正1回)
     └サンプル(右から左へ受け流す〜♪) 山中和義 2008/01/13 20:18:23  (修正1回)
      └高校数学「数値計算とコンピュータ」 山中和義 2008/01/22 20:37:31  (修正1回)

  流れ図(フローチャート)を描く 山中和義 2008/01/09 16:18:14  (修正1回)  ツリーへ

流れ図(フローチャート)を描く  返事を書く  ノートメニュー
山中和義 <drdlxujciw> 2008/01/09 16:18:14 ** この記事は1回修正されてます
JIS規格のような矢印を描けませんが、データをつくるだけで簡易に表示できます。


!流れ図(フローチャート)を描く

PICTURE chart(n) !流れ図記号の基本図形
SELECT CASE n
CASE 1 !端子
PLOT LINES: -0.4,0.4; 0.4,0.4
PLOT LINES: -0.4,-0.4; 0.4,-0.4
DRAW arc(90,270) WITH SCALE(0.4)*SHIFT(-0.4,0)
DRAW arc(-90,90) WITH SCALE(0.4)*SHIFT(0.4,0)
CASE 2 !結合子
DRAW arc(0,360) WITH SCALE(0.4)
CASE 3 !処理
PLOT LINES: -0.8,-0.5; 0.8,-0.5; 0.8,0.5; -0.8,0.5; -0.8,-0.5
CASE 4 !定義済み処理
PLOT LINES: -0.8,-0.5; 0.8,-0.5; 0.8,0.5; -0.8,0.5; -0.8,-0.5
PLOT LINES: -0.75,-0.5; -0.75,0.5
PLOT LINES: 0.75,-0.5; 0.75,0.5
CASE 5 !判断
PLOT LINES: -0.8,0; 0,-0.6; 0.8,0; 0,0.6; -0.8,0
CASE 6,16 !繰り返しの開始
PLOT LINES: -0.8,-0.5; 0.8,-0.5; 0.8,0.5; 0.6,0.7; -0.6,0.7; -0.8,0.5; -0.8,-0.5
CASE 7,17 !繰り返しの終了
PLOT LINES: -0.8,0.7; 0.8,0.7; 0.8,-0.3; 0.6,-0.5; -0.6,-0.5; -0.8,-0.3; -0.8,0.7
CASE 8 !手操作入力
PLOT LINES: -0.8,-0.5; 0.8,-0.5; 0.8,0.7; -0.8,0.3; -0.8,-0.5
CASE 9 !表示
PLOT LINES: -0.8,-0.3; 0.8,-0.7; 0.8,0.7; -0.8,0.3; -0.8,-0.3
CASE 10 !入出力
PLOT LINES: -0.9,-0.5; 0.5,-0.5; 0.9,0.5; -0.5,0.5; -0.9,-0.5
CASE 23 !準備
PLOT LINES: -0.9,0; -0.7,-0.5; 0.7,-0.5; 0.9,0; 0.7,0.5; -0.7,0.5; -0.9,0;
CASE ELSE !13
END SELECT
END PICTURE
PICTURE arc(s,e) !円弧を描く
FOR i=s TO e
PLOT LINES: COS(RAD(i)),SIN(RAD(i));
NEXT i
PLOT LINES
END PICTURE

PICTURE ArrowAndComment(d,c$) !「矢印とその注釈」を描く
SET TEXT JUSTIFY "LEFT","HALF"
SELECT CASE d !方向
CASE 0 !右
DRAW arrow WITH SHIFT(0.7,0) !矢印
PLOT TEXT, AT 0.8,0.2: c$ !注釈
CASE 1 !上
DRAW arrow WITH ROTATE(PI/2)*SHIFT(0,0.4)
PLOT TEXT, AT 0.2,0.7: c$
CASE 2 !左
DRAW arrow WITH ROTATE(PI)*SHIFT(-0.7,0)
PLOT TEXT, AT -1.1,0.2: c$
CASE 3 !下
DRAW arrow WITH ROTATE(-PI/2)*SHIFT(0,-0.4)
PLOT TEXT, AT 0.2,-0.7: c$
CASE ELSE
END SELECT
END PICTURE
PICTURE arrow !矢印を描く
PLOT LINES: 0,0; 0.5,0
PLOT AREA: 0.5,0; 0.4,0.05; 0.4,-0.05
END PICTURE

  つづき 山中和義 2008/01/09 16:20:23  (修正1回)  ツリーへ

Re: 流れ図(フローチャート)を描く  返事を書く  ノートメニュー
山中和義 <drdlxujciw> 2008/01/09 16:20:23 ** この記事は1回修正されてます
つづき


PICTURE FChart(n,d$,s$,c$) !流れ図記号を描く
IF BLEN(s$)>9 THEN LET a=BLEN(s$)/9 ELSE LET a=1 !枠
DRAW chart(n) WITH SCALE(a,1) !※横倍のみ

SET TEXT JUSTIFY "CENTER","HALF"
PLOT TEXT ,AT 0,0: s$ !処理の内容

IF d$<>"" AND VAL(d$(1:1))=0 THEN !右方向に矢印がある場合、左側に記述する
SET TEXT JUSTIFY "RIGHT","HALF"
PLOT TEXT ,AT -a,0: c$
ELSE
SET TEXT JUSTIFY "LEFT","HALF"
PLOT TEXT ,AT a,0: c$ !枠外の注釈
END IF

IF d$<>"" THEN !矢印
LET d=VAL(d$(1:1)) !1文字目は向き、2文字目以降は注釈
DRAW ArrowAndComment(d,d$(2:LEN(d$)))
END IF
END PICTURE
PICTURE FChart2(n,d1$,d2$,s$,c$) !流れ図記号を描く
DRAW FChart(n,d1$,s$,c$)

IF n=5 THEN !「判断」なら、もう1本
LET d2=VAL(d2$(1:1))
DRAW ArrowAndComment(d2,d2$(2:LEN(d2$)))
ELSEIF n=6 OR n=7 OR n=16 OR n=17 THEN !「繰り返し」なら、番号を対応させる
SET TEXT JUSTIFY "CENTER","HALF"
PLOT TEXT ,AT 0,0.4: "ループ "&d2$(1:1)
END IF
END PICTURE
!--------------------

SUB ReadData(n,d1$,d2$,s$,c$) !流れ図記号を得る
READ n
IF n=5 OR n=6 OR n=7 OR n=16 OR n=17 THEN !「判断」「繰り返し」は矢印が2本!
READ d1$,d2$
ELSE
READ d1$
LET d2$=""
END IF
READ s$,c$
END SUB
!-------------------- ここまでがサブルーチン



LET h=7 !行数
LET w=5 !列数

LET s=MAX((w+1)*2,(h+1)*1.5)
SET WINDOW 0,s,0,s !表示領域 ※正方形
DRAW grid(2,1.5)

FOR x=1 TO w !格子を走査する
FOR y=h TO 1 STEP -1
CALL ReadData(n,d1$,d2$,s$,c$) !流れ図記号を得る

DRAW FChart2(n,d1$,d2$,s$,c$) WITH SHIFT(x*2,y*1.5) !流れ図を描く
NEXT y
NEXT x



!列挙する ※h*w個


!例題3 縦の長さが12、横の長さが34の長方形の面積を計算する
DATA 1,"3","開始",""
DATA 3,"3","12→A",""
DATA 3,"3","34→B",""
DATA 3,"3","A*B→S",""
DATA 9,"3","S",""
DATA 1,"","終了",""
DATA 0,"","",""


!例題4 商品の定価を入力して、税込み(5%)の値段を計算する
DATA 1,"3","開始",""
DATA 8,"3","A",""
DATA 3,"3","A*1.05→B",""
DATA 9,"3","B",""
DATA 1,"","終了",""
DATA 0,"","",""
DATA 0,"","",""


!例題6 1から10までの和を表示する
DATA 1,"3","開始",""
DATA 3,"3","0→W",""
DATA 16,"3","1","K=1,10,1",""
DATA 3,"3","W+K→W",""
DATA 17,"3","1","",""
DATA 9,"3","W",""
DATA 1,"","終了",""


!例題8 1つの数値xを入力して,その値が1ならば「正解です」,そうでないならば「エラー」と表示する
DATA 1,"3","開始",""
DATA 8,"3","X",""
DATA 5,"3=","0≠","X:1",""
DATA 9,"3","""正解です""",""
DATA 13,"3","├",""
DATA 1,"","終了",""
DATA 0,"","",""

DATA 0,"","",""
DATA 0,"","",""
DATA 13,"3","┐",""
DATA 9,"","""エラー""",""
DATA 13,"2","┘",""
DATA 0,"","",""
DATA 0,"","",""


END

   └流れ図でアルゴリズムを検証する。2008.1.1... 山中和義 2008/01/10 12:53:57  (修正4回)  ツリーへ

Re: つづき  返事を書く  ノートメニュー
山中和義 <drdlxujciw> 2008/01/10 12:53:57 ** この記事は4回修正されてます
流れ図でアルゴリズムを検証する。2008.1.14

プログラムを実行すると、流れ図が表示されます。
まるで「迷路の脱出(ダンジョンゲーム)」のように見えます。

流れ図記号を、位置関係が把握しやすいように、格子状(表形式)に配置させて埋めていくとプログラムができていきます。
(たとえば、電子ブロック、ジグソーパズル、積み木のおもちゃなどの感じ)


●サンプルプログラムの使い方など
DATA文を使って、流れ図を描いていきます。(プログラムを実行する)

コンパイルの手法を使って、機械的なコード(BASIC言語)を生成します。(条件あり/なしの分岐の羅列)
コードはテキスト画面に表示されます。END文を付加すれば実行できます。

BASIC編集画面の「ファイル」メニューから「新規(N)」を選び、コードを貼り付けて実行します。



!流れ図(フローチャート)からコード(BASIC言語)を生成する

!・関数と手続きの再帰呼出しは不可

PICTURE chart(n) !流れ図記号の基本図形
SELECT CASE n
CASE 1 !端子
PLOT LINES: -0.4,0.4; 0.4,0.4
PLOT LINES: -0.4,-0.4; 0.4,-0.4
DRAW arc(90,270) WITH SCALE(0.4)*SHIFT(-0.4,0)
DRAW arc(-90,90) WITH SCALE(0.4)*SHIFT(0.4,0)
CASE 2 !結合子
DRAW arc(0,360) WITH SCALE(0.4)
CASE 3 !処理
PLOT LINES: -0.8,-0.5; 0.8,-0.5; 0.8,0.5; -0.8,0.5; -0.8,-0.5
CASE 4 !定義済み処理
PLOT LINES: -0.8,-0.5; 0.8,-0.5; 0.8,0.5; -0.8,0.5; -0.8,-0.5
PLOT LINES: -0.75,-0.5; -0.75,0.5
PLOT LINES: 0.75,-0.5; 0.75,0.5
CASE 5 !判断
PLOT LINES: -0.8,0; 0,-0.6; 0.8,0; 0,0.6; -0.8,0
CASE 6,16 !繰り返しの開始
PLOT LINES: -0.8,-0.5; 0.8,-0.5; 0.8,0.5; 0.6,0.7; -0.6,0.7; -0.8,0.5; -0.8,-0.5
CASE 7,17 !繰り返しの終了
PLOT LINES: -0.8,0.7; 0.8,0.7; 0.8,-0.3; 0.6,-0.5; -0.6,-0.5; -0.8,-0.3; -0.8,0.7
CASE 8 !手操作入力
PLOT LINES: -0.8,-0.5; 0.8,-0.5; 0.8,0.7; -0.8,0.3; -0.8,-0.5
CASE 9 !表示
PLOT LINES: -0.8,-0.3; 0.8,-0.7; 0.8,0.7; -0.8,0.3; -0.8,-0.3
CASE 10 !入出力
PLOT LINES: -0.9,-0.5; 0.5,-0.5; 0.9,0.5; -0.5,0.5; -0.9,-0.5
CASE 23 !準備
PLOT LINES: -0.9,0; -0.7,-0.5; 0.7,-0.5; 0.9,0; 0.7,0.5; -0.7,0.5; -0.9,0;
CASE ELSE !13
END SELECT
END PICTURE
PICTURE arc(s,e) !円弧を描く
FOR i=s TO e
PLOT LINES: COS(RAD(i)),SIN(RAD(i));
NEXT i
PLOT LINES
END PICTURE

PICTURE ArrowAndComment(d,c$) !「矢印とその注釈」を描く
SET TEXT JUSTIFY "LEFT","HALF"
SELECT CASE d !方向
CASE 0 !右
DRAW arrow WITH SHIFT(0.7,0) !矢印
PLOT TEXT, AT 0.8,0.2: c$ !注釈
CASE 1 !上
DRAW arrow WITH ROTATE(PI/2)*SHIFT(0,0.4)
PLOT TEXT, AT 0.2,0.7: c$
CASE 2 !左
DRAW arrow WITH ROTATE(PI)*SHIFT(-0.7,0)
PLOT TEXT, AT -1.1,0.2: c$
CASE 3 !下
DRAW arrow WITH ROTATE(-PI/2)*SHIFT(0,-0.4)
PLOT TEXT, AT 0.2,-0.7: c$
CASE ELSE
END SELECT
END PICTURE
PICTURE arrow !矢印を描く
PLOT LINES: 0,0; 0.5,0
PLOT AREA: 0.5,0; 0.4,0.05; 0.4,-0.05
END PICTURE

    ├つづき 山中和義 2008/01/10 12:55:58  (修正4回)  ツリーへ

Re: 流れ図でアルゴリズムを検証する。2008.1.1...  返事を書く  ノートメニュー
山中和義 <drdlxujciw> 2008/01/10 12:55:58 ** この記事は4回修正されてます
つづき

!   1
! ┌─↑─┐
!2← s$ →0 c$
! └─↓─┘
!   3
PICTURE FChart(n,d$,s$,c$) !流れ図記号を描く
IF BLEN(s$)>9 THEN LET a=BLEN(s$)/9 ELSE LET a=1 !枠
DRAW chart(n) WITH SCALE(a,1) !※横倍のみ

SET TEXT JUSTIFY "CENTER","HALF"
PLOT TEXT ,AT 0,0: s$ !処理の内容

IF d$<>"" AND VAL(d$(1:1))=0 THEN !右方向に矢印がある場合、左側に記述する
SET TEXT JUSTIFY "RIGHT","HALF"
PLOT TEXT ,AT -a,0: c$
ELSE
SET TEXT JUSTIFY "LEFT","HALF"
PLOT TEXT ,AT a,0: c$ !枠外の注釈
END IF

IF d$<>"" THEN !矢印
LET d=VAL(d$(1:1)) !1文字目は向き、2文字目以降は注釈
DRAW ArrowAndComment(d,d$(2:LEN(d$)))
END IF
END PICTURE
PICTURE FChart2(n,d1$,d2$,s$,c$) !流れ図記号を描く
DRAW FChart(n,d1$,s$,c$)

IF n=5 THEN !「判断」なら、もう1本
LET d2=VAL(d2$(1:1))
DRAW ArrowAndComment(d2,d2$(2:LEN(d2$)))
ELSEIF n=6 OR n=7 OR n=16 OR n=17 THEN !「繰り返し」なら、番号を対応させる
SET TEXT JUSTIFY "CENTER","HALF"
PLOT TEXT ,AT 0,0.4: "ループ "&d2$(1:1)
END IF
END PICTURE
!--------------------


SUB ReadData(n,d1$,d2$,s$,c$) !流れ図記号を得る
READ n
IF n=5 OR n=6 OR n=7 OR n=16 OR n=17 THEN !「判断」「繰り返し」は矢印が2本!
READ d1$,d2$
ELSE
READ d1$
LET d2$=""
END IF
READ s$,c$
END SUB


DEF ToString$(s$)=CHR$(34)&s$&CHR$(34) !クォーテーション付文字データへ

!流れ図記号
! ┌───┐
! │ 命令 │注釈
! └─↓─┘
!を
! 行番号
! 命令文 !注釈
! GOTO 飛び先の行番号
!のような組で、コードを生成する。

DEF trim$(s$)=LTRIM$(RTRIM$(s$)) !左右の空白を削除する

DEF var$(s$)=trim$(s$(POS(s$,"→")+1:LEN(s$))) !変数名を得る ※expr→V
DEF expr$(s$)=trim$(s$(1:POS(s$,"→")-1)) !式を得る ※EXPR→v
DEF exprL$(s$)=trim$(s$(1:POS(s$,":")-1)) !左辺式を得る ※EXPR:expr
DEF exprR$(s$)=trim$(s$(POS(s$,":")+1:LEN(s$))) !右辺式を得る ※expr:EXPR
DEF fnc$(s$)=trim$(s$(1:POS(s$&"{","{")-1)) !関数名を得る ※F{p1,p2,…,pn}
DEF parm$(s$)=trim$(s$(POS(s$&"{","{")+1:POS(s$&"}","}")-1))&"," !引数を得る ※f{P1,P2,…,Pn}
DEF var2$(s$)=trim$(s$(1:POS(s$,"=")-1)) !変数名を得る ※V=p1,p2,…,pn
DEF parm2$(s$)=trim$(s$(POS(s$,"=")+1:LEN(s$)))&"," !引数を得る ※v=P1,P2,…,Pn
DEF op$(s$)=trim$(s$(2:LEN(s$))) !演算子を得る ※xOP

DIM LBL(20),JMP(20),NAM$(50) !「繰り返し」「結合子」「端子」用の行番号

    ├つづき2 山中和義 2008/01/10 13:01:14  (修正4回)  ツリーへ

Re: 流れ図でアルゴリズムを検証する。2008.1.1...  返事を書く  ノートメニュー
山中和義 <drdlxujciw> 2008/01/10 13:01:14 ** この記事は4回修正されてます
つづき2

SUB CodeGen(n,d1$,d2$,s$,c$) !流れ図記号に対応するコードを生成する
PRINT 100*x+y !行番号(ラベル)

SELECT CASE n !命令文
CASE 1 !端子
CALL CodeComment(c$)
IF POS(s$,"開始")>0 THEN
ELSEIF POS(s$,"終了")>0 THEN
PRINT "STOP"
ELSEIF POS(s$,"戻る")>0 THEN
PRINT "RETURN"
ELSE !関数、手続き
CALL CodeParm(parm$(s$),1) !引数
END IF
CASE 2 !結合子
IF d1$="" THEN PRINT "GOTO";JMP(VAL(s$));"!";c$ !出口なら
CASE 3 !処理
PRINT "LET ";var$(s$);"=";expr$(s$);
CALL CodeComment(c$)
CASE 4 !定義済み処理
CALL CodeParm(parm$(s$),0) !引数を展開する
FOR ii=1 TO w !関数の先頭を探す
LET jj=1 !!!FOR jj=1 TO h
DO UNTIL jj>h !!!
IF NAM$(ii+w*jj)=fnc$(s$) THEN
PRINT "GOSUB";100*ii+jj;
CALL CodeComment(c$)
EXIT FOR !!!
END IF
LET jj=jj+1 !!!NEXT jj
LOOP !!!
NEXT ii
IF POS(s$,"→")>0 THEN PRINT "LET ";var$(s$);"=";fnc$(s$) !関数なら、戻り値を設定
CASE 5 !判断
PRINT "IF NOT(";exprL$(s$);op$(d1$);exprR$(s$);") THEN ";
CALL CodeJump(VAL(d2$(1:1))) !「判断」は矢印が2本!
CASE 6 !繰り返しの開始
LET d2=VAL(d2$)
IF s$<>"" THEN !前判定
PRINT "IF ";s$;" THEN GOTO";10000+LBL(d2)
ELSE !後判定
IF LBL(d2)>0 THEN PRINT 10000+LBL(d2) !無条件は負
END IF
CASE 7 !繰り返しの終了
LET d2=VAL(d2$)
IF s$="" THEN !前判定
PRINT "GOTO";ABS(LBL(d2)) !無条件は負
PRINT 10000+ABS(LBL(d2))
ELSE !後判定
PRINT "IF NOT(";s$;") THEN GOTO";10000+LBL(d2)
END IF
CASE 8 !手操作入力
PRINT "INPUT PROMPT ";ToString$(c$);": ";s$
CASE 9 !表示
PRINT "PRINT ";ToString$(c$);"; ";s$
CASE 10 !入出力
PRINT "READ ";s$
PRINT "DATA ";c$

CASE 13 !NOP、結線
PRINT "!NOP"
CASE 16 !繰り返しの開始(FOR)
CALL CodeParm(parm2$(s$),0) !引数
PRINT "PARM2_";var2$(s$);"=PARM2"
PRINT "PARM3_";var2$(s$);"=PARM3"
PRINT var2$(s$);"=PARM1" !始値
PRINT "GOTO";20000+100*x+y
PRINT 30000+100*x+y
PRINT var2$(s$);"=";var2$(s$);"+PARM3_";var2$(s$) !+増分
PRINT 20000+100*x+y
LET d2=VAL(d2$) !DO UNTIL型へ
PRINT "IF (";var2$(s$);"-PARM2_";var2$(s$);")*SGN(PARM3_";var2$(s$);")>0 THEN GOTO";10000+LBL(d2)
CASE 17 !繰り返しの終了(NEXT)
LET d2=VAL(d2$)
PRINT "GOTO ";30000+ABS(LBL(d2)) !LOOP型へ
PRINT 10000+ABS(LBL(d2))
CASE 23 !準備
PRINT s$;
CALL CodeComment(c$)

CASE ELSE
END SELECT

IF d1$<>"" THEN CALL CodeJump(VAL(d1$(1:1))) !次の処理へ
END SUB

    ├つづき3 山中和義 2008/01/11 21:06:32  (修正2回)  ツリーへ

Re: 流れ図でアルゴリズムを検証する。2008.1.1...  返事を書く  ノートメニュー
山中和義 <drdlxujciw> 2008/01/11 21:06:32 ** この記事は2回修正されてます
つづき3


SUB CodeJump(d) !飛び先を生成する
PRINT "GOTO";
SELECT CASE d !方向に応じて
CASE 0 !右
PRINT 100*(x+1)+y
CASE 1 !上
PRINT 100*x+(y+1)
CASE 2 !左
PRINT 100*(x-1)+y
CASE 3 !下
PRINT 100*x+(y-1)
CASE ELSE
END SELECT
END SUB
SUB CodeParm(t$,flg) !引数を生成する ※t$=p1,p2,…,pn,形式
LET i=1
LET p=POS(t$,",")
DO WHILE p>0 AND t$<>","
IF flg>0 THEN
PRINT "LET ";trim$(t$(1:p-1));"=PARM";STR$(i) !引数から
ELSE
PRINT "LET PARM";STR$(i);"=";trim$(t$(1:p-1)) !引数へ
END IF
LET t$=t$(p+1:LEN(t$)) !eat it

LET i=i+1 !次へ
LET p=POS(t$,",")
LOOP
END SUB
SUB CodeComment(c$) !注釈を生成する
IF c$<>"" THEN PRINT " !";
PRINT c$
END SUB
!-------------------- ここまでがサブルーチン



LET h=7 !行数
LET w=5 !列数

LET s=MAX((w+1)*2,(h+1)*1.5)
SET WINDOW 0,s,0,s !表示領域 ※正方形
DRAW grid(2,1.5)

!1パス目(流れ図の描画、飛び先の行番号の定義)
RESTORE 10
FOR x=1 TO w !格子を走査する
FOR y=h TO 1 STEP -1
CALL ReadData(n,d1$,d2$,s$,c$) !流れ図記号を得る

DRAW FChart2(n,d1$,d2$,s$,c$) WITH SHIFT(x*2,y*1.5) !流れ図を描く

SELECT CASE n !コード生成に備えて位置を記録する
CASE 1 !端子
IF POS(s$,"開始")>0 THEN
ELSEIF POS(s$,"終了")>0 THEN
ELSEIF POS(s$,"戻る")>0 THEN
ELSE !関数、手続きなら
LET NAM$(x+w*y)=fnc$(s$) !名称
END IF
CASE 2!結合子
IF d1$<>"" THEN LET JMP(VAL(s$))=100*x+y !入口なら
CASE 6,16 !繰り返しの開始
LET d2=VAL(d2$) !ループ番号
IF s$<>"" THEN !前判定なら
LET LBL(d2)=100*x+y
ELSE
IF LBL(d2)>0 THEN !後判定が確定なら
ELSE
LET LBL(d2)=-(100*x+y) !後判定か無条件か
END IF
END IF
CASE 7,17 !繰り返しの終了
LET d2=VAL(d2$) !ループ番号
IF LBL(d2)>0 THEN !前判定が確定なら
ELSE
IF s$<>"" THEN LET LBL(d2)=100*x+y !後判定なら
!無条件は負!
END IF
CASE ELSE
END SELECT
NEXT y
NEXT x

!2パス目(コード生成)
RESTORE 10
FOR x=1 TO w !格子を走査する
FOR y=h TO 1 STEP -1
CALL ReadData(n,d1$,d2$,s$,c$)

IF n>0 THEN CALL CodeGen(n,d1$,d2$,s$,c$) !コードを生成する
NEXT y
NEXT x

    └つづき4 山中和義 2008/01/13 20:10:24  (修正1回)  ツリーへ

Re: 流れ図でアルゴリズムを検証する。2008.1.1...  返事を書く  ノートメニュー
山中和義 <drdlxujciw> 2008/01/13 20:10:24 ** この記事は1回修正されてます
つづき4


!列挙する ※h*w個

10 DATA 1,"3","開始","点数の最大値を求める" !※記号番号,矢印の向き,内容,注釈
DATA 3,"3","-1→MAX",""
DATA 8,"3","a",""
DATA 5,"3<","0≧","a:0","" !※コードは最初の等号・不等号を使う
DATA 9,"3","MAX","最大値"
DATA 1,"","終了",""
DATA 0,"","",""

DATA 0,"","",""
DATA 0,"","",""
DATA 13,"2","┬","" !NOP、結線
DATA 5,"0>","1≦","a:MAX",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""

DATA 0,"","",""
DATA 0,"","",""
DATA 13,"2","┐",""
DATA 3,"1","a→MAX","更新"
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""

DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""

DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""


END




●サンプル(差し替え)


10 DATA 1,"3","開始","1+2+3+…+n が300を超えるnを計算する"
DATA 3,"3","0→S","和"
DATA 16,"3","1","n=1,300,1","" !※指定回数の繰り返し d2$はループ番号
DATA 3,"3","S+n→S","累積していく"
DATA 5,"0>","3<=","S:300","" !※前が真、後が偽
DATA 17,"3","1","","" !※前判定 d2$はループ番号、s$=""
DATA 2,"","1",""

DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 2,"","1","ループを抜ける" !BREAK
DATA 0,"","",""
DATA 0,"","",""

DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""

DATA 0,"","",""
DATA 2,"3","1",""
DATA 9,"3","n","計算結果"
DATA 1,"","終了",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""

DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""

     └サンプル(右から左へ受け流す〜♪) 山中和義 2008/01/13 20:18:23  (修正1回)  ツリーへ

Re: つづき4  返事を書く  ノートメニュー
山中和義 <drdlxujciw> 2008/01/13 20:18:23 ** この記事は1回修正されてます
サンプル(右から左へ受け流す〜♪)

●DO〜LOOP型、関数

40 DATA 1,"3","開始","分数 A1/B1+A2/B2 の計算" !※記号番号,矢印の向き,内容,注釈
DATA 8,"3","A1,B1, A2,B2","※正の整数"
DATA 3,"3","A1*B2+A2*B1→A3","分子"
DATA 3,"3","B1*B2→B3","分母"
DATA 4,"3","GCD{B1,B2}→g","" !関数 ※引数なし GCD{}→g
DATA 2,"","2","" !※出口
DATA 0,"","",""

DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 2,"3","2","" !※入口
DATA 9,"3","A3/g, B3/g","計算結果"
DATA 1,"","終了",""

DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""

DATA 1,"3","GCD{a,b}","最大公約数" !関数 ※引数なし GCD{}→g
DATA 6,"0","1","MOD(a,b)=0","" !※前判定 d2$はループ番号
DATA 0,"","",""
DATA 7,"3","1","","" !※前判定 d2$はループ番号、s$=""
DATA 3,"3","b→GCD",""
DATA 1,"","戻る",""
DATA 0,"","",""

DATA 0,"","",""
DATA 3,"3","b → t",""
DATA 3,"3","MOD(a,b)→b",""
DATA 3,"2","t → a",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""





●配列

10 DATA 1,"3","開始","5つの数値を昇順に整列する"
DATA 23,"3","DIM A(5)","" !s$を直接命令となる
DATA 16,"3","1","i=1,5,1","" !※指定回数の繰り返し d2$はループ番号
DATA 10,"3","A(i)","24,17,16,31,18" !c$はデータ
DATA 17,"0","1","","" !※前判定 d2$はループ番号、s$=""
DATA 0,"","",""
DATA 0,"","",""

DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 16,"3","2","i=4,1,-1",""
DATA 16,"0","3","J=1,i,1",""
DATA 0,"","",""

DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 13,"0","┌","" !NOP、結線
DATA 5,"3>","1<=","A(J):A(J+1)","" !※前が真、後が偽
DATA 3,"0","A(J)→t",""

DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 17,"0","2","",""
DATA 17,"1","3","",""
DATA 3,"1","t→A(J+1)",""
DATA 3,"1","A(J+1)→A(J)",""

DATA 0,"","",""
DATA 0,"","",""
DATA 0,"","",""
DATA 16,"3","4","i=1,5,1",""
DATA 9,"3","A(i)",""
DATA 17,"3","4","",""
DATA 1,"","終了",""

      └高校数学「数値計算とコンピュータ」 山中和義 2008/01/22 20:37:31  (修正1回)  ツリーへ

Re: サンプル(右から左へ受け流す〜♪)  返事を書く  ノートメニュー
山中和義 <drdlxujciw> 2008/01/22 20:37:31 ** この記事は1回修正されてます
高校数学「数値計算とコンピュータ」

情報Bのアルゴリズムも若干含めています。


流れ図(フローチャート)を描き、検証する ※コード(BASIC言語)生成機能付き

http://www.urban.ne.jp/home/kz4ymnk/seminar/basic/flow.lzh


 インデックスへ  EXIT
新規発言を反映させるにはブラウザの更新ボタンを押してください。