是时候问了? [英] Time to ask?

查看:43
本文介绍了是时候问了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在K& R。


我正在写这个练习并碰壁。

这是代码全..(因为这通常是要求)。我的问题

是;


1)为什么打印4行而不是预期的2行?

2)我是使用X代码编译这个....不确定这是否有任何

与它有关。当我单步执行代码时,我会尽可能地将b $ b写入HANGS HERE。它不会再进一步​​了。我假设这2个
是否相关?或不? ......但是,我觉得我需要一些更聪明的头脑。


提前谢谢./


#include< stdio.h>

#include< string.h>

#define MAXNUM 10

#define TESTCHAR ''T''


int getops(char s []);

int main(){

char s [ MAXNUM],输入;

while((type = getops(s))!= EOF){

开关(类型){


case''T'':

printf("%s \ n",s);

休息;

}


}


返回0;

}

/ *** ** GETOPS ****** /


int getch(void);

void ungetch(int c);

int getops(char s []){


int c,i;

while((c = getch())=='''' || c ==''\t'');

for(i = 0; i< = 1; i ++)/ ***** HANGS HERE *** /

ungetch(''T'');

}


/ ***** GETCH& UNGETCH ***** /


#define MAXBUFF 10

char buffer [MAXBUFF];

int buffp = 0;

int j = 1;

int getch(void){

return(buffp 0)? buffer [ - buffp]:getchar();


}


void ungetch(int c){

printf(" Printing line%d \ nn",j ++);


}

/ ******


输出(输入p):


>> / begin输出/<<



p

印刷线1

印刷线2

印刷线3

印刷线4


>> /输出结束/<< ;



** /

解决方案


int getops(char s []){


int c,i;

while((c = getch ())==''''|| c ==''\ t'');

for(i = 0; i< = 1; i ++)/ **** * HANGS HERE *** /

ungetch(''T'');

}



First我注意到的事情是getops没有返回任何东西。

这可能是问题吗?


MQ


mdh写道:


我还在K& R。


我在写这个练习的过程已经碰壁了。

这里是完整的代码..(因为这通常被要求)。我的问题

是;


1)为什么打印4行而不是预期的2行?

2)我是使用X代码编译这个....不确定这是否有任何

与它有关。当我单步执行代码时,我会尽可能地将b $ b写入HANGS HERE。它不会再进一步​​了。我假设这2个
是否相关?或不? ......但是,我觉得我需要一些聪明的头脑来这个

点。


提前谢谢./



我冒昧地剪掉你的代码。你没有编码

的问题。你有一个理解问题。 getch()和ungetch()是

而不是标准C函数。它们可以实现为扩展。 Borland

和DJGPP浮现在脑海中,在conio.h中宣称并且肯定还有其他人。

他们不是标准C.


-

Joe Wright

所有东西都应尽可能简单,但并不简单。

---阿尔伯特爱因斯坦---


你没有编码


问题。你有一个理解问题。 getch()和ungetch()是

而不是标准C函数。



OP已实现这些功能。看一下

列表的底部。


MQ


I am still at K&R.

I am in the process of writing this exercise and have run into a wall.
Here is the code in full..( as this is usually asked for). My questions
are;

1) Why does it print 4 lines instead of the expected 2?
2) I am using X-code to compile this....not sure if this has anything
to do with it. When I step through the code, I get as far as where I
have written "HANGS HERE" and it goes no further. I am assuming these 2
are related? or not? ...But, I think I need some smarter minds at this
point.

Thanks in advance./

# include <stdio.h>
# include <string.h>
# define MAXNUM 10
# define TESTCHAR ''T''

int getops(char s[]);
int main (){
char s[MAXNUM], type;
while ( (type=getops(s)) != EOF){
switch (type) {

case ''T'':
printf("%s\n", s);
break;
}

}

return 0;
}
/*****GETOPS******/

int getch(void);
void ungetch(int c);
int getops(char s[]){

int c, i;
while ( (c = getch()) == '' '' || c ==''\t'');
for ( i=0; i <= 1; i++) /*****HANGS HERE***/
ungetch(''T'');
}


/***** GETCH & UNGETCH*****/

# define MAXBUFF 10
char buffer[MAXBUFF];
int buffp=0;
int j=1;
int getch(void){
return (buffp 0) ? buffer[--buffp]: getchar() ;

}

void ungetch(int c){
printf("Printing line %d\n", j++);

}
/******

Output("p" entered):

>>/beginning of output/<<

p
Printing line 1
Printing line 2
Printing line 3
Printing line 4

>>/end of output/<<


**/

解决方案

int getops(char s[]){

int c, i;
while ( (c = getch()) == '' '' || c ==''\t'');
for ( i=0; i <= 1; i++) /*****HANGS HERE***/
ungetch(''T'');
}

First thing I have noticed is that getops does not return anything.
Could this be the problem?

MQ


mdh wrote:

I am still at K&R.

I am in the process of writing this exercise and have run into a wall.
Here is the code in full..( as this is usually asked for). My questions
are;

1) Why does it print 4 lines instead of the expected 2?
2) I am using X-code to compile this....not sure if this has anything
to do with it. When I step through the code, I get as far as where I
have written "HANGS HERE" and it goes no further. I am assuming these 2
are related? or not? ...But, I think I need some smarter minds at this
point.

Thanks in advance./

I have taken the liberty of snipping your code. You don''t have a coding
problem. You have an Understanding problem. getch() and ungetch() are
not Standard C functions. They may be implemented as extensions. Borland
and DJGPP come to mind, declared in conio.h and there are surely others.
They are not Standard C.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---


You don''t have a coding

problem. You have an Understanding problem. getch() and ungetch() are
not Standard C functions.

The OP has implemented these functions. Take a look at the bottom of
the listing.

MQ


这篇关于是时候问了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