class string { friend std::istream& operator>>(std::istream& is, string& str); friend std::ostream& operator<<(std::ostream& os, string& str); public: string() //默认构造函数 { m_length = 0; m_pdata = new char[1]; m_pdata[0] = '\0'; } string( char *str = NULL); // 构造函数 string( int len, char ch); ~string(); // 析构函数 string( string &str); // 复制构造函数 int size(){ return m_length;} // 获得字符串长度 string operator+(const string& str); // 重载'+'操作符 string& operator+=(const string& str); // 重载'+='操作符 string& operator=(const string& str); // 重载赋值操作符 bool operator==(const string &s1,const string &s2); // 比较两个字符串是否相等 char& operator[]( int index); // 重载下标操作符 string substr( int pos,int len); // 返回子字符串 string substr( int pos); // 返回子字符串 private: char *m_pdata; int m_length; }; string::string(char *str) // 构造函数 { m_length = strlen(str); m_pdata = new char[length+1]; if(m_pdata != NULL) { strcpy( m_pdata, str); } } string::string( int len, char ch); { m_pdata = new char[len+1]; for(int i = 0; i < n; i++) { m_pdata[i] =a; } m_pdata[len] = '\0'; } string::~string() // 析构函数 { if( m_pdata != NULL) { delete [] m_pdata; } length = 0; } string::string(string &str) // 复制构造函数 { m_length = str.size(); m_pdata = new char[length+1]; for( int i = 0; i < length; i++) { m_pdata[i] = str.m_pdata[i]; } b[m_length] = '\0'; } string string::operator+(const string& str) // 重载+ { int newLen = m_length + str.m_length; char *newstr = new char[newLen+1]; int count = 0; for( int i = 0; i<length; i++) { newstr[i] = this->b[i]; count++; } for( int i =0; count < newLen; count++,i++) { newstr[i] = a.b[i]; } newstr[newLen] = '\0'; string temp(newstr); delete [] newstr; return temp; } string& string::operator+=(const string& str) // 重载+= { int newLen = length + str.length; char *newstr = new char[newLen+1]; int count = 0; for(int i = 0; i < length; i++) { newstr[i] = this->m_pdata[i]; count++; } for(int i =0;count<newLen;count++,i++) { newstr[i] = str.m_pdata[i]; } newstr[newLen] = '\0'; string temp(newstr); *this = temp; delete [] newstr; return *this; } string& string::operator=(const string &str) // 重载= { if(this == &str) return *this; delete [] m_pdata; m_length = str.m_length; m_pdata = new char[m_length+1]; for(int i = 0; i < m_length; i++) { b[i] = a.b[i]; } b[length] = '\0'; return *this; } bool string::operator==(const string &s1,const string &s2) { if( s1.m_length != s2.m_length) { return false; } for( int i = 0; i < s1.m_length && i < s2.m_length; i++) { if( s1.m_data[i] != s2.m_data[i]) { return false; } } return true; } char& string::operator[]( int index) // 重载下标操作符 { if( index > m_length) return m_pdata[m_length-1]; else return m_pdata[index]; } ostream& operator<<(ostream& os, string& str) //重载输出符 { os << str.m_pdata; return os; } istream& operator>>( std::istream& is, string& str) //重载输入符 { is >> str.m_pdata; str.m_length = strlen(str.m_pdata); return is; } string string::substr( int pos, int len) // 两个接受不同参数的substr函数,返回子字符串 { char *p = new char[n+1]; for(int i=0;i<n;i++) { p[i]=b[pos]; pos++; } p[n]='\0'; string k(p); k.m_length=n; delete [] p; return k; } string string::substr( int pos) { int len = m_length; char *p = new char[len-pos+1]; int t = pos; for( int i = 0; t < len; t++,i++) { p[i] = b[t]; } p[t]='\0'; string k(p); k.m_length = len-pos; delete [] p; return k; }运算符 ">", "<", ">=", "<="和"!="均需要被重载用于字符串的比较,此处省略。
#include <iostream> #include <vector> #include <algorithm> using namespace std; void PrintBiggerNum( unsigned int Num) { int num[30] = {0}; int count = 0; unsigned int tmp = Num; while( tmp > 0) { num[count++] = tmp % 10; tmp /= 10; } int max = 0; int i = 0; for( i = 0; i < count; i++) { if( max < num[i] && i != 0) { max = num[i]; } else if(max > num[i] && i != 0) { for( int j = 0; j < i; j++) { if( num[j] > num[i]) { tmp = num[i]; num[i] = num[j]; num[j] = tmp; break; } } break; } } sort(num, num+i, greater<int>()); cout << Num << " ---> "; for( int i = count-1; i >= 0; i--) { cout << num[i]; } cout << endl; return ; } int main() { PrintBiggerNum(134651); return 0; }5. 数组逆时针旋转90度,以及证明你的方法的有效性
#include <iostream> #include <vector> #include <algorithm> using namespace std; #define ARRAY_LEN 3 int NumArray[ARRAY_LEN][ARRAY_LEN]; void RotateArray() { int i = 0, j = 0; for( i = 0; i < ARRAY_LEN; i++) { for( j = i+1; j < ARRAY_LEN; j++) { int temp = NumArray[i][j]; NumArray[i][j] = NumArray[j][i]; NumArray[j][i] = temp; } } for( i = 0; i < ARRAY_LEN/2; i++) { for( j = 0; j < ARRAY_LEN; j++) { int temp = NumArray[i][j]; NumArray[i][j] = NumArray[ARRAY_LEN - 1 - i][j]; NumArray[ARRAY_LEN - 1 - i][j] = temp; } } return ; } int main() { int index = 1; for( int i = 0; i < ARRAY_LEN; i++) { for( int j = 0; j < ARRAY_LEN; j++) { NumArray[i][j] = index++; } } for( int i = 0; i < ARRAY_LEN; i++) { for( int j = 0; j < ARRAY_LEN; j++) { cout << NumArray[i][j] << " "; } cout << endl; } cout << "<------------->" << endl; RotateArray(); for( int i = 0; i < ARRAY_LEN; i++) { for( int j = 0; j < ARRAY_LEN; j++) { cout << NumArray[i][j] << " "; } cout << endl; } return 0; }对于证明其正确性:
可以推导出相应的坐标对应。
By Andy @ 2013年11月1日