Results 1 to 2 of 2

Thread: Help

  1. #1
    Join Date
    Mar 2004
    Posts
    1

    Unanswered: Help

    hey Im writing an code in C that uses stacks and checks to see if the parentheses match up in a math formula, and then it does the math in a seperate function. My problem is that I send the information through to the function, but my pointer gives me the ASCII values instead of numerical values, and when i put a 50 in, it pushes the ascii value for the 5 and then pushes the value for the 0. I've pasted all my code below. the problem area is on the very bottom, if someone could get back to me today with help it would be greatly appreciated.


    #include <stdlib.h>
    #include<stdio.h>
    #include<malloc.h>


    typedef struct SStackNode {
    int iData;
    struct SStackNode *pNext;
    } TStackNode;

    typedef struct SStack {
    TStackNode *pTop;
    int iSize;
    } TStack;

    TStack *createStack();
    int top (TStack *pStack, int *pDataOut);
    int pushStack(TStack *pStack, int iDataIn);
    int popStack(TStack *pStack, int *iDataOut);
    int popStack(TStack *pStack, int *iDataOut);
    int isEmpty (TStack *pStack);
    int size (TStack *pStack);
    void destroyStack (TStack *pStack);
    int exprParentheses(char *sExpr);
    float exprEval(char *sExpr);
    int main()
    {
    printf("Welcome!\n");
    printf("***\n");
    exprParentheses("((A+B)*C)");
    exprParentheses("((A+B)*C");
    exprParentheses("(A+B)*C)");
    printf("***\n");
    exprEval("50 40 +");
    exprEval("50 40 *");
    exprEval("50 40 + 20 *");
    exprEval("50 40 + 20 * 60 /");
    printf("***\n");
    printf("Bye!");
    }

    TStack *createStack()
    {
    TStack *pStack;

    pStack = (TStack*)malloc(sizeof(TStack));
    if (pStack)
    {
    pStack->iSize = 0;
    pStack->pTop = NULL;
    }
    return pStack;
    }

    int top (TStack *pStack, int *pDataOut) {
    if (pStack->iSize == 0) return 0;

    *pDataOut = (pStack->pTop)->iData;

    return 1;}


    int pushStack(TStack *pStack, int iDataIn)
    {
    TStackNode *pNewNode;

    pNewNode = (TStackNode*)malloc(sizeof(TStackNode));
    if (pNewNode == NULL) return 0;
    pNewNode->iData = iDataIn;
    pNewNode->pNext = pStack->pTop;
    pStack->pTop = pNewNode;

    (pStack->iSize)++;

    return 1;
    }

    int popStack (TStack *pStack, int *iDataOut)
    {
    TStackNode *pTmp;

    if (pStack->iSize == 0) return 0;

    pTmp = pStack->pTop;
    *iDataOut= (pStack->pTop)->iData;
    pStack->pTop = (pStack->pTop)->pNext;
    free(pTmp);
    (pStack->iSize)--;

    return 1;
    }

    int isEmpty (TStack *pStack)
    {
    return (pStack->iSize == 0);
    }
    int size (TStack *pStack)
    {
    return pStack->iSize;
    }

    void destroyStack (TStack *pStack) {
    TStackNode *pTmp;

    if (pStack)
    {

    while (pStack->pTop)
    {
    pTmp = pStack->pTop;
    pStack->pTop = pStack->pTop->pNext;
    free (pTmp);
    }
    free(pStack);
    }
    }
    int exprParentheses(char *sExpr)
    {
    TStack *tempStack;
    int tempPtr;
    int counterleft,counterright;
    counterleft= 0;
    counterright= 0;
    tempStack = createStack();
    while (*sExpr != NULL)
    {

    if ( *sExpr == '(')
    {
    pushStack(tempStack, *sExpr);
    counterleft++;
    }
    else if (*sExpr == ')')
    {
    popStack(tempStack,&tempPtr);
    counterright++;

    }
    *sExpr++;
    }
    if ((isEmpty(tempStack)==1)&& (counterleft==counterright))
    printf("swell\n");
    else if (isEmpty(tempStack)==0)
    printf("too many left\n");
    else
    printf("too many right\n");
    }

    float exprEval(char *sExpr)
    {
    TStack *newStack;
    int data1;
    int data2;
    float xxx = 0;
    int Integer1;
    int Integer2;
    int Integer3;
    int xxx1= 0;
    newStack = createStack();


    ***************** THIS IS WHERE THE PROBLEM LIES***********
    while (*sExpr != NULL)
    {

    if (*sExpr == '*')
    {
    popStack(newStack,&data2);
    popStack(newStack,&data1);
    xxx = data1 * data2;
    xxx1 = xxx;
    pushStack(newStack,xxx);
    sExpr++;
    }
    else if (*sExpr == '/')
    {
    popStack (newStack, &data2);
    popStack (newStack, &data1);
    xxx=data1 /data2;
    xxx1 = xxx;
    pushStack(newStack,xxx);
    sExpr++;
    }
    else if (*sExpr == '+')
    {
    popStack (newStack,&data2);
    pushStack(newStack,&data1);
    xxx = data1 + data2;
    xxx1 = xxx;
    xxx1 = xxx;
    pushStack (newStack,xxx);
    sExpr++;
    }
    else if (*sExpr == '-')
    {
    popStack(newStack, &data2);
    popStack(newStack, &data1);
    xxx= data1-data2;
    xxx1 = xxx;
    pushStack(newStack,xxx);
    pushStack(newStack,xxx);
    sExpr++;
    }
    else if (*sExpr == ' ')
    sExpr++;
    else
    {
    Integer1 = 0;
    Integer2= 0;
    Integer3 = 0;
    Integer1 = *sExpr - 48;
    sExpr++;
    Integer2 = *sExpr - 48;
    Integer3 = Integer1 * 10 + Integer2;
    pushStack(newStack,Integer3);
    sExpr++;
    }
    }


    printf("Here is result: %d\n",xxx1);

    }

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    This problem has been dealt with about a gazillion times. I'd check the Knuth reference to see how he did it. I think that this is the most common approach, by a wide margin.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •