Stacks and Queues Project - Where to go from here?

It's split up into 3 parts and I'm really confused on how to finish it from here.


PART 1

#include<iostream>
#include "MyStack.h"
using namespace std;

int main()
{
int size = 1000;
char *input = 0;
input = new char[size]; //Array to hold user input

cout<<"Please enter a sentence. When finished, enter the word done after the sentence: ";
cin.getline(input, size);

DynIntStack *ptr;
ptr = new DynIntStack;

char scan;
for(int i = 0; i < size; i++)//Find out if the delimiters match or not.
{
if(input[i] == '{' || input[i] == '[' || input[i] == '(')
ptr->push(input[i]);
else if(input[i] == '}' || input[i] == ']' || input[i] == ')')
{
if(ptr->isEmpty())
{
cout<<"Right delimeter "<<input[i]<<" had no left delimeter."<<endl;
}
else
{
ptr->pop(scan);
if(input[i] == ']' && scan == '[')
{

}
else if(input[i] == ')' && scan == '(')
{
}
else if(input[i] == '}' && scan == '{')
{
}
else
cout<<"Left delimiter "<<scan<<" had no right delimiter."<<endl;
}
}
}

if(!ptr->isEmpty())
{
ptr->pop(scan);
cout<<"Left delimiter "<<scan<<" had no right delimiter."<<endl;
}

return 0;
}


#include<iostream>
#include "MyStack.h"
using namespace std;

int main()
{
int size = 1000;
char *input = 0;
input = new char[size]; //Array to hold user input

cout<<"Please enter a sentence. When finished, enter the word done after the sentence: ";
cin.getline(input, size);

DynIntStack *ptr;
ptr = new DynIntStack;

char scan;
for(int i = 0; i < size; i++)//Find out if the delimiters match or not.
{
if(input[i] == '{' || input[i] == '[' || input[i] == '(')
ptr->push(input[i]);
else if(input[i] == '}' || input[i] == ']' || input[i] == ')')
{
if(ptr->isEmpty())
{
cout<<"Right delimeter "<<input[i]<<" had no left delimeter."<<endl;
}
else
{
ptr->pop(scan);
if(input[i] == ']' && scan == '[')
{

}
else if(input[i] == ')' && scan == '(')
{
}
else if(input[i] == '}' && scan == '{')
{
}
else
cout<<"Left delimiter "<<scan<<" had no right delimiter."<<endl;
}
}
}

if(!ptr->isEmpty())
{
ptr->pop(scan);
cout<<"Left delimiter "<<scan<<" had no right delimiter."<<endl;
}

return 0;
}

PART 2

#ifndef DYNINTSTACK_H
#define DYNINTSTACK_H

class DynIntStack
{
private:
struct StackNode
{
char character;
int lineNum;
int count;
struct StackNode *next;
};
StackNode *top;

public:
DynIntStack() //Constructor
{top = 0;}

~DynIntStack(); //Destructor

void push(char);//Operations
void pop(char &);
bool isEmpty();
};
#endif

PART 3

#include<iostream>
using namespace std;
#include "MyStack.h"
DynIntStack::~DynIntStack() //Destructor code
{
StackNode *nodeptr = 0;
StackNode *nextNode = 0;

nodeptr = top;

while(nodeptr != 0)
{
nextNode = nodeptr;
delete nodeptr;
nodeptr = nextNode;
}
}

void DynIntStack::push(char letter)//Enter a char.
{
StackNode *newnode = 0;
newnode = new StackNode;
newnode->character = letter;

if(isEmpty())
{
top = newnode;
newnode->next = 0;
}
else
{
newnode->next = top;
top = newnode;
}
}

void DynIntStack::pop(char &letter)//Take out last char in.
{
StackNode *temp = 0;

if(isEmpty())
{

}
else
{
letter = top->character;
temp = top->next;
delete top;
top = temp;
}
}

bool DynIntStack::isEmpty()
{
bool status;

if(!top)
status = true;
else
status = false;
return status;
}
Project Description:

