Автор | Сообщение |
|
Отправлено: 08.11.12 16:13. Заголовок: Basic Palindrome vector
#include <iostream> #include <vector> #include <string> using namespace std; int main(){ string words; cout <<"Enter words : " ; getline( cin,words ); string wordsReverse( words.rbegin() , words.rend() ); cout << "\nBackwards , it's : " << wordsReverse << endl << endl; if( words == wordsReverse ) cout << words << " is a palindrome !" << endl; else cout << words << " is not a palindrome ! " << endl; system( "pause" );//Pause window return 0;//Exit Program } i know it's not hard . but the problem is , here is the question A palindrome is a sequence of characters, numbers or words that have the property of reading the same in either direction (backwards or forwards). In a palindrome white space and punctuation have no significance (they are typically ignored). Examples of palindromes are shown below. 121 - A numeric sequence GACTTCAG - A DNA Sequence Sator Arepo tenet opera rotas - A latin phrase for: The farmer by his labour keeps the wheels to the plough. As you can see in the last example, white space and capitalisation are ignored. You are to write a program that takes in a sequence and works out if it is/isn’t a palindrome. We are going to do this in three different ways. how to ignore capitalisation and while space ? Besides that . how to change the iterator STL to vector STL ?
|
|
|
Ответов - 39
, стр:
1
2
All
[только новые]
|
|
|
Отправлено: 08.11.12 16:33. Заголовок: I have not understoo..
I have not understood the last question about changing the STL iterator to the STL vector. What did you mean? To ignore white spaces and other non-alpha-digit symbols and to ignore capitalization you can for example copy a string into a vector of characters and then compare the vector with it itself using reverse iterators. Or you can write an appropriate function yourself. For example (without testing) for ( char c : words ) { if ( isalnum( c ) ) v.push_back( toupper( c ) ); } if ( v == std::vector<char>( v.rbegin(), v.rend() ) ) std::cout << "It is a polindrome\n";
|
|
|
|
Отправлено: 08.11.12 16:43. Заголовок: Sorry sir . here i d..
Sorry sir . here i did it // Palindrome.h // start points to the beginning of the data and end points to one past the end template<typename BidirectionalIterator> bool IsPalindrome(BidirectionalIterator start, BidirectionalIterator end) { if (start == end - 1) return true; end--; while (start <= end) { if (*start == *end) { start++; end--; } else return false; } return true; } //Main.cpp #include <iostream> #include <string> #include <vector> using namespace std; ostream & operator<<(ostream &, vector<int> &); int main() { string word = "tattarrattat"; // longest palindrome in English vector<int> number; for (int i = 0; i < 5; i++) number.push_back(i); for (int j = 4; j >= 0; j--) number.push_back(j); if (IsPalindrome( word.begin(), word.end())){ string reverse( word.rbegin() , word.rend() ); cout << "Forward : " << word << endl << "Backward : " << reverse << endl << "It is a palindrome.\n"; } else{ string reverse( word.rbegin() , word.rend() ); cout << "Forward : " << word << endl << "Backward : " << reverse << endl << "It is not a palindrome.\n"; } if (IsPalindrome(number.begin(), number.end())){ int Reversenumber ( number.rbegin() , number.rend() ); cout << "Forward : " << number << endl << "Backward : " << Reversenumber << endl << "It is a palindrome." << endl; } else{ int Reversenumber ( number.rbegin() , number.rend() ); cout << "Forward : " << number << endl << "Backward : " << Reversenumber << endl << "It is not a palindrome." << endl; } system( "pause" ); return 0; } ostream & operator<<(ostream & stream, vector<int> & vec) { for(int i = 0; i < vec.size(); i++) cout << vec; return stream; } my problem is . for the ReverseNumber variable , why I cnanot declare it as int Reversenumber ( number.rbegin() , number.rend() ); it got error error C2440: 'initializing' : cannot convert from 'std::reverse_iterator<_RanIt>' to 'int'
|
|
|
|
Отправлено: 08.11.12 16:56. Заголовок: Your code is invalid..
Your code is invalid. Let consider template function IsPalindrome First of all start can be equal to end. So you may not use expression start == end - 1 It would be more correctly to write if ( start == end || start == std::prev( end ) ) return true; Moreover you wrote that start and end are BiidirectionalIterator. Such iterators have no overload operators as end -1 or start <= end. These operators can be used only for random access iterators. As for the error then an object of type int shall be initialized by an integral expression.
|
|
|
|
Отправлено: 08.11.12 17:00. Заголовок: shall be initialized..
shall be initialized by an integral expression. ? can provide some sample for me?
|
|
|
|
Отправлено: 08.11.12 17:06. Заголовок: int Reversenumber( 1..
int Reversenumber( 10 ); or int Reversenumber = 10; or int Reversenumber = *number.begin(); Here 10 and *number.begin() are integral expressions.
|
|
|
|
Отправлено: 08.11.12 17:13. Заголовок: i get it . by the wa..
i get it . by the way , this is point to the begin only right? so if i wan point to end also? since i wan print from backward to the forward?
|
|
|
|
Отправлено: 08.11.12 17:20. Заголовок: An object of type in..
An object of type int does not points anywhere. It stores an integral value.:) I think that you meant a vector instead of an object of type int. So you should write std::vector<int> Reversenumber ( number.rbegin() , number.rend() );
|
|
|
|
Отправлено: 08.11.12 17:45. Заголовок: it's work ! her..
it's work ! here the full code implement // start points to the beginning of the data and end points to one past the end #include <iostream> #include <string> #include <vector> using namespace std; template<class T> bool IsPalindrome(T start, T end){ if ( start == end || start == std::prev( end ) ) return true; end--; while (start <= end){ if (*start == *end){ start++; end--; } else return false; } return true; } ostream & operator<<(ostream &, vector<int> &); int main() { string word = "GACTTCAG"; // longest palindrome in English vector<int> number; for (int i = 0; i < 5; i++) number.push_back(i); for (int j = 4; j >= 0; j--) number.push_back(j); if (IsPalindrome( word.begin(), word.end())){ string reverse( word.rbegin() , word.rend() ); cout << "Forward : " << word << endl << "Backward : " << reverse << endl << "It is a palindrome." << endl << endl; } else{ string reverse( word.rbegin() , word.rend() ); cout << "Forward : " << word << endl << "Backward : " << reverse << endl << "It is not a palindrome." << endl << endl; } if (IsPalindrome(number.begin(), number.end())){ vector<int> Reversenumber ( number.rbegin() , number.rend() ); cout << "Forward : " << number << endl << "Backward : " << Reversenumber << endl << "It is a palindrome." << endl; } else{ vector<int> Reversenumber ( number.rbegin() , number.rend() ); cout << "Forward : " << number << endl << "Backward : " << Reversenumber << endl << "It is not a palindrome." << endl; } system( "pause" ); return 0; } ostream & operator<<(ostream & stream, vector<int> & vec) { for(int i = 0; i < vec.size(); i++) cout << vec; return stream; } The vector should hold objects of type char. When the sequence is finished being read in, create two iterators. One iterator points to the beginning and the other the end of the vector. does my start and end correct? hehe
|
|
|
|
Отправлено: 08.11.12 17:56. Заголовок: For std::vector<..
For std::vector<int> you could use the simple comparision below instead of function IsPalindrome v == std::vector<int>( v.rbegin(), v.rend() )
|
|
|
|
Отправлено: 08.11.12 18:08. Заголовок: but my question want..
but my question wanted me to use 2 pointer . 1 vector and 2 iterator. which the iterator is point to begin and point to end data . and the code seem like wrong? v== vector<int>( v.rbegin() , v.rend() ) it should insert at which place?
|
|
|
|
Отправлено: 08.11.12 18:11. Заголовок: For example instead ..
For example instead of if (IsPalindrome(number.begin(), number.end())){ you could write if ( number == std::vector<int>( number.rbegin(), number.rend() ) ) { That is there is no need in any call of functon IsPalindrome
|
|
|
|
|
Отправлено: 08.11.12 18:15. Заголовок: i did like yours for..
i did like yours for my last program. but my friend told me that it's not fullfill the question? or he is the one that give me wrong information? The vector should hold objects of type char. When the sequence is finished being read in, create two iterators. One iterator points to the beginning and the other the end of the vector. because i also thought that begin and end is already create two iterator? isn't we are correct and my friend is wrong?
|
|
|
|
Отправлено: 08.11.12 18:23. Заголовок: In your program you ..
In your program you use a vector of type int. In the cited assignment there is said about of a vector of type char. As for iterators then member function end() returns the iterator that points beyond the last element of a vector. I do not know what your friend meant.
|
|
|
|
Отправлено: 08.11.12 18:45. Заголовок: so am i wrong? I saw..
so am i wrong? I saw that u told me i have to use a vector of type char
|
|
|
|
Отправлено: 08.11.12 18:51. Заголовок: I do not know whethe..
I do not know whether you are wrong. It will be said by the tutor who gave your the assignment.:)
|
|
|
|
Отправлено: 09.11.12 12:43. Заголовок: If to follow the ass..
If to follow the assignment text цитата: | Write a C++ program that reads a string/sequence from standard input till EOF. The string/sequence, including all white space, punctuation and newline characters should be read in character-by-character and stored into a vector. The vector should hold objects of type char. When the sequence is finished being read in, create two iterators. One iterator points to the beginning and the other the end of the vector. |
| then the code could look the following way #include <iostream> template <typename BidirectionalIterator> bool IsPolindrome( BidirectionalIterator first, BidirectionalIterator last ) { while ( first != last && first != --last && *first == *last ) ++first; return ( first == last ); } int main() { std::vector<char> v; std::istream::int_type c; std::cout << "Enter a sequence of characters: "; while ( ( c = std::cin.get() ) != EOF ) { v.push_back( c ); } if ( !v.empty() && v.back() == '\n' ) v.pop_back(); std::copy( v.begin(), v.end(), std::ostream_iterator<char>( std::cout ) ); std::cout << std::endl; std::cout << "The sequence is " << ( IsPolindrome( v.begin(), v.end() ) ? "" : "not " ) << "a polindrome\n"; return ( 0 ); }
|
|
|
|
Отправлено: 10.11.12 15:47. Заголовок: The code you showed ..
The code you showed shall not be compiled because you forgot to speciify header <vector> and you declared vector v twice. Update the code that we can discuss a correct code.:)
|
|
|
|
Отправлено: 10.11.12 16:04. Заголовок: Also take into accou..
Also take into account that there is statement if ( !v.empty() && v.back() == '\n' ) v.pop_back(); in my original code. It is important. Let assume that the user entered a sequence and pressed ENTER key and only after that he entered Ctrl+Z and ENTER (for EOF). In this case the vector will contain additional symbol of the new line that will prevent to determine correctly whether the entered sequence is a polindrome.
|
|
|
|
Отправлено: 10.11.12 16:06. Заголовок: #include<iostrea..
#include<iostream> #include <vector> #include<string> #include<stack> #include<queue> using namespace std; int main(){ stack<char> s; queue<char> q; int i = 0; char InputChar; //Vector holds an object type char vector<char> v; istream::int_type c; cout << "Enter a sequence of characters: "; while ( ( c = cin.get() ) != EOF ) { v.push_back( c ); } vector<char> v1(v.begin(),v.end()); vector<char> v2(v.rbegin(),v.rend()); for( i = 0 ; i < v.size() ; i++ ){ s.push(v1); } for( i = 0 ; i < v.size() ; i++ ){ q.push(v2); } if ( !v.empty() && v.back() == '\n' ) v.pop_back(); here is my current cuode that changed as you mentioned and i slightly know that my program when compare with top and front() it should be have something like this while(!q.empty() && !s.empty()){ if(q.front() != s.top()){
|
|
|
|
Отправлено: 10.11.12 16:11. Заголовок: You shall place this..
You shall place this statement if ( !v.empty() && v.back() == '\n' ) v.pop_back(); before filling the stack and the queue and other vectors.
|
|
|
|
Отправлено: 10.11.12 17:31. Заголовок: mind to explain what..
mind to explain what is the v.back() are? if the last character for vector v are '\n' will pop_back the vector container? and did i push correctly for stack and queue?
|
|
|
|
|
Отправлено: 10.11.12 17:36. Заголовок: Method back returns ..
Method back returns reference to the last element of the container. So you check whether the new line character was placed in the container because when the user was entering a sequence he pressed ENTER key. You have to remove this new line character otherwise the result of determining whether a sequence is a polindrome will be incorrect.
|
|
|
|
Отправлено: 10.11.12 17:49. Заголовок: okay. same meaning a..
okay. same meaning as mine isn't? haha so for my push for the stack and queue . is it correct?
|
|
|
|
Отправлено: 10.11.12 18:27. Заголовок: If you have deleted ..
If you have deleted that trailing new line character before pushing the sequence into the stack or the queue then all is o'k.
|
|
|
|
Отправлено: 10.11.12 18:45. Заголовок: okay.. now .. move o..
okay.. now .. move on the next way? so how should i compare for the queue and stack ? with q.front and s.top? i not really get the algorithm..
|
|
|
|
Отправлено: 10.11.12 18:56. Заголовок: It is your algorithm..
It is your algorithm. Why are you asking me about it?
|
|
|
|
Отправлено: 11.11.12 11:18. Заголовок: because i not really..
because i not really know it while(!q.empty() && !s.empty()){ if(q.front() != s.top()){ cout << "The word "; for( i = 0 ; i < v.size() ; i++ ){ cout << v ; } cout<< " is not a palindrome" << endl; break; } else if( q.front() == s.top() ){ i++; q.pop(); s.pop(); if( i = v.size()){ cout << "The word "; for( i = 0 ; i < v.size() ; i++ ){ cout << v ; } cout<< " is not a palindrome" << endl; break; } } } cannot work.. what is my error ??
|
|
|
|
Отправлено: 11.11.12 21:28. Заголовок: You should not ask ..
You should not ask such questions. Why do not you testt the code yourself? 1. Negation of condition ( q.front() != s.top() ) is condition ( q.front() == s.top() ) so there is no need to write else if( q.front() == s.top() ) . It is enpugh to write simply else 2. These two conditions ( !q.empty() && !s.empty() ) and ( i == v.size() ) are reduntant. You should use only one of them. 3. Instead of using the comparision operator in condition ( i = v.size() ) you wrote the assignment operator. 4. In the both parts of the if-else-if operator you wrote that the word is not a polindrome, Then a question arises when is the word a polindrome?! I would write the loop simply std::vector::size_type i = 0; while ( i < v.size() && q.front() == s.top() ) i++; std::cout << "The word "; for ( char c : v ) std::cout << c; std::cout << " is" << ( i == v.size() ? "" : " not" ) << " a polindrome." << std::endl;
|
|
|
|
Отправлено: 12.11.12 04:01. Заголовок: sorry .. what is for..
sorry .. what is for( char : v ) because it's not work for me..
|
|
|
|
Отправлено: 12.11.12 12:36. Заголовок: It is so-called the ..
It is so-called the range based for statement introduced in C++ 2011. If your compiler does not support this feature of the new C++ standard then you can change for ( char c : v ) std::cout << c; to for ( std::vector<char>::const_iterator it = v.begin(); it != v.end(); ++it ) { std::cout << *it; } or for ( std::vector<char>::size_type i = 0; i != v.size(); ++i ) { std::cout << v[ i ]; }
|
|
|
|
Отправлено: 12.11.12 16:10. Заголовок: it's can't w..
it's can't work. some people told me i have to use v to feed my s.top and q.front? how should i feed it?
|
|
|
|
|
Отправлено: 12.11.12 16:18. Заголовок: It was a typo. I for..
It was a typo. I forgot to specify pop commands for the stack and queue. It will look the following way std::vector::size_type i = 0; while ( i < v.size() && q.front() == s.top() ) { s.pop(); q.pop() i++; }
|
|
|
|
Отправлено: 12.11.12 16:25. Заголовок: getting a pop messag..
getting a pop message said that deque iterator not dereferencable. i think something wrong in my stack<char> s; queue<char> q; isn't? ><
|
|
|
|
Отправлено: 12.11.12 16:27. Заголовок: I do not see any der..
I do not see any dereferencing in my code snip.
|
|
|
|
Отправлено: 12.11.12 16:40. Заголовок: #include<iostrea..
#include<iostream> #include <vector> #include<string> #include<stack> #include<queue> using namespace std; int main(){ int i = 0; char InputChar; //Vector holds an object type char vector<char> v; istream::int_type c; cout << "Enter a sequence of characters: "; while ( ( c = cin.get() ) != EOF ) { v.push_back( c ); } while ( !v.empty() && v.back() == '\n' ) v.pop_back(); stack<char> s; queue<char> q; for( i = 0 ; i < v.size() ; i++ ){ s.push(v); q.push(v); } while ( i < v.size() && q.front() == s.top() ){ s.pop(); q.pop(); i++; } cout << "The word "; for( i = 0 ; i < v.size() ; i++){ cout << v; } cout << " is" << ( i == v.size() ? "" : " not" ) << " a polindrome." << endl; system( "pause" ); return 0; } i modified my program , and the error doesn't come out anymore. but the result i get is only palindrome. even i insert the sequence that are not palindrome
|
|
|
|
Отправлено: 12.11.12 17:20. Заголовок: I do not see where t..
I do not see where the stack and the queue are being filled with values. They both are empty. Here is a simplified example that demonstrates how the task can be done std::string str( "ABCDEFGHGFEDCBA" ); std::vector<char> v( str.begin(), str.end() ); std::stack<char> s; std::queue<char> q; for ( std::vector<char>::size_type i = 0; i < v.size(); i++ ) { s.push( v ); q.push( v ); } std::vector<char>::size_type i = 0; while ( i < v.size() && s.top() == q.front() ) { s.pop(); q.pop(); i++; } std::cout << "Word \""; for ( std::vector<char>::const_iterator it = v.begin(); it != v.end(); ++it ) { std::cout << *it; } std::cout << "\" is " << ( ( i == v.size() ) ? "" : "not " ) << "a polindrome\n";
|
|
|
|
Отправлено: 12.11.12 17:29. Заголовок: i already push it . ..
i already push it . mind to see the above program? i already filled it once I saw that i didn't fill. but i have the result that stated only palindrome even not palindrome
|
|
|
|
Отправлено: 12.11.12 17:44. Заголовок: #include <iostr..
#include <iostream> #include <vector> #include <queue> #include <stack> #include <cstdlib> int main() { std::vector<char> v; std::istream::int_type c; std::cout << "Enter a sequence of characters: "; while ( ( c = std::cin.get() ) != EOF ) { v.push_back( c ); } if ( !v.empty() && v.back() == '\n' ) v.pop_back(); std::stack<char> s; std::queue<char> q; for ( std::vector<char>::size_type i = 0; i < v.size(); i++ ) { s.push( v[ i ] ); q.push( v[ i ] ); } std::vector<char>::size_type i = 0; while ( i < v.size() && s.top() == q.front() ) { s.pop(); q.pop(); i++; } std::cout << "Word \""; for ( std::vector<char>::size_type j = 0; j != v.size(); ++j ) std::cout << v[ j ]; std::cout << "\" is " << ( ( i == v.size() ) ? "" : "not " ) << "a polindrome\n"; system( "Pause" ); return ( 0 ); }
|
|
|
|
Отправлено: 12.11.12 18:12. Заголовок: thanks for your help..
thanks for your help but i change the while loop to for loop before the pop() . and it's work. i just try to change it and it's work ! haha. thanks ><!
|
|
|
Ответов - 39
, стр:
1
2
All
[только новые]
|
|