Help on stack , queue, palindrome program...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2005
Posts: 16
Reputation: mysterio is an unknown quantity at this point 
Solved Threads: 0
mysterio mysterio is offline Offline
Newbie Poster

Help on stack , queue, palindrome program...

 
0
  #1
Oct 10th, 2005
Question:--
Write a program that read a line of text, changes each uppercase letter to lowercase and places each letter both in a queue and onto a stack.
The program should then verify whether the line of text is a palindrome


Output:

Please enter a line of text
I am A.I


i AM a.i

This is a palindrome

---------------------------------------------------------------------

How do i change the capital letter to smaller letter as above output example??? wat function i should add??
i using fstream function..now..wat should i do to change which i can enter text by my own??

thanks 4 helping..


My answers below:-

  1. #include<iostream>
  2. #include <cstdlib> // For EXIT_FAILURE and EXIT_SUCCESS
  3. #include <fstream>
  4. #include<string>
  5. using namespace std;
  6. const int MAX=50; // initialize max string size of 50 characters
  7. typedef char StackElement; // define StackElement
  8. typedef char QueueElement; // define QueueElement
  9. class Stack
  10. {
  11. public:
  12. Stack(){top=-1;arr[MAX]=0;} // default stack constructor
  13. void push(StackElement & ch); // push function
  14. StackElement topNpop(); // top and pop functions combined
  15. bool empty() const; // empty function
  16. private:
  17. StackElement arr[MAX]; // define char array
  18. int top; // define int top
  19.  
  20. };
  21. /*******************************************
  22. FUNCTION: push()
  23. DESCRIPTION: Pushes an element onto the stack
  24. PRECONDITION: Waiting for function call
  25. POSTCONTION: New element character on top of stack
  26. *******************************************/
  27. inline void Stack::push(StackElement & ch)
  28. {
  29. if(top<MAX)
  30. {
  31. top++; // increment top
  32. arr[top]=ch; // push onto stack
  33. }
  34. else
  35. {
  36. cout<<"Stack is full.\n"; // display stack is full
  37. }
  38.  
  39.  
  40. }
  41. /*******************************************
  42. FUNCTION: topNpop()
  43. DESCRIPTION: Reads and pops top element off the stack
  44. PRECONDIION: Waiting for function call
  45. POSTCONDITION: One element read and removed fromt he stack
  46. RETURN: Top element from stack
  47. ********************************************/
  48.  
  49. inline StackElement Stack::topNpop()
  50. {
  51. if(top>-1)
  52. {
  53. return(arr[top]); // returns top element
  54. top--; // remove froms stack
  55. }
  56. else
  57. {
  58. cout<<"Stack is empty.\n"; // display stack is empty
  59. return(0);
  60.  
  61. }
  62.  
  63. }
  64. /*******************************************
  65. FUNCTION: empty()
  66. DESCRIPTION: returns result value if stack is empty
  67. PRECONDITION: result=false
  68. POSTCONDITION: result may be true or remain false
  69. RETURN: result if true or false
  70. ********************************************/
  71. inline bool Stack::empty() const
  72. {
  73. bool result=false; // initialize bool as false
  74. if (top==-1)
  75. {
  76. result=true; // if top is -1 return result true
  77. return(result);
  78. }
  79. else
  80. {
  81. return(result); // else return false
  82. }
  83. }
  84. class Queue // Queue class
  85. {
  86. public:
  87. Queue(){front=0, back=0;arr[MAX]=0;} // Queue default constructor
  88. void addq(QueueElement & ch); // define addq
  89. QueueElement frontNremoveq(); // define frontNremove
  90. private:
  91. QueueElement arr[MAX]; // initialize QueueElement array
  92. int front, back; // initialize int front and back
  93.  
  94. };
  95. /*******************************************
  96. FUNCTION: addq()
  97. DESCRIPTION: adds an element onto the queue
  98. PRECONDITION: Waiting for element to add
  99. POSTCONDITION: New element now on the queue
  100. ********************************************/
  101. inline void Queue::addq(QueueElement &ch)
  102. {
  103. if(front!=(back+1)%MAX)
  104. {
  105. arr[back]=ch; // add element to back of queue
  106. back=(back+1)%MAX;
  107. }
  108. else
  109. {
  110. cerr<<"Error Queue is full\n"; // display queue is full
  111. }
  112. }
  113. /*******************************************
  114. FUNCTION: frontNremoveq()
  115. DESCRIPTION: reads and removes front element from queue
  116. PRECONDITION: front pointing to front of queue
  117. POSTCONDITION: front element is returned and then incremented
  118. ********************************************/
  119. inline QueueElement Queue::frontNremoveq()
  120. {
  121. if(front!=back)
  122. {
  123. return(arr[front]); // return front element
  124. front++; // remove front element
  125. }
  126. else
  127. {
  128. cout<<"Queue is empty.\n"; // display queue is empty
  129. return(0);
  130. }
  131. }
  132.  
  133. /***************************MAIN******************************/
  134. int main()
  135. {
  136. Stack S; // initialize stack
  137. Queue Q; // initialize queue
  138. string s;
  139. int i=0; // initialze int 'i'
  140. char string[MAX]; // initialize char string
  141. bool RESULT=false; // initilize bool RESULT to false
  142.  
  143.  
  144.  
  145. ifstream inside ("palindromeinput.txt");
  146. if (! inside.is_open())
  147. { cout << "Error opening file"; exit (1); }
  148.  
  149. while (! inside.eof() )
  150. {
  151. inside.getline (string,100);
  152. cout << string << endl;
  153. }
  154.  
  155. while(string[i]!=NULL)
  156. {
  157. S.push(string[i]); // push chars individually from string to
  158. Q.addq(string[i]); // stack and queue
  159. i++; // next char
  160. }
  161. while(i>0)
  162. {
  163. if(S.topNpop()==Q.frontNremoveq()) // compare each element from
  164. { // stack and queue
  165. RESULT=true; // if same for all chars return true
  166. }
  167. else
  168. {
  169. RESULT=false; // if not same for any char break and return false
  170. break;
  171. }
  172. i--;
  173. }
  174.  
  175.  
  176. if(RESULT==true)
  177. {
  178. cout<<string<<" is a palindrome\n"; // display if true
  179. }
  180. else
  181. {
  182. cout<<string<<" is not a palindrome\n"; // display if false
  183. }
  184.  
  185.  
  186.  
  187. ofstream outside ("palindromeoutput.txt");
  188. if (outside.is_open())
  189. {
  190.  
  191. outside << string;
  192. outside<< string;
  193. outside.close();
  194. }
  195. return 0;
  196. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help on stack , queue, palindrome program...

 
0
  #2
Oct 10th, 2005
How do i change the capital letter to smaller letter as above output example
use tolower() to convert a character to lower-case. It only works on one character, so if you want to convert a whole string to lower case you have to create a loop to iterate each character. Here is a c++ example.
  1. #include <string>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <cctype>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. string str = "UPPER-CASE";
  11. transform(str.begin(),str.end(),str.begin(),tolower);
  12. cout << str << endl;
  13. return 0;
  14. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help on stack , queue, palindrome program...

 
0
  #3
Oct 10th, 2005
I'm not terribly STL-savvy, what is this error telling me?
  1. //Error E2285 testpp.cpp 11: Could not find a match for 'transform<InputIterator1,InputIterator2,OutputIterator,BinaryOperation>(char *,char *,char *,charT (*)(charT,const locale &))' in function main()
...while I continue searching for an answer on my own...

[edit]Found this:
transform
template<class InIt, class OutIt, class Fn1>
    OutIt transform(InIt first, InIt last, OutIt dest,
        Fn1 func);
template<class InIt1, class InIt2, class OutIt,
    class Fn2>
    OutIt transform(InIt1 first1, InIt1 last1,
        InIt2 first2, OutIt dest, Fn2 func);
The first template function evaluates *(dest + N) = func(*(first + N)) once for each N in the range [0, last - first). It then returns dest + (last - first). The call func(*(first + N)) must not alter *(first + N).

The second template function evaluates *(dest + N) = func(*(first1 + N), *(first2 + N)) once for each N in the range [0, last1 - first1). It then returns dest + (last1 - first1). The call func(*(first1 + N), *(first2 + N)) must not alter either *(first1 + N) or *(first2 + N).
[Dani - Here I really miss the fixed fonts! Follow the dinkumware link and note my previous replies as to why. Oh, and font sizes would have been beneficial as well.]

[edit=2]Ahh.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help on stack , queue, palindrome program...

 
0
  #4
Oct 11th, 2005
Originally Posted by Dave Sinkula
I'm not terribly STL-savvy, what is this error telling me?
  1. //Error E2285 testpp.cpp 11: Could not find a match for 'transform<InputIterator1,InputIterator2,OutputIterator,BinaryOperation>(char *,char *,char *,charT (*)(charT,const locale &))' in function main()
...while I continue searching for an answer on my own...
Dev-C++ (gcc) has a compiler bug
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help on stack , queue, palindrome program...

 
0
  #5
Oct 11th, 2005
Originally Posted by Ancient Dragon
Dev-C++ (gcc) has a compiler bug
Borland must have the same bug then.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 16
Reputation: mysterio is an unknown quantity at this point 
Solved Threads: 0
mysterio mysterio is offline Offline
Newbie Poster

Re: Help on stack , queue, palindrome program...

 
0
  #6
Oct 12th, 2005
Originally Posted by Ancient Dragon
use tolower() to convert a character to lower-case. It only works on one character, so if you want to convert a whole string to lower case you have to create a loop to iterate each character. Here is a c++ example.
  1. #include <string>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <cctype>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. string str = "UPPER-CASE";
  11. transform(str.begin(),str.end(),str.begin(),tolower);
  12. cout << str << endl;
  13. return 0;
  14. }

