Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************ stack.h ************/
- #include<vector>
- #include<iostream>
- #include<algorithm>
- using namespace std;
- class stack
- {
- public:
- bool push(const string&);
- bool pop(string &elem);
- bool peek(string &elem);
- bool empty() const {return _stack.empty();}
- bool full() const {return _stack.size()==_stack.max_size();}
- int size() const {return _stack.size();}
- bool find(const string &elem) const;
- int count(const string &elem) const;
- private:
- vector<string> _stack;
- };
- /********** stack.cpp **********/
- #include "stack.h"
- bool stack::push(const string& str)
- {
- if(full()) return false;
- _stack.push_back(str);
- return true;
- }
- bool stack::pop(string& elem)
- {
- if(empty()) return false;
- elem = _stack.back();
- _stack.pop_back();
- return true;
- }
- bool stack::peek(string& elem)
- {
- if(empty()) return false;
- elem = _stack.back();
- return true;
- }
- bool stack::find (const string &elem) const
- {
- vector<string>::const_iterator it = _stack.begin();
- return (::find(it,_stack.begin(),elem))!=_stack.end();
- }
- int stack::count(const string &elem) const
- {
- return (::count(_stack.begin(),_stack.end(),elem));
- }
- /*********** main.cpp *************/
- #include "stack.cpp"
- #include<fstream>
- int main()
- {
- stack st;
- string str;
- ifstream readfile("sentence.txt");
- while(readfile>>str && !st.full())
- {
- st.push(str);
- }
- if (st.empty())
- {
- cout<<"Oops! No strings were read\n";
- return 0;
- }
- st.peek(str);
- if (st.size()==1 && str.empty())
- {
- cout<<"Oops! No strings were read\n";
- return 0;
- }
- cout<<"The number of elements read are: "<<st.size()<<endl;
- cout<<"The sentence in reverse order is: "<<endl;
- while(!st.empty())
- {
- st.pop(str);
- cout<<str<<' ';
- }
- cout<<"\nEnter a word to search: ";
- cin>>str;
- if(st.find(str))
- cout<<"The word was found "<<st.count(str)<<" no. of times"<<endl;
- else
- cout<<"The word was not found"<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement