看板 NTUEE107HW 關於我們 聯絡資訊
//code 6 #include <iostream> #include <cstring> using namespace std; class MyStr{ private: int length; char * buf; public: MyStr(const char* str){ length = strlen(str); buf = new char[length+1]; strncpy(buf, str, length+1); } MyStr(const MyStr& str){ length = str.length; buf = new char[length+1]; strncpy(buf, str.buf, length+1); } ~MyStr(){delete [] buf;} MyStr operator --(){ for(int i=0;i<length;++i){ char &ch = buf[i]; if(ch >='A' && ch <= 'Z'){ ch += 'a'-'A';//to lowercase break; } } return *this; } //to be continued //code 6 continued MyStr& operator --(int x){ for(int i=length-1;i>=0;--i){ char &ch = buf[i]; if(ch >='a' && ch <= 'z'){ ch += 'A'-'a';//to uppercase break; } } return *this; } void print(){cout << buf << endl;} }; int main(void){ MyStr my_str("Happy New Year of ram"); my_str.print(); (--(my_str--)).print(); my_str.print(); ((--(my_str--))----).print(); my_str.print(); } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.216.80.233