tình hình là mấy bũa giờ ko code... bây giờ ngồi code lại thì gặp ngay cái lỗi này..
bác nào có kinh nghiệm thì xem lại giúp mình nhá....
thank các bác trước...........
bác nào có kinh nghiệm thì xem lại giúp mình nhá....
thank các bác trước...........
Code:
#include <stdio.h> #include <iostream> #include <string> #include <conio.h> using namespace std; class String { private: char *string; int lenght; public: String(); String(char *s); ~String(); String &operator=(const String &); friend istream & operator>>( istream &, String &); friend ostream & operator<<( ostream &, String &); friend String operator+(const String &, const String &); friend bool operator>(const String &, const String &); friend bool operator<(const String &, const String &); friend bool operator==(const String &, const String &); friend bool operator!=(const String &, const String &); char &operator[](int &); }; /*************************************************************/ String::String() { string = new char[1]; string[0]='\0'; lenght = 0; } /*************************************************************/ String::~String() { if(string!=NULL) delete []string; } /*************************************************************/ String::String(char *s) { this->lenght = strlen(s); this->string = new char[lenght+1]; strcpy(string,s); } /************************************************************/ istream & operator>>( istream &input, String &s) { input.ignore(1); input.getline(s.string,50); return input; } /*************************************************************/ ostream & operator<<(ostream &output, String &s) { output<<s.string; output<<endl; return output; } /************************************************************/ String &String::operator=(const String &s) { lenght = s.lenght; strcpy(string,s.string); return (*this); } /*************************************************************/ String operator+(const String &s1, const String &s2) { String Result; Result.lenght = s1.lenght + s2.lenght; Result.string = new char [ Result.lenght+1]; for(int i=0; i<s1.lenght; ++i) Result.string[i] = s1.string[i]; for(int j=0; j<s2.lenght; ++j) Result.string[s1.lenght+j] = s2.string[j]; return Result; } /*************************************************************/ char &String::operator[](int &x) { return string[x]; } /**************************************************************/ bool operator>(const String &s1, const String &s2) { if(s1.lenght>s2.lenght) return true; else { if(strcmp(s1.string,s2.string)>0) return true; } return false; } /****************************************************************/ bool operator<(const String &s1, const String &s2) { if(s1.lenght<s2.lenght) return true; else { if(strcmp(s1.string,s2.string)<0) return true; } return false; } /***************************************************************/ bool operator==(const String &s1, const String &s2) { if(strcmp(s1.string,s2.string)==0) return true; return false; } /***************************************************************/ bool operator!=(const String &s1, const String &s2) { if(s1.lenght!=s2.lenght) return true; else { if(strcmp(s1.string,s2.string)!=0) return true; } return false; } int main() { String *S1; String S2("Nguyen Binh Khiem"); int n; cout<<"nhap so chuoi: "; cin>>n; S1 = new String[n]; for(int i=0; i<n; ++i) { cout<<"nhap chuoi thu "<<i+1<<": "; cin>>S1[i]; } for(int j=0; j<n; ++j) { cout<<"chuoi thu "<<j+1<<" la: "; cout<<S1[j]; } getch(); return 0; }
Comment