2016-10-10 6 views
0

"演習1-23。Cプログラムからすべてのコメントを削除するプログラムを作成する 引用符で囲まれた文字列と文字定数 を正しく処理することを忘れないでください。K&R Exe。 1-23:いくつかの合併症

1)私は完全に新しいコーディングだと私は、私は、少なくとも問題に正しい方法を考えているかどうかを知りたいと思った:K & Rのpg.34

は基本的に、私は2つの質問があります。

2)//\nまたは/*から*/まで無視するようにコードが構築されています。しかし、白い/*コメントは常に1つを残す/


入力:abc/*comment*/123

出力:abc/123


入力:abc/*123

OutpのUT:abc/


#include <stdio.h> 
char s[1000]; //Principal array 
int countS; //Number of char in array 

int deletSingleLineComments(void); 
int deletMultiLineComments(void); 

int main(void){ 
    int c; 
    while((c=getchar())!=EOF){ 
     s[countS]=c; 
     ++countS; 
    } 

    deletMultiLineComments(); //Function 1 
    deletSingleLineComments(); //Function 2 

    printf("\ns[]=\n%s\n\ncountS[]=%d\n",s,countS); 
} 



//Functions 1 
int deletMultiLineComments(void){ 
    char t[1000]; 
    int i=0; 
    int inComment=0; 
    int diff=0; 
    int a,b,c; 

    while(i<=countS){ 
     t[i]=s[i]; 
     ++i; 
    } 
    i=0; 

    while(i<=countS){ 

     if(t[i]=='/' && t[i+1]=='*'){ 
      inComment=1; 
     } 

     if(inComment==1){ 
      ++diff; //to equilibrate the number 
     } 

     if(inComment==0){ 
      s[i-diff]=t[i]; 
     } 

     if(t[i]=='*' && t[i+1]=='/'){ 
      inComment=0; 
     } 
     ++i; 
    } 
    s[i-diff+1]='\0'; 
    countS=i-diff; 

    printf("\nt[]=\n%s\n",t); 
} 



//Function 2 
int deletSingleLineComments(void){ 
    int i=0; 
    char t[1000]; 
    int inComment=0; 
    int diff=0; 

    while(i<=countS){ 
     t[i]=s[i]; 
     ++i; 
    } 
    i=0; 

    while(i<=countS){ 

     if(t[i] == '/' && t[i+1] == '/'){ 
      inComment=1; 
     } 

     if(t[i]=='\n'){ 
      inComment=0; 
     } 

     if(inComment==1){ 
      ++diff; 
     } 

     if(inComment==0){ 
      s[i-diff]=t[i]; 
     } 
     s[i-diff+1]='\0'; 
     ++i; 
    } 
    countS=i-diff; 
} 

ありがとうございました。

+0

は、あなたのプログラムの実行をトレースするためにデバッガを使用することがありますか?あるいは基本的なprintfデバッグさえ?効果的なデバッグの経験を積み重ねることは、その努力の価値があります。 – kaylum

+0

デバッガをインストールする必要がありますか、それとも既に持っていますか?私はIOのエルキャピタンでTextWranglerでコーディングしています。 –

答えて

0
while(i<=countS){ t[i]=s[i];... } 

文字列はゼロベースであることに注意してください。たとえば、"ABC"は、長さが3であり、ゼロインデックスから開始し、最後の有効インデックスは23ではない)です。したがって、あなたはi < string_length

while(i < countS){ t[i]=s[i];... } 

に条件を変更する必要がありt[i+1]にアクセスする際iが有効である一方で、i+1がバウンドの外にある可能性があるためまた、注意してください。別の文字列を代入するために

if (i < (countS - 1)) 
    if(t[i]=='/' && t[i+1]=='*') 

、あなたは第二の可変kを導入し、各割り当て後kをインクリメントすることができます。この方法は、diff変数を使用し、加算と減算を行うよりも簡単です(私の意見では)。また

は、むしろchar t[1000];よりも、あなたは長さcountSの一時変数を宣言するchar *t = malloc(countS);を使用することができ、それはfree(t)で終わりで解放する必要があります。コンパイラが可変長配列をサポートしている場合は、char t[countS]とするだけです。

例:

char s[1000]; //Principal array 
int countS; //Number of char in array 

//Functions 1 
void deletMultiLineComments(void) 
{ 
    char *t = malloc(countS); 
    int i = 0; 
    int k = 0; 
    int inComment = 0; 

    while (i < countS) 
    { 
     t[i] = s[i]; 
     ++i; 
    } 

    i = 0; 
    while (i < countS) 
    { 
     if (i < countS - 1) 
     if (t[i] == '/' && t[i + 1] == '*') 
     { 
      inComment = 1; 
      i+=2; 
      continue; 
     } 

     if (inComment == 1) 
     { 
      if (i < countS - 1) 
      if (t[i] == '*' && t[i + 1] == '/') 
      { 
       inComment = 0; 
       i+=2; 
       continue; 
      } 
     } 

     if (inComment == 0) 
     { 
      s[k] = t[i]; 
      k++; 
     } 

     ++i; 
    } 

    free(t); 
    s[k] = '\0'; 
    countS = k; 

    printf("mulitline comment removed %s\n", s); 
} 

//Function 2 
void deletSingleLineComments(void) 
{ 
    char *t = malloc(countS); 
    int i = 0; 
    int k = 0; 
    int inComment = 0; 

    while (i < countS) 
    { 
     t[i] = s[i]; 
     ++i; 
    } 

    i = 0; 
    while (i < countS) 
    { 
     if (i < countS - 1) 
      if (t[i] == '/' && t[i + 1] == '/') 
      { 
       inComment = 1; 
       i += 2; 
       continue; 
      } 

     if (t[i] == '\n') 
     { 
      inComment = 0; 
     } 

     if (inComment == 0) 
     { 
      s[k] = t[i]; 
      k++; 
     } 

     i++; 
    } 
    free(t);  
    s[k] = '\0'; 
    countS = k; 

    printf("single comment removed %s\n", s); 
} 

int main(void) 
{ 
    //get input 
    scanf("%s", s); 

    countS = 0; 
    while (s[countS]) countS++; 

    deletMultiLineComments(); //Function 1 
    deletSingleLineComments(); //Function 2 
} 
関連する問題