K& R 5-10
我想知道我是否可以寻求帮助。我一天中大部分时间都在苦苦挣扎这个
,并且无法理解发生了什么。问题
是(1)它不起作用!! (但通常在编译时退出...和
(2)当逐步执行时(我使用Xcode)它会在指示的函数中没有任何错误地崩溃
消息。我解剖了它,单独运行
个别零件...这似乎工作正常。感谢
提前。
我已供应3个命令行参数(22.3,11.9和''+''所以
argc是4)。
我包括完整的代码,因为可能有一个很多我错过了
和搞砸了。
I wonder if I could ask for help. I have been struggling with this
most of the day, and cannot understand what is going on. The problems
are (1) it does not work!! (but exits normally on compilation...and
(2) when stepped through ( I use Xcode) it crashes without any error
message, at the indicated function. I have dissected it, run
individual parts on their own...which seem to work fine. Thanks in
advance.
I have supplied 3 command line arguments ( 22.3, 11.9, and ''+'' so the
argc is 4).
I am including the full code, as there are probably a lot I am missing
and messing up.
> >>>>>>>>>>>
>>>>>>>>>>>
#include< stdio。 h>
#include< math.h>
#define MAXOP 100
#define NUMBER 0
int getop(char *);
void ungets(char *);
void push(double);
double pop (void);
int main( int argc,char * argv []){
char s [MAXOP];
double op2;
while(--argc 0){
ungets(" ");
ungets(* ++ argv);
switch(getop(s)){
case NUMBER:
push(atof(s));
休息;
case''+'':
push(pop( )+ pop());
休息;
默认:
printf(错误:未知输入);
休息;
}
}
printf(" \t%.8g" ;,pop());
返回0;
}
/ ********* /
#define MAXSTACK 50
双重静态堆栈[MAXSTACK];
int static stkp = 0;
double pop(void){
if(stkp 0)
返回堆栈[ - stkp];
else {
printf(" Error:Stack empty");
返回0.00;
}
}
void push(double i){
if(stkp< MAXSTACK)
stack [stkp ++] = i;
else
printf(" Error:Stack overflow");
}
/ ********** /
#include< string.h>
void ungetch(int) ;
void ungets(char * s){/ ***我认为问题在这里*** /
int i;
i = strlen(s);
while(i 0)
ungetch(s [ - i]);
}
/ ********** /
#define MAXBUF 25
char buf [MAXBUF];
static int bufp = 0;
void ungetch(int c){
if(bufp< MAXBUF)
buf [bufp ++] = c;
else
printf(&;错误:Ungetch buffer overflow);
}
int getch(无效){
return((bufp == 0)?getch():buf [ --bufp]);
}
#include< ctype.h>
int getch( ()*(b) = s [0] = getch())=="" || c ==''\ t'');
s [1] =''\''';
if(!isdigit(c)&& c!=''。''){
返回c; / ** c不是数字** /
if(isdigit(c))
while(isdigit(* s ++ = c = getch() ));
if(c ==''。'')
while(isdigit(* s ++ = c = getch()));
* s =''\''';
if(c!= EOF)
ungetch(c);
返回NUMBER;
}
返回0;
>
}
#include <stdio.h>
#include <math.h>
#define MAXOP 100
#define NUMBER 0
int getop(char *);
void ungets( char *);
void push(double);
double pop( void);
int main ( int argc, char *argv[]){
char s[MAXOP];
double op2;
while ( --argc 0){
ungets(" ");
ungets(*++argv);
switch(getop(s)){
case NUMBER:
push(atof(s));
break;
case ''+'':
push(pop() + pop());
break;
default:
printf("Error: Unknown input");
break;
}
}
printf("\t %.8g", pop());
return 0;
}
/*********/
#define MAXSTACK 50
double static stack[MAXSTACK];
int static stkp=0;
double pop(void){
if ( stkp 0)
return stack[--stkp];
else{
printf("Error: Stack empty");
return 0.00;
}
}
void push(double i){
if (stkp < MAXSTACK)
stack[stkp++]=i;
else
printf("Error: Stack overflow");
}
/**********/
#include <string.h>
void ungetch( int );
void ungets( char *s){ /*** I think the problem is here ***/
int i;
i=strlen(s);
while ( i 0)
ungetch(s[--i]);
}
/**********/
# define MAXBUF 25
char buf[MAXBUF];
static int bufp=0;
void ungetch( int c ){
if (bufp < MAXBUF)
buf[bufp++]=c;
else
printf("Error: Ungetch buffer overflow");
}
int getch(void){
return ( (bufp==0) ? getch(): buf[--bufp]);
}
#include <ctype.h>
int getch(void);
int getop(char *s){
int c;
while( (c=s[0]=getch()) == " " || c == ''\t'');
s[1] = ''\0'';
if ( !isdigit(c) && c != ''.''){
return c; /** c is not a number **/
if ( isdigit(c))
while ( isdigit(*s++=c=getch()));
if ( c == ''.'')
while ( isdigit(*s++=c=getch()));
*s=''\0'';
if ( c != EOF)
ungetch(c);
return NUMBER;
}
return 0;
}
mdh写道:
mdh wrote:
我想知道我是否可以寻求帮助。我一天中大部分时间都在苦苦挣扎这个
,并且无法理解发生了什么。问题
是(1)它不起作用!! (但通常在编译时退出...和
(2)当逐步执行时(我使用Xcode)它会在指示的函数中没有任何错误地崩溃
消息。我解剖了它,单独运行
个别零件...这似乎工作正常。感谢
提前。
我已供应3个命令行参数(22.3,11.9和''+''所以
argc是4)。
我包括完整的代码,因为可能有一个我错过了很多
和搞砸了。
I wonder if I could ask for help. I have been struggling with this
most of the day, and cannot understand what is going on. The problems
are (1) it does not work!! (but exits normally on compilation...and
(2) when stepped through ( I use Xcode) it crashes without any error
message, at the indicated function. I have dissected it, run
individual parts on their own...which seem to work fine. Thanks in
advance.
I have supplied 3 command line arguments ( 22.3, 11.9, and ''+'' so the
argc is 4).
I am including the full code, as there are probably a lot I am missing
and messing up.
[snip]
[snip]
>
>
int getop(char * s){
int c;
while((c = s [0] = getch())=="" || c ==''\ t'');
s [1] = ''\ 0'';
if(!isdigit(c)&& c!=''。''){
int getop(char *s){
int c;
while( (c=s[0]=getch()) == " " || c == ''\t'');
s[1] = ''\0'';
if ( !isdigit(c) && c != ''.''){
------------------------------- ^
你确定吗?这是正确的t?看起来不对我。
-------------------------------^
Are you sure this is correct? Doesn''t look right to me.
>
返回c; / ** c不是数字** /
if(isdigit(c))
while(isdigit(* s ++ = c = getch() ));
>
return c; /** c is not a number **/
if ( isdigit(c))
while ( isdigit(*s++=c=getch()));
>
if(c ==''。'')
while (isdigit(* s ++ = c = getch()));
* s =''\ 0'';
if(c!= EOF)
ungetch(c);
返回NUMBER;
}
>
返回0;
}
>
if ( c == ''.'')
while ( isdigit(*s++=c=getch()));
*s=''\0'';
if ( c != EOF)
ungetch(c);
return NUMBER;
}
return 0;
}
HTH
Bj?rn
HTH
Bj?rn
4月15日晚上11点22分,Bj?rn Augestad< b ... @ metasystems.nowrote:
On Apr 15, 11:22 pm, Bj?rn Augestad <b...@metasystems.nowrote:
int getop(char * s){
int c;
int getop(char *s){
int c;
while((c = s [0] = getch())=="" || c ==''\ t '');
s [1] =''\''';
while( (c=s[0]=getch()) == " " || c == ''\t'');
s[1] = ''\0'';
if(!isdigit(c)&& c!=''。''){
if ( !isdigit(c) && c != ''.''){
------------------------------- ^
你确定吗?这是对的?对我来说不合适。
-------------------------------^
Are you sure this is correct? Doesn''t look right to me.
我从K&
I have taken most of it from K&
R以及Tondo和Gimpel获取了大部分资金。我不是在这个阶段肯定会发生什么事情,所以你可能是正确的,但我认为这是正确的。
R and from Tondo and Gimpel. I am not
sure at this stage what is going on, so you may well be correct, but I
think it is correct.