c++ - Using Own Stack Class in Encoding Program - Determine Size and Top -


i wrote encoding program using c++ stack library. trying implement own stack class, notice there size() , top() member functions in stack library. not sure how implement code without these functions, or how write functions in class them work code have.

here areas stack library functions being called in readfileencode(string filename, stack<char> &text, string cypher) function:

ifstream file(filename, ios::in | ios::binary); stack<char> temp; char ch;  while (file.get(ch)){     temp.push(ch ^ cypher[temp.size() % cypher.length()]); }  while (!temp.isempty()){     text.push(temp.top());     temp.pop(); } 

here stack class:

#include<iostream>  #define true 1 #define false 0  template <class type>  class stack{ struct node{     type element;     node *next; };  public: node *top; int stacksize;  stack(void);            //constructor ~stack(void);           //destructor free stack  void push(type & value); type pop(void); type peek(void); int isempty(void);      //returns true if empty void print(void); void reset(void);       //pop elements off stack size_t size(void) const; type topof(void) const; };  template <class type> stack<type>::stack(void){ top = null; stacksize = 0; }  template <class type> stack<type>::~stack(void){ cout << "entering stack destructor" << endl; reset(); cout << "exiting stack destructor" << endl; }  template <class type> void stack<type>::push(type & value){ node *temp = new node;  if (temp == null){     cout << "push: memory allocation error" << endl;     exit(1); }  temp->element = value; temp->next = top; top = temp; stacksize++; }  template <class type> type stack<type>::pop(void){ type returnelement;  if (top != null){     node *temp = top;     returnelement = top->element;     top = top->next;     delete temp;        //delete node     stacksize--; }  return(returnelement); }  template <class type> type stack<type>::peek(void){ type returnelement; if (top != null)     returnelement = top->element; cout << "peek: " << returnelement << endl; return(returnelement); }  template <class type> int stack<type>::isempty(void){ if (stacksize == 0)     return(true); else     return(false); }  template <class type> void stack<type>::reset(void){ cout << "reset stack" << endl; while (isempty() != true){     pop(); } }  template <class type> void stack<type>::print(void){ cout << "inside print stack" << endl; cout << "stack size = " << stacksize << endl;  node * temp = top; while (temp != null){     cout << " " << temp->element << endl;     temp = temp->next; } }  template <class type> size_t size(void) const{ return stacksize; }  template <class type> type stack<type>::topof(void) const{ return (*top).element; } 

this stack class based on know of stack's. if there wrong, because first time have written stack class.

so basically, having problems either 1) writing size() , top() functions, or 2) rewriting while loops in readfileencode() function use have. had writing code work stack library, trying implement own class causing me problems. appreciated.

edit 1: of dyp, changed variable name of int size int stacksize everywhere appears in class. also, change function top() topof()

edit 2: changed void push(type &value) void push(type const& value) in instances. code updated above. used following main() test class:

#include <iostream> #include <cstdlib> #include "stack.h"  using namespace std;  int main() { stack<int> s; s.push(1); s.push(2); s.push(3); s.push(4); s.push(5);  cout << "size: " << s.size() << endl; cout << "top element: " << s.topof() << endl;  s.pop(); s.pop();  cout << "size: " << s.size() << endl; cout << "top element: " << s.topof() << endl; cout << "empty: " << s.isempty() << endl;  s.pop(); s.pop(); s.pop();  cout << "size: " << s.size() << endl; //cout << "top element: " << s.top() << endl; cout << "empty: " << s.isempty() << endl;  system ("pause"); } 

everything worked fine, when attached stack.h file encoding program, following errors:

at file.put(text.top()); "term not evaluate function taking 0 arguments," , same error @ text.push(temp.top());. error, not sure how fix it.

i had move public: in class above node *top; int stacksize; because got errors them being private. not problem test program. not sure if ok either.

i used following code test stack:

 int main() { stack<int> s; s.push(1); s.push(2); s.push(3); s.push(4); s.push(5);  cout << "size: " << s.size() << endl; cout << "top element: " << s.topof() << endl;  s.pop(); s.pop();  cout << "size: " << s.size() << endl; cout << "top element: " << s.topof() << endl; cout << "empty: " << s.isempty() << endl;  s.pop(); s.pop(); s.pop();  cout << "size: " << s.size() << endl; //cout << "top element: " << s.top() << endl; cout << "empty: " << s.isempty() << endl;  system ("pause"); } 

i changed code in following stack class following , seems have worked:

void push(type const& value); size_t size(void) const; type topof(void) const; 

and:

void stack<type>::push(type const& value){ node *temp = new node;  if (temp == null){     cout << "push: memory allocation error" << endl;     exit(1); }  temp->element = value; temp->next = top; top = temp; stacksize++; }  template <class type> size_t stack<type>::size(void) const{ return stacksize; }  template <class type> type stack<type>::topof(void) const{ return (*top).element; } 

also changed occurrences of top() in main topof(). not sure if proper, works, , take dyp's advice , turn in @ codereview.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -