Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8616147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:32:56+00:00 2026-06-12T05:32:56+00:00

I am new to problem solving. And I was solving a problem in UVA

  • 0

I am new to problem solving. And I was solving a problem in UVA called Expression. I think I have solved the problem because my code gives the correct output for every possible test case. But still I am getting WA. It seems that somewhere I have print a newline which I am not doing properly. The problem says “The output file will have each postfix expression all on one line. Print a blank line between different expressions.” Can someone please explain this to me a little bit clearly. I have asked the question in the group but I am not getting an answer. And the previous discussions ain’t helping either. Thanks in advance.

#include<iostream>
#include<map>
#include<stack>
#include<vector>
#include<cstdio>

using namespace std;

void push_into_stack(char c, vector< char > &ans, stack< char > &st);
void work_with_stack(vector< char > &ans, stack< char > &st);

int main(void)
{
    freopen("input.txt", "r", stdin);
int t;
char dummy;
cin >> t;

for(int i=1; i<=t; i++)
{
    vector< char > exp, ans;
    stack< char > st;
    char c;

    while(cin >> c)
        exp.push_back(c);

    for(int i=0; i<exp.size(); i++)
        if(exp[i]=='+' || exp[i]=='-' || exp[i]=='*' || exp[i]=='/') push_into_stack(exp[i], ans, st);
        else if(exp[i]=='(') st.push(exp[i]);
        else if(exp[i]==')') work_with_stack(ans, st);
        else ans.push_back(exp[i]);

    while(!st.empty())
    {
        ans.push_back(st.top());
        st.pop();
    }

    for(int i=0; i<ans.size(); i++)
        cout << ans[i];
    cout << endl;
}
return 0;

}

void push_into_stack(char c, vector< char > &ans, stack< char > &st)
{
    map< char, int > mp;
    mp['/']=2;
    mp['*']=2;
    mp['+']=1;
    mp['-']=1;

while(true)
{
    if(!st.empty() && mp[c]<=mp[st.top()])
    {
        ans.push_back(st.top());
        st.pop();
    }
    else
    {
        st.push(c);
        break;
    }
}
return;

}

void work_with_stack(vector< char > &ans, stack< char > &st)
{
    while(true)
    {
        if(st.top()=='(') break;
        ans.push_back(st.top());
        st.pop();
    }
    st.pop();
    return;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:32:58+00:00Added an answer on June 12, 2026 at 5:32 am

    Um… I guess the quality of the answer can only reflect the quality of the question… but how about:

    int main(void) {
       char postfixone[] = "4 5 7 2 + - *            -16";
       char postfixtwo[] = "3 4 + 2  * 7 /             2";
       char postfixthree[] = "5 7 + 6 2 -  *            48";
       printf("%s\n\n",postfixone);
       printf("%s\n\n",postfixtwo);
       printf("%s\n\n",postfixthree);
    }
    
    mike@linux-4puc:~> ./a.out 
    4 5 7 2 + - *            -16
    
    3 4 + 2  * 7 /             2
    
    5 7 + 6 2 -  *            48
    

    Each one is on a line with a new line in between…

    EDIT:
    I guess you’re using C++ and printing the lines here:

    for(int i=0; i<ans.size(); i++)
         cout << ans[i];
     cout << endl; 
    

    You’re printing one new line per postfix with the endl, try:

     cout << endl << endl;
    

    instead to insert the extra blank inbetween lines.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have new problem. My code: .method public static void Main() cil managed {
I am having a new little problem; I have a little pointer called: int
I'm new to WPF and before I dive in solving a problem in completely
I have a new problem with a python script. When I try to run
I'm currently facing new problem with operators. Using following code, I want to make
For the problem I am solving, I have to run a series of calls
First of, I'm new to ajax and Java Script.. I have spend days solving
I have a very big problem solving this issue I have 2 tables first
I'm currently solving a problem of starting external tool from .net app. I have
New problem with VS2005 Team Build: Building locally a solution of a mobile client

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.