C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 14th, 2004, 02:58 PM
silicon silicon is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 1 silicon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Fstream Palindrome check

Hello everyone, I've recently learned about queues and fully understand that they follow a fifo order as opposed to stacks that follow a filo order.
I am now trying to write a program that will check a text file for a word and check to see if it is the same forwards and backwards. I've been able to get that to work but am still having problems.

I am trying to get the program to
1)read my entry (DAD) in the palindromeinput.txt
2) check to see if it is a palindrome
3) write the results in palindromeoutput.txt (DAD is a palindrome)

Steps 2 and 3 are not working correctly and I cannot figure out why.
If you have any suggestions please let me know


Thanks in advance

Code:


#include<iostream> 
#include <cstdlib> // For EXIT_FAILURE and EXIT_SUCCESS 
#include <fstream> 
#include<string> 
using namespace std; 
const int MAX=50;	// initialize max string size of 50 characters 
typedef char StackElement;  // define StackElement 
typedef char QueueElement;  // define QueueElement 
class Stack 
{ 
public: 
   Stack(){top=-1;arr[MAX]=0;}   // default stack constructor 
	  void push(StackElement & ch);  // push function 
	  StackElement topNpop();		 // top and pop functions combined 
	  bool empty() const;			// empty function 
private: 
	  StackElement arr[MAX];	// define char array 
	  int   top;				  // define int top 
	
}; 
/******************************************* 
FUNCTION: push() 
DESCRIPTION: Pushes an element onto the stack 
PRECONDITION: Waiting for function call 
POSTCONTION: New element character on top of stack 
*******************************************/ 
inline void Stack::push(StackElement & ch) 
{ 
   if(top<MAX) 
   { 
	  top++;		  // increment top 
	  arr[top]=ch;  // push onto stack 
   } 
   else 
   { 
	  cout<<"Stack is full.\n";  // display stack is full 
   } 


} 
/******************************************* 
FUNCTION: topNpop() 
DESCRIPTION: Reads and pops top element off the stack 
PRECONDIION: Waiting for function call 
POSTCONDITION:  One element read and removed fromt he stack 
RETURN: Top element from stack 
********************************************/ 

inline StackElement Stack::topNpop() 
{ 
   if(top>-1) 
   { 
	  return(arr[top]);  // returns top element 
	  top--;			// remove froms stack 
   } 
   else 
   { 
	  cout<<"Stack is empty.\n";  // display stack is empty 
	  return(0); 

   } 

} 
/******************************************* 
FUNCTION: empty() 
DESCRIPTION: returns result value if stack is empty 
PRECONDITION: result=false 
POSTCONDITION: result may be true or remain false 
RETURN: result if true or false 
********************************************/		  
inline bool Stack::empty() const 
{ 
   bool result=false;   // initialize bool as false 
   if (top==-1)		  
   { 
	  result=true;		// if top is -1 return result true 
	  return(result); 
   } 
   else 
   { 
	  return(result);	 // else return false 
   } 
} 
class Queue						   // Queue class 
{ 
public: 
	  Queue(){front=0, back=0;arr[MAX]=0;}   // Queue default constructor 
	  void addq(QueueElement & ch);		  // define addq 
	  QueueElement frontNremoveq();		 // define frontNremove 
private: 
	  QueueElement arr[MAX];			// initialize QueueElement array 
	  int front, back;				  // initialize int front and back 
	
}; 
/******************************************* 
FUNCTION: addq() 
DESCRIPTION: adds an element onto the queue 
PRECONDITION: Waiting for element to add 
POSTCONDITION: New element now on the queue 
********************************************/ 
inline void Queue::addq(QueueElement &ch) 
{ 
   if(front!=(back+1)%MAX) 
   { 
	  arr[back]=ch;	 // add element to back of queue 
	  back=(back+1)%MAX; 
   } 
   else 
   { 
	  cerr<<"Error Queue is full\n";  // display queue is full 
   } 
} 
/******************************************* 
FUNCTION: frontNremoveq() 
DESCRIPTION: reads and removes front element from queue 
PRECONDITION: front pointing to front of queue 
POSTCONDITION: front element is returned and then incremented 
********************************************/ 
inline QueueElement Queue::frontNremoveq() 
{ 
   if(front!=back) 
   { 
	  return(arr[front]);	// return front element 
	  front++;			// remove front element 
   } 
   else 
   { 
	  cout<<"Queue is empty.\n";  // display queue is empty 
	  return(0); 
   } 
} 

