程序编译没错误,但是就是执行有问题。 
具体问题是: 
执行完串连接测试后(串连接测试执行没有问题),但是接着在字符串匹配测试中,却跳过了第一个字符串的输入。  
请问是什么原因??  



********************************************
[code=c]

#include<iostream>
#include<malloc.h>
#include<assert.h>
using namespace std;

//String类 完成生成字符串
class String
{
    private:
        int length,size;
        char *ch;

    public:
        String()
        {    
            ch = (char*)malloc(20*sizeof(char));
            assert(ch != NULL);
            length = 0;
            size = 20;
        }
        
        int getLength();
        void setLength(int);
        char* getString();
        void initString();
        void showString();

};

void String::setLength(int i)
{
    length = i;
}

int String::getLength()
{
    return length;
}

char* String::getString()
{
    return ch;
}

void String::initString()
{
    char temp;
    temp = cin.get();
    while(temp != '\n')
    {
        if((length+1)>size)
        {
            ch = (char*)realloc(ch,(size+10)*sizeof(char));
            assert(ch != NULL);
            size += 10;
        }

        ch[length] = temp;
        length++;
        temp = cin.get();
    }
}

void String::showString()
{
    cout.write(ch,length);
}

//Next类 完成一个int类型的可变长数组
class Next
{
    public:
        int length;
        int* next;

    public:
        Next()
        {
            length = 0;
            next = NULL;
        }
        void setNext(String);
        void showNext();
};

void Next::setNext(String str)
{
    next = new int[str.getLength()];
    assert(next != NULL);
    length = str.getLength();

    char* ch = str.getString();
    int i = 0,j = 0;
    next[0] = 0;

    while(i<length-1)
    {
        if(j == 0||ch[i] == ch[j-1])
        {
            ++i;++j;
            next[i] = j;
        }
        else j = next[j-1];
    }
}

void Next::showNext()
{
    for(int i=0;i<length;i++)
    {
        cout<<next[i]<<'\t';
    }
}

//StringHandle类  集成对String类的操作
class StringHandle
{
    private:
        Next ne;
    public:
        void strInsert(String &,String,int);
        void setNext(String,Next);
        int indexKMP(String,String,int);
};

void StringHandle::strInsert(String &str1, String str2,int pos)
{
    if(str2.getLength() == 0)
        return;
    if(pos < 1||pos > str1.getLength())
    {
        cout<<"定位有错"<<endl;
        return;
    }
    
    int len1 = str1.getLength(),len2 = str2.getLength();
    char* ch = (char*)realloc(str1.getString(),(len1+len2)*sizeof(char));
    str1.setLength(len1+len2);

    for(int i = len1-1;i >= pos-1;i--)
        ch[i + len2] = ch[i];
    
    char* ch2 = str2.getString();
    for(int i = 0;i < len2;i++)
        ch[pos+i-1] = ch2[i];
}

int StringHandle::indexKMP(String str1,String str2,int pos)
{
    ne.setNext(str2);
    ne.showNext();
    cout<<endl;

    char* ch1 = str1.getString();
    char* ch2 = str2.getString();
    int i = pos - 1;
    int j = 1;
    while(i<=str1.getLength() && j<=str2.getLength())
    {
        if(j==0||ch1[i]==ch2[j-1])
            {
                ++i;++j;
            }
        else
            j = ne.next[j-1];
    }
    if(j>str2.getLength()) return i-(str2.getLength()-1);
    else return 0;
}

void main()
{
    while(1){
    String str1,str2,str3,str4;
    StringHandle strhandle;
    int choice,i;
    
/*    cout<<"请选择要测试的项目:"<<endl;
    cout<<"1:字符串的连接测试"<<endl;
    cout<<"2:字符串的匹配测试"<<endl;
    cin>>choice;cout<<endl;

    switch(choice)
    {
    case 1:*/
        cout<<"***************字符串连接测试***************"<<endl;
        cout<<"请输入第一个字符串:";
        str1.initString();cout<<flush;
        cout<<"请输入第二个字符串:";
        str2.initString();cout<<flush;

        cout<<"请输入要插入的位置:";
        cin>>i;
        cout<<"插入结果:";

        strhandle.strInsert(str1,str2,2);
        str1.showString();
        cout<<endl;//break;

//    case 2:
        cout<<"***************字符串匹配测试***************"<<endl;
        cout<<"请输入第一个字符串:";
        str3.initString();cout<<flush;
        cout<<"请输入子字符串:";
        str4.initString();cout<<flush;

        cout<<"请输入开始搜索的位置:"<<endl;
        cin>>i;cout<<flush;
        cout<<"匹配结果(字串在母串的位置):";cout<<flush;
        cout<<strhandle.indexKMP(str3,str4,i)<<endl;//break;

//    default:
//        cout<<"命令错误"<<endl;
//    }
    }
}

[/code]