为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

喵喔喔 2023-03-18 22:18:42
#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);elseprintf("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 100int sp=0;double val[MAXVAL];void push(double f){if (sp<MAXVAL)val[sp++]=f;elseprintf("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 100char 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");elsebuf[bufp++]=c;}
查看完整描述

1 回答

?
心有法竹

TA贡献1866条经验 获得超5个赞

ungetch()是库函数中的函数,原型是这样的:int ungetch(int);
你自己又定义一个类型返回值类型不一样的,那就不要包含下面这个头文件了嘛:
#include <conio.h>

补充:
搂主,你说的调试不是单步调试吧?编译都有错,根本调试不了。

--------------------Configuration: aaa - Win32 Debug--------------------
Compiling...
b.cpp
E:\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 types
c:\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 constant
E:\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> 
#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; 
}


查看完整回答
反对 回复 2023-03-21
  • 1 回答
  • 0 关注
  • 69 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信