include <iostream> #include <string> #include <cmath> #include <iomanip> #include <fstream> #include <cmath> #include <vector> #include <cstdlib> //____________________________________________________________________________________ using namespace std; //____________________________________________________________________________________ class CommandLineException { public: CommandLineException(int max, int actual) { cout << " Too many command line arguments. " << endl; } }; //____________________________________________________________________________________ class FileException { public: FileException(char* fn) { cout << " File " << fn << " could not be opened. " << endl; } }; //____________________________________________________________________________________ class Stack_Empty_Exception { public: Stack_Empty_Exception(char* a) { cout << " I am . " << a <<" empty" << endl; } }; //____________________________________________________________________________________ class Stack_Full_Exception { public: Stack_Full_Exception(char* fn) { cout << " I am " << fn << " full. " << endl; } }; //____________________________________________________________________________________ class Stack { public: Stack(int); ~Stack(); bool isFull(void); // check less than size bool isEmpty(void); void Push(char v); char Pop(void); private: int size; int top_of_stack; char* s; }; //____________________________________________________________________________________ Stack::Stack() { int sz = 100; size = sz; top_of_stack = -1; s = new char[size]; } //____________________________________________________________________________________ Stack::~Stack() { if(s) { delete[]s; } } //____________________________________________________________________________________ bool Stack::isFull(void) { return top_of_stack >= size - 1; } //____________________________________________________________________________________ bool Stack::isEmpty(void) { return top_of_stack < 0; } //____________________________________________________________________________________ void Stack::Push( char v) { if (isFull()) throw Stack_Full_Exception(); s[++top_of_stack] = v; } //____________________________________________________________________________________ char Stack::Pop(void) { if (isEmpty()) throw Stack_Empty_Exception(); return s[top_of_stack--]; } //____________________________________________________________________________________ int main(int argc, char * argv[]) { try { char infile[100]; // input file char outfile[100]; // output file if (argc == 1) { cout << " Enter the input file name. " << endl; cin >> infile; cout << " Enter the output file name. " << endl; cin >> outfile; cout << endl; } else if (argc == 2) { strcpy(infile, argv[1]); cout << " Enter the output file name. " << endl; cin >> outfile; cout << endl; } else if ( argc == 3) { strcpy(infile, argv[1]); strcpy(outfile, argv[2]); } else { throw CommandLineException(2,argc -1); } ifstream i(infile); if(!i) throw FileException(infile); ofstream o(outfile); if(!o) throw FileException(outfile); o.close(); i.close(); } catch(...) { cout << " Program Terminated. " << endl; exit(EXIT_FAILURE); } void is_Palindrome(isstream& i, ostream& o) { for(;;) { string word; i >> word; if ( i.eof()) break; o << endl; o << word << " is a palindrome. " << endl; } // end of for loop } // end of is_Palindrome function. bool check_Palindrome( string& word) { Stack reverse; for ( int i = 0; i < word.length(); i++) { reverse.push(word[i]); } for ( int j = 0; j < word.length(); j++) { if (word[j] != reverse.pop()) return false; else return true; } } // end of check_palindrome return 0; }
mpiling... p02.cpp D:\MSDev98\CS2613\p02\p02.cpp(79) : error C2511: 'Stack::Stack' : overloaded member function 'void (void)' not found in 'Stack' D:\MSDev98\CS2613\p02\p02.cpp(59) : see declaration of 'Stack' D:\MSDev98\CS2613\p02\p02.cpp(111) : error C2512: 'Stack_Full_Exception::Stack_Full_Exception' : no appropriate default constructor available D:\MSDev98\CS2613\p02\p02.cpp(118) : error C2512: 'Stack_Empty_Exception::Stack_Empty_Exception' : no appropriate default constructor available D:\MSDev98\CS2613\p02\p02.cpp(178) : error C2065: 'isstream' : undeclared identifier D:\MSDev98\CS2613\p02\p02.cpp(178) : error C2065: 'i' : undeclared identifier D:\MSDev98\CS2613\p02\p02.cpp(178) : error C2065: 'o' : undeclared identifier D:\MSDev98\CS2613\p02\p02.cpp(178) : error C2275: 'ostream' : illegal use of this type as an expression d:\vc98\include\iosfwd(257) : see declaration of 'ostream' D:\MSDev98\CS2613\p02\p02.cpp(179) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition D:\MSDev98\CS2613\p02\p02.cpp(183) : error C2065: 'word' : undeclared identifier D:\MSDev98\CS2613\p02\p02.cpp(183) : warning C4552: '>>' : operator has no effect; expected operator with side-effect D:\MSDev98\CS2613\p02\p02.cpp(184) : error C2228: left of '.eof' must have class/struct/union type D:\MSDev98\CS2613\p02\p02.cpp(186) : error C2563: mismatch in formal parameter list D:\MSDev98\CS2613\p02\p02.cpp(186) : error C2568: '<<' : unable to resolve function overload could be 'class std::basic_ostream<unsigned short,struct std::char_traits<unsigned short> > &__cdecl std::endl(class std::basic_ostream<unsigned short,struct std::char_traits<unsigned short> > &)' d:\vc98\include\ostream(377) : see declaration of 'endl' or 'class std::basic_ostream<char,struct std::char_traits<char> > &__cdecl std::endl(class std::basic_ostream<char,struct std::char_traits<char> > &)' d:\vc98\include\ostream(372) : see declaration of 'endl' or 'class std::basic_ostream<_E,_Tr> &__cdecl std::endl(class std::basic_ostream<_E,_Tr> &)' d:\vc98\include\ostream(367) : see declaration of 'endl' D:\MSDev98\CS2613\p02\p02.cpp(187) : error C2297: '<<' : illegal, right operand has type 'char [19]' D:\MSDev98\CS2613\p02\p02.cpp(192) : error C2601: 'check_Palindrome' : local function definitions are illegal Error executing cl.exe. p02.obj - 14 error(s), 1 warning(s)
#include <iostream> #include <string> #include <cctype> int IsMirrorPalindrome( const char* word, size_t length ) { if( length <= 1) return 1; else return ( (word[0] == word[length-1]) && IsMirrorPalindrome(word+1,length-2)); } int IsPalindrome(std::string test) { std::string tempcopy; std::string::iterator iter = test.begin(); const std::string::iterator end = test.end(); while ( iter != end ) { const char c = *iter; if ( (!std::ispunct(c)) && (!std::isspace(c)) ) tempcopy.push_back(std::tolower(c)); ++iter; } if (IsMirrorPalindrome(tempcopy.c_str(),tempcopy.length())) return 1; else return 0; } int main() { std::string test; std::cout<<" enter string.... "; std::getline(std::cin,test); if (IsPalindrome(test)) std::cout<<std::endl<<"palindrome"<<std::endl; else std::cout<<std::endl<<" not palindrome"<<std::endl; getchar(); }
//____________________________________________________________________________________________________________________ #include <iostream> #include <string> #include <cmath> #include <iomanip> #include <fstream> #include <cmath> #include <vector> #include <cstdlib> //____________________________________________________________________________________ using namespace std; //____________________________________________________________________________________ class CommandLineException { public: CommandLineException(int max, int actual) { cout << " Too many command line arguments. " << endl; } }; //____________________________________________________________________________________ class FileException { public: FileException(char* fn) { cout << " File " << fn << " could not be opened. " << endl; } }; //____________________________________________________________________________________ class Stack { public: Stack(); ~Stack(); bool isFull(void); bool isEmpty(void); void Push(char v); char Pop(void); class Stack_Empty_Exception{public:Stack_Empty_Exception();}; class Stack_Full_Exception{public:Stack_Full_Exception();}; private: int size; int top_of_stack; char* s; }; //____________________________________________________________________________________ Stack::Stack_Empty_Exception::Stack_Empty_Exception() { cout << " The stack is empty. " << endl; } //____________________________________________________________________________________ Stack::Stack_Full_Exception::Stack_Full_Exception() { cout << " The stack is full. " << endl; } //____________________________________________________________________________________ Stack::Stack() { int sz = 100; size = sz; top_of_stack = -1; s = new char[size]; } //____________________________________________________________________________________ Stack::~Stack() { if(s) delete[]s; } //____________________________________________________________________________________ bool Stack::isFull(void) { return top_of_stack >= size - 1; } //____________________________________________________________________________________ bool Stack::isEmpty(void) { return top_of_stack < 0; } //____________________________________________________________________________________ void Stack::Push( char v) { if (isFull()) throw Stack_Full_Exception(); s[++top_of_stack] = v; } //____________________________________________________________________________________ char Stack::Pop(void) { if (isEmpty()) throw Stack_Empty_Exception(); return s[top_of_stack--]; } //____________________________________________________________________________________ bool is_Palindrome(ifstream& i, ofstream& o); void Palindrome_mgr(string str1, ofstream& o); //____________________________________________________________________________________ int main(int argc, char* argv[]) { try { char infile[255]; // input file char outfile[255]; // output file if (argc == 1) { cout << " Enter the input file name. " << endl; cin >> infile; cout << " Enter the output file name. " << endl; cin >> outfile; cout << endl; } else if (argc == 2) { strcpy(infile, argv[1]); cout << " Enter the output file name. " << endl; cin >> outfile; cout << endl; } else if ( argc == 3) { strcpy(infile, argv[1]); strcpy(outfile, argv[2]); } else { throw CommandLineException(2,argc -1); } ifstream i(infile); if(!i) throw FileException(infile); ofstream o(outfile); if(!o) throw FileException(outfile); string word; for(;;) { if(i.eof()) break; i >> word; } Palindrome_mgr(word, o); o.close(); i.close(); } catch(...) { cout << " Program Terminated. " << endl; exit(EXIT_FAILURE); } return 0; } bool is_Palindrome(string check1, string check2 ) { if (check1 == check2) return true; else return false; } // end of is_Palindrome function. void Palindrome_mgr(string str1, ofstream& o) { string str2; bool swapped; Stack s; for ( int i = 0; i < str1.length(); i++) { s.Push(str1[i]); str2 += s.Pop(); } swapped = is_Palindrome(str1,str2); if (swapped) cout << str1 << " is a palindrome." << endl; else cout<< str1 << " is not a palindrome. " << endl; } // end of Palindrome_mgr.
string word; //declare a single string for(;;) { if(i.eof()) //if find EOF break; //stop i >> word; ///else read in each word, but don't do anything with it. } Palindrome_mgr(word, o); //send last word read in to this function.
| DaniWeb Message | |
| Cancel Changes | |