evaluering af postfix
jeg er ved at lave en lommeregner i c, i første omgang har jeg fundet en infix til postfix converter og er så ved at lave en evaluering af postfix, men kan ikke få det til at virke... nogen som kan hjælpe?#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#define MAX 50
char output[MAX] ;
char stack[MAX] ;
char input[MAX] ;
char *s, *t ; /*pointers to input and output strings*/
char ch; /*choice*/
int top; /*Stack top*/
int l ; /*length of infix string*/
/*Function Prototypes*/
void Initialize (void);
void SetExpression (char *);
char PopFromStack (void );
void PushOnStack (char);
int priority (char);
void ConvertToPostfix (void);
/* variabler og funktioner til evaluering af postfix */
int number;
int left;
int right;
int count;
char calc;
int resultat;
void evaluatepostfix(void);
int main( )
{
Initialize ( ) ;
printf ( "\nEnter an expression in infix form: " ) ;
gets ( input ) ;
printf ( "\nSpecify output expression type, 1)Prefix 2)Postfix " ) ;
ch=getch();
SetExpression (input) ;
if(ch=='1') /*Infix->Prefix*/
{
strrev ( s );
strrev ( output ) ;
printf ( "\nThe Prefix expression is: " ) ;
}
else
{
ConvertToPostfix ( ) ;
printf ( "\nThe Postfix expression is: " ) ;
}
puts(output);
evaluatepostfix();
getch( ) ;
}
void Initialize (void)
{
top = -1 ;/*Make stack empty*/
strcpy ( output, "" ) ;
strcpy ( stack, "" ) ;
l = 0 ;
}
void SetExpression ( char *str )
{
s = str ;
l = strlen ( s ) ;
*( output + l ) = '\0' ;
t = output;
}
/* adds operator to the stack */
void PushOnStack ( char c )
{
if ( top == MAX - 1 )
printf ( "\nStack is full.\n" ) ;
else
{
top++ ;
stack[top] = c ;
printf("%c", stack[top]);
}
}
/* pops an operator from the stack */
char PopFromStack (void )
{
if ( top == -1 ) /* Stack is empty*/
return -1 ;
else
{
char item = stack[top] ;
top-- ;
return item ;
}
}
/* returns the priotity of the operator */
int priority ( char c )
{
if ( c == '^' ) return 3 ;/*Exponential operator*/
if ( c == '*' || c == '/' || c == '%' ) return 2 ;
else if ( c == '+' || c == '-' ) return 1 ;
else return 0 ;
}
/* converts the Infix expression to Prefix form */
/* converts the infix expr. to postfix form */
void ConvertToPostfix (void)
{
char opr ;
while ( *s )
{ /*Skip white spaces, if any*/
if ( *( s ) == ' ' || *( s ) == '\t' )
{
s++ ;
continue ;
}
if ( isdigit ( *( s ) ) || isalpha ( *( s ) ) )/*Operands*/
{
while ( isdigit ( *( s ) ) || isalpha ( *
( s ) ) )
{
*( t ) = *( s ) ;
s++ ;
t++ ;
}
}
if ( *( s ) == '(' )/*Opening Parenthesis*/
{
PushOnStack ( *( s ) ) ;
s++ ;
}
if ( *( s ) == '*' || *( s ) == '+' || *( s )
== '/' || *( s ) == '%' || *( s ) == '-' || *( s )
== '^' )
{
if ( top != -1 )
{
opr = PopFromStack ( ) ;
while ( priority ( opr ) >=
priority ( *( s ) ) )
{
*( t ) = opr ;
t++ ;
opr =
PopFromStack ( ) ;
}
PushOnStack ( opr ) ;
PushOnStack ( *( s ) ) ;
}
else PushOnStack ( *( s ) ) ;
s++ ;
}
if ( *( s ) == ')' )/*Closing Parenthesis*/
{
opr = PopFromStack ( ) ;
while ( opr != '(' )
{
*( t ) = opr ;
t++ ;
opr = PopFromStack ( ) ;
}
s++ ;
}
}
while ( top != -1 )/*While stack is not empty*/
{
opr = PopFromStack ( ) ;
*( t ) = opr ;
t++ ;
}
}
void evaluatepostfix()
{
strcpy(stack, ""); /* tømmer stack */
l = l + 1 ;
t -= l;
top = -1; /* resetter top, da stack også er resat */
number = 0;
l = strlen(output);
count = 0;
while ( count != l ) {
if( isdigit(*t) && number == 0 ) {
*t *= 10;
number = *t;
count++;
t++;
}
else if ( number !=0 ) {
number += *t;
PushOnStack(number);
number == 0;
count++;
t++;
}
else if ( isalpha(*t) ) {
left = PopFromStack();
right = PopFromStack();
switch (*t) {
case '+':
calc = left + right;
case '-':
calc = left - right;
case '*':
calc = left * right;
case '/':
calc = left / right;
}
PushOnStack(calc);
count++;
t++;
}
}
resultat = PopFromStack();
if( top == -1 ) {
printf("resultatet er: %s", stack);
}
else {
printf("det er sket en fejl");
}
}