zoukankan      html  css  js  c++  java
  • Road to Coder _String

    周次

    学习时间

    新编写代码行数

    博客量(篇)

    学到知识点

             

    第8周

    约200

    1

    串的基本操作

      1 #include"stdio.h"
      2 #include"stdafx.h"
      3 #include"stdlib.h"
      4 #include"windows.h"
      5 #include"conio.h"
      6 #include"string.h"
      7 
      8 #pragma warning(disable 4996)
      9 
     10 #define MAXLEN 50
     11 
     12 typedef struct
     13 {
     14     char str[MAXLEN];
     15     int len;
     16 }String;
     17 
     18 void PrintString(String s);
     19 void StrDelete(String *s, int pos, int len);
     20 void StrInsert(String *s, int pos, String t);
     21 void StrCompare(String s, String t);
     22 void Index_BF(String s, String t);
     23 void Get_next(String t, int next[50]);
     24 void Index_KMP(String s, String t);
     25 
     26 
     27 void main()
     28 {
     29     int pos =0;
     30     int len = 0;
     31     String temp;
     32     String MyString;
     33     printf("请输入一个字符串:");
     34     gets_s(MyString.str);
     35     MyString.len = strlen(MyString.str);
     36     PrintString(MyString);
     37 
     38     printf("请输入第二个字符串:");
     39     gets_s(temp.str);                        //临时变量二次利用
     40     temp.len = strlen(temp.str);
     41     StrCompare(MyString, temp);
     42 
     43     printf("
    对以上两个字符串的BF匹配后:");
     44     Index_BF(MyString,temp);
     45     printf("
    对以上两个字符串的KMP匹配后:");
     46     Index_KMP(MyString,temp);
     47     system("pause");
     48 
     49     printf("请输入要插入的字符:");
     50     gets_s(temp.str);
     51     temp.len = strlen(temp.str);
     52     printf("请输入要插入的位置:");
     53     scanf_s("%d", &pos);
     54     StrInsert(&MyString,pos,temp);
     55     PrintString(MyString);
     56 
     57     printf("
    请输入删除的位置以及长度:");
     58     scanf_s("%d%d",&pos,&len);
     59     StrDelete(&MyString,pos,len);
     60     PrintString(MyString);
     61 
     62     system("pause");
     63 }
     64 
     65 void PrintString(String s)
     66 {
     67     int i;
     68     for (i = 0; i < s.len; i++)
     69     {
     70         printf("%c",s.str[i]);
     71     }
     72     printf("
    ");
     73 }
     74 
     75 void StrInsert(String *s, int pos, String t)            //在串s下标为pos的地方插入串t
     76 {
     77     
     78     int i;
     79         if (pos<0 || pos> s->len)  printf("插入位置不合法!");
     80         else
     81             if (s->len + t.len <= MAXLEN)
     82             {
     83                 for (i = s->len + t.len - 1; i >= t.len + pos-1; i--)
     84                     s->str[i] = s->str[i - t.len];
     85                 for (i = 0; i < t.len; i++)
     86                     s->str[i + pos] = t.str[i];
     87                     s->len = s->len + t.len;
     88             }
     89             else if (pos + t.len <= MAXLEN)
     90             {
     91                 for (i = MAXLEN - 1; i > t.len + pos - 1; i--)  s->str[i] = s->str[i - t.len];
     92                 for (i = 0; i < t.len; i++)    s->str[i=pos] = t.str[i];
     93                 s->len = MAXLEN;
     94             }
     95             else if (s->len + t.len > MAXLEN)
     96             {
     97                 for (i = 0; i < MAXLEN - pos; i++)    s->str[i + pos] = t.str[i];
     98             }
     99 }
    100 
    101 void StrDelete(String *s, int pos, int len)            //删除pos后len个元素
    102 {
    103     int i;
    104     if (pos < 0 || pos>(s->len - len))
    105     {
    106         printf("删除参数不合法!");
    107     }
    108     else
    109         for (i = pos + len; i < s->len; i++)
    110         {
    111             s->str[i - len] = s->str[i];
    112             
    113         }
    114     s->len = s->len - len;
    115 }
    116 
    117 void StrCompare(String s, String t)
    118 {
    119     int i,result=0;
    120     for (i = 0; i < s.len && i < t.len; i++)
    121         if (s.str[i] != t.str[i])
    122             result = s.str[i] - t.str[i];
    123         else
    124             result = s.len - t.len;
    125 
    126     if (result == 0)
    127             printf("(%s)和(%s)两串相等!", s.str, t.str);
    128     else if (result > 0)
    129         printf("(%s)较大
    ", s.str);
    130     else 
    131         printf("(%s)较大
    ", t.str);
    132 
    133 }
    134 
    135 
    136 void Index_BF(String s, String t)                        //Brute Force 暴力匹配算法
    137 {
    138     int slen = strlen(s.str);
    139     int tlen =strlen(t.str);
    140     int i = 0;
    141     int j = 0;
    142     while (i <slen && j< tlen)
    143     {
    144         if(s.str[i] == t.str[j])
    145         {
    146             i++;
    147             j++;
    148         }
    149         else
    150         {
    151             i = i - j + 1;
    152             j = 0;
    153         }
    154     }
    155     if (i >= tlen)
    156         printf("匹配成功,串<%s>在主串<%s>的第%d位!
    ", t.str, s.str, i - j+1);
    157     else
    158         printf("匹配失败!
    ");
    159 }
    160 
    161 void Get_next(String t,int next[])
    162 {
    163     int i=0, j=-1;
    164     next[0] = -1;
    165     while (i < t.len-1)
    166     {
    167         if ( j==-1 || t.str[i] == t.str[j])
    168         {
    169             i++;
    170             j++;
    171             if (t.str[i] != t.str[j])
    172                 next[i] = j;
    173             else
    174                 next[i] = next[j];
    175         }
    176         else
    177         {
    178             j = next[j];
    179         }
    180     }
    181 }
    182 
    183 void Index_KMP(String s, String t)            // Knuth-Morris-Pratt 字符串查找算法,简称为 “KMP算法”,常用于在一个文本串S内查找一个模式串P 的出现位置,这个算法由Donald Knuth、Vaughan Pratt、James H. Morris三人于1977年联合发表,故取这3人的姓氏命名此算法。
    184 {
    185     int i = 0;
    186     int j = 0;
    187     int next[50];
    188 
    189 
    190     Get_next(t,next);
    191192 
    193     while (i < s.len && j < t.len)
    194     {
    195         if (j==-1 || s.str[i] == t.str[j])
    196         {
    197             i++;
    198             j++;
    199         }
    200         else
    201         {
    202             j = next[j];
    203         }
    204     }
    205     if (j == t.len)
    206         printf("匹配成功,串<%s>在主串<%s>的第%d位!
    ", t.str, s.str, i - j+1);    //因为得到的下标从0开始,所以得到的结果+1便于验证
    207     else
    208         printf("匹配失败!
    ");
    209 }

  • 相关阅读:
    GIT(分布式版本控制系统)
    mysql入门
    百度云上传下载会出现乱码,和丢失数据,.h,.so都会出现,暂未发现丢包现象
    qte搭建
    移植tslib
    网页移植前的问题(包括触摸屏)
    网页移植开发板
    开发板boa移植
    dump工具下,资料的备份与还原
    python-时间模块
  • 原文地址:https://www.cnblogs.com/520-42/p/8973699.html
Copyright © 2011-2022 走看看