1. Implement a string class in C++ with basic functionality like comparison, concatenation, input and output. Please also provide some test cases and using scenarios (sample code of using this class).
Please do not use MFC, STL and other libraries in your implementation.
我的实现方案如下,这道题真地对c++的主要特性都进行了较好地考察.
String.h:
#ifndef STRING_H
#define STRING_H
#include
using namespace std;
class String{
public:
String();
String(int n,char c);
String(const char* source);
String(const String& s);
//String& operator=(char* s);
String& operator=(const String& s);
~String();
char& operator[](int i){return a[i];}
const char& operator[](int i) const {return a[i];}//对常量的索引.
String& operator+=(const String& s);
int length();
friend istream& operator>>(istream& is, String& s);//搞清为什么将>>设置为友元函数的原因.
//friend bool operator< (const String& left,const String& right);
friend bool operator> (const String& left, const String& right);//下面三个运算符都没必要设成友元函数,这里是为了简单.
friend bool operator== (const String& left, const String& right);
friend bool operator!= (const String& left, const String& right);
private:
char* a;
int size;
};
#endif
String.cpp:
#include “String.h”
#include
#include
String::String(){
a = new char[1];
a[0] = ‘\0′;
size = 0;
}
String::String(int n,char c){
a = new char[n + 1];
memset(a,c,n);
a[n] = ‘\0′;
size = n;
}
String::String(const char* source){
if(source == NULL){
a = new char[1];
a[0] = ‘\0′;
size = 0;
}
else
{ size = strlen(source);
a = new char[size + 1];
strcpy(a,source);
}
}
String::String(const String& s){
size = strlen(s.a);//可以访问私有变量.
a = new char[size + 1];
//if(a == NULL)
strcpy(a,s.a);
}
String& String::operator=(const String& s){
if(this == &s)
return *this;
else
{
delete[] a;
size = strlen(s.a);
a = new char[size + 1];
strcpy(a,s.a);
return *this;
}
}
String::~String(){
delete[] a;//
}
String& String::operator+=(const String& s){
int j = strlen(a);
int size = j + strlen(s.a);
char* tmp = new char[size+1];
strcpy(tmp,a);
strcpy(tmp+j,s.a);
delete[] a;
a = tmp;
return *this;
}
int String::length(){
return strlen(a);
}
main.cpp:
#include
#include “String.h”
using namespace std;
bool operator==(const String& left, const String& right)
{
int a = strcmp(left.a,right.a);
if(a == 0)
return true;
else
return false;
}
bool operator!=(const String& left, const String& right)
{
return !(left == right);
}
ostream& operator<<(ostream& os,String& s){
int length = s.length();
for(int i = 0;i < length;i++)
//os << s.a[i];这么不行,私有变量.
os << s[i];
return os;
}
String operator+(const String& a,const String& b){
String temp;
temp = a;
temp += b;
return temp;
}
bool operator<(const String& left,const String& right){
int j = 0;
while((left[j] != ‘\0′) && (right[j] != ‘\0′)){
if(left[j] < right[j])
return true;
else
{
if(left[j] == right[j]){
j++;
continue;
}
else
return false;
}
}
if((left[j] == ‘\0′) && (right[j] != ‘\0′))
return true;
else
return false;
}
bool operator>(const String& left, const String& right)
{ int a = strcmp(left.a,right.a);
if(a > 0)
return true;
else
return false;
}
istream& operator>>(istream& is, String& s){
delete[] s.a;
s.a = new char[20];
int m = 20;
char c;
int i = 0;
while (is.get(c) && isspace(c));
if (is) {
do {s.a[i] = c;
i++;
/*if(i >= 20){
cout << “Input too much characters!” << endl;
exit(-1);
}*/
if(i == m – 1 ){
s.a[i] = ‘\0′;
char* b = new char[m];
strcpy(b,s.a);
m = m * 2;
s.a = new char[m];
strcpy(s.a,b);
delete[] b;
}
}
while (is.get(c) && !isspace(c));
//如果读到空白,将其放回.
if (is)
is.unget();
}
s.size = i;
s.a[i] = ‘\0′;
return is;
}
int main(){
String a = “abcd”;
String b = “www”;
//String c(6,b);这么写不对.
String c(6,’l');
String d;
String e = a;//abcd
String f;
cin >> f;//需要输入…
String g;
g = a + b;//abcdwww
if(a < b)
cout << “a < b” << endl;
else
cout << “a >= b” << endl;
if(e == a)
cout << “e == a” << endl;
else
cout << “e != a” << endl;
b += a;
cout << a << endl;
cout