亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

誰幫我調試下這段程序!只是程序的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 關注
  • 101 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號