3.1 表达式求值(递归实现)

表达式可以是只有一个项组成,或者是多个项加减组成;

项可以由一个因子组成,或者是多个因子乘除组成;

因子可以由一个整数或者一个(表达式)组成。

#include<iostream>
#include<cstring>
using namespace std;
int term();
int expr();
int factor();

int expr()
{
	int result = term();
	bool more = true;	
	while(more)
	{
		char c = cin.peek();
		if(c == '+' || c == '-')
		{
			cin.get();
			int value = term();
			if(c == '+')
				result += value;
			else if(c == '-')
				result -= value;
		}
		else more = false; 
	}	
	return result;
}

int term()
{
	int result = factor();
	bool more = true;
	while(more)
	{
		char c = cin.peek();
		if(c == '*' || c == '/')
		{
			cin.get();
			int value = factor();
			if(c == '*')
				result *= value;
			else
				result /= value;
		}
		else
			more = false;
	}
	return result;
}

int factor()
{
	int result = 0;
	char c = cin.peek();
	if(c == '(')
	{
		cin.get();//去掉左括号 
		result = expr();
		cin.get();//去掉右括号 
	}
	else
	{
		while(isdigit(c))
		{
			result = result*10 + c - '0';
			cin.get();
			c = cin.peek() ;
		}
	} 
	return result;
}
int main()
{
	cout << expr() << endl;
	//cout << term() << endl;
//	cout << factor() << endl; 
	return 0;
} 

还有一种方法是通过栈来实现,后面更新~(因为递归就是通过栈实现的)

调试代码:

#include<iostream>
#include<cstring>
using namespace std;
int term();
int expr();
int factor();
 
int expr()
{
	cout<<"---expr---"<<endl;
	int result = term();
	cout<<"expr_first_result: "<<result<<endl;
	bool more = true;	
	while(more)
	{
		char c = cin.peek();
		cout<<"expr_cin.peek(): "<<c<<endl;
		if(c == '+' || c == '-')
		{
			cin.get();
			int value = term();
			if(c == '+')
				result += value;
			else if(c == '-')
				result -= value;
		}
		else more = false; 
	}
	cout<<"expr_return_result: "<<result<<endl;	
	cout<<"---expr---END"<<endl;
	cout<<endl;
	return result;
}
 
int term()
{
	cout<<"---term---"<<endl;
	int result = factor();
	cout<<"term_first_result: "<<result<<endl;
	bool more = true;
	while(more)
	{
		char c = cin.peek();
		cout<<"term_cin.peek(): "<<c<<endl;
		if(c == '*' || c == '/')
		{
			cin.get();
			int value = factor();
			if(c == '*')
				result *= value;
			else
				result /= value;
		}
		else
			more = false;
	}
	cout<<"term_return_result: "<<result<<endl;
	cout<<"---term---END"<<endl;
	cout<<endl;
	return result;
}
 
int factor()
{
	cout<<"---factor---"<<endl;
	int result = 0;
	char c = cin.peek();
	cout<<"factor_cin.peek(): "<<c<<endl;
	if(c == '(')
	{
		cin.get();//去掉左括号 
		result = expr();
		cin.get();//去掉右括号 
	}
	else
	{
		while(isdigit(c))
		{
			result = result*10 + c - '0';
			cin.get();
			c = cin.peek() ;
		}
	} 
	cout<<"factor_return_result: "<<result<<endl;	
	cout<<"---factor---END"<<endl;
	cout<<endl;
	return result;
}
int main()
{
	freopen("in.txt","r",stdin); 
	freopen("out.txt","w",stdout); 
	cout << expr() << endl;
	return 0;
} 

输入:

(2+3)+(5+7)+9/3

结果:

---expr---
---term---
---factor---
factor_cin.peek(): (
---expr---
---term---
---factor---
factor_cin.peek(): 2
factor_return_result: 2
---factor---END

term_first_result: 2
term_cin.peek(): +
term_return_result: 2
---term---END

expr_first_result: 2
expr_cin.peek(): +
---term---
---factor---
factor_cin.peek(): 3
factor_return_result: 3
---factor---END

term_first_result: 3
term_cin.peek(): )
term_return_result: 3
---term---END

expr_cin.peek(): )
expr_return_result: 5
---expr---END

factor_return_result: 5
---factor---END

term_first_result: 5
term_cin.peek(): +
term_return_result: 5
---term---END

expr_first_result: 5
expr_cin.peek(): +
---term---
---factor---
factor_cin.peek(): (
---expr---
---term---
---factor---
factor_cin.peek(): 5
factor_return_result: 5
---factor---END

term_first_result: 5
term_cin.peek(): +
term_return_result: 5
---term---END

expr_first_result: 5
expr_cin.peek(): +
---term---
---factor---
factor_cin.peek(): 7
factor_return_result: 7
---factor---END

term_first_result: 7
term_cin.peek(): )
term_return_result: 7
---term---END

expr_cin.peek(): )
expr_return_result: 12
---expr---END

factor_return_result: 12
---factor---END

term_first_result: 12
term_cin.peek(): +
term_return_result: 12
---term---END

expr_cin.peek(): +
---term---
---factor---
factor_cin.peek(): 9
factor_return_result: 9
---factor---END

term_first_result: 9
term_cin.peek(): /
---factor---
factor_cin.peek(): 3
factor_return_result: 3
---factor---END

term_cin.peek(): 
term_return_result: 3
---term---END

expr_cin.peek(): 
expr_return_result: 20
---expr---END

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值