当前位置: 代码迷 >> C语言 >> [讨论]此程序是否有错?
  详细解决方案

[讨论]此程序是否有错?

热度:155   发布时间:2007-09-25 17:06:52.0
[讨论]此程序是否有错?

目的:
将空格串替换为最少的制表符和空格,但要保持单词之间的空格不变!
代码:
#include<stdio.h>
#define TABINC 8
void main()
{
int c,nb,nt,pos;
nb=0;//of blanks necessary
nt=0;//of tabs necessary
for(pos=1;(c=getchar())!=EOF;++pos)

if(c==' ')
{
if(pos%TABINC!=c)
++nb;//increment of blanks
else
{
nb=0;//reset of blanks
++nt;//one more tab
}
}

else
{
for(;nt>0;--nt)
putchar('\t');//output tabs
if(c=='\t');//forget the blanks
nb=0;
else //output blanks
for(;nb>0;--nb)
putchar(' ');
putchar(c);
if(c=='\n')
pos=0;
else if(c=='\t')
pos=pos+(TABINC-(pos-1)%TABINC)-1;
}
}

搜索更多相关的解决方案: 单词  necessary  include  制表符  

----------------解决方案--------------------------------------------------------


----------------解决方案--------------------------------------------------------
以下是引用yangzhifu在2007-9-25 17:06:52的发言:

目的:
将空格串替换为最少的制表符和空格,但要保持单词之间的空格不变!
代码:
#include<stdio.h>
#define TABINC 8
void main()
{
int c,nb,nt,pos;
nb=0;//of blanks necessary
nt=0;//of tabs necessary
for(pos=1;(c=getchar())!=EOF;++pos)

if(c==' ')
{
if(pos%TABINC!=c)
++nb;//increment of blanks
else
{
nb=0;//reset of blanks
++nt;//one more tab
}
}

else
{
for(;nt>0;--nt)
putchar('\t');//output tabs
if(c=='\t');//forget the blanks这里要这个;么?如果要,下面的else没有匹配的了……
nb=0;
else //output blanks
for(;nb>0;--nb)
putchar(' ');
putchar(c);
if(c=='\n')
pos=0;
else if(c=='\t')
pos=pos+(TABINC-(pos-1)%TABINC)-1;
}
}


----------------解决方案--------------------------------------------------------
当然要了!匹配啊!
----------------解决方案--------------------------------------------------------
嘴硬 哈哈

----------------解决方案--------------------------------------------------------

  相关解决方案