Implementing Stack Class with Vector

I'm trying to do a palindrome check from a text file that reads each line separately and tells whether or not the line is a palindrome and outputs that information to a new file, from the driver program, after creating the stack class using a vector instead of an array. My understanding of this seems to be halted, and I've searched up and down without being able to pick up on this. If my vector is in private data member of the Stack class, should I be passing my inFile through the class or still in the driver? Do my member functions look alright using vector functions to implement them? Is it possible to even make the palindrome check in the driver?

Assignment: Write a driver program to do palindrome check. In your driver program, you are required to use the Stack class that you create in this lab to do the string processing. Your program need to be able to read our provided text file, find which line is palindrome and which line is not, and output all of palindromes line by line to a new file.

Hint: You may consider to use a string(initialized as empty) and your Stack class in your program that reads a line of sentence from the file, one character at a time, pushing only each letter character/number character(not special sign, etc.) onto a stack as it is read and simultaneously append it to the string. Then empty out the stack and put all of the letters/numbers to a second string, which is used to hold the reversed string. Next compare the first string and the second string to see if both are the same.


header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <vector>
using namespace std;

template <class T>
class Stack{

private:
	vector<T> data;

public:
	bool Empty(); //checks if stack is empty 
	void Push(T item); //adds item to stack
	T Top(); //returns item at top of stack
	void Pop(); //removes item at top of stack
	friend ostream& operator<<(ostream&out, const Stack &s); //overloaded output operator

};


implementation file
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
#include <iostream>
#include <vector>
#include "Stack.h"
using namespace std;

template <class T>
bool Stack<T>::Empty() //checks if stack is empty 
{
	return data.empty();
}

template <class T>
void Stack<T>::Push(T item) //adds item to stack
{
	data.push_back(item);

}

template <class T>
T Stack<T>::Top() //returns item at top of stack
{
	if(data.back()>-1)
	return data.back();

	else
		cerr<<"Stack is empty\n";
}

template <class T>
void Stack<T>::Pop() //removes item at top of stack
{
	data.pop_back();
}

template <class T>
ostream & operator<<(ostream&out, const Stack<T> &s) //overloaded output operator
{
	while(!s.Empty())
	{
	out<<s.Top();
	s.Pop();
	}return out;
}


driver file
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
#include <iostream>
#include <vector>
#include <string>
#include "Stack.h"
#include <fstream>
using namespace std;

int main()
{
	ifstream inFile;
	ofstream outFile;
	inFile.open("palindrome.txt");
	outFile.open("outfile.txt");

	Stack<string> s;
	string holdPalindrome="";
//I know the following isn't really right but I stopped here
	while(inFile)
	{
		holdPalindrome.append(s.Top());
		outFile<<holdPalindrome<<endl;
	}

	inFile.close();
	outFile.close();

		return 0;
}
Last edited on
Topic archived. No new replies allowed.