3 solutions

  • 1
    @ 2022-1-2 14:13:40

    可以通过自己//几行来看运行步骤 写得不好,仅供参考

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
    	char a[1005];
    	cin>>a;
    	int len=strlen(a);
    	stack<char>s;
    	for(int i=0;i<len;i++){
    		if(a[i]>=48&&a[i]<=58)
    		cout<<a[i];
    		else{
    			if(a[i]=='('){
    				s.push(a[i]);
    			}
    			else if(a[i]=='+'||a[i]=='-'){
    				if(s.empty()||s.top()=='('){
    				//	cout<<" in ";
    					s.push(a[i]);
    				}
    				else{
    					while(!s.empty()){
    						if(s.top()!='('){
    					//	cout<<" chu ";
    						cout<<s.top();
    						s.pop();}
    					//	cout<<"g";}
    						else{
    					//		cout<<"qaq";
    							break;
    						}
    					}
    				//	cout<<" ru ";
    					s.push(a[i]);
    				}
    			}
    			else if(a[i]=='*'||a[i]=='/'){
    				if(s.empty()||s.top()=='('||s.top()=='+'||s.top()=='-')
    				s.push(a[i]);
    				else{
    					while(s.top()!='+'&&s.top()!='-'&&s.top()!='('&&!s.empty()){
    						cout<<s.top();
    				//		cout<<"  chengchu  ";
    						s.pop();
    					}
    					s.push(a[i]);
    				}
    			}
    			else if(a[i]==')'){
    				while(s.top()!='('){
    					cout<<s.top();
    					s.pop();
    				}
    				if(s.top()=='('){
    				s.pop();}
    			}
    		}
    	}
    	while(!s.empty()){
    		cout<<s.top();
    		s.pop();
    	}
    	return 0;
    }
    

    Information

    ID
    294
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    6
    Tags
    # Submissions
    83
    Accepted
    27
    Uploaded By