Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,303 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 766 people online right now. Registration is fast and FREE... Join Now!




C programming: Parsing Program using Stack

 
Reply to this topicStart new topic

C programming: Parsing Program using Stack, having trouble making it work right.

gkgranada
12 Jan, 2009 - 08:28 AM
Post #1

New D.I.C Head
*

Joined: 6 Jul, 2008
Posts: 10


My Contributions
this is an exercise in our structures class (i'm actually a week late, T_T)

the exercise is to make a parsing program using stacks, with the push, pop, and stacktop functions being put in a separate c file, to be accessed using #include (i'm not sure how to do that since it didn't work when i attempted it, so for now i put it together with my main function.)

my basic algo:

1. get the expression
2. ignore all elements that aren't '(' or ')'
3. if it's '(', i push it into the stack
4. if it's ')', i pop an element from the stack.

my problem is that, no matter what expression i enter, it's always invalid, even if it's just '()' which should be valid.

my code:

CODE


#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define MAX 50

int top = -1;

void push (char stack[], char *ptrdata)
{

    if (top == MAX)
    {    
        printf ("Stack is full, cannot push an element");
    }
    else if (top<MAX)
    {
        top = top +1;
        stack[top]=*ptrdata;
            
    }
}

pop ( char stack[])
{
    char temp[MAX];
    int x = 0;
    
    if (top <0)
    {
        printf ("Stack is empty, cannot pop an element.");
        getch ();
    }
    else if (top >= 0)
    {
        temp[x] = stack [top];
        stack [top]=" ";
        top = top - 1;
        x++;
    }
}

stacktop (char stack[], char data[])
{
    if (top >=0)
    {
        stack [top] = data;
        printf ("%c", stack[top]);
        getch ();
        return;
    }
    else if  (top < 0)
    
    {
        printf ("Stack is empty, cannot stacktop an element.");
        getch ();
    }
}

main ()
{
    char data[MAX], stack[MAX], *ptrdata;
    
    clrscr ();
    
    ptrdata = &data;
    printf ("Enter expression: \n\n");
    gets (data);
    while (*ptrdata!=NULL)
    {
        if (*ptrdata != '(' || *ptrdata != ')')
        {
            ptrdata++;
        }
        else if (*ptrdata=='(')
        {
            push (stack, *ptrdata);
            ptrdata++;
        }
        else if  (*ptrdata==')')
        {
            pop (stack);
            ptrdata++;
        }
        
        ptrdata++;
    }

    if (stack == NULL)
        {
            printf ("\n\nValid expression!");
        }
    else if (stack != NULL)
    {
        printf ("\n\nInvalid expression!");
    }
    
    getch ();
}
    


any help would be very, Very much appreciated.

and if anyone could also explain to me how to create the stack.c separate from my main function. do i just past them there? or should i put header files?
also, could anyone explain to me what the following warnings mean?
"suspicious pointer conversion"
"non-portable pointer conversion"

User is offlineProfile CardPM
+Quote Post


GWatt
RE: C Programming: Parsing Program Using Stack
12 Jan, 2009 - 12:08 PM
Post #2

//
Group Icon

Joined: 1 Dec, 2005
Posts: 2,453



Thanked: 50 times
Dream Kudos: 525
My Contributions
When you're including a file that is in the same folder as the source code, you must use quotation marks.
CODE

#include "stack.h"

If you used <> then it won't work because the preprocessor only searches through certain directories for the file name. Take your extra functions and stick them in a file called stack.h the use the #include statement in the code block above. That should fix your problem.
Also your pointer warnings mean that you're trying to assign a value to a variable when the variable is not of the same type as the value. For example int a = "hello world" is an example of that. Yours are more subtle because you're assigning char* to chars.

A pointer is a type that holds the memory location of a variable.
CODE

int a = 5, *b;
b = &a;   /* assign the memory location of a to the value of b
the ampersand (&) gives the memory location of a variable.
&a is the same thing as b.
the asterisk (*) assumes that the value of a variable is a
memory location and then fetches the value AT that memory location
a is the same thing as *b
*/

back to your code.
In your pop function you have this code:
stack[top] = " ";
now " " is a char*, but stack[top] is a char. If you want to put an empty space in your stack, then do this:
stack[top] = ' ';

At the beginning of your stacktop function you have this code:
stack[top] = data
stack and data are both char arrays, which are used the same way as char*. You're essentially trying to assign a char* to a char.

In your main function you try to assign the memory location of data to ptrdata. Since data is an array, and therefore a pointer all that is necessary for data and ptrdata to reference the same memory is
ptrdata = data
Your while (*ptrdata != NULL) is also erroneous because you're attempting to compare the value referenced by ptrdata and NULL. What you should be trying to do is compare the value of ptrdata and NULL.
while(ptrdata != NULL)

And finally, I think you're trying to test if the stack array is empty. Instead of comparing it to NULL, why don't you just test the value of the top variable? Since it looks like top holds the index of the first element, and when stack is empty top should be -1, just test if top is -1, and then your stack is empty.
User is offlineProfile CardPM
+Quote Post

iimememine
RE: C Programming: Parsing Program Using Stack
12 Jan, 2009 - 12:34 PM
Post #3

New D.I.C Head
*

Joined: 22 Jul, 2008
Posts: 20



Thanked: 1 times
My Contributions
I also notice a classic SM* :

CODE
if (*ptrdata != '(' || *ptrdata != ')')



i.e. "If (x is not A) or (x is not cool.gif. Since x cannot be both A and B, the expression is always true.

You want && instead.

Good luck,

Aaron


ps. SM = 'smooth move'; I had a math teacher that used give almost full credit if we got a difficult problem wrong due to a simple mistake. (e.g. he'd mark "-1 SM!" on a twenty point problem)

You could theoretically get an A in the course hardly ever getting any answers right... those were the days...
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 05:50PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month