Overview: This project is for testing the use and understanding of stacks. In this assignment you
will be writing a program that reads in a stream of text and tests for mismatched delimiters.
Your program should start by prompting for exactly one integer(0 or 1). This should determine if
your code should use your own custom stack (0) or the STL stack (1).
Based on the above input, your program should use a stack class or STL stack that stores, in each
node, a character (char) , a line number (int) and a character count (int). This can either be based
on a dynamic stack or a static stack from the book, modified according to these requirements. I
suggest using a stack of structs that have the char, line number and character count as members,
but you can do this separately if you wish.
Second your program should start reading in lines of text from the user. This reading in of lines
of text (using getline) should only end when it receives the line “DONE”.
While the text is being read, you will use this stack to test for matching “blocks”. That is, the text
coming in will have the delimiters to bracket information into blocks, the braces {}, parentheses
(), and brackets [ ]. A string is properly delimited if each right delimiter is matched with a
preceding left delimiter of the same type in such a way that either the resulting blocks of
information are disjoint, or one of them is completely nested in the other. Write a program that
uses your stack to check whether the entered text containing the blocks is properly delimited.
Implementation:
All while accepting input, keep track of how many lines you have received already. To do the
test, as you receive a line of text, start processing each line. To process each line, you have a few
cases to deal with:
1) Ignore all non-delimiters.
2) If you receive a left delimiter,{([, push it onto the stack along with the line number and
character in that line that the delimiter was found.
3) If you receive a right delimiter, ]}), pop the top of the stack and compare the characters. If the
left matches with a right delimiter, then that is fine and continue processing. If they DO NOT
MATCH, that is a problem, so output an error message showing the mismatch like the
following:
Mismatched delimiter ( found at line 3, char 27 does not match } found at line 7, char 9
4) If your stack is ever empty when you need to pop, output an error message like the following:
Right delimiter } had no left delimiter found at line 27, char 23
5) If you reach the end of input and the stack is not empty, output the contest of the stack
similarly as follows:
Left delimiter [ at line 10, char 25 had no right delimiter.
Left delimiter [ at line 7, char 3 had no right delimiter.
Left delimiter { at line 3, char 20 had no right delimiter.
Turnin details:
1) Your stack should be implemented in its own .cpp file, with an appropriate .h file. You should
then have another .cpp file that uses that stack class to test input for matching braces. This is the
minimum, you can choose to use more files if you want. Follow proper programming standards
in the supplement for this.
2) Insure that your stack class works even for extremely large inputs, that is, make sure to check
on whether your stack class is actually able to get memory. If unable to get enough memory, it
should display an appropriate error message and shut down.
3) Make sure your program takes in an integer to use to determine what type of stack to use, then
starts reading in lines of text in a loop until it receives the one line by itself with the character
“DONE”. Do not use request any other inputs.
Please edit your first post and put the code inside code tags. To do this, highlight the code and click the <> button to the right of the edit window. This will make the code show up with line numbers so we can refer to them when answering questions.

Looking at part 1, you're supposed to push the delimiter character AND the line number & character number. You're only pushing the delimiter.

The input ends with the user enters "DONE" but your program prompts the user to enter "done". Case matters.

Your code reads only one line.

Your code scans all characters in the input array rather than just the characters read in. I suggest that you use std::string rather than a fixed-length buffer.

Why is are you allocating a DynIntStack on the heap instead of just making it a local variable? Right now you're leaking the stack at the end of the program.

DynIntStack's destructor doesn't work right, you aren't walking through the stack right.

I suggest that you create a constructor for StackNode so you're guaranteed that you'll construct it with all 3 pieces of data:
1
2
3
4
5
6
struct StackNode {
    StackNode(char ch, int ln, int ct) :
        character(ch), lineNum(ln), count(ct), next(NULL)
        {}
    ...
};


DynIntStack::pop() needs to return the line number and character number too.
Topic archived. No new replies allowed.