`
lovnet
  • 浏览: 6720430 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

HDU 1088 Write a simple HTML Browser

 
阅读更多
题目链接:Click here~~
继续水字符串处理。PE了好几次,没有考虑 '\t' 的情况。
题意:将一段HTML代码格式按要求调整。
解题思路:
用一个pos记录当前行已经输入了几个字符,然后如果位于行首,单词前不用输空格,否则输空格。
还有就是一些小细节处理,比如最后要输出一个换行,其他不再详述,见代码。
#include <stdio.h>
int pos,l;
void BR()
{
    printf("\n");
    pos = 0;
}
void HR()
{
    for(int i=1;i<=80;i++)
        putchar('-');
}
int main()
{
    pos = l = 0;
    char ch,word[88];
    while((ch = getchar())!=EOF)
    {
        if(ch == ' ' || ch == '\n' || ch == '\t')
        {
            if(l == 0)
                continue;
            word[l] = '\0';
            if(pos+l+1 > 80)
                BR();
            printf(pos?" %s":"%s",word);
            pos += (pos?l+1:l);
            l = 0;
        }
        else if(ch == '<')
        {
            ch = getchar();
            if(ch == 'b')
                BR();
            else
            {
                if(pos)
                    BR();
                HR();
                BR();
            }
            scanf("%*c%*c");
        }
        else
            word[l++] = ch;
    }
    BR();
    return 0;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics