Oct 15, 2013 at 2:45am UTC
Having some issues with figuring out syntax.
The assignment:
Stack Homework:
Write a stack program that can determine whether or not the following symbols are balanced: ( ) { } [ ] /* */
If a left symbol is found, push it onto the stack. If a right symbol is found top/pop the stack.
Test your code well.
There should be no prompt for an output file, no long scroll of tokens or message's. Hard code the input filename as: stack.cpp
Example output: No errors found.
or
Error, symbol not balanced: {
** Note: If a symbol is found that does not match and pair with last push, end the program.
Maybe some of you can give me a hand in getting this syntax right.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <stdlib.h>
#include <iomanip>
#include <sstream>
#include <stdio.h>
using namespace std;
/*-------------------------------------------------------------------------------------------------------------*/
class stack
{
struct stackListNode // Struct linked list
{
int data;
stackListNode *next;
};
stackListNode *top;
public :
stack() // Creates stack, and sets top to NULL
{
top = NULL;
}
/*----------------------------------------- Push --------------------------------------------------------*/
void push(int token) // Push token onto the stack and create new node for top of stack
{
stackListNode *newNode = new stackListNode;
newNode-> data = token;
newNode-> next = top;
top = newNode;
}
/*--------------------------------------- Pop ---------------------------------------------------------*/
int pop()
{
if (IsEmpty())
{
cout << "Stack is empty!\n" << endl;
return NULL;
}
stackListNode *temp = top;
top = temp-> next;
}
/*------------------------------------------ Peek -----------------------------------------------------------*/
int peek()
{
if (IsEmpty())
{
cout << "Stack is empty!\n" << endl;
return NULL;
}
return top-> data;
}
/*--------------------------------------------- Is Empty ---------------------------------------------------*/
int IsEmpty()
{
return top == NULL;
}
}
/*----------------------------------------------------------------------------------------------------------*/
/*--------------------------------------------- Main ------------------------------------------------------*/
int main(int argc, char *argv[])
{
if (argc != 2) // Usage statement of how to use program via command line
{
cout << "\nUsage: " << argv[0] << " 'stack.cpp'\n" << endl;
return (1);
}
ifstream input_File;
input_File.open("stack.cpp" , ios::in);
if (!input_File.is_open())
{
cout << "Error: Unable to open input file\n" << endl;
return (1);
}
/*-------------------------------------------- Read new node ------------------------------------------------------*/
char symbol;
char openComment = '*' ;
char closeComment = '\ ' ; // <-------- Error Here: If I remove the space after \ its an error
// Error: Missing closing quote ???????
while (input_File)
{
input_File.get(symbol);
if (symbol == ' ' ) continue ;
else
{
switch (symbol)
{
case '(' : stack::push;
break ;
case '{' : stack::push;
break ;
case '[' : stack::push;
break ;
case '/' :
continue ;
if (strcmp (openComment,symbol) !=0) // <------- Error Here: "argument of type 'char' is
// incompatible with parameter of type 'const char *'
continue ;
case '*' :
//symbol = openComment;
stack::push;
break ;
}
}
/*-----------------------------------------------------------------------------------------------------------------*/
system("pause" );
return 0;
}
Errors are marked in the code itself.
Last edited on Oct 16, 2013 at 3:50am UTC
Oct 15, 2013 at 3:39am UTC
1 IntelliSense: identifier "push" is undefined c:\users\zero\documents\visual studio 2010\projects\stackhomework\stackhomework\stackhomework.cpp 126 15 StackHomework
Sorry about that.
I am wanting to read in a left symbol, maybe more, and then when i get to my first right symbol, I want to pop the stack. Unless I have an unmatching symbol such as: ( }, then I want to post an error message with that symbol.
Last edited on Oct 15, 2013 at 3:41am UTC
Oct 15, 2013 at 3:57am UTC
`push()' is a member of the `stack' class.
Any reference to `push()' must be through an instance of the `stack' class.
¿sounds familiar?
Oct 15, 2013 at 4:08am UTC
As in stack::push.... Wow, you know what the sad part is, I have done that about 4 times. It was still giving me an error, but now miracle!
Any ideas on how I can get a pairing of: '/* ' ?
I know I have to search for / first, then I have to check the next symbol for *, if it is, then push them both, any ideas?