字符串类程序(很全)
来源:互联网 发布:分析化学酸碱滴定数据 编辑:程序博客网 时间:2024/06/14 00:29
/*
/*brief: 字符串类程序
/*time:2011/12/14
/*author:gaojj
*/
#include<iostream>
using namespace std;
class TestString {
public:
TestString(); //生成一个空串
void initString(char *string); //生成一个带初值的非空串
virtual ~TestString(); //析构一个对象
void copy(char* destStr, char *sourStr); //复制一个已有串;
void append(char* s1, char* s2);//连接两个字符串
char* switchChar(TestString str); //转换成char类型的串,比如能被puts()函数所调用
bool empty(char* s); //测试串是否为空
int strLenth(char* s); //测试并返回串的长度
friend TestString creatStringObject(char* s); //从标准设备读入一个串值来创建一个串对象
TestString operator+(TestString &str);//+
TestString& operator=(TestString& str); //=
bool operator==(TestString& str);
bool operator!=(TestString& str); //!=
bool operator>=(TestString& str); //>=
bool operator<=(TestString& str);
TestString& operator+=(TestString& str);
TestString& operator++();//前置++
TestString& operator++(int n);//后置++
char operator[](int n); //取出串中的第i个元素([ ]的重载)
void operator()(char* string); //用函数调用运算符(),来实现“显示串内容”
void clearObject(char* str);//清除一个已有串为空串(不释放对象)
char* getString();
int settestNum(int n);
int gettestNum();
private:
char *str;
int testNum;
};
//生成一个空串
TestString::TestString()
{
str = NULL;
testNum =0;
}
//析构一个对象
TestString::~TestString()
{
cout << "创建的对象已析构!" << endl;
}
//生成一个带初值的非空串
void TestString::initString(char* string)
{
if (NULL != string)
{
char* stringTemp = new char[(strlen(string) + 1)];
int a = strlen(string);
strncpy_s(stringTemp, (strlen(string) + 1), string, strlen(string));
stringTemp[strlen(string)] = '\0';
str = stringTemp;
cout << "初始值的字符串是:" << str << endl;
}
else
cout << "输入的是空字符串" << endl;
}
//从标准设备读入一个串值来创建一个串对象
TestString clearObject(char* s)
{
TestString *pTestStr = new TestString;
pTestStr->initString(s);
return *pTestStr;
}
//测试串是否为空
bool TestString::empty(char* s)
{
if(s == NULL)
return true;
else
return false;
}
//复制一个已有串
void TestString::copy(char* destStr, char *sourStr)
{
int i = 0;
while(sourStr[i] != '\0')
{
destStr[i] = sourStr[i];
i++;
}
destStr[i] = '\0';
cout << "拷贝到的目的串中的字符是:" << destStr << endl;
}
//连接两个字符串
void TestString::append(char *s1, char *s2)
{
char* tempStr = new char[strlen(s1) + strlen(s2) + 1];
strncpy_s(tempStr,(strlen(s1) + strlen(s2) + 1), s1, strlen(s1));
strncat_s(tempStr,(strlen(s1) + strlen(s2) + 1), s2, strlen(s2));
tempStr[strlen(s1) + strlen(s2) + 1] = '\0';
cout << "输出连接好的字符串数组:" << tempStr << endl;
}
//转换成char类型的串,比如能被puts()函数所调用
char* TestString::switchChar(TestString str)
{
return str.str;
}
//测试并返回串的长度
int TestString::strLenth(char* s)
{
int iLen = 0;
while ((s[iLen]!= '\0') && (s != NULL))
iLen++;
return iLen;
}
//用函数调用运算符(),来实现“显示串内容”
void TestString::operator ()(char *string)
{
str = string;
cout << "用函数调用运算符(),来实现显示串内容是:"<< str << endl;
}
//获取到字符串
char* TestString::getString()
{
if (str==NULL)
{
cout << "****你生成了一个空的字符串!!****"<< endl;
return 0;
}
else
return str;
}
//取得测试的数字
int TestString::gettestNum()
{
return testNum;
}
//设置测试的数字
int TestString::settestNum(int n)
{
testNum = n;
return 0;
}
// '=' 功能的测试
TestString& TestString::operator=(TestString& str)
{
this->str = str.str;
return *this;
}
// '+' 功能的测试
TestString TestString::operator+(TestString &str)
{
char *temp;
temp=new char[strlen((*this).str)+strlen(str.str)+1];
strcpy(temp,(*this).str);
strcat(temp,str.str);
TestString result;
result.initString(temp);
return result;
}
// '==' 功能的测试
bool TestString::operator==(TestString& str)
{
if(strcmp((*this).str, str.str) == 0)
return true;
else
return false;
}
// '!=' 功能的测试
bool TestString::operator!=(TestString& str)
{
if(strcmp((*this).str,str.str) < 0)
return true;
else
return false;
}
// '>=' 功能的测试
bool TestString::operator>=(TestString& str)
{
if(strcmp((*this).str,str.str) >= 0)
return true;
else
return false;
}
// '<=' 功能的测试
bool TestString::operator<=(TestString& str)
{
if(strcmp((*this).str,str.str) <= 0)
return true;
else
return false;
}
// '<=' 功能的测试
TestString& TestString::operator+=(TestString& str)
{
char *temp;
temp=new char[20];
strcpy(temp, (*this).str);
strcat(temp,str.str);
this->str = temp;
return *this;
}
// '前置++' 功能的测试
TestString& TestString::operator++()
{
this->testNum++;
return *this ;
}
// '后置++' 功能的测试
TestString& TestString::operator++(int n)
{
TestString * newComp = new TestString(*this);
this->testNum++;
return *newComp;
}
//"[]"的功能的测试
char TestString::operator[](int n)
{
char* temp;
temp=new char[strlen((*this).str)+1];
strcpy(temp, this->str);
temp[strlen((*this).str)+1] = '\0';
return *(temp + n);
}
//清除一个已有串为空串(不释放对象)
void TestString::clearObject(char* str)
{
str = NULL;
cout << "已清除一个已有串为空串" << endl;
}
//主菜单
void menu()
{
cout << "***********************字符串类程序****************"<< endl;
cout << "1 生成一个空串\n"
<< "2 生成一个带初值的非空串\n"
<< "3 复制一个已有串\n"
<< "4 连接两个串\n"
<< "5 转换成char类型的串\n"
<< "6 测试串是否为空\n"
<< "7 测试并返回串的长度\n"
<< "8 从标准设备读入一个串值来创建一个串对象\n"
<< "9 '+' 功能的测试\n"
<< "10 '='功能的测试\n"
<< "11 '=='功能的测试\n"
<< "12 '!='功能的测试\n"
<< "13 '>='功能的测试\n"
<< "14 '<='功能的测试\n"
<< "15 '+='功能的测试\n"
<< "16 '前++'功能的测试\n"
<< "17 '后++'功能的测试\n"
<< "18 '[ ]'的重载\n"
<< "19 显示串内容\n"
<< "20 清除一个已有串为空串(不释放对象)\n"
<< "21 退出系统\n";
cout << "************************************************"<< endl;
cout<< "请选择你要测试的字符串类的功能编号:";
}
//推出函数
void quit()
{
char YorN;
cout << "是否退出系统?请输入(Y/N)!" << endl;
cin >> YorN;
if((YorN == 'y') || (YorN == 'y'))
exit(0);
else
cout << "请重新输入测试功能的编号!" << endl;
}
//生成一个空串
void case1()
{
cout << "你选择的是生成一个空串:" << endl;
TestString string;
string.getString();
}
void case2()
{
cout <<"生成一个带初值的非空串:" << endl;
TestString string;
char tempStr[20] = "wangsi";
string.initString(tempStr);
}
void case3()
{
cout << "复制一个已有串:" << endl;
TestString string;
char sourStr[20] = "wangsi";
char destStr[sizeof(sourStr)];
string.copy(destStr, sourStr);
}
void case4()
{
cout << "连接两个字符串:" << endl;
TestString string;
char* s1 = "wang";
char* s2 = "si";
cout<< "s1 = wang " << endl;
cout<< "s2 = si " << endl;
string.append(s1, s2);
}
void case5()
{
cout << "转换成char类型的串:" << endl;
TestString string;
cout << "string对象";
string.initString("wangsi");
string.switchChar(string);
cout << "输出转换后的字符串:" << string.switchChar(string) << endl;
}
void case6()
{
cout << "测试字符串是否为空:" << endl;
TestString string;
char tempStr[20] = " ";
cout << "输入要输出的字符串:";
cin >> tempStr;
if(string.empty(tempStr))
cout<< "输入的字符串是空的!" << endl;
else
cout<< "输入的字符串不是空的" << endl;
}
void case7()
{
cout << "测试并返回串的长度:" << endl;
TestString string;
char tempStr[20] = " ";
cout << "输入要计算长度的字符串:";
cin >> tempStr;
cout << "要计算长度的字符串长度是:" << string.strLenth(tempStr) << endl;
}
void case8()
{
cout << "从标准设备读入一个串值来创建一个串对象:" << endl;
char tempStr[20] = " ";
cout << "从标准设备读入一个串值:";
cin >> tempStr;
cout << "已经创建一个串对象,对象中包含字符串:" << clearObject(tempStr).getString() << endl;
}
void case9()
{
cout << "重载'+'的功能:" << endl;
TestString s1;
s1.initString("wang");
TestString s2;
s2.initString("si");
TestString s3;
cout << "****重载'+'后输出组合在一起的字符串:"<< endl;
s3 = s1+s2;
}
void case10()
{
cout << "重载'='的功能:" << endl;
TestString s1;
s1.initString("wang");
TestString s3;
cout << "****重载'='后输出字符串:"<< endl;
s3 = s1;
s3(s3.getString());
}
void case11()
{
cout << "重载'=='的功能:" << endl;
TestString s1;
TestString s2;
TestString s3;
cout << "s1";
s1.initString("wang");
cout << "s2";
s2.initString("wang");
cout << "s3";
s3.initString("si");
if(s1 == s2)
cout << "s1和s2相等!"<< endl;
else
cout << "s1和s2不相等!" << endl;
if(s1 == s3)
cout << "s1和s2相等!"<< endl;
else
cout << "s1和s2不相等!"<< endl;
}
void case12()
{
cout << "重载'!='的功能:" << endl;
TestString s1;
TestString s2;
TestString s3;
cout << "s1";
s1.initString("wang");
cout << "s2";
s2.initString("wang");
cout << "s3";
s3.initString("si");
if(s1 != s2)
cout << "s1和s2不相等!"<< endl;
else
cout << "s1和s2相等!" << endl;
if(s1 != s3)
cout << "s1和s2不相等!"<< endl;
else
cout << "s1和s2相等!"<< endl;
}
void case13()
{
cout << "重载'>='的功能:" << endl;
TestString s1;
TestString s2;
TestString s3;
cout << "s1";
s1.initString("hello");
cout << "s2";
s2.initString("wang");
cout << "s3";
s3.initString("si");
if(s1 >= s2)
cout << "s1>=s2!"<< endl;
else
cout << "s1<s2!" << endl;
if(s3 >= s2)
cout << "s3>=s2!"<< endl;
else
cout << "s1<s2!"<< endl;
}
void case14()
{
cout << "重载'<='的功能:" << endl;
TestString s1;
TestString s2;
TestString s3;
cout << "s1";
s1.initString("hello");
cout << "s2";
s2.initString("wang");
cout << "s3";
s3.initString("si");
if(s1 <= s2)
cout << "s1<=s2!"<< endl;
else
cout << "s1>s2!" << endl;
if(s3 <= s2)
cout << "s3<=s2!"<< endl;
else
cout << "s1>s2!"<< endl;
}
void case15()
{
cout << "重载'+='的功能:" << endl;
TestString s1;
TestString s2;
cout << "s1";
s1.initString("wang");
char tempStr[20] = " ";
cout << "输入要+=的字符串:";
cin >> tempStr;
cout << "s2";
s2.initString(tempStr);
s1+=s2;
cout << "输出 s1+=s2 后的字符串是:" << s1.getString() << endl;
}
void case16()
{
cout << "重载'前置++'的功能:" << endl;
TestString TestStr;
int n;
cout <<"输入要测试的数字:";
cin >> n;
TestStr.settestNum(n);
++TestStr;
cout << "'前++'后的数字是:" << TestStr.gettestNum() <<endl;
}
void case17()
{
cout << "重载'后置++'的功能:" << endl;
TestString s1;
int n;
cout <<"输入要测试的数字:";
cin >> n;
s1.settestNum(n);
s1++;
cout << "'后置++'后的数字是:" << s1.gettestNum() <<endl;
}
void case18()
{
int n = 0;
cout <<"'[]'的重载:" << endl;
TestString string;
string.initString("wangsi");
cout << "输入串中的第i个元素的下标:"<< endl;
cin >> n;
cout << "输入的下标为"<< n << "的元素为:" << string[n] << endl;
}
void case19()
{
cout << "用函数调用运算符(),来实现“显示串内容:" << endl;
TestString TestStr;
char tempStr[20] = " ";
cout << "输入要输出的字符串:";
cin >> tempStr;
TestStr(tempStr);
}
void case20()
{
cout << "清除一个已有串为空串(不释放对象):" << endl;
TestString TestStr;
TestStr.initString("wangsi");
TestStr.clearObject(TestStr.getString());
}
void case21()
{
quit();
}
//case的选择函数
void chooseCase(int num)
{
switch(num)
{
case 1: case1(); break;
case 2: case2(); break;
case 3: case3(); break;
case 4: case4(); break;
case 5: case5(); break;
case 6: case6(); break;
case 7: case7(); break;
case 8: case8(); break;
case 9: case9(); break;
case 10: case10(); break;
case 11: case11(); break;
case 12: case12(); break;
case 13: case13(); break;
case 14: case14(); break;
case 15: case15(); break;
case 16: case16(); break;
case 17: case17(); break;
case 18: case18(); break;
case 19: case19(); break;
case 20: case20(); break;
case 21: case21(); break;
default:
cout << "没有输入正确的编号,请再次输入编号:" ; break;
}
}
int main(){
int num;
menu();
while(1)
{
cin >> num;
if( (num <= 22 ) && (num >= 1))
chooseCase(num);
else
break;
}
return 0;
}
- 字符串类程序(很全)
- 字符串的全排列程序
- 求字符串全排列的递归算法(java程序)
- 【字符串】字符串的全排列
- H面试程序(8):不含重复字母的字符串的全排列
- 字符串的全排列
- 数据库连接字符串(全)
- 字符串的全排列
- 字符串全排列问题
- 字符串全排列
- 字符串全排列问题
- 字符串全攻略
- 字符串全排列
- 全表搜索字符串
- 求字符串全排列
- 字符串全排列组合
- 字符串全半角转换
- 求字符串全排列
- 姜迅谈阿里巴巴数据架构设计经验与挑战
- 什么是简单边界点(Simple border points)
- Linux批量添加和删除用户详解
- 一步一步创建VC2005解决方案
- Object-c基础
- 字符串类程序(很全)
- 第十一章 netfilter/iptables全攻略
- 黑马程序员—方法日志总结
- qt
- Object之hashCode
- 你真的了解SQL的索引吗?
- VBS控制Excel常见方法
- Linux系统下配置双网卡路由表的配置
- linux双网卡_NAT (转载)