数据结构自定义字符串的基本操作


#include <cstdio>
#include <iostream> 
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define MAXLEN 255
typedef int Status;

//串的存储结构(静态
typedef struct{
    char ch[MAXLEN+1];
    int length;
}SString; 

//字符串的输入,从1开始存储 
void StrInput(SString &S){
    int i = 0;
    int j = 1;
    char ch[MAXLEN];
    fflush(stdin);//清空缓冲区 
    gets(ch);
//    puts(ch);
    while(ch[i]!='\0'){
        S.ch[j]= ch[i];
        i++,j++;//j多走了一次 
    }
    S.length = j-1;
    fflush(stdin);
    
}


//字符串的输出
void StrOutput(SString S){
    int i =0;
    for(i=1;i<=S.length;i++){
        cout<<S.ch[i]; 
    }
     
}

//求串的长度 
int StrLen(SString S){
    return S.length;
}

//串的比较
int StrCompare(SString S,SString T){
    int i = 1,j = 1;
    
    while(i<=S.length&&j<=T.length){
        if(S.ch[i]==T.ch[j]){
            i++,j++;
        
        }
        else{
            return S.ch[i]-T.ch[j];
        }
    }
    if(j>T.length) return S.length- T.length;//字符串T走完了 

//串的连接 
Status StrConcat(SString &S,SString T){
    int j=StrLen(S)+1;
    int i=1;
    if(j==MAXLEN) return ERROR;
    for(j,i;j<MAXLEN,i<=StrLen(T);j++,i++){
        S.ch[j] = T.ch[i];
        
    }
    S.length+=i;
    
    return OK;

int BF(SString S,SString T){
    int i=1,j=1;
    while(i<=S.length&&j<=T.length){
        if(S.ch[i]==T.ch[j]) i++,j++;
        else{
            i=i-j+2;//i回到上次开始的下一个位置a 
            j=1;
        }
    }
    if(j>T.length) return i-T.length;
    else{
        return 0;//匹配失败0 
    }
}

//字符统计从A到Z的各个字符个数; 
void CharCout(SString S){
    int i = 1,flag = 1,ret;
    char character[100];//从1开始存储各个字符出现的次数 
    for(i=0;i<=25;i++){
        character[i]= 0;//把数组初始化,才能不影响后面的判断 
    }
    for(i=1;i<=S.length;i++){
        if(S.ch[i]>=65&&S.ch[i]<=90){
            ret = S.ch[i]-65;
            character[ret]+=1;
        }
    }
    
    
    for(i=0;i<=25;i++){
    //    printf("%d ",character[i]);
        if(character[i]>0) printf("%c %d\n",i+65,character[i]);
        flag = 0;
    }
    
    if(flag) cout<<"该串不符合统计标准,没有符合条件的字符!";
}

//判断栈操作是否合法
bool CheckStr(SString S){
    int i = 1;
    int ret,temp;
    if(S.length>MAXLEN) return false;//栈空间为满不能入栈
    if(S.ch[1]=='O') return false;//空栈时不能出栈 
    for(i=1;i<=S.length;i++){
        if(S.ch[i]=='I') ret++;
        else{
            temp++;
        }
        if(temp>ret){
            return false;//在某一时刻出栈数大于入栈数 
        }
    }
    if(temp!=ret) return false;//最后入栈数与出栈数不一致 
    else{
        return true;
    }

int main(){
    SString S;
    SString s1,s2;
    SString t1,t2;
    SString u1,u2;
    SString ch;
    SString Stack; 
    int choice; 
    do { 
        cout<<"\n\n===================================";
        cout<<"\n           自定义字符串的基本操作           ";
        cout<<"\n===================================";
        cout<<"\n           1:串的输入" ;
        cout<<"\n           2:串的输出" ;
        cout<<"\n           3:串的长度" ;
        cout<<"\n           4:串的比较" ;
        cout<<"\n           5:串的连接" ;    
        cout<<"\n           6:模式匹配BF算法" ;    
        cout<<"\n           7:字符统计" ; 
        cout<<"\n           8:判断栈的操作序列是否合法" ; 
        cout<<"\n           0:操作结束" ;
        cout<<"\n===================================";
        cout<<"\n请输入你的选择:"; 
        cin>>choice;
        switch (choice){
            case 1:    
                    cout<<"请输入串:";
                    StrInput(S);
                    cout<<"输入的串为:";
                    StrOutput(S);
                       break;
            case 2:    
                    cout<<"输出的串:";
                    StrOutput(S);
                       break;
            case 3:    
                    cout<<"字符串的长度为:"<<StrLen(S);
                     
                       break; 
                       
            
            case 4:
                    cout<<"请输入比较的第一个串:";
                    StrInput(s1);
                    cout<<"请输入比较的第二个串:";
            
                    StrInput(s2);
                    if(StrCompare(s1,s2)<0) cout<<"第一个串小于第二个串!";
                    else if(StrCompare(s1,s2)>0) cout<<"第一个串大于第二个串!";
                    else{
                        cout<<"两串相等!";
                    } 
                    break;  
            case 5:
                    cout<<"请输入第一个串:";
                    StrInput(u1);
                    cout<<"请输入第二个串:";
                    StrInput(u2);
                    if(StrConcat(u1,u2)==ERROR) cout<<"串1空间已满,无法继续拼接!";
                    cout<<"拼接好的新串为:";
                    StrOutput(u1); 
                
                
                    break;        
            case 6:    
                    cout<<"请输入主串:";
                    StrInput(t1);
                    cout<<"请输入模式串:";
                    StrInput(t2);
                    if(!BF(t1,t2)) cout<<"匹配失败!";
                    else{
                        cout<<"模式串在主串中的位置为:"<<BF(t1,t2);
                    }
                    break;
            case 7:
                    cout<<"请输入串:";
                    StrInput(ch);
                    CharCout(ch);
                    break;
            case 8:
                    cout<<"请输入存放栈操作序列的串:";
                    
                    StrInput(Stack);
                    if(!CheckStr(Stack)) cout<<"操作不合法!";
                    else{
                        cout<<"操作合法!"; 
                    } 
                    break;
            case 0:    break; 
            default:cout<<"\n输入错误,重新输入!";            
        }
    } while (choice) ;
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值