//cpp
#include<iostream>
using namespace std;
#include"acb.h"
int main()
{
sqstr s1,t1,str;
input(s1,"adssdaxczefrcbxcvxqwgcxx");
input(t1,"sadxccxassdyii");
cout<<"s1:"; output(s1);
cout<<"t1:"; output(t1);
str=maxstr(s1,t1);
cout<<"str:";
output(str);
cout<<str.length<<endl;
return 0;
}
//acb.h
#define MaxSize 100 //串中的最多字符个数
typedef struct
{
char data[MaxSize];
int length;
}sqstr;
void input(sqstr &s, char str[]) //串的赋值运算
{
int i=0;
while (str[i]!='\0')
{
s.data[i]=str[i];
i++;
}
s.length=i;
}
sqstr maxstr(sqstr s,sqstr t)
{
sqstr str;
int midx=0,mlen=0,tlen,i=0,j,k;
while (i<s.length) //用i扫描串s
{
j=0; //用j扫描串t
while (j<t.length)
{
if(s.data[i]==t.data[j])
{
tlen=1;
for (k=1;i+k<s.length && j+k<t.length && s.data[i+k]==t.data[j+k];k++)
{
tlen++;
}
if (tlen>mlen) //将较大长度付给midx和mlen
{
midx=i; //记录当前相同字符串的下标
mlen=tlen; //记录当前相同字符串的长度
}
j=j+1;
}
else j++;
}
i++; // 继续扫描s中第i字符之后的字符
}
for (i=0;i<mlen;i++) //将最大公共子串复制到str中
{
str.data[i]=s.data[midx+i];
}
str.length=mlen;
return str; //返回最大公共子串
void output(sqstr s) //串的输出运算
{
int i;
for(i=0;i<s.length;i++)
cout<<s.data[i];
cout<<endl;
}
//--------------------Configuration: 1125 - Win32 Debug--------------------
Compiling...
1125.cpp
C:\Users\51207\Desktop\vC\11\1125.cpp(5) : error C2601: 'main' : local function definitions are illegal
C:\Users\51207\Desktop\vC\11\1125.cpp(17) : fatal error C1004: unexpected end of file found
执行 cl.exe 时出错.
1125.obj - 1 error(s), 0 warning(s)