谁帮我调试下这段程序!只是程序的ungetch声明类型不匹配?解释下?

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

#define MAXOP 100
#define NUMBER '0'

int getop(char []);
void push(double);
double pop(void);

main()
{
int type;
double op2;
char s[MAXOP];

while ((type=getop(s))!=EOF) {
switch(type) {
case NUMBER:
push(atof(s));
case '+':
push(pop()+pop());
case '*':
push(pop()*pop());
case '-':
op2=pop();
push(pop()-op2);
break;
case '/':
op2=pop();
if(op2!=0.0)
push(pop()/op2);
else
printf("error:zero divisor\n");
break;
case '\n':
printf("\t%.8g\n",pop());
break;
default:
printf("error:unknown command %s\n",s);
break;
}
}
return 0;
}

#define MAXVAL 100
int sp=0;
double val[MAXVAL];

void push(double f)
{
if (sp<MAXVAL)
val[sp++]=f;
else
printf("error:stack fill,can't push %g\n",f);
}

double pop(void)
{
if (sp>0)
return val[--sp];
else {
printf("error: stack empty\n");
return 0.0;
}
}

#include <ctype.h>

int getch(void);
void ungetch(int);

int getop(char s[])
{
int i,c;

while ((s[0]=c=getch())==''||c=='\t')
;
s[1]='\0';
if (!isdigit(c)&&c!='.')
return c;
i=0;
if (isdigit(c))
while (isdigit(s[++i]=c=getch()))
;
if (c=='.')
while (isdigit(s[++i]=c=getch()))
;
s[i]='\0';
if (c!=EOF)
ungetch(c);
return NUMBER;
}

#define BUFSIZE 100

char buf[BUFSIZE];
int bufp=0;

int getch(void)
{
return (bufp>0)?buf[--bufp] :getchar();
}

void ungetch(int c)
{
if(bufp >=BUFSIZE)
printf("ungetch: too many chatacters\n");
else
buf[bufp++]=c;
}

喵喔喔
浏览 113回答 1
1回答

心有法竹

ungetch()是库函数中的函数,原型是这样的:int ungetch(int);你自己又定义一个类型返回值类型不一样的,那就不要包含下面这个头文件了嘛:#include <conio.h>补充:搂主,你说的调试不是单步调试吧?编译都有错,根本调试不了。--------------------Configuration: aaa - Win32 Debug--------------------Compiling...b.cppE:\MYDOC\VC6\aaa\b.cpp(75) : error C2556: 'void __cdecl ungetch(int)' : overloaded function differs only by return type from 'int __cdecl ungetch(int)'c:\program files\microsoft visual studio\vc98\include\conio.h(104) : see declaration of 'ungetch'E:\MYDOC\VC6\aaa\b.cpp(75) : error C2371: 'ungetch' : redefinition; different basic typesc:\program files\microsoft visual studio\vc98\include\conio.h(104) : see declaration of 'ungetch'E:\MYDOC\VC6\aaa\b.cpp(81) : error C2137: empty character constantE:\MYDOC\VC6\aaa\b.cpp(111) : error C2556: 'void __cdecl ungetch(int)' : overloaded function differs only by return type from 'int __cdecl ungetch(int)'c:\program files\microsoft visual studio\vc98\include\conio.h(104) : see declaration of 'ungetch'Error executing cl.exe.b.obj - 4 error(s), 0 warning(s)能编译通过的如下,修改了两处:#include <stdio.h>&nbsp;#include <stdlib.h>&nbsp;//#include <conio.h> //自己定义了函数库中的函数,就不要使用函数库了#define MAXOP 100&nbsp;#define NUMBER '0'&nbsp;int getop(char []);&nbsp;void push(double);&nbsp;double pop(void);&nbsp;main()&nbsp;{&nbsp;int type;&nbsp;double op2;&nbsp;char s[MAXOP];&nbsp;while ((type=getop(s))!=EOF) {&nbsp;switch(type) {&nbsp;case NUMBER:&nbsp;push(atof(s));&nbsp;case '+':&nbsp;push(pop()+pop());&nbsp;case '*':&nbsp;push(pop()*pop());&nbsp;case '-':&nbsp;op2=pop();&nbsp;push(pop()-op2);&nbsp;break;&nbsp;case '/':&nbsp;op2=pop();&nbsp;if(op2!=0.0)&nbsp;push(pop()/op2);&nbsp;else&nbsp;printf("error:zero divisor\n");&nbsp;break;&nbsp;case '\n':&nbsp;printf("\t%.8g\n",pop());&nbsp;break;&nbsp;default:&nbsp;printf("error:unknown command %s\n",s);&nbsp;break;&nbsp;}&nbsp;}&nbsp;return 0;&nbsp;}&nbsp;#define MAXVAL 100&nbsp;int sp=0;&nbsp;double val[MAXVAL];&nbsp;void push(double f)&nbsp;{&nbsp;if (sp<MAXVAL)&nbsp;val[sp++]=f;&nbsp;else&nbsp;printf("error:stack fill,can't push %g\n",f);&nbsp;}&nbsp;double pop(void)&nbsp;{&nbsp;if (sp>0)&nbsp;return val[--sp];&nbsp;else {&nbsp;printf("error: stack empty\n");&nbsp;return 0.0;&nbsp;}&nbsp;}&nbsp;#include <ctype.h>&nbsp;int getch(void);&nbsp;void ungetch(int);&nbsp;int getop(char s[])&nbsp;{&nbsp;int i,c;&nbsp;while ((s[0]=c=getch())==' '||c=='\t')//注意空格的写法&nbsp;;&nbsp;s[1]='\0';&nbsp;if (!isdigit(c)&&c!='.')&nbsp;return c;&nbsp;i=0;&nbsp;if (isdigit(c))&nbsp;while (isdigit(s[++i]=c=getch()))&nbsp;;&nbsp;if (c=='.')&nbsp;while (isdigit(s[++i]=c=getch()))&nbsp;;&nbsp;s[i]='\0';&nbsp;if (c!=EOF)&nbsp;ungetch(c);&nbsp;return NUMBER;&nbsp;}&nbsp;#define BUFSIZE 100&nbsp;char buf[BUFSIZE];&nbsp;int bufp=0;&nbsp;int getch(void)&nbsp;{&nbsp;return (bufp>0)?buf[--bufp] :getchar();&nbsp;}&nbsp;void ungetch(int c)&nbsp;{&nbsp;if(bufp >=BUFSIZE)&nbsp;printf("ungetch: too many chatacters\n");&nbsp;else&nbsp;buf[bufp++]=c;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP