//string.h文件,实现类的声明
#include<iostream.h>
const int N=200;
int n ,flag=0;
class string //string类声明
{
private:
char str[N]; //字符串一维数组
int len; //串的长度
public: //外部接口
string(char *p) //字符串的构造
{
copy(str,p);
}
string (){};
void copy(char *s1,char *s2);
void copy(char *s);
void strset() //字符串的输入
{
cin>>str;
}
void strout() //字符串的输出
{
cout<<str<<endl;
}
int length ();
void StrAssign(char*s); //串赋值
int StrCompare(char*s); //串比较
int StrLengh(); //求串长
void Concat(char*s); //串连接
void SubString(char*s,int a,int b); //求子串
int ClearString(char*s); //清空子串
};
//string.cpp文件,类实现
#include "string.h" //包含类的声明头文件
//string类的实现
int strlen(char*s)
{
int i=0;
while(s[i++]!='\0');
return i-1;
}
int string::length()
{
int i=0;
while(str[i++]!='\0');
return i-1;
}
void string::StrAssign(char*s) //串赋值
{
int i,k=length();
for(i=0;i<k;i++)
{
str[i]==s[i];
}
str[i]='\0';
}
int string::StrCompare(char*s) //串比较
{
int i=0;
int k1,k2,t;
k1=strlen(s);k2=length();
if(k1<k2)
{t=k1;k1=k2;k2=t;}
for(i=0;i<k1;i++)
{
if(s[i]<str[i])
return 1;
else if(s[i]>str[i])
return -1;
}
return 0;
}
int string::StrLengh() //求串长
{
int i=0;
while(str[i++]!='\0');
return i-1;
}
void string::Concat(char*s) //串连接
{
int i=0,k=length();
while(s[i]!='\0')
{
str[k]=s[i];
k++; i++;
}
str[k]='\0';
}
void string::SubString(char*s,int a,int b) //求子串
{
int i,k=0;
for(i=a-1;i<b;i++)
{
str[k]=s[i];
k++;
}
str[k]='\0';
}
int string::ClearString(char*s) //清空子串
{
int i,k=strlen(s);
for(i=0;i<k;i++)
{
s[i]=NULL;
}
return 1;
}
void show()
{
char yorn;
cout<<"\n是否退出系统[Y/N]:";
cin>>yorn;
cout<<endl;
if(yorn=='y'||yorn=='Y') flag=1;
else
if(yorn=='n'||yorn=='N'){flag=0;cout<<"请再次输入功能序号\n";
}
else
{
flag=0; cout<<"输入有误,请重新输入"<<endl;
}
}
void menu() //功能列表
{
cout<<"***************************************"<<endl;
cout<<" 字符串类的设计与实现"<<endl;
cout<<"***************************************"<<endl;
cout<<"设计人:XXX"<<endl;
cout<<"学号:110306XXXX"<<endl;
cout<<"功能选择菜单:"<<endl;
cout<<"1)串赋值\n"<<"2)串比较\n"<<"3)求串长\n"<<"4)串连接\n"<<"5)求子串\n"<<"6)清空子串\n";
cout<<"请您输入相应功能的序号!"<<endl;
}
void c1() //测试功能1:串赋值
{
string s;
char s1[N];
cout<<"您选择的功能是串赋值\n";
cout<<"请输入字符串,并以回车结束:";
s.strset();
s.StrAssign(s1);
cout<<"串赋值为:";
s.strout();
show();
}
void c2() //测试功能2:串比较
{
string s;
char s1[N];
cout<<"您选择的功能是串比较\n";
cout<<"请输入第一串字符并以回车结束: ";
s.strset();
cout<<"请输入第二串字符并以回车结束: ";
cin>>s1;
cout<<"结果:"<<s.StrCompare(s1)<<endl;
show();
}
void c3() //测试功能3:求串长
{
string s;
char s1[N];
cout<<"您选择的功能是求串长\n";
cout<<"请输入字符串并以回车结束: ";
s.strset();
cout<<"结果:"<<s.StrLengh()<<endl;
show();
}
void c4() //测试功能4:串连接
{
char s1[N];
string s;
cout<<"您选择的功能是串连接\n";
cout<<"请随意输入一串字符并以回车结束:";
s.strset();
cout<<"请输入一串想连接的字符并以回车结束:";
cin>>s1;
s.Concat(s1);
cout<<"连接后的新字符串为:";
s.strout();
show();
}
void c5() //测试功能5:求子串
{
string s;
int a,b;
char s1[N];
cout<<"您选择的功能是求子串\n";
cout<<"请输入母串,并以回车结束:";
cin>>s1;
cout<<"请输入子串的始位置:";
cin>>a;
cout<<"请输入子串的末位置:";
cin>>b;
cout<<endl;
s.SubString(s1,a,b);
cout<<"所取子串为:";
s.strout();
show();
}
void c6() //测试功能6:清空子串
{
string s;
char s1[N];
cout<<"您选择的功能是清空子串\n";
cout<<"请输入所要清空的子串,并以回车结束:";
cin>>s1;
cout<<endl;
cout<<"结果:"<<s.ClearString(s1)<<endl;
show();
}
void cs(char &n1) //开关键
{
n=n1-'0';
switch(n)
{
case 1: c1();break;
case 2: c2();break;
case 3: c3();break;
case 4: c4();break;
case 5: c5();break;
case 6: c6();break;
default: cout<<"您输入的功能序号有误,请重新输入!!"<<endl;
}
}
//main.cpp主函数
#include "string.h"
int main() //主函数
{
char a;
menu();
while(1)
{
if(flag)
{
cout<<"谢谢你的使用!\n"; break;
}
cin>>a;
cs(a);
}
return 0;
}
评论0