📄 ll.cpp
字号:
//递归下降分析--计算表达式
#include <stdio.h>
#include <stdlib.h>
char token;
/*global token variable*/
int exp(void);
int term(void);
int factor(void);
bool isgit(char);
void error(void)
{
fprintf(stderr,"Error happened \n");
exit(1);
}
void match(char exceptionToken)
{
if(exceptionToken==token)
token= getchar();
else
error();
}
int main()
{
int result ;
token=getchar();
result = exp();
if(token='\n')
printf("%d\n",result);
else
error();
return 0;
}
int exp(void)
{
int tmp=term();
while((token=='+')||(token=='-'))
{
switch(token)
{
case '+':
match('+');
tmp+=term();
break;
case '-':
match('-');
tmp-=term();
break;
}
}
return tmp;
}
int term(void)
{
int temp=factor();
while((token=='*'))
{
match('*');
temp*=factor();
}
return temp;
}
int factor()
{
int tmp;
bool isdigit(char );
if(token=='(')
{
match('(');
tmp=exp();
match(')');
}
else
if(isdigit(token))
{
ungetc(token,stdin);
scanf("%d",&tmp);
token=getchar();
}
else
error();
return tmp;
}
bool isdigit(char p)
{
if(p>='0' && p<='9')
return true;
else
return false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -