4 solutions

  • 0
    @ 2022-1-13 22:03:45
    #include<stdio.h>
    #include<string.h>
    #include<stack>
    #include<queue>
    using namespace std;
    int main()
    {
      char arr2[1000];
      char arr[1000];
      scanf("%s",arr);
      stack<char>room;
      int j = 0;
      for(int i = strlen(arr) - 1; i >= 0; i--)
      {
      	if(arr[i] >= 48 && arr[i] <= 57)
      	{
      		arr2[j] = arr[i];
      		j ++;
    	  }
    	else if(arr[i] == ')' || arr[i] == '(')
    	{
    		if(arr[i] == ')')room.push(arr[i]);
    		else
    		{
    			while(room.top() != ')')
    	  	    {
    	  		if(room.size() == 0)break;
    	  		arr2[j] = room.top();
    	  		j ++;
    			room.pop();
    		    }
    		room.pop();
    		}	
    	}
    	else
    	{
    		if(room.size() == 0)
    		{
    			room.push(arr[i]);
    		}
    		else
    		{
    			if(arr[i] == '*' || arr[i] == '/')room.push(arr[i]);
    			else if(arr[i] == '+' )
    			{
    				if(room.top() == '+' || room.top() == '-'||room.top() == ')')room.push(arr[i]);
    				else
    				{
    						while(room.size() != '+' || room.top() != '-')
    						{
    						   arr2[j] = room.top();
    						   j ++;
    						    room.pop();
    							if(room.size() == 0)break;	
    						}
    						 room.push(arr[i]);
    				}
    			}
    			else
    			{
    				if(room.top() == '-' || room.top() == '(')room.push(arr[i]);\
    				else
    				{
    					while(room.top() != '-'&&room.top() != ')')
    					{
    						arr2[j] = room.top();
    						j ++;
    						room.pop();
    				  		if(room.size() == 0)break;
    					}
    					room.push(arr[i]);
    				}
    			}
    		}
    	}
    	}
    	 while(room.size() != 0)
    	 {
    	 	arr2[j] = room.top();
    	 	j++;
    	 	room.pop();
    	 	if(room.size() == 0)break;
    	 }
    	 while(j--)
    	 {
    	 	printf("%c",arr2[j]);
    	 }
      return 0;
    }
    

    Information

    ID
    295
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    4
    Tags
    # Submissions
    39
    Accepted
    19
    Uploaded By