Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my header file:

C++
#include<stdio.h>
#include<ctype.h>
#define SIZE 50
#define MAX 30
char stack[SIZE] , infx[MAX] , pofx[MAX];
int top,stack1[SIZE];
void pushin(char);
char popout();
int prior(char);
int solve();
int push(int,int);
int pop(int);

THIS IS MY MAIN FILE:

#include"header.h"

int main()
{                         /* Main Program */
	char ch, elem;
	int i=0, k=0 ,val=0;
	top = -1;

	scanf("%s",infx);
	
	while( ( ch=infx[i++] ) != '\0' )
	{
		if( ch == '(')
		{ 
			pushin(ch);
		}
		else if( isalnum(ch) )
		{
			pofx[k++]=ch;
		}
		else if( ch == ')')
		{
			while( stack[top] != '(' )
                         {
		        	pofx[k++]=popout();
                         }
			      elem = popout(); /* Remove ( */
		}
		else
		{       /* Operator */
			while( prior( stack[top] ) >= prior(ch) )
				pofx[k++] = popout();
			pushin(ch);
		}
                  printf(" top.................>%d\n", top);
	}
	while( top != - 1 )
	{
		/* Pop from stack till empty */
		pofx[k++] = popout();
	}
	pofx[k] ='\0';          
	printf("%s(): Postfix:  %s\n" , __FUNCTION__, pofx);
        
        
           val = solve();
        
        printf("\n %d\n", val);

  return 0;     
}

THIS IS MY PUSHIN FUNCTION:

#include"header.h"
//push(char);
void pushin(char elem)
{                       /* Function for PUSH operation */
    stack[++top]=elem;
}

THIS IS MY POPOUT FUNCTION:

#include"header.h"

char popout( )
{
	if( top  == - 1 )
	{
		printf( "stack is empty\n" );
		return;
	}
	
	return( stack[top--]);

}

THIS IS MY PRIORITY FUNCTION:

#include"header.h"
int prior(char elem)
{                 /* Function for precedence */
    switch(elem)
    {
    case '#': return 0;
    case '(': return 1;
    case '+':
    case '-': return 2;
    case '*':
    case '/': return 3;
    }
}

THIS IS MY PUSH FUNCTION:

#include"header.h"

int push(int ele, int dup_top)
{
	printf("%d\t\t\t\t\t\t",dup_top);
   if(dup_top > SIZE)
	exit( 1 );
   dup_top = dup_top + 1;
   stack1[dup_top]= ele;
   printf("element ::%d", ele);
   printf("stack1[dup_top]===========%d\n",stack1[dup_top]);
   return dup_top ;
    

}


THIS IS MY POP FUNCTION:

#include"header.h"
int pop(int level_top)
{
	if(level_top == -1 )
	{
		printf( "stack is empty\n" );   
		return;
	}

        return(  stack1[level_top--] );

}


THIS IS MY SOLVE FUNCTION:

#include"header.h"

int solve()
{  
	int op1, op2 ,temp , i , j = 0 , result, ret_push, top1;
        int element;
	char ch;
        top1= -1;

	printf( "%s(): Entry\n", __FUNCTION__ );

	//while(ch = pofx[j++] != '\0')
        for(i=0;i<strlen(pofx);i++)>
	printf("%d.....",pofx[i]-'0');
	ch = pofx[j];
	while(ch != '\0')
	{
		top1 =-1;
			printf("%d\n\n\n\n",ch-'0');
	//	printf( "%s(): %c\n", __FUNCTION__, ch );
		if( isdigit(ch) )
		{
			printf("%d\n\n\n\n",ch-'0');
			printf( "Pushing %d to stack\n", ch - 48 );
          //              element = ch - '0';
		ret_push = push( (ch - 48) ,top1); /* Push the operand */
                        
                         
                         
			for( i=0; stack1[i]; i++ )
                         {
				printf( "push stack : %d\t" , stack1[i] );
                         }

				printf( "\n" );
		

                        printf( "no of elements in stack : %d\n" , top1 + 1 );
		}
		else
		{        /* Operator,pop two  operands */
		        op2= pop(ret_push);
			
                      for( i=0; stack1[i]; i++ )
                      {

		        printf( "pop stack : %d\t" , stack1[i]);
                      }
                                 
				printf( "\n" );
                       // printf("topstack= %d", topstack);
                       // pop();
		printf( "no of elements in stack : %d\n" , top1 +1 ); 

                        printf("op2 =  %d\n", op2);
	
			op1  = pop(ret_push);
			
                        for( i=0; stack1[i]; i++ )
                        {

				printf( "pop stack : %d\t" , stack1[i] );
                         }
                            
				printf( "\n" );
                       // printf("topstack: %d\n", topstack);
		printf( "no of elements in stack : %d\n" , top1 + 1 );

                        printf( "op1: %d\n", op1 );

			switch( ch )
			{
				case '+':temp = op1+op2 ;
					 break;
				case '-':temp = op1-op2 ;
					 break;
				case '*':temp = op1*op2 ;
					 break;
				case '/':temp = op1/op2 ;
					 break;
			}
			push( temp, top1 );
		}

		ch = pofx[++j];
	}

	result = pop(ret_push);


	printf( "%s(): result: %d\n", __FUNCTION__, result );

	return ( result );
}


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 19-Nov-14 0:53am
v2
Comments
OriginalGriff 19-Nov-14 6:56am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
"is not working please help" is not a good description of the problem: we have no idea what it should do, so we have even less idea what it is doing wrong! :laugh:
Tell us what it does that you don't expect, or what it doesn't do that you do expect!

And just dumping your whole code on us and expected us to work out what bit(s) you are talking about is rather rude: particularly when the code is neither commented or self-documenting! So cut it down: just show us the relevant parts.

Help us to help you!

Use the "Improve question" widget to edit your question and provide better information.
Member 11246512 19-Nov-14 12:23pm    
actually in my code my postfix operation is working but when i come to the solve function......in solve function pushing of operation is not working niether pooping of element......example: if my postfix expression is 55+ then it is only pushing the second element in stack and in poppping it is popping top is decremented but popping only once so it gives fro operator1 as 0 and operator2 as 5 and giving result as 5 rather than zero.....now you might have understand the problem so please solve as i am stuck up in this

1 solution

There are numerous errors in your code. The most prominent one that comes to my eye is the implementation of push and pop.
C++
int push(int ele, int dup_top)

You return the new stack top index. But in your solve function, you just assign that value to push_ret and never update your top1 variable. That is a clear mistake. I would recommend to update the stack-top inside your push function, like this:
C++
void push (int value)
{
    stack1[++topIdx] = value;
}
// make topIdx a global variable and declare it next to stack1.

The same applies to your pop function, which at the moment does not decrement to top index at all. It should read:
C++
char pop ()
{
   return stack1[topidx--];
}

That would make your code much more readable and correct at least the most profound errors.
 
Share this answer
 
Comments
Member 11246512 19-Nov-14 12:24pm    
sir i did like this but error is not going
nv3 19-Nov-14 12:31pm    
As I said, there are numerous other errors in your code. For example, you are accessing stack[top] in your main function and top might have a value of -1 at that time. That's an obvious one. I would recommend, you start your program in a debugger and step through it line by line, always comparing what the program is doing with what you think it should do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900