zoukankan      html  css  js  c++  java
  • 设计一个字符串类,并将字符串处理函数的内容进行封装

    #include<iostream>
    using namespace std;
    class String {
    
    public:
        //定义类的成员变量
        char s[10] = "abcd";
        char *ps = s;
    
        const char t[10] = "dcba";
        const char *pt = t;
    
        char S[10] = "ABCDEF";
        
        
    //构造函数部分
        String();
        String(const String & c);
    
    //字符串处理函数
        //连接
        void strcat();
        //复制
        void strcpy();
        //复制n个
        void strcpy(int n);
        //比较
        int strcmp();
        //取长度
        int strlen( char *s);
        //转大小写
        void strwr( char *s);
        void strup( char *s);
        
        ~String();
    };
    
    String::~String()
    {
    }
    
    String::String()
    {
    }
    //拷贝函数
    String::String(const String & c) {  //由于是拷贝,所以把参数用const限定
        const char* s = c.s;
        cout << "1";
    };
    
    
    
    
    
    
    void String::strcat()
    {
        while (*ps) {  //把指针移动到最后
            ++ps;
        }
    
        while (*pt) {  //当*pt有值  就进入
            *ps = *pt;  //复制
            ++pt;  //指针后移
            ++ps;
        }
        cout << s << endl;  //输出下连接复制是否正确
        return;
    }
    
    void String::strcpy()
    {
        while ((*ps = *pt) != '') { //一直到*pt指向的内容为就跳出
            ps++;  //往后移动指针
            pt++;
        }
        *ps = *pt;  //把也给*ps
        cout << s << endl;
        return;
    }
    
    void String::strcpy(int n) //复制前n个
    {
        int i = 0;
        while (*pt) {  //首先也要判断pt有值
            if (i<n) {
                *ps = *pt;
                ++i;   
                ps++;
                pt++;
            }
            else {   //如果已经复制了n个 那就结束
                break;
            }
        }
        //结束复制之后要 最后加空字符
            *ps = '';
        
        cout << s << endl;
        return;
    }
    
    int String::strcmp()
    {
        //用for循环  找到不相等的地方
        for (; *ps == *pt; ++ps, ++pt) {
            if (*ps == '') { //如果两个完全相等 会进去
                return 0;
            }
        }
        return (*ps - *pt);
        //返回当前指的位置 相减 就是字符对应的数值相减
    }
    
    int String::strlen( char * s)
    {
        int i = 0;
        while (*s) { //计算的有效长度(不加空字符)
            ++s;
            ++i;
        }
        return i;
    
    }
    
    void String::strwr( char * s)
    {
         char*m = s;
        while (*s)
        {   //要判断是否为大写字母
            if (*s >= 'A'&&*s <= 'Z') {
                *s += 32;
            }
            ++s;
        }
        //由于指针已经把 类里面的成员S  变为小写
        //故直接输出就可以验证
        cout << S << endl;
        return;
    }
    
    void String::strup( char * s)
    {
         char*m = s;
        while (*s)
        {
            if (*s >= 'a'&&*s <= 'z') {
                *s -= 32;
            }
            ++s;
        }
        cout << S << endl;
        return;
    }
    void main()
    {
        //调用函数
        cout << "strcat:" << endl;
        String e1;
        e1.strcat();
        cout << endl;
    
        cout << "strcpy:" << endl;
        String e2;
        e2.strcpy();
    
        cout << endl;
    
        cout << "strncpy:" << endl;
        String e3;
        e3.strcpy(3);
    
        cout << endl;
    
        String e4;
        cout << "strcmp:" << endl << e4.strcmp() << endl;
        cout << endl;
    
        String e5;
        cout << "strlen:" << endl << e5.strlen(e5.s) << endl;
        cout << endl;
    
        cout << "strwr:" << endl;
        String e6;
        e6.strwr(e6.S);
        cout << endl;
    
        cout << "strup:" << endl;
        String e7;
        e7.strup(e6.S);
        cout << endl;
    
        system("pause");
        return;
    }
  • 相关阅读:
    算法设计与分析——背包问题求解
    并行多核体系结构基础——第五章的多级高速缓存中的包含、排他、NINIE策略
    并行多核体系结构基础——第五章知识点和课后习题
    多耦合效应下的网络传播演化与分析
    算法设计与分析——矩阵链相乘求解
    算法设计与分析——动态规划之矩阵连乘
    并行多核体系结构基础——第五章的伪LRU(PLRU)算法简要介绍
    算法设计与分析——分治DC算法
    常见概率分布的特征函数推导
    算法设计与分析——最长公共子序列问题
  • 原文地址:https://www.cnblogs.com/Stephen-Jixing/p/9866714.html
Copyright © 2011-2022 走看看