/***************************MAIN*******************  ***********/ 
int main() 
{ 
   Stack S;   // initialize stack 
   Queue Q;   // initialize queue 
string s; 
   int i=0;   // initialze int 'i' 
   char string[MAX];  // initialize char string 
   bool RESULT=false;  // initilize bool RESULT to false 
	

  
ifstream inside ("palindromeinput.txt"); 
  if (! inside.is_open()) 
  { cout << "Error opening file"; exit (1); } 

  while (! inside.eof() ) 
  { 
	inside.getline (string,100); 
	cout << string << endl; 
  } 

while(string[i]!=NULL) 
   { 
	  S.push(string[i]);  // push chars individually from string to 
	  Q.addq(string[i]);	  // stack and queue 
	  i++;	  // next char 
   } 
   while(i>0) 
   { 
	  if(S.topNpop()==Q.frontNremoveq())  // compare each element from 
	  {							  // stack and queue 
		 RESULT=true;  // if same for all chars return true 
	  } 
	  else 
	  { 
		 RESULT=false;  // if not same for any char break and return false 
		 break; 
	  } 
	  i--; 
   }	


if(RESULT==true) 
   { 
	  cout<<string<<" is a palindrome\n";  // display if true 
   } 
   else 
   { 
	  cout<<string<<" is not a palindrome\n"; // display if false 
   } 
	


ofstream outside ("palindromeoutput.txt"); 
  if (outside.is_open()) 
  { 
  
	outside << string; 
	outside<< string; 
	outside.close(); 
  } 
  return 0; 
} 
 

Reply With Quote
  #2  
Old July 15th, 2004, 05:51 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
Is this a school assignment that has to be implemented with a stack and a queue or will any solution suffice? If you can use any method then I have to say its a novel approach but just a bit too complicated. You could read the string in, reverse it and then strcmp it with the original to see if its a palindrome.

-KM-

Reply With Quote
  #3  
Old July 10th, 2005, 09:02 AM
codeless codeless is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 4 codeless User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 18 m 36 sec
Reputation Power: 0
Question implementing a palindrome with a stack and a queue

Quote:
Originally Posted by kode_monkey
Is this a school assignment that has to be implemented with a stack and a queue or will any solution suffice? If you can use any method then I have to say its a novel approach but just a bit too complicated. You could read the string in, reverse it and then strcmp it with the original to see if its a palindrome.

-KM-

http://forums.devarticles.com/showthread.php?p=34117#post34117

I would I modify the code from the above post to use use a stack and a queue (templates).>?

Reply With Quote
  #4  
Old October 13th, 2005, 07:10 AM
jason07 jason07 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 1 jason07 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 53 sec
Reputation Power: 0
Quote:
Originally Posted by silicon
Hello everyone, I've recently learned about queues and fully understand that they follow a fifo order as opposed to stacks that follow a filo order.
I am now trying to write a program that will check a text file for a word and check to see if it is the same forwards and backwards. I've been able to get that to work but am still having problems.

I am trying to get the program to
1)read my entry (DAD) in the palindromeinput.txt
2) check to see if it is a palindrome
3) write the results in palindromeoutput.txt (DAD is a palindrome)

Steps 2 and 3 are not working correctly and I cannot figure out why.
If you have any suggestions please let me know


Thanks in advance

Code:


#include<iostream> 
#include <cstdlib> // For EXIT_FAILURE and EXIT_SUCCESS 
#include <fstream> 
#include<string> 
using namespace std; 
const int MAX=50;	// initialize max string size of 50 characters 
typedef char StackElement;  // define StackElement 
typedef char QueueElement;  // define QueueElement 
class Stack 
{ 
public: 
   Stack(){top=-1;arr[MAX]=0;}   // default stack constructor 
	  void push(StackElement & ch);  // push function 
	  StackElement topNpop();		 // top and pop functions combined 
	  bool empty() const;			// empty function 
private: 
	  StackElement arr[MAX];	// define char array 
	  int   top;				  // define int top 
	
}; 
/******************************************* 
FUNCTION: push() 
DESCRIPTION: Pushes an element onto the stack 
PRECONDITION: Waiting for function call 
POSTCONTION: New element character on top of stack 
*******************************************/ 
inline void Stack::push(StackElement & ch) 
{ 
   if(top<MAX) 
   { 
	  top++;		  // increment top 
	  arr[top]=ch;  // push onto stack 
   } 
   else 
   { 
	  cout<<"Stack is full.\n";  // display stack is full 
   } 


} 
/******************************************* 
FUNCTION: topNpop() 
DESCRIPTION: Reads and pops top element off the stack 
PRECONDIION: Waiting for function call 
POSTCONDITION:  One element read and removed fromt he stack 
RETURN: Top element from stack 
********************************************/ 

