Thread: Modifying a stack program to accept characters

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    1

    Modifying a stack program to accept characters

    Hello, I am working on an assignment where we take a stack program and make it so it accepts characters. Then I need to add a main function that asks the user to input a series of braces or parenthesis and indicate whether or not they're properly nested. My program I need to modify is as follows: I do know a lot of the basics such as printf, return 0; and whatever, but these stacks are really messing with me. Any ideas or help on the subject?

    Code:
    #include <stdbool.h>
    #define STACK_SIZE 100
    
    
    int contents[STACK_SIZE];
    int top = 0;
    
    
    void make_empty(void)
    {
    top = 0;
    }
    
    
    bool is_empty(void)
    
    
    {
        return top == 0;
    }
    bool is_full(void)
    {
        return top == STACK_SIZE;
    }
    
    
    void push(int i)
    {
        if (is_full())
                stack_overflow();
        else
            contents[top++] = i;
    }
    
    
    int pop(void)
    {
        if (is_empty())
            stack_underflow();
        else
            return contents[--top];
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,567
    For starters, you should declare a stack struct that looks like this:
    Code:
    struct stack
    {
        char contents[STACK_SIZE];
        int top;
    };
    Then for each of the stack functions, add a new first parameter of type struct stack * if it modifies the stack, or const struct stack * otherwise. Of course, you need to change the code to use this parameter instead of global variables.

    This way, if your teacher asks you to have two stacks at the same time in the same program, it can be done.

    Besides this, you also need to change the parameter of push and return value of pop to be of type char. Once you do these changes, you would have the code for stacks of char, and so you can start writing the main function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A program that can only accept a numerical value?
    By Bano360 in forum C Programming
    Replies: 4
    Last Post: 11-15-2010, 07:55 PM
  2. Create program that accept modules
    By rluceac in forum C++ Programming
    Replies: 16
    Last Post: 04-11-2009, 03:11 PM
  3. Need help modifying my program
    By Northstar in forum C Programming
    Replies: 8
    Last Post: 11-11-2007, 10:25 PM
  4. help modifying a program...
    By sweetly in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2003, 03:57 PM
  5. Help with Modifying Program
    By omalleys in forum C Programming
    Replies: 0
    Last Post: 06-19-2002, 09:45 AM

Tags for this Thread