刚学习C,编了一个小程序,作用是用一个结构规定关键字到规定字符之间字符。但是这个程序运行到最后无法停止,求高人指点。

解决方案 »

  1.   

    #include <iostream.h>
    #define _M_AXP64
    #include <windows.h>
    #include <string.h>
    #include <stdio.h>
    #include<cstdlib>void main()
    {//做一个程序,返回key后边和“;”所有字符,如字符串是zxckeycomeon1;zxckeycomeon2;mnl 匹配串“key”
    char a[100];
    char keyt[100];
    char key[10];
    char exchange[100];
    int n=0;
    char *ahead=a;
    char *keythead=keyt;//更改为字符串
    char *exchangeUse=exchange;
    //设置两个能移动的指针
    //设几个指针
    ZeroMemory(keyt,sizeof(keyt));
    ZeroMemory(a,sizeof(a));
    ZeroMemory(exchange,sizeof(exchange));
    cout<<"输入主串"<<endl;
    cin>>a;
    cout<<"输入关键词"<<endl;
    cin>>key;
        while(*ahead!='\0')
    {
    int whetherpipei(char *str1,char *str2);
        n=whetherpipei(a,key);
        cout<<"从第"<<n<<"个字符将后边的字符复制给另一个字符串"<<endl;//从输入的关键字开始,到这一行结束
                      for(int i=1;i<=n;i++)
      {ahead++;}//推进到第N个字符

                             while(*ahead!=';')//把N+1个字符到;复制给keyt
    {

                              *keythead=*ahead;
                             keythead++;
                                  ahead++;
    }//这时候ahead指针应该是指向剩下的字符串的头指针
                              cout<<keyt<<endl;
    while (*ahead!='\0')//这段程序将a这个字符串后边的字符付给了exchange这个字符串
    {
     *exchangeUse=*ahead;
         exchangeUse++;
         ahead++;
    }
    cout<<"打印一个exchange看看结果"<<exchange<<endl;
    strcpy(a,exchange);
        exchangeUse=exchange;
    ahead=a;//这两段程序把exchangeUse和ahead两个指针赋为新的exchange和a的头指针,(但是这里很可能有问题!)
    cout<<"打印一个a看看结果"<<a<<endl;//把exchange给了a,这样a就剩下了第一次匹配后剩下的信息
    cout<<"打印一个head指针的位置"<<ahead<<endl;
    }//while
    }//mainint whetherpipei(char *str1,char *str2)//设置一个函数,判断是否能匹配,如果可以匹配,将自动匹配并返回一个值
    {
    char *p=str1;//头指针
        char *q=str1+strlen(str1)-1;//尾指针
        char *m=str2;//头指针
        char *n=str2+strlen(str2)-1;//尾指针
        char *str1start=p;
        char *str2start=m;
    char *panduan=q-strlen(str2)+2;//再看看
    int index=1;
    int indexHistorySign=1;//这个是用于记录index
    if(strlen(str1)<strlen(str2))
    {
    cout<<"子串或关键词长度错误,请重新输出"<<endl;return -1;
    }
    else
    {
    while((p!=panduan))  
    {
    while(*p==*m)
    {    

    if(m==n)//这个地方原来认为错了
    {
    cout<<"可以匹配";
    return index;
    system("pause");
    }//if
    else
    {
    m++;
    p++;
    index++;
    }//else
    }//while
    p=str1start+1;
    str1start++;
    m=str2start;
    index=indexHistorySign+1;
    indexHistorySign++;
    }//while
    cout<<"无法匹配";
    return 0;
    }//else
    }//whether