Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct stackelem
- {
- stackelem* next;
- char payload;
- };
- char temp [128];
- class stack
- {
- private:
- stackelem *data;
- public:
- stack () {data = NULL;}
- void reset() {
- while (data) pop();
- }
- bool empty() {
- return data ? false : true;
- }
- void push (char value)
- {
- stackelem* newelem = new stackelem;
- newelem->next = data;
- newelem->payload = value;
- data = newelem;
- }
- char pop()
- {
- if (data->payload)
- {
- char rvalue = data->payload;
- stackelem* old = data;
- data = data->next;
- delete old;
- return rvalue;
- }
- else {
- cerr << "Stack is empty \n";
- return 0;
- }
- }
- };
- int main ()
- {
- stack st;
- fstream file ("input.txt", ios::in);
- if (!file)
- {
- cerr << "File not opened\n";
- return -1;
- }
- while (file)
- {
- st.reset();
- file.getline (temp, sizeof(temp));
- // clean noise characters
- int j = 0;
- for (int i = 0; temp[i]; i++)
- if ((temp[i] >= '0') && (temp[i] <= '9'))
- temp[j++] = temp[i];
- temp[j]=0;
- cout << "processing [" << temp << "] : ";
- // stack algorithm start
- if (j) {
- int k;
- for (k = 0; k < j/2; k++)
- st.push (temp[k]);
- if (j&1)
- k++;
- do
- {
- if ( st.pop() != temp[k++] )
- {
- cout << "not symmetric [" << k << "]" << endl;
- temp[k] = -1;
- break;
- }
- } while (temp[k]);
- if (!temp[k])
- cout << "symmetric " << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement