首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

刚运行就崩溃啊求解决二分查找法查找字符串,该怎么解决

2012-04-09 
刚运行就崩溃啊,求解决二分查找法查找字符串[size16px]刚运行就崩溃啊,求解决#includeiostreamusing na

刚运行就崩溃啊,求解决二分查找法查找字符串
[size=16px]
刚运行就崩溃啊,求解决
#include<iostream>
using namespace std;
#include<string.h>
#define n 6
char *binary(char *s)
{
static char *pstr[n]={"const","double","enum","float","signed","unsigned"};
int low,high,mid;
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(strcmp(s,pstr[mid])<0)
high=mid-1;
else if (strcmp(s,pstr[mid])>0)
low=mid+1;
else return (pstr[mid]);
}
return 0;
}
void main()
{
char *a,*find;
cout<<"请输入要查找的字符串:";
cin.getline(find,10);
a=binary(find);
if(a!=0)
cout<<"找到字符串"<<a<<endl;
else cout<<"没找到"<<endl;
}[/size]

[解决办法]
下断点调试,是最好的解决办法.
[解决办法]
都会写二分了,还不懂得指针要初始化?
[解决办法]

C/C++ code
#include<iostream>#include<string>using namespace std;#define n 6char *binary(char *s){    static char *pstr[n]={"const","double","enum","float","signed","unsigned"};    int low,high,mid;    low=0;    high=n-1;    while(low<=high)    {        mid=(low+high)/2;        if(strcmp(s,pstr[mid])<0)            high=mid-1;        else if (strcmp(s,pstr[mid])>0)            low=mid+1;        else return (pstr[mid]);    }    return 0;}int main(){    const int LEN = 10;    char *a,*find;    find = new char[LEN];    cout<<"请输入要查找的字符串:";    cin.getline(find,10);    a=binary(find);    if(a!=0)        cout<<"找到字符串"<<a<<endl;    else cout<<"没找到"<<endl;    system("pause");    return 0;}
[解决办法]
三楼内存泄漏了啊。。。。

热点排行