xxxxx

061loujiazheng 2020-11-18 20:44:44 4 返回题目

#include <bits/stdc++.h> using namespace std; int st[100000]; int head;

void push(char x) { st[++head]=x; }

char top() { return st[head]; }

void pop() { if(head>0) head--; } int size(){ return head; }

bool empty() { if(head==0) return true; return false; }
string str; int main() {

cin>>str;
int len;
len=str.size();
int x=0,y=0,s=1,sum=0;
for(int i=0;i<len;i++){
	if(str[i]=='('){
		push(str[i]);
	}
	else if(str[i]>='0' && str[i]<='9'){
		x=x+(str[i]-'0')*s;
		y=x;
		s=s*10;
	}
	else if(str[i]=='+'){
		if(top()=='('){
			x=y+x;
		}
		else{
			sum=sum+x;
			x=0;
		}
		s=0;
	}
	else if(str[i]=='-'){
		if(top()=='('){
			x=y-x;
		}
		else{
			sum=sum-x;
			x=0;
		}
		s=0;
	}
	else if(str[i]=='*'){
		if(top()=='('){
			x=y*x;
		}
		else{
			sum=sum*x;
			x=0;
		}
		s=0;
	}
	else if(str[i]=='/'){
		if(top()=='('){
			x=y/x;
		}
		else{
			sum=sum/x;
			x=0;
		}
		s=0;
	}
	else if(str[i]=='^'){
		if(top()=='('){
			x=y*y;
		}
		else{
			sum=sum*sum;
			x=0;
		}
		s=0;
	}
	else if(str[i]==')'){
		pop();
		s=0;
	}
}

cout<<sum;
return 0; 

}

{{ vote && vote.total.up }}

共 1 条回复

061loujiazheng

你们可以看下,我只想到这。有想法的和我讨论下