本文介绍了在我的postfix评估代码我的解决功能不起作用请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的头文件:



this is my header file:

#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]已添加代码块 - OriginalGriff [/ edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

int push(int ele, int dup_top)



您返回新的堆栈顶部索引。但是在你的求解函数中,你只需将该值赋给push_ret,并且永远不会更新你的top1变量。这是一个明显的错误。我建议更新push函数中的stack-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:

void push (int value)
{
    stack1[++topIdx] = value;
}
// make topIdx a global variable and declare it next to stack1.



这同样适用于你的pop函数,它目前根本不会递减到顶部索引。它应该是:


The same applies to your pop function, which at the moment does not decrement to top index at all. It should read:

char pop ()
{
   return stack1[topidx--];
}



这将使您的代码更具可读性和正确性,至少是最严重的错误。


That would make your code much more readable and correct at least the most profound errors.


这篇关于在我的postfix评估代码我的解决功能不起作用请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 02:33