0

8051 MCU で文をカウントすると思われる以下のコードをインターネットで見つけました。疑問符がある場所で正確に何が起こっているのか、誰か説明してもらえますか。どんな種類の助けでも大歓迎です。

#include<string.h>

char code  *text=" what  is a program? that  has,   a   a lot of errors!  When "   ;     
char code  *text1=" you compile. this  file,   uVision. reports a number of? ";
char code  *text2=" problems that you! may interactively correct. "  ;  //Null characters are also included in array!!!

void count ( char pdata*  ,  char pdata*); 

void main (void){

char pdata   Nw,Ns;
char data TextNw[2],TextNs[2];
    count(&Nw, &Ns); // call subroutine
    TextNw[0]=Nw/10;   //?????????????????????????????????
    TextNw[1]=Nw%10;   //?????????????????????????????????
    TextNs[0]=Ns/10;   //?????????????????????????????????
    TextNs[1]=Ns%10;   //?????????????????????????????????

    while(1);

}


void count ( char pdata *Nw, char pdata  *Ns ){


unsigned char N, i, ch;
typedef enum  {idle1, idle2} state;   //?????????????????????????????????
state S;   // begining state


    P2=0x00;        // pdata bank definition it must be performed first!! 
    *Ns=*Nw=0;      // without proper start-up there is no initialisation, initialise now!! 
    S=idle1;        // beginning state
    N=strlen(text)+strlen(text1)+strlen(text2)+3; //????????????? + 3 to acount 3 Null characters!
    P2=0x00;                                      // pdata bank definition
    for(i=0;i!=N;i++){
        ch=text[i];                               // take a caharacter from the text
        switch (S)
        {
            case (idle1):{
                if (ch==0) break;                 // skip NULL terminating character!
                if (ch!=' '){
                            S=idle2;
                            (*Nw)++;
                            }
                break;
            }
            case(idle2):{
                if (ch==0) break;                  // skip NULL terminating character!
                if((ch==' ')||(ch==','))S=idle1;
                else if ((ch=='?')||(ch=='.')||(ch=='!')){
                                                        S=idle1;
                                                        (*Ns)++;
                                                        }
                break;
            }
          }

     }

} 
4

1 に答える 1

1

このプログラムは、2 つのことを組み合わせて実行します。つまり、テキスト内の文の数を数え、テキスト内の単語の数を数えます。カウントが完了すると、結果が 2 文字の配列に格納されます。たとえば、3 つの文で 57 語の場合、結果は次のように保存されます: TextNw = {'5','7'}and TextNs = {'0','3'}.

変数Nには、3 つのヌル終了文字 (文ごとに 1 つ) が追加されたテキストの全長が含まれます。

アルゴリズムは単語と文章を同時に数えます。状態ではidle1、カウントは単語カウント モードです。状態ではidle2、カウントはセンテンス カウント モードです。モードは、読み取られている現在の文字に応じて交換されます。区切り文字が検出されると、適切なカウンターが増加します。

于 2015-05-03T21:03:23.570 に答える