ok, here's is what I've come up with so far. it works but it doesn't implement the atof() since I really don't understand where I'm supposed to put it or what purpose it serves within the program. please help me figure out where this function is needed within the program. I used an example from the book and modified it. she said this was legal in the assignment post.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#define MAXSIZE 50
typedef struct {
char elements[MAXSIZE];
int count;
} Stack;
void initialize(Stack *stackPtr);
int isEmpty(Stack *stackPtr);
void push(Stack *stackPtr, int x);
void pop(Stack *stackPtr, int * x);
int main()
{
char PostfixString[MAXSIZE + 1];
int LeftOperand, RightOperand, Result;
int i;
char c;
char s[MAXSIZE];
int howlong;
Stack EvalStack;
initialize(&EvalStack);
printf("Enter the postfix string: ");
fgets(PostfixString, sizeof(PostfixString), stdin);
howlong = strlen(PostfixString);
for(i = 0; i < howlong; i++)
{
c = PostfixString[i];
s[0] = c;
s[1] = '\0';
PostfixString[i] = c;
if(isdigit(c))
{
push(&EvalStack, (int)atoi(s));
}
else if(c == '+' || c == '-' || c == '*' || c == '/')
{
pop(&EvalStack, &RightOperand);
pop(&EvalStack, &LeftOperand);
switch(c) {
case '+' : push(&EvalStack, LeftOperand + RightOperand);
break;
case '-' : push(&EvalStack, LeftOperand - RightOperand);
break;
case '*' : push(&EvalStack, LeftOperand * RightOperand);
break;
case '/' : push(&EvalStack, LeftOperand / RightOperand);
break;
default: break;
}
}
}
pop(&EvalStack, &Result);
printf("Value of the expression is: %d.\n", Result);
return 0;
}
void initialize(Stack *stackPtr)
{
stackPtr->count = 0;
}
int isEmpty(Stack *stackPtr)
{
if(stackPtr->count == 0)
return 1;
else
return 0;
}
int isFull(Stack *stackPtr)
{
if(stackPtr->count == 50)
return 1;
else
return 0;
}
void push(Stack *stackPtr, int x)
{
if(stackPtr->count == MAXSIZE)
{
printf("The stack is full.\n");
return;
}
else
{
stackPtr->elements[stackPtr->count] = x;
stackPtr->count++;
}
}
void pop(Stack *stackPtr, int *x)
{
if(stackPtr->count == 0)
{
printf("The stack is empty, cannot delete.\n");
exit(0);
}
else
{
stackPtr->count--;
*x = stackPtr->elements[stackPtr->count];
}
return;
}