C程序转换成C++

paglezjq 2008-06-03 10:58:38
#include<stdio.h>
#include<string.h>

// 辅助数组,取决于字符集和,默认的采用 ASCII字符集,256个元素
#define LEN 256
int BM(char *s, char *p, int index, int position[])
/*
参数说明:
char *s: 匹配串
char *p: 模式串
int index: 模式串匹配的起始位置,是匹配串的索引
int position[] 辅助数组,
*/
{
int len = strlen(s);
int i,j, nextindex;
i = strlen(p)-1;//减1是因为要去掉最后的那个'\0'
j = index+strlen(p)-1;//第一次调用 BMMatcher 时 index = 0,因为下面的 for 循环是从模式串的末尾开始比较,所以匹配串的初始比较位置应该是从开头数模式串长度个位置开始。

for(; i>=0; i--, j--)
{
if(s[j] != p[i])
break;
}

if(i<0) //i<0 说明模式串已经遍历完毕
return 0; /*匹配成功*/
else if(position[s[j]]>0)//当出现不匹配时,查看匹配串当前位置的字符有没有出现在模式串中
nextindex = index + i - position[s[j]];
//index 是当前的匹配串起始偏移量,i 是模式串还剩的比较字串数目, position[s[j]]是所出现的第一个不匹配的字符在匹配串中的位置。这样下次比较就从匹配串中出现 s[j] 的位置开始比较
else nextindex = index + 1;

if(nextindex > LEN-strlen(p))
return -1; /*匹配失败,无法进行下一次匹配*/
else
return nextindex; /*匹配失败,需要下一次匹配*/
}

/*测试, 匹配串 和 模式串都使用小写字符*/
int main()
{
int position[LEN]={0}; /*辅助数组*/
char *src="it is just a test, what would you do?"; /*匹配串*/
char *patten="what would"; /*模式串*/
int i, nextindex, index=-2, pos=0;

for(i=0; i<strlen(patten); i++) /*构造辅助数组,关键的一步,但是很简单*/
position[patten[i]]=i;

index = BM(src, patten, 0, position);

while(!(index==-1 || index==0)) /*循环匹配,直到匹配成功,或者匹配失败结束*/
{
nextindex = index;
index = BM(src, patten, nextindex, position);
}

if(index == -1)
printf("Can not find it\n");

if(index == 0)
printf("Find it, the index is:%d",nextindex);
return 0;
}

如上,要修改哪些语句~~~十分感谢
...全文
93 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
simo110 2008-06-03
  • 打赏
  • 举报
回复
楼上的已经帮楼主改成了面向过程的C++
姑且也称为C++吧
交上去就好了
毕竟C++是多泛型的
K行天下 2008-06-03
  • 打赏
  • 举报
回复
帮你改了下:


#include <iostream>
#include <string>
using namespace std;

// 辅助数组,取决于字符集和,默认的采用 ASCII字符集,256个元素
#define LEN 256
int BM(char *s, char *p, int index, int position[])
/*
参数说明:
char *s: 匹配串
char *p: 模式串
int index: 模式串匹配的起始位置,是匹配串的索引
int position[] 辅助数组,
*/
{
int len = strlen(s);
int i,j, nextindex;
i = strlen(p)-1;//减1是因为要去掉最后的那个'\0'
j = index+strlen(p)-1;//第一次调用 BMMatcher 时 index = 0,因为下面的 for 循环是从模式串的末尾开始比较,所以匹配串的初始比较位置应该是从开头数模式串长度个位置开始。

for(; i>=0; i--, j--)
{
if(s[j] != p[i])
break;
}

if(i <0) //i <0 说明模式串已经遍历完毕
return 0; /*匹配成功*/
else if(position[s[j]]>0)//当出现不匹配时,查看匹配串当前位置的字符有没有出现在模式串中
nextindex = index + i - position[s[j]];
//index 是当前的匹配串起始偏移量,i 是模式串还剩的比较字串数目, position[s[j]]是所出现的第一个不匹配的字符在匹配串中的位置。这样下次比较就从匹配串中出现 s[j] 的位置开始比较
else nextindex = index + 1;

if(nextindex > LEN-strlen(p))
return -1; /*匹配失败,无法进行下一次匹配*/
else
return nextindex; /*匹配失败,需要下一次匹配*/
}

/*测试, 匹配串 和 模式串都使用小写字符*/
int main()
{
int position[LEN]={0}; /*辅助数组*/
char *src="it is just a test, what would you do?"; /*匹配串*/
char *patten="what would"; /*模式串*/
int i, nextindex, index=-2, pos=0;

for(i=0; i <strlen(patten); i++) /*构造辅助数组,关键的一步,但是很简单*/
position[patten[i]]=i;

index = BM(src, patten, 0, position);

while(!(index==-1 || index==0)) /*循环匹配,直到匹配成功,或者匹配失败结束*/
{
nextindex = index;
index = BM(src, patten, nextindex, position);
}

if(index == -1)
cout<<"Can not find it\n";

if(index == 0)
cout<<"Find it, the index is:"<<nextindex<<endl;

//system("pause");
return 0;
}
leelittlelong 2008-06-03
  • 打赏
  • 举报
回复
不用改,直接拿去编译执行不行么?
babyvox1999 2008-06-03
  • 打赏
  • 举报
回复
C++包容C的
bargio_susie 2008-06-03
  • 打赏
  • 举报
回复
printf 改成cout 就行了. hehe ..
独孤过儿 2008-06-03
  • 打赏
  • 举报
回复
老师留了一个C++的作业,然后你找了份C的代码,可是又不能把这个交上去,所以就找人来改了,对不?

64,439

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