1-22

#include <stdio.h>

#define MAXCOL 10
#define TABIN 8 

char line[MAXCOL];

int exptab(int pos);
int findblnk(int pos);
void printl(int pos);
int newpos(int pos);

main()
{
    int c, pos = 0;
    int temp = 1;

    while ((c = getchar())!=EOF)
    {
        line[pos] = c;
        if(c == '/t')
            pos = exptab(pos);//若为制表符,把它转换成空格,并且该行结束。直接输出
        else if(c == '/n'){
            printl(pos);
            pos = 0;
        }
        else if(++pos >= MAXCOL){        //这里的空格不管的
            pos = findblnk(pos);//整理,去掉最后的空格
            printl(pos);
            pos = newpos(pos);  //找到新的最早的位置。重置过后。
        }
    }
}

/*printl:print line until pos column*/
void printl(int pos)
{
    int i;
    for (i = 0; i<pos; ++i)
        putchar(line[i]);
    if(pos > 0)
        putchar('/n');
}

/*exptab: expand tab into blanks*/
int exptab(int pos)
{
    line[pos] = ' ';
    for(++pos; pos<MAXCOL && pos %TABIN != 0; ++pos)
        line[pos] = ' ';//ÔÚ¸ÃÐÐÓöµ½ÖƱí·û£¬ÔòÔÚÓöµ½ÏÂÒ»¸öÖƱí·û֮ǰʣϵÄÓÿոñÌîÂú¡£
    if(pos<MAXCOL)
        return pos;
    else{
        printl(pos);
        return 0;
    }
}

/*findblnk: find blank's position*///去掉末尾的空格
int findblnk(int pos)
{
    while(pos >0 && line[pos] != ' ')
        --pos;
    if(pos == 0)
        return MAXCOL; // no blanks
    else
        return pos+1;
}

int newpos(int pos)
{
    int i, j;

    if(pos <= 0 || pos >= MAXCOL)
        return 0;
    else{
        i = 0;
        for(j = pos; j<MAXCOL; ++j)
        {
            line[i] = line[j];
            ++i;
        }
        return i;
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值