C++字符串运算程序

来源:互联网 发布:java三大框架视频教程 编辑:程序博客网 时间:2024/06/07 10:34

#include <cstdlib>
#include <iostream>
//#include<iomainip>
#include<string>
using namespace std;
class Mystring{
     private:
            char p[30];
            int num;
             public:
                    Mystring operator+(Mystring str1){
                    Mystring temp;
                    strcpy(temp.p,p);
                    strcat(temp.p,str1.p);
                    return temp;
                    }
                    Mystring operator-(Mystring str1)
                    {
                    Mystring temp;
                    strcpy(temp.p,p);
                    int i=0;
                    while(temp.p[i]!='/0')
                    {
                    if(temp.p[i]==str1.p[0])
                    {
                    for(int k=i;temp.p[k]!='/0';k++)
                    {
                    temp.p[k]=temp.p[k+1];
                    }
                    num++;
                    i--;
                    }
                    i++;
                    }
                    temp.num=num;
                    return temp;
                    }
                    void show()
                    {
                    cout<<p<<endl;
                    }
                    void show_num()
                    {
                    cout<<"共删除字符数:"<<num<<endl;
                    }
                    Mystring(char s1[])
                    {
                    num=0;
                    strcpy(p,s1);
                    }
                    Mystring()
                    {
                    num=0;
                    }
                  
      }; 
int main(int argc, char *argv[])
{
   Mystring a("abcdef"),b("def");
   Mystring c;
   cout<<"字符串a=";
   a.show();
   cout<<"字符串b=:";
   b.show();
   c=a+b;
   c.show();
   c=c-"d";
   cout<<"字符串c-/"d/"=";
   c.show();
   c.show_num();
    system("PAUSE");
    return EXIT_SUCCESS;
}

原创粉丝点击