this what i have done...
still hv some problems...

1.my output:-
Please enter a line of text:
i am not a.i
i am not a.i
This is a palindrome <-----it must be this is not a palindrome(uncorrect answers)
Press any key to continue


2. how to use the touppercase() n tolowercase()combine as below???

I am A.I

i AM a.i



  1. #include<iostream>
  2. #include <cstdlib> // For EXIT_FAILURE and EXIT_SUCCESS
  3. #include <fstream>
  4. #include<string>
  5.  
  6. #include <algorithm>
  7. #include <iomanip>
  8. #include <cctype>
  9.  
  10. using namespace std;
  11.  
  12. const int MAX=50; // initialize max string size of 50 characters
  13. typedef char StackElement; // define StackElement
  14. typedef char QueueElement; // define QueueElement
  15. class Stack
  16. {
  17. public:
  18. Stack(){top=-1;arr[MAX]=0;} // default stack constructor
  19. void push(StackElement & ch); // push function
  20. StackElement topNpop(); // top and pop functions combined
  21. bool empty() const; // empty function
  22. private:
  23. StackElement arr[MAX]; // define char array
  24. int top; // define int top
  25.  
  26. };
  27. /*******************************************
  28. FUNCTION: push()
  29. DESCRIPTION: Pushes an element onto the stack
  30. PRECONDITION: Waiting for function call
  31. POSTCONTION: New element character on top of stack
  32. *******************************************/
  33. inline void Stack::push(StackElement & ch)
  34. {
  35. if(top<MAX)
  36. {
  37. top++; // increment top
  38. arr[top]=ch; // push onto stack
  39. }
  40. else
  41. {
  42. cout<<"Stack is full.\n"; // display stack is full
  43. }
  44.  
  45.  
  46. }
  47. /*******************************************
  48. FUNCTION: topNpop()
  49. DESCRIPTION: Reads and pops top element off the stack
  50. PRECONDIION: Waiting for function call
  51. POSTCONDITION: One element read and removed fromt he stack
  52. RETURN: Top element from stack
  53. ********************************************/
  54.  
  55. inline StackElement Stack::topNpop()
  56. {
  57. if(top>-1)
  58. {
  59. return(arr[top]); // returns top element
  60. top--; // remove froms stack
  61. }
  62. else
  63. {
  64. cout<<"Stack is empty.\n"; // display stack is empty
  65. return(0);
  66.  
  67. }
  68.  
  69. }
  70. /*******************************************
  71. FUNCTION: empty()
  72. DESCRIPTION: returns result value if stack is empty
  73. PRECONDITION: result=false
  74. POSTCONDITION: result may be true or remain false
  75. RETURN: result if true or false
  76. ********************************************/
  77. inline bool Stack::empty() const
  78. {
  79. bool result=false; // initialize bool as false
  80. if (top==-1)
  81. {
  82. result=true; // if top is -1 return result true
  83. return(result);
  84. }
  85. else
  86. {
  87. return(result); // else return false
  88. }
  89. }
  90. class Queue // Queue class
  91. {
  92. public:
  93. Queue(){front=0, back=0;arr[MAX]=0;} // Queue default constructor
  94. void addq(QueueElement & ch); // define addq
  95. QueueElement frontNremoveq(); // define frontNremove
  96. private:
  97. QueueElement arr[MAX]; // initialize QueueElement array
  98. int front, back; // initialize int front and back
  99.  
  100. };
  101. /*******************************************
  102. FUNCTION: addq()
  103. DESCRIPTION: adds an element onto the queue
  104. PRECONDITION: Waiting for element to add
  105. POSTCONDITION: New element now on the queue
  106. ********************************************/
  107. inline void Queue::addq(QueueElement &ch)
  108. {
  109. if(front!=(back+1)%MAX)
  110. {
  111. arr[back]=ch; // add element to back of queue
  112. back=(back+1)%MAX;
  113. }
  114. else
  115. {
  116. cerr<<"Error Queue is full\n"; // display queue is full
  117. }
  118. }
  119. /*******************************************
  120. FUNCTION: frontNremoveq()
  121. DESCRIPTION: reads and removes front element from queue
  122. PRECONDITION: front pointing to front of queue
  123. POSTCONDITION: front element is returned and then incremented
  124. ********************************************/
  125. inline QueueElement Queue::frontNremoveq()
  126. {
  127. if(front!=back)
  128. {
  129. return(arr[front]); // return front element
  130. front++; // remove front element
  131. }
  132. else
  133. {
  134. cout<<"Queue is empty.\n"; // display queue is empty
  135. return(0);
  136. }
  137. }
  138.  
  139. /***************************MAIN******************************/
  140. int main()
  141. {
  142. Stack S; // initialize stack
  143. Queue Q; // initialize queue
  144. string s ;
  145. int i=0; // initialze int 'i'
  146. char string[MAX]; // initialize char string
  147. bool RESULT=false; // initilize bool RESULT to false
  148.  
  149.  
  150. strcpy(string," ");
  151. //transform(s.begin(),s.end(),s.begin(),toupper);
  152.  
  153. cout << "Please enter a line of text: " << endl;
  154.  
  155. cin.getline (string,100);
  156.  
  157. while(string[i]!=NULL)
  158. {
  159. S.push(string[i]); // push chars individually from string to
  160. Q.addq(string[i]); // stack and queue
  161. i++; // next char
  162. }
  163. while(i>0)
  164. {
  165. if(S.topNpop()==Q.frontNremoveq()) // compare each element from
  166. { // stack and queue
  167. RESULT=true; // if same for all chars return true
  168. }
  169. else
  170. {
  171. RESULT=false; // if not same for any char break and return false
  172. break;
  173. }
  174. i--;
  175. }
  176.  
  177.  
  178. if(RESULT==true)
  179. {
  180. cout<<string<<" \nThis is a palindrome\n"; // display if true
  181. }
  182. else
  183. {
  184. cout<<string<<" \nThis is not a palindrome\n"; // display if false
  185. }
  186.  
  187. cout<<s<<s<<endl;
  188.  
  189. return 0;
  190. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help on stack , queue, palindrome program...

 
0
  #7
Oct 12th, 2005
1. start out by replacing that C-style character array named "string" with a c++ string class and name it something else, such as line. You are writing a c++ program, so use c++ classes whenever possible. Not that it can't be done with C, just that it's more consistent.

2. replace cin.getline() with getline(cin,line)

3. paste that transform line after the getline() in #2 above


After making the above changes your program seems to work ok for the strings I entered.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 16
Reputation: mysterio is an unknown quantity at this point 
Solved Threads: 0
mysterio mysterio is offline Offline
Newbie Poster

Re: Help on stack , queue, palindrome program...

 
0
  #8
Oct 12th, 2005
Originally Posted by Ancient Dragon
1. start out by replacing that C-style character array named "string" with a c++ string class and name it something else, such as line. You are writing a c++ program, so use c++ classes whenever possible. Not that it can't be done with C, just that it's more consistent.

2. replace cin.getline() with getline(cin,line)

3. paste that transform line after the getline() in #2 above


After making the above changes your program seems to work ok for the strings I entered.
my program is C++ rite???
mind show me where should i add the transform function??
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help on stack , queue, palindrome program...

 
0
  #9
Oct 13th, 2005
Originally Posted by mysterio
my program is C++ rite???
Yes, it is.

Originally Posted by mysterio
mind show me where should i add the transform function??
I already told you. You can do your own copy/paste, my fingers can't ready your keyboard :eek:
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 16
Reputation: mysterio is an unknown quantity at this point 
Solved Threads: 0
mysterio mysterio is offline Offline
Newbie Poster

Re: Help on stack , queue, palindrome program...

 
0
  #10
Oct 14th, 2005
Originally Posted by Ancient Dragon
Yes, it is.



I already told you. You can do your own copy/paste, my fingers can't ready your keyboard :eek:
show me at least example..then i can only can do it
how to make stack.h,queue.h,palindrome.cpp seperate files....??
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC