関数がどのように呼び出されるかを理解できません。
Input 1 2 3 + + [Enter] //入力の間にスペースがあることに注意してください
Output 6 //これは正しい
1 -> プログラムのコンパイル時 while ステートメントが関数 getop(s) を呼び出します。
2 -> getop() 関数では、getch() 関数が呼び出され、次に getchar() が呼び出されるため、このステップでは入力として 1 を読み取り、それを返します。
3 -> c が数字であるかどうかをチェックし、それが true であるため、再び getch() を呼び出してスペースを読み取り、その値を返します。次に、false と評価される数字であるかどうかをチェックし、次に移動します。次の発言。
4 -> 最後に ungetch() が実行され、バッファに 1 が保存されます
このステップでは、入力がどのように読み取られているか、および getch と ungetch の使用方法を理解できません
#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))
{
switch(type)
{
//Here all operation are performed as push pop addition etc.
//This part of code is simple
}
}
プッシュとポップ関数の定義は簡単なので書いていません
#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 character\n");
else
buf[bufp++]=c;
}