写的一个有关anangrams的程序,
用于检验两个单词是不是含有相同字母的(可以顺序不同)。
总体的思路是,如果两个单词的长度一样,那么就把其中的字幕按照子目标的顺序排列,
如果两个单词排列得到的结果相同,即返回“正确”,否则,则是“错误”
程序如下:include<iostream>
using namespace std;int compare(char list1[20],char list2[20],int count) //检验函数
{ int i=0,j=0,n=0;
for(i=count;i>=1;i--) //第一个单词冒泡排序
{
int currentMaxIndex1=0;
char currentMax1=list1[0];
for(j=1;j<=i;j++)
{
if(currentMax1<list1[j])
{
currentMax1=list1[j];
currentMaxIndex1=j;
}
} if(currentMaxIndex1!=i)
{
list1[currentMaxIndex1]=list1[i];
list1[i]=currentMax1;
}
}
for(i=count;i>=1;i--) //第二个单词,冒泡排序
{
int currentMaxIndex2=0;
char currentMax2=list2[0];
for(j=1;j<=i;j++)
{
if(currentMax2<list2[j])
{
currentMax2=list2[j];
currentMaxIndex2=j;
}
} if(currentMaxIndex2!=i)
{
list2[currentMaxIndex2]=list2[i];
list2[i]=currentMax2;
}
} for(i=0;i<count;i++)
{
if(list1[i]==list2[i])
n++;
else
{
return 0;
break;
}
} if(n==count)
return 1;
else
return 0;}int main()
{
char list1[20]={0},list2[20]={0};
int i,count1=0,count2=0,result; cout<<"please input the first word:"<<endl;
cin.getline(list1,20);
cout<<"the first word is:"<<endl;
for (i=0;i<sizeof(list1);i++)
{
cout<<list1[i];
count1++;
}
cout<<endl;
cout<<"please input the second word:"<<endl;
cin.getline(list2,20);
cout<<"the second word is:"<<endl;
for (i=0;i<sizeof(list2);i++)
{
cout<<list2[i];
count2++;
}
cout<<endl; if (count1==count2)
{
result=compare(list1,list2,count1);
if(result==1)
cout<<"the two words are anagrams ^^"<<endl;
else
cout<<"the two words are not anagrams!"<<endl;
}
else
cout<<"the two words are not anagrams!"; return 0;
}
这个程序编译的时候没有问题,运行的时候,也能达到目的,但是运行时会出这样的问题:Run-Time Check Failure #2 - Stack around the variable 'list2' was corrupted.请问这是怎么回事呀,
谢谢各位大侠了~。
用于检验两个单词是不是含有相同字母的(可以顺序不同)。
总体的思路是,如果两个单词的长度一样,那么就把其中的字幕按照子目标的顺序排列,
如果两个单词排列得到的结果相同,即返回“正确”,否则,则是“错误”
程序如下:include<iostream>
using namespace std;int compare(char list1[20],char list2[20],int count) //检验函数
{ int i=0,j=0,n=0;
for(i=count;i>=1;i--) //第一个单词冒泡排序
{
int currentMaxIndex1=0;
char currentMax1=list1[0];
for(j=1;j<=i;j++)
{
if(currentMax1<list1[j])
{
currentMax1=list1[j];
currentMaxIndex1=j;
}
} if(currentMaxIndex1!=i)
{
list1[currentMaxIndex1]=list1[i];
list1[i]=currentMax1;
}
}
for(i=count;i>=1;i--) //第二个单词,冒泡排序
{
int currentMaxIndex2=0;
char currentMax2=list2[0];
for(j=1;j<=i;j++)
{
if(currentMax2<list2[j])
{
currentMax2=list2[j];
currentMaxIndex2=j;
}
} if(currentMaxIndex2!=i)
{
list2[currentMaxIndex2]=list2[i];
list2[i]=currentMax2;
}
} for(i=0;i<count;i++)
{
if(list1[i]==list2[i])
n++;
else
{
return 0;
break;
}
} if(n==count)
return 1;
else
return 0;}int main()
{
char list1[20]={0},list2[20]={0};
int i,count1=0,count2=0,result; cout<<"please input the first word:"<<endl;
cin.getline(list1,20);
cout<<"the first word is:"<<endl;
for (i=0;i<sizeof(list1);i++)
{
cout<<list1[i];
count1++;
}
cout<<endl;
cout<<"please input the second word:"<<endl;
cin.getline(list2,20);
cout<<"the second word is:"<<endl;
for (i=0;i<sizeof(list2);i++)
{
cout<<list2[i];
count2++;
}
cout<<endl; if (count1==count2)
{
result=compare(list1,list2,count1);
if(result==1)
cout<<"the two words are anagrams ^^"<<endl;
else
cout<<"the two words are not anagrams!"<<endl;
}
else
cout<<"the two words are not anagrams!"; return 0;
}
这个程序编译的时候没有问题,运行的时候,也能达到目的,但是运行时会出这样的问题:Run-Time Check Failure #2 - Stack around the variable 'list2' was corrupted.请问这是怎么回事呀,
谢谢各位大侠了~。
解决方案 »
- ADO打开数据库失败的问题
- C/S同机测试正常,局域网内两台机器测试时失败???
- 如何从一个可执行文件中得知它用了哪些ocx和dll?
- 纪念我7天前的一次笔试错误,真TMD,散分
- 求助—关于BCGControlBar皮肤的问题??????
- 请问怎样实现树和其他控件之间的拖放功能?
- 不用MFC做一个按钮,怎么样搞?有思路的说一点思路,有源码的提供一下源码。
- 如何用WMEncoder开发包实现屏幕录象?
- 请问当把wave文件加入为程序本身的资源时,改如何播放?PlaySound好像有问题。
- 用midl的问题。。。
- 用Debug编译的程序能打开,为什么换成Release就不行了呢
- CopyFile 怎么用啊?
即其长度=字符串长度+1(字符串结束符\0)改成试试:
char list1[21]={0},list2[21]={0};