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;
}
添加回答
舉報