ONP i think my logic is correct.... bt not gettng submittd.....need help

#include
using namespace std;
void push(char ch);
char pop();
int top=-1;
char infix[400];
char postfix[400],stack[400];
int p=-1;
int priority(char symbol);
int main()
{
int t;
scanf("%d",&t);
char *a=new char[t];
fflush(stdin);

   for(int k=0;k<t;k++)
   {
    for(int i=0;i<400;i++)
    {
            scanf("%c",&infix[i]);
            if(infix[i]=='\n')
            {
             infix[i]='\0';
            break;
            }
    }
    for(int i=0;infix[i]!='\0';i++)
    {
            char ch=infix[i];
            if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^'||ch=='('||ch==')')
            {
                if(ch=='(')
                  push(ch);
                else if(ch==')')
                {
                   char c;
                   while((c=pop())!='(')
                   {  
                      p++;
                      postfix[p]=c;
                   }
                   
                }
                else
                {
                    char c;
                    while(priority(c=pop())>priority(ch))
                    {
                        p++;
                        postfix[p]=c;
                    }
                    push(c);
                    push(ch);
                }
            
            }
            else if(ch>=97&&ch<=122)
            {
            p++;
            postfix[p]=ch;
            }
    
    }
    a[k]=new char[p+2];
    for(int j=0;j<=p;j++)
    {
        a[k][j]=postfix[j];
        if(p==j)
        a[k][j+1]='\0';
    }
    
    top=-1;
    p=-1;
  }  
    printf("\n");
    for(int k=0;k<t;k++)
    {
      for(int j=0;a[k][j]!='\0';j++)
           printf("%c",a[k][j]);
       printf("\n");
}
    
    return 0;
}
void push(char ch)
{
     top++;
     stack[top]=ch; 
}
char pop()
{
    if(top==-1)
    return -1; 
    char ch;
    ch=stack[top];
    top--;
    return ch;
}
int priority(char ch)
{
    if(ch=='+'||ch=='-')
    return 1;
    if(ch=='(')
    return 0;
    if(ch=='*'||ch=='/')
    return 2;
    if(ch=='^')
    return 3;
    
}

from the first look I can tell that

1.you are using scanf and printf for stdin and stdout , for that you must include stdio header file.

2.There is no need to store the results of all the testcases, you can print them for each test case ( I used to do the same in the beginning :slight_smile: )

3.You didn’t give any priority to closing bracket

4.This -> “a[k]=new char[p+2];” is wrong , Check the syntax

Apart from these there might be some more errors , check them

Logic is correct i think.

EDIT:

WORKING CODE HERE:

I Just Modified your code… your logic is correct

#include<iostream>
    #include<stdio.h>
    using namespace std; 
    void push(char ch);
    char pop();
    int top=-1;
    char infix[400];
    char postfix[400],stack[400];
    int p=-1;
    int priority(char symbol); 
    int main() 
    { 
    	int t; 
    	scanf("%d",&t); 
    //	char *a=new char[t]; 
    	fflush(stdin);
    t++;
while(t--)    	{
    		for(int i=0;i<400;i++)
    		{
    			scanf("%c",&infix[i]);
    			if(infix[i]=='\n')
    			{
    				infix[i]='\0'; break; 
    				}
    				}
    				for(int i=0;infix[i]!='\0';i++) 
    				{ char ch=infix[i];
    				if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^'||ch=='('||ch==')') 
    				{
    					if(ch=='(') push(ch); else if(ch==')') 
    					{
    						char c; while((c=pop())!='(')
    						{
 
    							p++; postfix[p]=c; }
 
                }
            else
                {
                    char c;
                    while(priority(c=pop())>priority(ch))
                    {
                        p++;
                        postfix[p]=c;
                    }
                    push(c);
                    push(ch);
                }
 
            }
            else if(ch>=97&&ch<=122)
            {
            p++;
            postfix[p]=ch;
            }
 
    }
    for(int j=0;j<=p;j++)
    {
        printf("%c",postfix[j]);
    }
    printf("\n");
    top=-1;
    p=-1;
    }
     return 0;
    }
    void push(char ch) 
    {
    	top++;
    	stack[top]=ch;
    	}
    	char pop() 
    	{ if(top==-1) return -1;
    	char ch; ch=stack[top]; 
    	top--;
    	return ch; } 
    	int priority(char ch) 
    	{
    		if(ch=='+'|| ch=='-') return 1;
    		if(ch=='(' || ch == ')') return 0; 
    		if(ch=='*'||ch=='/') return 2; 
    		if(ch=='^') return 3;
    	}

there is no compilation error …so syntax is ol right.
nd i have included required header file… forgot to mention it here.
i dont thnk there is ny need to give it some priority…becz i do not need to store it nywhere

1.see if you do not give it any priority , then it will cause run time error because in the priority function it will come to end of the function without any return value. So giving priority is a must here even if u don’t store.

Updated the answer with working code… look into that.

hi guys. stack <char*> expr, gives err when compiled. i wanna save(push) ±*/ stuff in stacks in c++. how can i do that?
thnx.

even i did switch-case statement, it also gave err

switch(input)
case(’(’): expr.push("(");
case(’+’): expr.push("+");

compiler says: err, sorry you cant save them into const char

NOTE: focus on overall lead score

Your output must consist of a single
line containing two integers W and L,
where W is 1 or 2 and indicates the
winner and L is the maximum lead
attained by the winner.

there L is maximum overall lead, but it needs to be calculated per round