现在的位置: 首页 > 综合 > 正文

2.2 在不使用运算符&&或|| 的条件下编写一个与上面的for循环语句等价的循环语句。

2013年07月29日 ⁄ 综合 ⁄ 共 426字 ⁄ 字号 评论关闭

enum loop{NO, YES};
enum loop okloop = YES;
i = 0;
while(okloop == YES)
{
 if(i >= lim - 1)
  okloop = NO;
 else if((c = getchar()) != '\n')
  okloop = NO;
 else if(c == EOF)
  okloop = NO;
 else
  s[i++] = c;
}

这里用了 一个枚举变量。

 

int main(void)
{
/*
for (i = 0; i < lim-1 && (c=getchar()) != '\n' && c != EOF; ++i)
s[i] = c;
*/
int i = 0,
lim = MAX_STRING_LENGTH,
c;
char s[MAX_STRING_LENGTH];
while (i < (lim - 1))
{
c = getchar();
if (c == EOF)
break;
else if (c == '\n')
break;
s[i++] = c;
}
s[i] = '\0'; /* terminate the string */
return 0;
}

抱歉!评论已关闭.