Create a parametric string type. The basic type is to act as container class that contains a class T object. In the prototype case, the object is a char. The normal end-of-string sentinel is 0. The standard behavior should model the functions found in the string library. The class definition could parameterize the sentinel as well. Such a type exists in the standard library string.
创建一个参数化字符串类型,它是一个包含对象class T的容器类。在字符串原型中,对象是char类型,字符串结束标志是0。实现该参数化类型时可模仿string库,类定义时也可以将字符串结束标志进行参数化。
//本程序用VCSP6编译通过
#include<iostream>
#include<cassert>
#include<string>
using namespace std;
const int max_size = 100;
template<typename T>
class my_string
{
public:
my_string(const my_string& str)//define constructor
{
len = str.len;
end = str.end;
for(int i = 0; i < len; i++)
{
s[i] = str.s[i];
}
}; // constructor function 1
my_string()
{
len = 0;
end = 0;
s[0] = end;
};
my_string(const T * str,const T End):end(End)
{
len = 0;
while(str[len] != end)
{
s[len] = str[len++];
}
s[len] = end;
} ; // constructor function 2
~my_string() {} ; // destructor function
my_string& operator=(const my_string& str) // '=' -> overloading,assignment operations
{
len = str.len;
end = str.end;
for(int i = 0; i < len; i++)
{
s[i] = str.s[i];
}
return *this;
};
T operator[](int index) // overloading of '[]'
{
return s[index];
};
//define the friend functions
friend my_string& operator+(const my_string& str1,const my_string& str2); //the overloading of '+'
friend bool operator==(const my_string& str1,const my_string& str2); // judge if two strings are equal
int size()
{
return len;
};
bool empty()
{
return len == 0;
};
void print()
{
for(int i=0;i<len;i++)
cout<<s[i];
cout<<endl;
};
private:
int len; //record the length of the string
T end; //the ends of a string
T s[max_size]; // s stores the string
};
template <typename T>
bool operator==(const my_string<T>& str1,const my_string<T>& str2)
{
if(str1.len == str2.len)
{
for(int i = 0; i < len; i++)
{
if(str1.s[i] != str2.s[i])
return 0;
}
return 1;
}
return 0;
}
template <typename T>
my_string<T> operator+(const my_string<T>& str1,const my_string<T>& str2) //the overloading of '+'
{
my_string newmy_string;
newmy_string.len = str1.len + str2.len - 1;
for(int i = 0; i < str1.len; i++)
{
newmy_string.s[i] = str1.s[i];
}
for(int j = 0; j <= str2.len; j++)
{
newmy_string.s[i+j] = str2.s[j];
}
return newmy_string;
}
int main()
{
char a;
int a1;
double a2;
char type1[10];
int type;
char up[100];
int flag1=0,flag2=0,flag3=0;
int number1[100], num;
double number2[100];
cout << "我实现了 +,==,=,[] ,empty(),size()这六个功能,欢迎使用" << endl;
cout<<"********************************************************"<<endl;
cout<<"请输入您需要的类型(char int or double):"<<endl;
cin>>type1;
if(strcmp(type1,"char")==0)
flag1=1;
if(strcmp(type1,"int")==0)
flag2=1;
if(strcmp(type1,"double")==0)
flag3=1;
cout<<"请输入结束标志:(对应您输入的类型)"<<endl;
if(flag1==1)
cin>>a;
if(flag2==1)
cin>>a1;
if(flag3==1)
cin>>a2;
cout<<"*******************************************************"<<endl;
cout<<"请输入数据:"<<endl;
cout<<"如果您选择char 型,输入一行数据,以对应结束标志结束"<<endl;
cout<<"如果是其他类型,请输入数的个数"<<endl;
if(flag1==1)
{
cin>>up;
}
if(flag2==1)
{
cin>>num;
for (int i = 0; i < num; i++)
{
cout << "请输入第" << i+1 << "个数" << endl;
cin >> number1[i];
}
number1[num] = a1;
}
if(flag3==1)
{
cin>>num;
for(int i=0;i<num;i++)
{
cout << "请输入第" << i+1 << "个数" << endl;
cin >> number2[i];
}
number2[num]=a2;
}
my_string<char> temp1(up, a);
my_string<int> temp2(number1, a1);
my_string<double> temp3(number2, a2);
cout<<"****************************************************"<<endl;
cout<<"1.打印整行数据:"<<endl;
cout<<"2.求输入数据量的大小:"<<endl;
cout<<"0.结束操作:"<<endl;
cin>>type;
while(type!=0)
{
switch(type)
{case 1:
{
if(flag1==1)
temp1.print();
if(flag2==1)
temp2.print();
if(flag3==1)
temp3.print();
}
break;
case 2:
{
if(flag1==1)
cout<<temp1.size()<<endl;
if(flag2==1)
cout<<temp2.size()<<endl;
if(flag3==1)
cout<<temp3.size()<<endl;
}
break;
}
cin>>type;
}
return 0;
}