1.结果展示

[C++]串的基本数据结构与特定文本查找

[C++]串的基本数据结构与特定文本查找

2.结果展示

[C++]串的基本数据结构与特定文本查找

程序1代码:

#include

#include

using namespace std;

#define MAXSIZE 100

#define ElemType char //元素数据类型

typedef struct{

ElemType *base;

ElemType *top;

int stacksize;

}SqStack;

int main(){

SqStack s;

int i,textlength;//i计次变量;

ElemType input[MAXSIZE],e;

//--------------函数调用声明-----------

int InitStack(SqStack &S); //初始化函数

int Push(SqStack &S,ElemType &e); //入栈

int Pop(SqStack &S,ElemType &e); //出栈

int GetTop(SqStack &S,ElemType &e); //取栈顶元素

//--------------------------------------

if(InitStack(s))

cout<<"\n【栈】初始化:成功\n请输入要判断的字符串(长度<=100):\n";

else{

cout<<"【栈】初始化:失败\n";

exit(0);

}

cin>>input;

i=0;

while (input[i]!='\0'){//统计输入字符串长度

i++;

}

textlength=i;

for(i=0;i

Push(s,input[i]);

}

for(i=(textlength/2)+textlength%2;i

GetTop(s,e);

if(e==input[i]){//判断后半部分是否与前半部分对称

Pop(s,e);

}else{

cout<<"该字符串不是回文!\n";

break;

}

}

if(i==textlength)

cout<<"该字符串是回文!\n";

return 0;

}

int InitStack(SqStack &S){

S.base=new ElemType[MAXSIZE];

if(!S.base) exit(0);

S.top=S.base;

S.stacksize=MAXSIZE;

void CP();

CP();

return 1;

}

int Push(SqStack &S,ElemType &e){

if(S.top-S.base==S.stacksize) return 0;

*(S.top)=e;

S.top++;

return 1;

}

int Pop(SqStack &S,ElemType &e){

if(S.top ==S.base ) return 0;

--S.top;

e=*(S.top);

return 1;

}

int GetTop(SqStack &S,ElemType &e){

if(S.top!=S.base){

e=*(S.top-1);

return 1;

}else{

return 0;

}

}

void CP(){

#define Pf cout

const char a[]={0x54,0x68,0x65,0x20,0x70,0x72,0x6f,0x67,0x72,0x61,0x6d,0x20,0x62,0x79,0x20,0x43,0x4a,0x50,0x00};

if(a[15]*a[16]*a[17]!=396640) exit(0);

Pf<

}

程序2代码:

#include

#include

#include

using namespace std;

typedef struct{

char *ch;

int length;

}String;

int main(){

String s1,s2;

int where;

void CP();

void InputString(String &S);

int Index_BF(String &F,String &S);

CP();

cout<<"\n第一个字符串为主串,第二个为子串!\n";

InputString(s1);

InputString(s2);

where=Index_BF(s1,s2);//记录字符串出现的位置

if(where>=0)

cout<<"子串出现位置为:"<<

else

cout<<"未找到子串!\n";

return 0;

}

void InputString(String &S){

cout<<"\n请输入字符串的长度:\n";

cin>>S.length;

cout<<"请输入字符:\n";

S.ch =new char[S.length];

cin>>S.ch;

S.length=strlen(S.ch);//重定义字符串实际长度

}

int Index_BF(String &F,String &S){//F,主串;S,子串;

int i=0,j=0;

while(i

if(F.ch[i]==S.ch[j]){

i++;

j++;

}else{

j=0;

i=i-j+2;

}

}

if(j>S.length-1) return i-S.length+1;

return -1;

}

void CP(){

#define Pf cout

const char a[]={0x54,0x68,0x65,0x20,0x70,0x72,0x6f,0x67,0x72,0x61,0x6d,0x20,0x62,0x79,0x20,0x43,0x4a,0x50,0x00};

if(a[15]*a[16]*a[17]!=396640) exit(0);

Pf<

}