如何从键盘输入得到 gets ( next_input ) == NULL ?

drizztguai 2008-02-10 05:37:10
输入一组字符串,如果连续几行相同,则将此行输出.
比如输入:
iamlesbian
iamlesbian
iamloli
iamloli
iamloli
iamyourhoney

则输出
iamlesbian
iamloli

弄了个半天好象调试是没错误了,思路估计也正确, 但不知道输入才可以... 就不是知道怎么输入才能得到 gets ( next_input ) != NULL 的结果... 希望大虾们解释一下


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_COLS 128


int
main ( void )
{
char now_input [MAX_COLS];
char next_input [MAX_COLS];
char output [MAX_COLS];

gets ( now_input ); /* 这个函数的意义更大的再与输入,而不在他返回的指针 */

while ( gets ( next_input ) != NULL );{
/*
** 相同则拷贝到output 中 输出。
*/
if ( !(strcmp ( now_input , next_input )) ){
strcpy ( output , now_input );
puts ( output );
}
strcpy ( now_input , next_input );
}
return EXIT_SUCCESS;
}



...全文
130 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ygy4444 2011-11-26
  • 打赏
  • 举报
回复
这程序编得,再错多几次真成蕾丝边了~~~~~
visame 2008-02-11
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_COLS 128


int
main ( void )
{
char now_input [MAX_COLS];
char next_input [MAX_COLS];
char output [MAX_COLS];
bool already=false;//加了一个already标志位
gets ( now_input );
while ( gets ( next_input ) != NULL )
{
if ( (strcmp ( now_input , next_input )==0) )
{
if (already==false)
{
strcpy (output , now_input );
puts ( output );
already=true;
}
}
else
{
strcpy (now_input , next_input );
already=false;
}
}
return EXIT_SUCCESS;
}
drizztguai 2008-02-10
  • 打赏
  • 举报
回复
我的程序再输入2次 iamloli
就会自动输出一遍 iamloli了。.

郁闷。
drizztguai 2008-02-10
  • 打赏
  • 举报
回复
谢谢楼上的...

果然达到效果了。

那现在我想实现这样的效果应该怎么更改程序呢?
我输入

iamlesbian
iamlesbian
iamloli
iamloli
iamloli
iamyourhoney

然后屏幕再打出:

iamlesbian
iamloli

nobush 2008-02-10
  • 打赏
  • 举报
回复
while ( gets ( next_input ) != NULL ); 末位的分號不要
drizztguai 2008-02-10
  • 打赏
  • 举报
回复
谢谢。

可是我试了Ctrl+Z
只是在屏幕上出现了 ^Z 的效果...

程序并没有终止的意思...

为什么呢。
drizztguai 2008-02-10
  • 打赏
  • 举报
回复
谢谢。

可是我试了Ctrl+Z
只是在屏幕上出现了 ^Z 的效果...

程序并没有终止的意思...

为什么呢。
mLee79 2008-02-10
  • 打赏
  • 举报
回复
win 下 Ctrl+Z , linux 下 Ctrl+D .
drizztguai 2008-02-10
  • 打赏
  • 举报
回复
晕.
应该把 if ( !(strcmp ( now_input , next_input )) ){
---------->
if ( !(strcmp ( now_input , next_input ) )&& (strcmp ( output , now_input)) ){

这样三行相同的话也就只输出一行了.

对了,还有个类似的问题。 就是如何输入EOF?


#include <stdio.h>

int
main( void )
{
double nc;
for ( nc=0; getchar() !=EOF; ++nc)
;
printf("%.0f \n",nc);
}

怎样输入才能得到eof?

69,370

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