利用递归下降分析解决表达式求值问题之noj35

感谢scuxc的指出错误,在这向大家道歉,其中的预处理是不需要的。以(1*2)为例的左推导:

exp->term

term->factor

factor->(exp)

->(exp)

->(term)

->(term*facctor)

->(factor*factor)

->(1*factor)

->(1*2)

->(1*2)

所以是不需要预处理的。但是预处理部分涉及到一些变量的声明,并没有删除。但是这些并不影响程序的运行正性

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=35

这道题应该是数据结构的经题目。记得大二时的数据结构课程报告其中一篇就是表达式求值。今天在看编译原理时看到自顶向下分析之递归下降分析时看到了一个例程-----简单整型算术的递归下降程序计算器。我立马想到了这道题。

先写一下表达式文法:

                                          exp -> exp addop  term | term

                   addop -> + | -

                   term -> term mulop factor | factor

                   mulop -> *  |  /

                   factor -> (exp) | number

 

在求解表达式值时,如果直接用表达式文法写代码,,那么首先应该调用exp,而这将导致一个无限循环,因为exp和term都最终转换为factor,这样将无法区分exp和term,因此会有exp无限调用自己。因此要使用EBNF,将·exp表示为term{addop term),将term表示为factor{mulop facotor}。这样根据EBNF就可以写一个较为简单的程序。注意这个EBNF中在乘法和除法的子表达式中是不需要用括号的,例如不包括这种情况(1*2),所以我们要对表达式做一个预处理,将括号内只有乘除运算的括号去除。利用括号匹配来确定两个括号的管辖范围,然后在管辖范围内查找是否包含加减运算。如果不包含,则去除这两个括号。然后就是求值。贴下code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
int len, pos;
const int MAX = 1000 + 10;
char token;
char str[MAX];
int stack[MAX];
bool flag[MAX];
double exp();
double factor();
char getChar();
double term();
void error()
{
	fprintf(stderr, "Error");
	exit(1);
}

void match(char expectedToken)
{
	if (token == expectedToken)
	{
		token = getChar();
	}
	else
	{
		error();
	}
}
/*预处理*/
void init()
{
	int i, top = 0, j;
	pos = 0;
	memset(flag, 0, sizeof(flag));
	len = strlen(str);
	bool used = false;
	for (i=0; i<len; i++)
	{
		if (str[i] == '(')
		{
			stack[top++] = i;
		}
		else if (str[i] == ')')
		{
			for (j=stack[top-1]+1; j<i; j++)
			{
				if (!flag[j]&&(str[j] == '+' || str[j] == '-'))
				{
					//puts("hahha");
					used = true;
				}
				if ((str[j] == '(' || str[j] == ')') && !flag[j])
				{
					continue;
				}
				flag[j] = true;
			}
			if (used)
			{
				//puts("hahha");
				flag[stack[top-1]] = flag[i] = true;
			}
			used = false;
			top--;
		}
	}
	int tmp = 0;
	char str1[MAX];
	for (i=0; i<len; i++)
	{
		//printf("%d\n", flag[i]);
		if ((str[i] == '(' || str[i] == ')') &&!flag[i])
		{
			continue;
		}
		else
		{
			str1[tmp++] = str[i];
		}
	}
	str1[tmp] = 0;
	memset(str, 0, sizeof(str));
	strcpy(str, str1);
	len = tmp;
}



double getnum()
{
	bool dot = false;
	pos--;
	double tmp1 = 0, tmp2 = 1;
	while (true)
	{
		if (isdigit(str[pos]) && !dot)
		{
			tmp1 = tmp1 * 10 + (str[pos] - '0');
		}
		else if (isdigit(str[pos]) && dot)
		{
			tmp2 = tmp2 * 0.1;
			tmp1 = tmp1  + tmp2 * (str[pos] - '0');
		}
		else if (str[pos] == '.')
		{
			dot = true;
		}
		else
		{
			break;
		}
		pos++;
	}
	return tmp1;
}
void solve()
{
	double result;
	init();
	//puts(str);
	token = getChar();
	result = exp();
	if (token == '=')
	{
		printf("%.2lf\n", result);
	}
	else
	{
		error();
	}
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%s", str);
		solve();
	}
}

double exp()
{
	double temp = term();
	while (token == '+' || token == '-')
	{
		switch (token)
		{
			case '+':
				match('+');
				temp += term();
				break;
			case '-':
				match('-');
				temp -= term();
				break;
		}
	}
	return temp;
}
double term()
{
	double temp = factor();
	while (token == '*' || token == '/')
	{
		switch (token)
		{
			case '*':
				match('*');
				temp *= factor();
				break;
			case '/':
				match('/');
				temp /= factor();
				break;
		}
	}
	return temp;
}

double factor()
{
	double temp;
	if (token == '(')
	{
		match('(');
		temp = exp();
		match(')');
	}
	else if (isdigit(token))
	{
		temp = getnum();
		token = getChar();
	}
	else
	{
		error();
	}
	return temp;
}
char getChar()
{
	if (pos<len)
	{
		return str[pos++];
	}
	else
	{
		error();
	}
}


欢迎纠正指错

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值