『Cプログラミング診断室』目次次(第7章 文字処理は得意 文字列処理)

第7章 文字処理は得意

goto


リスト7−2の後半に、goto Jump2; がありますね。上のif文でmsgnumがTIMEUPのとき、次の switch文の途中に飛び込んでいます。goto文を使うにしても、これはとんでもない使い方です。 switch文の途中や、for ループの途中へのgoto、つまり、{} で囲んだブロック内部に外から飛び 込むのは、 gotoの使い方の中でも最悪です。こんな書き方は、どんな理由があっても「厳禁」で す。

ここは、どうしてわざわざgoto文を使っているのだろうと思ってしまいます。つぎのswitch文の case 3 のところへ飛び込んでいますが、これならば、case 3の個所に case TIMEUPを並べて書い てしまえば、goto文を使う必要などまったくありません。まったく想像もできない使い方です。

リスト7−3の中にもgoto文があります。152行で名札Retryにgotoすることで、ループを実現し ています。このループは、ループの最後で終了判定をしているので、do-while文で実現するのが自 然です。

177行の名札 Next2 は、どこからも参照されていないので、消してしまいましょう。 リスト7−5、7−6が、goto文を取り除いたものです。break か return しかなくなり、見通 しが良くなっているでしょう。

 
リスト7−6 Job Process【修正版】

     1	/********************************************************************************/
     2	/*                                                                              */
     3	/*       *** CmdMake_DTS ***                                                    */
     4	/*                                                                              */
     5	/*       Job process of Command Make program                                    */
     6	/*                                                                              */
     7	/********************************************************************************/
     8	
     9	#include "mydefs.h"
    10	
    11	#define UNYOU0PATH      "/D0/DATA/Unyou_0"
    12	#define UNYOU1PATH      "/D0/DATA/Unyou_1"
    13	
    14	/********************************************************************************/
    15	/*       Job 2 (運用時間の登録抹消)                                           */
    16	/********************************************************************************/
    17	Job2()
    18	{
    19	        int     msgnum2;                /* message number               */
    20	        int     fpath;                  /* file path                    */
    21	        int     i;                      /* offset for unyou table       */
    22	        char    date[9];                /* arry of yy,mm,dd,hh,mm       */
    23	
    24	        static  char    *mes_coms[]  = { M150, M170, M180 };
    25	        static  char    *mes_unyou[] = { M692, M694 };
    26	        static  char    *mes_time[]  = { M720, M730 };
    27	        static  char    *mes_nums[]  = {
    28	                        M900, M910, M920, M930, M940, M950, M960, M970, M980, M990,
    29	                        M150,           /* chuushi              */
    30	                        M192            /* retry                */
    31	        };
    32	
    33	        /* 運用時刻の表示 */
    34	        cls();
    35	        DispMsgs( Number(mes_unyou), mes_unyou );
    36	
    37	        /* 運用データの読み込み */
    38	        fpath = open((jobid==0)? UNYOU0PATH: UNYOU1PATH, S_IREAD);
    39	        if(fpath == ERROR)
    40	                xexit(413);
    41	        lseek(fpath, 0L, 0);
    42	        if(read( fpath, unyoutbl, sizeof(unyoutbl)) == ERROR)
    43	                xexit(414);
    44	        close(fpath);
    45	
    46	        /* 運用データの表示 */
    47	        for( i=0; i<Number(unyoutbl); i++ ) {
    48	                locate( stderr, mes_unyou[i]->x+KANJILEN*4, mes_unyou[i]->y );
    49	                fprint_time( wpath, NULL, &unyoutbl[offset] );
    50	        }
    51	
    52	        DispMsgs( Number(mes_coms), mes_coms );
    53	        switch( ReadLPen( Number(mes_coms), mes_coms, 60 * 1) ) {
    54	        case TIMEUP:
    55	        case 1: /* 中止     */
    56	                return;
    57	        case 2: /* 印字     */
    58	                if(PaperChk() == ON){
    59	                        PaperChkPutc(CR);
    60	                        if(PaperChk() == ON){
    61	                                fprint_time( ppath, M692, 0 );
    62	                                fprint_time( ppath, M694, 1 );
    63	                        }
    64	                        PaperChkPutc(CR);
    65	                }
    66	        case 3: /* 次へ     */
    67	        default:
    68	                break;
    69	        }
    70	
    71	        do {    /* 運用開始時刻と運用終了時刻の入力 */
    72	                cls();
    73	                DispMsgs( Number(mes_time), mes_time );
    74	                DispMsgs( Number(mes_nums), mes_nums );
    75	
    76	                msgnum2 = Select3( Number(mes_time), Number(mes_nums),
    77	                                        mes_nums, mes_time, date );
    78	                if(msgnum2 == msgcnt - 1)
    79	                        return;
    80	        } while( msgnum2 == msgcnt )
    81	
    82	        unyoutbl[0].t_year   = ON;
    83	        unyoutbl[0].t_hour   = atoin(&date[0],2);
    84	        unyoutbl[0].t_minute = atoin(&date[2],2);
    85	        unyoutbl[1].t_year   = ON;
    86	        unyoutbl[1].t_hour   = atoin(&date[4],2);
    87	        unyoutbl[1].t_minute = atoin(&date[6],2);
    88	
    89	        /* 中止 か 実行 */
    90	        cls();
    91	        if( Select2() == STOP2 )
    92	                return;
    93	        UnyouIssue(FALSE);
    94	}
    95	
    96	/********************************************************************************/
    97	/*      n文字の文字列の整数変換                                                */
    98	/********************************************************************************/
    99	int     atoin( str, n )
   100	  char  *str;
   101	  int   n;
   102	{
   103	        char    buf[20];
   104	
   105	        strncpy( buf, str, n );
   106	        buf[n] = '\0';
   107	        return  atoi( buf );
   108	}
   109	
   110	/********************************************************************************/
   111	/*      時刻の表示と印字                                                        */
   112	/********************************************************************************/
   113	fprint_time( f, mes, offset )
   114	  FILE     *f;
   115	  message  *mes;
   116	  int      offset;
   117	{
   118	        int     hh=0, mm=0;
   119	        char    outbuf[40];  
   120	
   121	        if(unyoutbl[offset].t_year == ON) {
   122	                hh = unyoutbl[offset].t_hour;
   123	                mm = unyoutbl[offset].t_minute;
   124	        }
   125	
   126	        if( mes ) {
   127	                sprintf( outbuf, "  %s  %02d:%02d", mes->text, hh, mm );
   128	        } else {
   129	                sprintf( outbuf, "  %02d:%02d", hh, mm );
   130	        }
   131	        fprintf( f, "%s\n", outbuf );
   132	        fflush( f );
   133	}
   134	
   135	/********************************************************************************/
   136	/*                      End of File "job.c"                                     */
   137	/********************************************************************************/


Copyright1996 Hirofumi Fujiwara. No reproduction or republication without written permission
『Cプログラミング診断室』目次次(第7章 文字処理は得意 文字列処理)