串的基本操作及统计字符频度-数据结构类C语言

目录

一、功能函数定义(func.cpp)

二、主函数调用(main.cpp)

三、头文件声明(before.h)

一、功能函数定义(func.cpp)

#include <iostream>
#include "before.h"

using namespace std;

status AssignString(SString &S,char *input)
{
    char *p;    //指示字符串
    int i;      //记录字符串长度

    for(i = 0,p = input;*p;i++,p++);          //算字符串的长度
    if(i > Maxlen) return ERROR;              //字符串长度超过数组最大长度
    if(!i)
    {
        S.length = 0;
        S.ch[1] = '#';
        return OVERFLOW;
    }//input为空
    else
    {
        for(int j = 1;j < i+1;j ++)
        {
            if(input[j-1] == ' ') S.ch[j] = '#';  //注意字符数组是从0开始存储字符
            else S.ch[j] = input[j-1];            //注意字符数组是从0开始存储字符
        }
        S.length = i;
        return OK;                  //建立串S成功
    }
}//输入串

void OutString(SString S)
{
    if(S.length == 0) cout << "该串为空,无法输出!" << endl;
    else
    {
        for(int i = 1;i < S.length+1;i ++)
        {
            cout << S.ch[i] << "  ";
            if(i%7 == 0) cout << endl;
        }
        cout << endl;
    }
}//输出串

status LengthString(SString S)
{
    return S.length;
}//返回串的长度

status CompareString(SString S,SString T)
{
    for(int i = 1;i < S.length && i < T.length;i ++) if(S.ch[i] != T.ch[i]) return S.ch[i]-T.ch[i];

    return S.length-T.length;        //若字符都一样,则长度长的串大
}//比较串

status StrCopy(SString &T,SString S)
{
    if(S.length == 0) return ERROR;
    for(int i = 1;i < S.length+1;i ++) T.ch[i] = S.ch[i];
    T.length = S.length;
    return OK;
}//串的复制

status StrEmpty(SString S)
{
    if(S.length == 0) return OK;
    else return ERROR;
}//串的判空

status ClearString(SString &S)
{
    S.length = 0;
    return OK;
}//清空串

status Concat(SString &T,SString S1,SString S2)
{
    int i;
    for(i = 1;i < S1.length+1;i ++)
        T.ch[i] = S1.ch[i];
    for(;i < S1.length+S2.length+1;i ++)
        T.ch[i] = S2.ch[i-S1.length];
    T.length = S1.length+S2.length;
    return OK;
}//串的连接

status SubString(SString &sub,SString S,int pos,int len)
{
    if(pos+len-1 > S.length) return OVERFLOW;
    if(S.length == 0) return ERROR;
    for(int i = pos;i < pos+len;i ++) sub.ch[i-pos+1] = S.ch[i];
    sub.length = len;
    return OK;
}//查找子串

status Replace(SString &S,SString T,SString V)
{
    if(V.length > T.length) return OVERFLOW;
    else
    {
        int index = Index_BF(S,T,0);
        if(index == ERROR) return ERROR;
        StrDelete(S,index,T);
        StrInsert(S,index,V);
    }
    return OK;
}//串的替换

status StrInsert(SString &S,int pos,SString T)
{
    if(pos<0 || pos>S.length+1 || pos+T.length > Maxlen) return OVERFLOW;
    for(int i = S.length;i >= pos;i --) S.ch[i+T.length] = S.ch[i];
    for(int i = 1;i < T.length+1;i ++) S.ch[pos+i-1] = T.ch[i];
    S.length += T.length;
    return OK;
}//串的插入

status StrDelete(SString &S,int pos,SString T)
{
    if(pos<0 || pos>S.length || T.length>S.length) return OVERFLOW;
    int index = Index_BF(S,T,pos);
    for(int i = index;i < index+T.length+1;i ++) S.ch[i] = S.ch[i+T.length];
    S.length -= T.length;
    return OK;
}//串的删除

void DestroyString(SString &S)
{
    exit(0);
}//串的销毁

status Index_BF(SString S,SString T,int pos)
{
    int i = pos;
    int j = 1;
    while(i<=S.length && j<=T.length)
    {
        if(T.ch[j] == S.ch[i])
        {
            j ++;
            i ++;
        }
        else
        {
            i = i-j+2;
            j = 1;
        }
    }
    if(j > T.length) return i-T.length;
    else return ERROR;
}//BF算法

