-1

次の html を C のコンソールに出力したいのですが、私のコードにはバグがあり、本当に混乱してしまいました。バグは、最後の char 配列を再度出力することです。

出力例:

<html>
    <body>
        <p>Hey There!</p>
        <p>You can search for things on the internet at:
            <ul>
            <li> <a href="http://www.google.com">Google</a></li>
            <li> <a href="http://www.bing.com">Bing</a></li>
            </ul>
        </p>
    </body>
    </html>

My output:
<html>
    <body>
        <p>Hey There!</p>
        <p>You can search for things on the internet at:
            <ul>
            <li> <a href="http://www.google.com">Google</a></li>
            following line is incorrect. It prints the above line again.
            <li> <a href="http://www.bing.com" href="http://www.google.com">Bing</a></li> 
            </ul>
        </p>
    </body>
    </html>

私のコード:

#include <stdio.h>
#include <stdint.h>
#include <string.h>
void open_tag(char tag[], char attribute[]){
char str1[100];
char str2[100];
char str3[100];
//The bug happens here.The attribute[] is incorrect.
strcpy (str1,tag);
strncat (str1, attribute, 100);
strcpy (str2,"<");
strcpy (str3,">");
strncat (str2, str1, 100);
strncat (str2, str3, 50);
printf ("%s",str2);
}
void close_tag(char tag[]){
char str1[20];
char str2[20];
char str3[20];
strcpy (str1,tag);
strcpy (str2,"</");
strcpy (str3,">");
strncat (str2, str1, 6);
strncat (str2, str3, 6);
printf ("%s",str2);
}

int main(int argc, char *argv[])
{
open_tag("html", "");
printf("\n");
open_tag("body", "");
printf("\n");
open_tag("p", "");
printf("Hey There!");
close_tag("p");
printf("\n");
open_tag("p", "");
printf("You can search for things on the internet at:");
printf("\n");
open_tag("ul", "");
printf("\n");
open_tag("li", "");
char arr2[] = {' ','h','r','e','f','=','\"','h','t','t','p',':','/','/','w','w','w','.','g','o','o','g','l','e','.','c','o','m','\"'};
open_tag("a", arr2);
printf("Google");
close_tag("a");
close_tag("li");
printf("\n");
open_tag("li", "");
char arr1[] = {' ','h','r','e','f','=','\"','h','t','t','p',':','/','/','w','w','w','.','b','i','n','g','.','c','o','m',' ','\"'};  
open_tag("a", arr1);
printf("Bing");
close_tag("a");
close_tag("li");
printf("\n");
close_tag("ul");
printf("\n");
close_tag("p");
printf("\n");
close_tag("body");
printf("\n");
close_tag("html");
printf("\n");
return 0;
}
4

3 に答える 3

2

を作成して渡すattributeと、null で終了しません。そして、( を使用してstrcat) 100 文字を強制的にコピーし、最後に文字を追加しました ( <and >)

それらを渡す前に、文字列arr1と文字列にターミネータ文字をarr2(文字を追加して) 配置することをお勧めします。'\0'

次に、を使用しないstrncat(..., 100)でください。プレーンを使用してstrcat()ください。

于 2013-02-11T04:48:52.423 に答える
1

これ:

void open_tag(char tag[], char attribute[]){
    char str1[100];
    char str2[100];
    char str3[100];
    //The bug happens here.The attribute[] is incorrect.
    strcpy (str1,tag);
    strncat (str1, attribute, 100);
    strcpy (str2,"<");
    strcpy (str3,">");
    strncat (str2, str1, 100);
    strncat (str2, str3, 50);
    printf ("%s",str2);
}

これは、私が今まで見た中で最も複雑な書き方の 1 つに違いありません。

void open_tag(char const *tag, char const *attribute)
{
    printf("<%s %s>", tag, attribute);
}

この置換では、タグまたは属性の長​​さに恣意的な制限が課されないことに注意してください。そのため、信頼性も高くなります (さらに読みやすくなります)。

このclose_tag()関数も同様にグロテスクです — 次のように縮小できます。

void close_tag(char const *tag)
{
    printf("</%s>", tag);
}

もちろん、これは null で終了する文字列を関数に渡すことを前提としています。そうしないと、何も見えなくなってしまいます。だから、あなたのコード:

char arr2[] = {' ','h','r','e','f','=','\"','h','t','t','p',':','/','/','w','w','w','.','g','o','o','g','l','e','.','c','o','m','\"'};
open_tag("a", arr2);

次のように、より簡潔なものにする必要があります。

open_tag("a", "href=\"http://www.google.com\"");

リテラル文字列の constness は、char const *tag引数が変更されていないため、引数を望ましいものにします。

于 2013-02-11T04:54:20.600 に答える
0

コードをクリーンアップし、

void open_tag(char* tag, char* attribute){

printf("<%s%s>",tag,attribute);

}

void close_tag(char* tag){

printf("</%s>",tag);    

}

int main(int argc, char *argv[])
{
char arr1[] = {' ','h','r','e','f','=','\"','h','t','t','p',':','/','/','w','w','w','.','b','i','n','g','.','c','o','m',' ','\"','\0'};  
char arr2[] = {' ','h','r','e','f','=','\"','h','t','t','p',':','/','/','w','w','w','.','g','o','o','g','l','e','.','c','o','m','\"','\0'};

open_tag("html", "");
printf("\n\t");

open_tag("body", "");
printf("\n\t\t");

open_tag("p", "");
printf("Hey There!");
close_tag("p");
printf("\n\t\t");

open_tag("p", "");
printf("You can search for things on the internet at:\n\t\t\t");
open_tag("ul", "");
printf("\n\t\t\t\t");
open_tag("li", "");

open_tag("a", arr2);
printf("Google");
close_tag("a");
close_tag("li");
printf("\n\t\t\t\t");
open_tag("li", "");
open_tag("a", arr1);
printf("Bing");
close_tag("a");
close_tag("li");
printf("\n\t\t\t");
close_tag("ul");
printf("\n\t\t");
close_tag("p");
printf("\n\t");
close_tag("body");
printf("\n");
close_tag("html");
printf("\n");

return 0;
}

出力:

<html>
    <body>
        <p>Hey There!</p>
        <p>You can search for things on the internet at:
            <ul>
                <li><a href="http://www.google.com">Google</a></li>
                <li><a href="http://www.bing.com ">Bing</a></li>
            </ul>
        </p>
    </body>
</html>

コードの問題: 1. インデント 2. 配列内のヌル文字 3. 不要なコード

私はあなたのコードをきれいにしましたが、あなたはこのコーディング方法が実際には必要ではないことを認識しています.

于 2013-02-11T05:21:53.017 に答える