Homework Assignment #5

profilejwilk
stacks_balance.zip

balance.cpp

#include <iostream> #include <list> #include <stack> using namespace std; /* * This program reads from standard input, checking for balancing of * () [], and {} pairs. * * If an error is detected, then the program prints an error message to * standard output. The messages are one of the following: * "Unmatched { in line N" * "Unmatched } in line N" * "Mismatched { and } in line N" * where the character { could be replaced by any of {, (, or [ * and the charcter } could be replaced by any of }, ], or ) and * N is the line number in which the error was detected. */ /* * Use this routine to signal errors when you have encountered a )]} * that does not match the most recent ([{ * * @param left character that we were trying to match (one * of '(', '[', or '{') * @param right character that saw instead (one * of ')', ']', or '}') * @param lineNumber line of input in which the right character * was encountered. * */ void mismatchError (char left, char right, int lineNumber) { cout << "*Mismatched " << left << " and " << right << " in line " << lineNumber << endl; } /* * Use this routine to signal errors when you have encountered a {[( * and there are no )]} (even non-matching ones) left in the input, or * if you have encountered a )]} with no preceding ({[ (even non-matching * ones) in the input. * * @param c Character that was not properly matched. * @param lineNumber line of input in which that character was encountered. */ void unmatchedError (char c, int lineNumber) { cout << "*Unmatched " << c << " in line " << lineNumber << endl; } typedef /* ... declaration of stack type, must use std::stack ... */ StackType; /* ... initialization & utility routines, if needed ... */ int main() { StackType stk; char c; int lineNumber = 0; /* ... additional initialization code, if needed ... */ while (!cin.eof()) { c = cin.get(); switch (c) { case '(': /* ... action to take on seeing this character ... */ break; case '[': /* ... action to take on seeing this character ... */ break; case '{': /* ... action to take on seeing this character ... */ break; case ')': /* ... action to take on seeing this character ... */ break; case ']': /* ... action to take on seeing this character ... */ break; case '}': /* ... action to take on seeing this character ... */ break; case '\n': ++lineNumber; } } /* ... Finalization & clean-up code, if needed ... */ cout << "Done" << endl; return 0; }

bin/Linux/balance

bin/Windows/balance.exe

make.dep

makefile

DIR=${PWD} ASST=$(notdir ${DIR}) MAINPROG=balance # ######################################################################## # Macro definitions for "standard" C and C++ compilations # CPPFLAGS=-g -std=c++11 CFLAGS=-g TARGET=$(MAINPROG) CPPS=balance.cpp LINK=g++ $(CPPFLAGS) # CC=gcc CXX=g++ # # # In most cases, you should not change anything below this line. # # The following is "boilerplate" to set up the standard compilation # commands: # OBJS=$(CPPS:%.cpp=%.o) DEPENDENCIES = $(CPPS:%.cpp=%.d) %.d: %.cpp touch $@ %.o: %.cpp $(CXX) $(CPPFLAGS) -MMD -o $@ -c $*.cpp # # Targets: # all: $(TARGET) $(TARGET): $(OBJS) $(LINK) $(FLAGS) -o $(TARGET) $(OBJS) $(LFLAGS) clean: -/bin/rm -f *.d *.o $(TARGET) make.dep: $(DEPENDENCIES) -cat $(DEPENDENCIES) > $@ include make.dep