void CountString(SString S)
{
    int digit[10],ch[26];
    for(int i = 0;i < 10;i ++) digit[i] = 0;
    for(int i = 0;i < 26;i ++) ch[i] = 0;

    int j;
    for(int i = 1;i < S.length+1;i ++)
    {
        switch(S.ch[i])
        {
            case 'A'...'Z':
                j = S.ch[i]-65;
                ch[j] ++;
                continue;
            case '0'...'9':
                digit[S.ch[i]-48] ++;
                continue;
        }
    }

    //输出
    cout << "----此处为字符频度统计结果----" << endl;
    for(int i = 0;i < 26;i ++) printf("   字母 %c 的个数为 %d !\n",i+65,ch[i]);
    for(int i = 0;i < 10;i ++) printf("   数字 %d 的个数为 %d !\n",i,digit[i]);
    cout << "----------------------------" << endl;
}//统计字符频度

//菜单调用
void Menu()
{
    printf("\t\t\t*****************此为串的功能菜单*******************\n");
    printf("\t\t\t    1、输入串                       2、输出串       \n");
    printf("\t\t\t    3、串的长度                     4、串的比较     \n");
    printf("\t\t\t    5、串的复制                     6、串判空       \n");
    printf("\t\t\t    7、清空串                       8、连接串       \n");
    printf("\t\t\t    9、指定长度子串                 10、串的替换     \n");
    printf("\t\t\t    11、串的插入                    12、串的删除     \n");
    printf("\t\t\t    13、串的销毁                    14、模式匹配BF算法\n");
    printf("\t\t\t    15、字符频度                    0、退出程序      \n");
    printf("\t\t\t**************************************************\n");
}
//
//Created by somewon on 2022/10/25.
//

二、主函数调用(main.cpp)

#include <iostream>
#include "before.h"

using namespace std;

