odbhut.shei.chhele
odbhut.shei.chhele

Reputation: 6264

I am having difficulty with printing a newline

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.

I have asked the question in the group but I am not getting an answer. And the previous discussions aren't helping either. What can I try next?

#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;
}

Upvotes: 0

Views: 202

Answers (1)

Mike
Mike

Reputation: 49523

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.

Upvotes: 1

Related Questions