zoukankan      html  css  js  c++  java
  • 串的基础操作


    参考书籍:严蔚敏版数据结构 数据结构高分阅读等
     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 #define  maxSize 1000
     6 #define WWStr(str) #str
     7 #define WWLine "-------------"
     8 
     9 //参考书籍:严蔚敏版数据结构 数据结构高分阅读等
    10 /**
    11  定长顺序存储表示的串
    12  */
    13 typedef struct{
    14     char str[maxSize+1];
    15                 //maxSize为串的最大长度 后边的加1为了存放‘’结束标记的
    16     int length;
    17 }FixedOrderedString;
    18 
    19 /**
    20  动态分配存储表示的串
    21  */
    22 typedef struct {
    23     char *ch;   //指向动态分配存储区首地址的字符指针
    24     int length; //串的长度
    25     
    26 }UnfixedString;
    27 
    28 /**
    29  串的相关操作
    30  */
    31 void stringOperation();
    32 
    33 /**
    34  用ch给非固定的字符串str赋值
    35  
    36  @param str 源串
    37  @param ch 目的串
    38  @return 1 赋值成功 0 赋值失败
    39  */
    40 int assignUnfixedStrWithChar(UnfixedString &str,const char *ch);
    41 /**
    42  返回不定长的串的长度
    43  
    44  @param unfixedStr 不定长的串
    45  @return unfixedStr的长度
    46  */
    47 int lengthOfUnfixedStr(UnfixedString unfixedStr);
    48 
    49 /**
    50  比较字符串str1 和str2 返回两个字符串相差的长度 (如果是长度相同返回的第一个不同的字母的差值  长度不同的情况而且前边的字母都相同 返回的是长度的差值)
    51  
    52  @param str1 不定长字符串str1
    53  @param str2 不定长字符串str2
    54  @return 返回两个字符串相差的长度
    55  */
    56 int compareTwoUnfixedStr(UnfixedString str1,UnfixedString str2);
    57 
    58 
    59 int main(int argc, const char * argv[]) {
    60     stringOperation();
    61     return 0;
    62 }
    63 
    64 #pragma mark - 串的相关操作
    65 void stringOperation(){
    66     UnfixedString *unfixedStr;
    67     unfixedStr = new UnfixedString;
    68 //    unfixedStr->ch = NULL;
    69 //    unfixedStr ->length = 0;
    70     unfixedStr->ch = "abc";
    71     unfixedStr->length = 3;
    72     int currentLen = lengthOfUnfixedStr(*unfixedStr);
    73     cout<<WWStr(当前串的长度:)<<currentLen<<endl;
    74 //    assignUnfixedStrWithChar(*unfixedStr, NULL);
    75     assignUnfixedStrWithChar(*unfixedStr, "iOSiOS");
    76     if(unfixedStr->length == 0){
    77         cout<<WWStr(赋值后)<<WWStr(当前字符串为空)<<endl;
    78     }else{
    79         cout<<WWStr(赋值后)<<unfixedStr->ch<<endl;
    80     }
    81 //    currentLen = lengthOfUnfixedStr(*unfixedStr);
    82     cout<<WWStr(当前串的长度:)<<currentLen<<endl;
    83     UnfixedString *unfixedStr2 = new UnfixedString();
    84     unfixedStr2->ch = "abcd";
    85     cout<<lengthOfUnfixedStr(*unfixedStr2)<<endl;
    86     
    87     int lenCom = compareTwoUnfixedStr(*unfixedStr, *unfixedStr2);
    88     cout<<lenCom;
    89 
    90 }
    
    

    串赋值的相关操作:

    串赋值(源串给目的串赋值)考虑4种情况:

    1.赋值前:源串为空且目的串为空 赋值后:目的串为空

      赋值过程:把目的设置为空 长度设置为0

    2.赋值前:源串为空  目的串不为空

      赋值过程:释放目的串空间  把目的串设置为空 长度设置为0

      赋值后:目的串为空

    3.赋值前:源串不为空 赋值前目的串为空

      赋值过程:释放目的串空间 计算源串的长度 根据源串改变目的串的长度和值

     赋值后:目的串和源串长度相同 目的串

    4.赋值前:源串和目的串均不为空

      赋值过程:释放目的串 计算源串长度 用源串依次给目的串赋值并且改变长度

     

     1 /**
     2  用ch给非固定的字符串str赋值
     3 
     4  @param str 源串
     5  @param ch 目的串
     6  @return 1 赋值成功 0 赋值失败
     7  */
     8 int assignUnfixedStrWithChar(UnfixedString &str,const char *ch){
     9     
    10     cout<<WWLine<<WWStr(赋值串的操作)<<WWLine<<endl;
    11     if (str.ch) {
    12         delete &str;
    13 //        delete str.ch;
    14     }
    15     int len = 0;
    16     const char *c = ch;
    17     if (c == NULL) {
    18         cout<<WWStr(用空串给目的串);
    19         if (str.ch) {
    20             cout<<str.ch;
    21         }else{
    22             cout<<WWStr(空串);
    23         }
    24         cout<<WWStr(赋值)<<endl;
    25         str.ch = NULL;
    26         str.length = 0;
    27         return 1;
    28     }
    29     
    30     while (*c) {
    31         //计算源串的长度
    32         ++ len;
    33         ++ c;
    34     }
    35     
    36     if (len == 0) {
    37         //源串长度为0的情况下 设置目的串的值和长度
    38         str.ch = NULL;
    39         str.length = 0;
    40         return 1;
    41     }else{
    42         //有的时候c的值可能并不等于ch的值。。。。。
    43 //        cout<<WWLine<<WWStr(用源串)<<c;
    44         cout<<WWLine<<WWStr(用源串)<<ch;
    45         if (str.ch == NULL) {
    46             cout<<WWStr(给目的串空串)<<WWStr(赋值)<<endl;
    47         }else{
    48             cout<<WWStr(给目的串)<<str.ch<<WWStr(赋值)<<endl;
    49         }
    50         //取len + 1 是为了多分配一个空间 存放''字符
    51 //        str.ch = (char *)malloc(sizeof(char) * (len + 1));
    52         str.ch = new char[len+1];
    53         if(str.ch == NULL){
    54             //分配空间失败
    55             return 0;
    56         }else{
    57             c = ch;
    58             //循环条件使用"<="的原因是把ch最后的’‘复制到新串中作为结束标记
    59             for (int i = 0; i<= len; ++i,++c)
    60                 str.ch[i] = *c;
    61             str.length = len;
    62             return 1;
    63         }
    64     }
    65 }

     

    测试结果有:

     

    1.


    unfixedStr->ch = NULL; unfixedStr->length = 0; assignUnfixedStrWithChar(*unfixedStr, NULL);

    -------------赋值串的操作-------------

    用空串给目的串空串赋值

    赋值后当前字符串为空

    Program ended with exit code: 0

     

    2.

    unfixedStr->ch = "abc";
    unfixedStr->length = 3;
    assignUnfixedStrWithChar(*unfixedStr, NULL);

    -------------赋值串的操作-------------

    用空串给目的串abc赋值

    赋值后当前字符串为空

    Program ended with exit code: 0

     

    3.

     unfixedStr->ch = NULL;
     unfixedStr ->length = 0;
    assignUnfixedStrWithChar(*unfixedStr, "iOSiOS");

    -------------赋值串的操作-------------

    -------------用源串iOSiOS给目的串空串赋值

    赋值后iOSiOS

    Program ended with exit code: 0

     

    4.

     unfixedStr->ch = "abc";
     unfixedStr->length = 3;
    assignUnfixedStrWithChar(*unfixedStr, "iOSiOS");

    -------------赋值串的操作-------------

    -------------用源串iOSiOS给目的串abc赋值

    赋值后iOSiOS

     

     

    计算串的长度

    #pragma mark - 返回不定长的串的长度
    int lengthOfUnfixedStr(UnfixedString unfixedStr){
        return unfixedStr.length;
    }

    有时候我还想写上更多。。。

    像下边这样:

    未完待续。。。

     

     

     

     

     

     

     

     

     

     

     

     

    我会不定期分享 iOS 相关技术文章
  • 相关阅读:
    前端必看的数据可视化入门指南
    win10下查看进程,杀死进程
    前端如何使用proxyTable和nginx解决跨域问题
    vue-wechat-title动态修改title
    npm与cnpm混用导致的问题
    sass与less对比学习
    nginx,作为前端的你会多少?
    Ubuntu16.04安装python3.7及相应的pip
    漫游Kafka设计篇之性能优化(7)
    漫游Kafka设计篇之消息传输的事务定义(5)
  • 原文地址:https://www.cnblogs.com/ITCoderW/p/7496156.html
Copyright © 2011-2022 走看看