inline StackElement Stack::topNpop() 
{ 
   if(top>-1) 
   { 
	  return(arr[top]);  // returns top element 
	  top--;			// remove froms stack 
   } 
   else 
   { 
	  cout<<"Stack is empty.\n";  // display stack is empty 
	  return(0); 

   } 

} 
/******************************************* 
FUNCTION: empty() 
DESCRIPTION: returns result value if stack is empty 
PRECONDITION: result=false 
POSTCONDITION: result may be true or remain false 
RETURN: result if true or false 
********************************************/		  
inline bool Stack::empty() const 
{ 
   bool result=false;   // initialize bool as false 
   if (top==-1)		  
   { 
	  result=true;		// if top is -1 return result true 
	  return(result); 
   } 
   else 
   { 
	  return(result);	 // else return false 
   } 
} 
class Queue						   // Queue class 
{ 
public: 
	  Queue(){front=0, back=0;arr[MAX]=0;}   // Queue default constructor 
	  void addq(QueueElement & ch);		  // define addq 
	  QueueElement frontNremoveq();		 // define frontNremove 
private: 
	  QueueElement arr[MAX];			// initialize QueueElement array 
	  int front, back;				  // initialize int front and back 
	
}; 
/******************************************* 
FUNCTION: addq() 
DESCRIPTION: adds an element onto the queue 
PRECONDITION: Waiting for element to add 
POSTCONDITION: New element now on the queue 
********************************************/ 
inline void Queue::addq(QueueElement &ch) 
{ 
   if(front!=(back+1)%MAX) 
   { 
	  arr[back]=ch;	 // add element to back of queue 
	  back=(back+1)%MAX; 
   } 
   else 
   { 
	  cerr<<"Error Queue is full\n";  // display queue is full 
   } 
} 
/******************************************* 
FUNCTION: frontNremoveq() 
DESCRIPTION: reads and removes front element from queue 
PRECONDITION: front pointing to front of queue 
POSTCONDITION: front element is returned and then incremented 
********************************************/ 
inline QueueElement Queue::frontNremoveq() 
{ 
   if(front!=back) 
   { 
	  return(arr[front]);	// return front element 
	  front++;			// remove front element 
   } 
   else 
   { 
	  cout<<"Queue is empty.\n";  // display queue is empty 
	  return(0); 
   } 
} 

/***************************MAIN*******************  ***********/ 
int main() 
{ 
   Stack S;   // initialize stack 
   Queue Q;   // initialize queue 
string s; 
   int i=0;   // initialze int 'i' 
   char string[MAX];  // initialize char string 
   bool RESULT=false;  // initilize bool RESULT to false 
	

  
ifstream inside ("palindromeinput.txt"); 
  if (! inside.is_open()) 
  { cout << "Error opening file"; exit (1); } 

  while (! inside.eof() ) 
  { 
	inside.getline (string,100); 
	cout << string << endl; 
  } 

while(string[i]!=NULL) 
   { 
	  S.push(string[i]);  // push chars individually from string to 
	  Q.addq(string[i]);	  // stack and queue 
	  i++;	  // next char 
   } 
   while(i>0) 
   { 
	  if(S.topNpop()==Q.frontNremoveq())  // compare each element from 
	  {							  // stack and queue 
		 RESULT=true;  // if same for all chars return true 
	  } 
	  else 
	  { 
		 RESULT=false;  // if not same for any char break and return false 
		 break; 
	  } 
	  i--; 
   }	


if(RESULT==true) 
   { 
	  cout<<string<<" is a palindrome\n";  // display if true 
   } 
   else 
   { 
	  cout<<string<<" is not a palindrome\n"; // display if false 
   } 
	


ofstream outside ("palindromeoutput.txt"); 
  if (outside.is_open()) 
  { 
  
	outside << string; 
	outside<< string; 
	outside.close(); 
  } 
  return 0; 
} 
 



i wanna ask somethings...in ur program...how can it change stack and queue in #include<.h>
i mean #include<stack.h> and #<queue.h> and <.cpp> seperately?? can show me how to???

thanks...in advance....

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Fstream Palindrome check


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway