@@ -23,6 +23,13 @@ class CompareStacks: public Problem<T,D>{
2323 // Compare the stacks
2424 void runCompare (int buffer = 0 );
2525
26+ // IO
27+ void printCompare (){
28+ std::string str = Problem<T,D>::toString ();
29+ str += " \n " + (*mNormalStack ).toString ();
30+ std::cout << str << std::endl;
31+ }
32+
2633private:
2734 // Stack Functions: defined by user
2835 virtual D readInput (std::vector<std::string> line) = 0;
@@ -54,57 +61,60 @@ CompareStacks<T,D>::CompareStacks(std::string fileName, int size, int space, int
5461==============================================================================*/
5562template <class T , class D >
5663Data<T,D> CompareStacks<T,D>::popCompare(){
57- std::cout << " Debug CompareStacks::pop 1" << std::endl;
5864 return Problem<T,D>::mStack ->pop (*this );
59- std::cout << " Debug CompareStacks::pop 2" << std::endl;
6065}
6166
6267template <class T , class D >
6368void CompareStacks<T,D>::runCompare(int buffer){
64- Problem<T,D>::initStackIntern ();
65- while ((Problem<T,D>::mInput .good ())) {
66- std::streampos position = Problem<T,D>::mInput .tellg ();
67- for (int i = 1 ; i <= buffer; i++) {
68- bool bIndex = Problem<T,D>::top (i).mIndex == mNormalStack ->top (i).mIndex ;
69- bool bData = Problem<T,D>::top (i).mData == mNormalStack ->top (i).mData ;
70- if (!bIndex || !bData) {
71- throw " The top $(i)st elements are different" ;
69+ try {
70+ Problem<T,D>::initStackIntern ();
71+ while ((Problem<T,D>::mInput .good ())) {
72+ std::streampos position = Problem<T,D>::mInput .tellg ();
73+ (*Problem<T,D>::mStack ).setPosition (position);
74+ for (int i = 1 ; i <= buffer; i++) {
75+ bool bIndex = Problem<T,D>::top (i).mIndex == mNormalStack ->top (i).mIndex ;
76+ bool bData = Problem<T,D>::top (i).mData == mNormalStack ->top (i).mData ;
77+ if (!bIndex || !bData) {
78+ Problem<T,D>::println ();
79+ std::cout << mNormalStack ->toString () << std::endl;
80+ throw " The top $(i)st elements are different" ;
81+ }
7282 }
73- }
74- std::vector<std::string> line = Problem<T,D>::readLine ();
75- if ( (line.front ()== " -1" ) || (line.front ()==" " ) ) {
76- break ;
77- }
78- D data = readInput (line);
79- Problem<T,D>::mIndex ++; // Might have to move
80- std::cout << " Debug runCompare 1" << std::endl;
81- if (Problem<T,D>::emptystack () != mNormalStack ->isempty ()) {
82- throw " One stack is empty and not the other" ;
83- }
84- std::cout << " Debug runCompare 2" << std::endl;
85- while ( (!(Problem<T,D>::emptystack ())) && (popCondition (data)) ) {
86- std::cout << " Debug runCompare 2.1" << std::endl;
87- Data<T,D> elt = popCompare ();
88- std::cout << " Debug runCompare 2.2" << std::endl;
89- Data<T,D> eltNormal = mNormalStack ->pop ();
90- std::cout << " Debug runCompare 2.3" << std::endl;
91- bool bIndex = elt.mIndex == eltNormal.mIndex ;
92- bool bData = elt.mData == eltNormal.mData ;
93- std::cout << " Debug runCompare 2.4" << std::endl;
94- if (!bIndex || !bData) {
95- throw " The two elements popped are different" ;
83+ std::vector<std::string> line = Problem<T,D>::readLine ();
84+ if ( (line.front ()== " -1" ) || (line.front ()==" " ) ) {
85+ break ;
86+ }
87+ D data = readInput (line);
88+ Problem<T,D>::mIndex ++; // Might have to move
89+ if ((*Problem<T,D>::mStack ).isempty () != mNormalStack ->isempty ()) {
90+ (*Problem<T,D>::mStack ).isempty ();
91+ Problem<T,D>::println ();
92+ std::cout << mNormalStack ->toString () << std::endl;
93+ (*Problem<T,D>::mStack ).isempty ();
94+ throw " One stack is empty and not the other" ;
95+ }
96+ while ( (!(Problem<T,D>::emptystack ())) && (popCondition (data)) ) {
97+ Data<T,D> elt = Problem<T,D>::pop ();
98+ Data<T,D> eltNormal = mNormalStack ->pop ();
99+ popAction (elt);
100+ bool bIndex = elt.mIndex == eltNormal.mIndex ;
101+ bool bData = elt.mData == eltNormal.mData ;
102+ if (!bIndex || !bData) {
103+ Problem<T,D>::println ();
104+ std::cout << *Problem<T,D>::mContext << std::endl;;
105+ std::cout << mNormalStack ->toString () << std::endl;
106+ throw " The two elements popped are different" ;
107+ }
108+ }
109+ if (pushCondition (data)) {
110+ Data<T,D> elt (Problem<T,D>::mIndex ,data);
111+ pushAction (elt);
112+ Problem<T,D>::push (elt);
113+ mNormalStack ->push (elt);
96114 }
97- std::cout << " Debug runCompare 2.5" << std::endl;
98- popAction (elt);
99- std::cout << " Debug runCompare 2.6" << std::endl;
100- }
101- std::cout << " Debug runCompare 3" << std::endl;
102- if (pushCondition (data)) {
103- Data<T,D> elt (Problem<T,D>::mIndex ,data);
104- pushAction (elt);
105- Problem<T,D>::push (elt);
106- mNormalStack ->push (elt);
107115 }
116+ } catch (char const * e) {
117+ std::cout << e << std::endl;
108118 }
109119}
110120
0 commit comments