int main()
{
    int choice;
    status fine;
    SString S;
    SString A,B,sub,T;
    int pos = 1,len = 0;
    char a[Maxlen],b[Maxlen];
    char input[Maxlen*2];
    do {
        Menu();
        cout << "--请输入你的选择:";
        cin >> choice;
        switch(choice)
        {
            case 1:
                cout << "--请输入进行操作的字符串:" << endl;
                getchar();
                gets(input);
                fine = AssignString(S,input);
                if(fine == ERROR) cout << "输入的字符串长度超过既定字符串!请再次进行操作!" << endl;
                else if(fine == OVERFLOW) cout << "该字符串为空!请确认是否正确!" << endl;
                else
                {
                    cout << "当前字符串创建成功!" << endl;
                    cout << "当前字符串为(#代表空格):" << endl;
                    OutString(S);
                }
                break;
            case 2:
                cout << "当前字符串为(#代表空格):" << endl;
                OutString(S);
                break;
            case 3:
                cout << "当前字符串的长度为:" << LengthString(S) << endl;
                break;
            case 4:
                cout << "--请输入进行操作的字符串A:" << endl;
                getchar();
                gets(a);
                cout << "--请输入进行操作的字符串B:" << endl;
                gets(b);
                AssignString(A,a);
                AssignString(B,b);
                cout << "当前字符串A为(#代表空格):" << endl;
                OutString(A);
                cout << "当前字符串B为(#代表空格):" << endl;
                OutString(B);
                fine = CompareString(A,B);
                if(fine == 0) cout << "字符串A和字符串B一样!且长度一致!" << endl;
                else if(fine > 0) cout << "字符串A大于字符串B!" << endl;
                else cout << "字符串A小于字符串B!" << endl;
                break;
            case 5:
                fine = StrCopy(T,S);
                if(fine == ERROR) cout << "被复制的字符串为空!" << endl;
                else
                {
                    cout << "复制成功!新建立字符串T为:" << endl;
                    OutString(T);
                }
                break;
            case 6:
                fine = StrEmpty(S);
                if(fine == OK) cout << "当前字符串为空!" << endl;
                else cout << "当前字符串不为空!" << endl;
                break;
            case 7:
                fine = ClearString(S);
                if(fine == OK) cout << "当前字符串清空成功!" << endl;
                break;
            case 8:
                cout << "--请输入进行操作的字符串S1:" << endl;
                getchar();
                gets(a);
                cout << "--请输入进行操作的字符串S2:" << endl;
                gets(b);
                AssignString(A,a);
                AssignString(B,b);
                fine = Concat(T,A,B);
                if(fine == OK)
                {
                    cout << "S1字符串与S2字符串连接成功!新建成的字符串T为:" << endl;
                    OutString(T);
                }
                break;
            case 9:
                cout << "--请输入需要查看子串的位序:";
                cin >> pos;
                cout << "--请输入需要查看子串的长度:";
                cin >> len;
                fine = SubString(sub,S,pos,len);
                if(fine == OVERFLOW) cout << "子串长度不合理!" << endl;
                else if(fine == OK)
                {
                    cout << "所求的子串为:" << endl;
                    OutString(sub);
                }
                else cout << "该字符串S为空,无法查找子串!" << endl;
                break;
            case 10:
                cout << "--请输入进行被替换的字符串T:" << endl;
                getchar();
                gets(a);
                cout << "--请输入进行替换的字符串V:" << endl;
                gets(b);
                AssignString(A,a);
                AssignString(B,b);
                fine = Replace(S,A,B);
                if(fine == ERROR) cout << "当前字符串S中无字符串T!" << endl;
                else if(fine == OVERFLOW) cout << "当前替换的字符串的长度不合理!" << endl;
                else
                {
                    cout << "替换成功!" << endl;
                    cout << "当前字符串S为:" << endl;
                    OutString(S);
                }
                break;
            case 11:
                cout << "--请输入插入字符串的起始位序:";
                cin >> pos;
                cout << "--请输入插入的字符串T:" << endl;
                getchar();
                gets(input);
                AssignString(T,input);
                fine = StrInsert(S,pos,T);
                if(fine == OVERFLOW) cout << "当前插入字符串的次序不合理!" << endl;
                else
                {
                    cout << "插入字符串成功!" << endl;
                    cout << "当前字符串S为:" << endl;
                    OutString(S);
                }
                break;
            case 12:
                cout << "--请输入删除字符串的起始位序:";
                cin >> pos;
                cout << "--请输入删除的字符串T:" << endl;
                getchar();
                gets(input);
                AssignString(T,input);
                fine = StrDelete(S,pos,T);
                if(fine == OVERFLOW) cout << "当前起始位序不合理!" << endl;
                else
                {
                    cout << "删除字符串T成功!" << endl;
                    cout << "当前字符串S为:" << endl;
                    OutString(S);
                }
                break;
            case 13:
                DestroyString(S);
                break;
            case 14:
                cout << "--请输入进行操作的主串S:" << endl;
                getchar();
                gets(a);
                cout << "--请输入进行操作的子串T:" << endl;
                gets(b);
                AssignString(A,a);
                AssignString(B,b);
//                cout << "当前主串S为(#代表空格):" << endl;
//                OutString(A);
//                cout << "当前子串T为(#代表空格):" << endl;
//                OutString(B);
                cout << "--请输入开始查看的pos位的值:";
                cin >> pos;
                fine = Index_BF(A,B,pos);
                if(fine == ERROR) cout << "该主串无对应的子串!" << endl;
                else cout << "第" << pos << "位之后第一个子串出现的位置为:" << fine << endl;
                break;
            case 15:
                CountString(S);
                break;
            case 0:
                goto END;
        }
    }while(choice);
    END:
    cout << "当前程序运行完毕,欢迎下次修改!" << endl;
    return 0;
}
//
//Created by somewon on 2022/10/25.
//

三、头文件声明(before.h)

#ifndef _BEFORE_H
#define _BEFORE_H

#define OK 1
#define ERROR 0
#define OVERFLOW (-1)

#define Maxlen 255     //串的最大长度

typedef int status;

typedef struct
{
    char ch[Maxlen+1];  //存储串的一维数组
    int length;         //串的当前长度
}SString;

//基本操作函数的声明
status AssignString(SString &S,char *input);        //输入串
void OutString(SString S);                          //输出串
status LengthString(SString S);                     //返回串的长度
status CompareString(SString S,SString T);          //串的比较
status StrCopy(SString &T,SString S);               //串的复制
status StrEmpty(SString S);                         //串的判空
status ClearString(SString &S);                     //清空串
status Concat(SString &T,SString S1,SString S2);    //连接串
status SubString(SString &sub,SString S,int pos,int len); //查找子串
status Replace(SString &S,SString T,SString V);     //串的替换
status StrInsert(SString &S,int pos,SString T);     //串的插入
status StrDelete(SString &S,int pos,SString T);     //串的删除
void DestroyString(SString &S);                     //串的销毁
status Index_BF(SString S,SString T,int pos);       //BF算法
void CountString(SString S);                        //统计字符频度

//调用菜单声明
void Menu();         //功能菜单

#endif
//
//Created by somewon on 2022/10/25.
//

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值