表达式的计算(C++实现)

#include"stack"
#include"iostream"
#include"cmath"
#include"cstring"
using namespace std;

bool is_operator(char op);
void deal_with_minus(char per[]);
int priority(char op);
float calculate(float a,float b,char s);
float run(char per[]);
void display_sep();

char per[100];
int main(){
	//freopen("test.txt","r",stdin); 
	char post[100];
	display_sep();
	cout<<"表达式:";
	while(cin>>per)
	{
		float h=run(per);		
		cout<<"answer="<<h<<endl;
	    cout<<endl<<"表达式:";
	}
	return 0;
}

bool is_operator(char op)  //  判断 op 是否是操作符 
{
	switch(op)
	{
	case '+':
	case '-':
	case '*':
	case '/':
	case '(':
	case ')':
	case 's':
	case 'c':
	case '^':
		return 1;
	default : 
		return 0;
	}
}
/* 处理负号的方式,例如输入的表达式为 -3+(-4)*5,则处理后为 0-3+(0-4)*5    */
void deal_with_minus(char per[]){     // 处理待计算的表达式里面的负号 
	int i,j;
	if(strlen(per)==1)	return ;
	if(per[0]=='-')   
		{
		for(j=strlen(per)-1;j>=0;j--)
				per[j+1]=per[j];
		per[0]='0';
		}
	for(i=1;i<=strlen(per)-2;i++)
		{	
		if(per[i]=='('&&per[i+1]=='-')
			{
			 for(j=strlen(per)-1;j>=i;j--)
			    	per[j+1]=per[j];
			 per[i]='0';
			 i++;
			}
		}
}
int priority(char op)    //求操作符的优先级 
{
	switch(op)
	{
	case '#':
		return -1;
	case ')':
		return 4;
	case '+':
	case '-':
		return 1;
	case '*':
	case '/':
		return 2;
	case 's':
	case 'c':
	case '^':
		return 3;
	case '(':
		return 0;
	default :
		return -1;
	}
}
float calculate(float a,float b,char op){  // 计算 a (op) b 的结果 
	float t;
	switch(op){
	case '+':t=a+b;break;
	case '-':t=a-b;break;
	case '*':t=a*b;break;
	case '/':t=a/b;break;
	case '^':t=pow(a,b);
	}
	return t;
}
float run(char per[]){     // 计算整个表达式的值 
	int i=0;
	deal_with_minus(per);  // 处理负号 
	stack<char> s;   // 运算符的栈 
	s.push('#');   
	stack<float> p;  // 结果的栈 
	p.push('#');
	if(strlen(per)==1){
		p.push(per[0]-48);
		return p.top();
	}
	else{
		while(per[i]!='\0'){
			float t=0;
			int a=is_operator(per[i]);
		    if(a==0)
			{
				bool flag=1;
				int y=-1;
				while((is_operator(per[i])==0||per[i]=='.')&&per[i]!='\0')
				{	
					if(per[i]=='.')
					{	
						flag=0;
						i++;
						continue;
					}
					if(flag)
					{
						t=t*10+(per[i]-48);
						i++;
					}
					else
					{
						t=t+pow(10,y)*(per[i]-48);
						i++;
						y=y-1;
					}
				}
				p.push(t);
			}
			else if(per[i]=='s'||per[i]=='c'){
				s.push(per[i]);
				i=i+3;
			}
			else if(per[i]=='('){
				s.push(per[i]);
				i++;
			}
			else if(per[i]==')')
			{	
				while(s.top()!='(')
				{
					float temp1=p.top();
    				p.pop();
    				float temp2=p.top();
    				p.pop();
    				float t=calculate(temp2,temp1,s.top());
	    			s.pop();
	    			p.push(t);
					
				}
				
				s.pop();
				i++;
			}
			else if(s.top()=='#'&&a==1)
			{
			    s.push(per[i]);
				i++;
			}
		    else
			{
		    	int b=priority(per[i]);
	     		int c=priority(s.top());
				if(s.top()=='s'){
					float t=sin(p.top());
					p.pop();
					p.push(t);
					s.pop();
				}
				else if(s.top()=='c'){
					float t=cos(p.top());
					p.pop();
					p.push(t);
					s.pop();
				}
		    	else if(b-c<=0)
				{
			    	float temp1=p.top();
			    	p.pop();
		     		float temp2=p.top();
		    		float t=calculate(temp2,temp1,s.top());
		    		p.pop();
		    		s.pop();
		    		p.push(t);
				}
	    		else
		     		{
		     		s.push(per[i]);
		     		i++;
					}
			}
		}
	}
    while(p.top()!='#'&&s.top()!='#')
	{	
		if(s.top()=='s')
		{
			float t=sin(p.top());
			p.pop();
			p.push(t);
			s.pop();
		}
		else if(s.top()=='c')
		{
			float t=cos(p.top());
			p.pop();
			p.push(t);
			s.pop();
		}
		else
		{
			float temp1=p.top();//cout<<temp1;
    		p.pop();
    		float temp2=p.top();//cout<<temp2;
    		float t=calculate(temp2,temp1,s.top());
			p.pop();
			s.pop();
	    	p.push(t);
		}
    		
	}
    return p.top();
}
void display_sep(){
	cout<<"******************************************************************"<<endl;
	cout<<"* 支持 + - * /  幂  正弦 余弦等运算                             *"<<endl;
	cout<<"* 幂运算用 ^ 表示,正弦运算用 s 表示,余弦运算用 c 表示         *"<<endl; 
	cout<<"* 输入示例,如果要计算 3*5+4^2+sin(2)+cos(3)                    *"<<endl; 
	cout<<"*           则输入 3*5+4^2+s2+c3                                *"<<endl;
	cout<<"******************************************************************"<<endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值