赞 | 0 |
VIP | 0 |
好人卡 | 0 |
积分 | 1 |
经验 | 3258 |
最后登录 | 2015-10-15 |
在线时间 | 23 小时 |
Lv1.梦旅人
- 梦石
- 0
- 星屑
- 50
- 在线时间
- 23 小时
- 注册时间
- 2014-9-26
- 帖子
- 24
|
加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
如题...本人数据结构的作业不会做了...
按照自己推算过一遍感觉没有错误,编译也没出错的代码- void ChirlStr(SEQSTRING T) //求子串//for后面内容没有执行
- {
- int len=T.len;
- for(int i=0;(len-i)==1;i++) //整数i为子串长度
- {
- for(int x=0,j=i;x>len-1;x++)
- { cout<<T.ch[x];
- if (j==0)
- {
- cout<<endl;
- j=i;
- x=x-i;
- }
- else
- j--;
- }
- }
- cout<<"除此以外还有一个“”空串"<<endl;
- }
- SEQSTRING ContectStr(SEQSTRING T1,SEQSTRING T2) //连接两个串
- { //T2和T1的混乱?
- int len1=T1.len;
- int x=0;
- T.len=0;
- for(int i = 0;i==len1;)
- {
- T.ch[i]=T1.ch[i];
- i++;
- x = i;
- cout<<len1<<"\t";
- cout<<"i="<<i<<"\t";
- cout<<"x="<<x<<"\t";
- }
- cout<<T.ch<<endl; //将T1赋值给T后输出T的值,实际上输出了T2?
- cout<<"T.len="<<x<<endl;
- for(int len2=i+1;len2==T2.len;len2++)
- {
- T.ch[i]=T2.ch[len2];
- i++;
- }
- T.len=T1.len+T2.len;
- return T;
- }
- int ppStr(SEQSTRING T1,SEQSTRING T2) //判断T2是否在T1中//假设T1为123,T2为1,输出匹配不能?
- {
- int len=T1.len,len2=T2.len;
- int x;
- if (len2>len) return (0);
- for(int i=0;i>(len-len2);i++) //j为T中的起始位,j>(len-i)即T1中已经找不到其他长度与T2相等的子串
- {
- for(x=0;x==len2;x++)
- { if (T2.ch[x]!=T1.ch[i+x]) x=len2-1;
- else if (x==(len2-1)) return (1);
- }
- }
- return (0);
- }
复制代码- #include<iostream.h>
-
- #define MAXSIZE 80
- typedef struct
- {char ch[MAXSIZE];
- int len;
- }SEQSTRING;
- SEQSTRING S,T1,T2,T;
- SEQSTRING InputStr() //输入字符串
- { int i;
- T.len=0;
- cout<<"请输入字符串:"<<endl;
- cin>>T.ch;
- for(i=0;T.ch[i]!='\0';i++)
- { T.len++;
- cout<<T.ch[i]<<" ";
-
- }
- cout<<endl;
- return T;
- }
- void OutputStr(SEQSTRING T) //输出字符串
- { cout<<T.ch<<endl;
- }
- int LenStr(SEQSTRING T) //求字符串长度
- { return (T.len);
- }
- int EqualStr(SEQSTRING T1,SEQSTRING T2) //判字符串是否相等
- {int i=0;
- if(T1.len!=T2.len)
- return(0);
- else
- for(i=0;i<T1.len;i++)
- if (T1.ch[i]!=T2.ch[i])
- return(0);
- else
- return(1);
- }
- void ChirlStr(SEQSTRING T) //求子串//for后面内容没有执行
- {
- int len=T.len;
- for(int i=0;(len-i)==1;i++) //整数i为子串长度
- {
- for(int x=0,j=i;x>len-1;x++)
- { cout<<T.ch[x];
- if (j==0)
- {
- cout<<endl;
- j=i;
- x=x-i;
- }
- else
- j--;
- }
- }
- cout<<"除此以外还有一个“”空串"<<endl;
- }
- SEQSTRING ContectStr(SEQSTRING T1,SEQSTRING T2) //连接两个串
- { //T2和T1的混乱?
- int len1=T1.len;
- int x=0;
- T.len=0;
- for(int i = 0;i==len1;)
- {
- T.ch[i]=T1.ch[i];
- i++;
- x = i;
- cout<<len1<<"\t";
- cout<<"i="<<i<<"\t";
- cout<<"x="<<x<<"\t";
- }
- cout<<T.ch<<endl; //将T1赋值给T后输出T的值,实际上输出了T2?
- cout<<"T.len="<<x<<endl;
- for(int len2=i+1;len2==T2.len;len2++)
- {
- T.ch[i]=T2.ch[len2];
- i++;
- }
- T.len=T1.len+T2.len;
- return T;
- }
- int ppStr(SEQSTRING T1,SEQSTRING T2) //判断T2是否在T1中//假设T1为123,T2为1,输出匹配不能?
- {
- int len=T1.len,len2=T2.len;
- int x;
- if (len2>len) return (0);
- for(int i=0;i>(len-len2);i++) //j为T中的起始位,j>(len-i)即T1中已经找不到其他长度与T2相等的子串
- {
- for(x=0;x==len2;x++)
- { if (T2.ch[x]!=T1.ch[i+x]) x=len2-1;
- else if (x==(len2-1)) return (1);
- }
- }
- return (0);
- }
- SEQSTRING CRSTR(SEQSTRING T1,SEQSTRING T2)
- {
- return T1;
- }
- void main() //主函数
- { int i=0,j=1,len=0,l;
- int ch1;
- S.len=0;
- T1.len=0;
- T2.len=0;
- while(1)
- {cout<<"\t\t\t 串子系统\n"<<endl;
- cout<<"\t\t**********************************"<<endl;
- cout<<"\t\t* 1------输入串 *"<<endl;
- cout<<"\t\t* 2------求串长 *"<<endl;
- cout<<"\t\t* 3------串比较 *"<<endl;
- cout<<"\t\t* 4------求子串 *"<<endl;
- cout<<"\t\t* 5------联接串 *"<<endl;
- cout<<"\t\t* 6------串匹配 *"<<endl;
- cout<<"\t\t* 7------插入串 *"<<endl;
- cout<<"\t\t* 8------删除串 *"<<endl;
- cout<<"\t\t* 9------显示串 *"<<endl;
- cout<<"\t\t* 0------返 回 *"<<endl;
- cout<<"\t\t**********************************\n"<<endl;
- cout<<"\t 请输入选项序号(0--9)!"<<endl;
- cin>>ch1;
-
- switch(ch1)
- {
- case 1: S=InputStr();break;
- case 2: len=LenStr(S);cout<<len<<endl;break;
- case 3:
- { T1=InputStr();
- T2=InputStr();
- l=EqualStr(T1,T2);
- if(l) cout<<"串1与串2相等!"<<endl;
- else cout<<"串1与串2不等!"<<endl;
- break;
- }
- case 4:
- {
- S=InputStr();
- ChirlStr(S);
- break;
- }
- case 5:
- {
- T1=InputStr();
- T2=InputStr();
- T=ContectStr(T1,T2);
- OutputStr(T);
- break;
- }
- case 6:
- {T1=InputStr();
- T2=InputStr();
- l=ppStr(T1,T2);
- if(l) cout<<"匹配成功"<<endl;
- else cout<<"匹配失败"<<endl;
- break;
- }
- }
- if(ch1==0) break;
- }
- }
复制代码 |
|