#includestdio.h#includeiostream.h#includestring.h#includestdlib.h#defineN50#defineM50voidReplaceString(char*Src,char*a,char*b){char*ptr;intlen=strlen(Src);ptr=(char*)malloc(sizeof(char)*len+10);strcpy(ptr,Src);intsubLen_a=strlen(a);intsubLen_b=strlen(b);intj;intk=subLen_b-subLen_a;for(inti=0;ilen;i++){if(0==strncmp(&ptr[i],a,subLen_a)){for(j=i+subLen_a;jlen;j++)ptr[j+k]=ptr[j];strncpy(&ptr[i],b,subLen_b);}}ptr[len+k]='\0';strcpy(Src,ptr);}voidxiaojie(chara[],charb[]){charA[N][N],B[N][N];intn=0,m=0,i,j;intlenth_a=strlen(a),lenth_b=strlen(b);for(i=0;ilenth_a;i++){if(a[i]!='V'){A[n][m]=a[i];m++;}else{A[n][m]='\0';m=0;n++;}}A[n][m]='\0';intk=n;//A的记录行数n=0,m=0;for(i=0;ilenth_b;i++){if(b[i]!='V'){B[n][m]=b[i];m++;}else{B[n][m]='\0';m=0;n++;}}B[n][m]='\0';intr=n;//B的记录行数//检查两子句是否含有互补对for(n=0;n=k;n++){for(m=0;m=r;m++){if((B[m][0]=='~')&&(B[m][1]==A[n][0]))//出现互补对{//置换charx[N][N],y[N][N];intg=0,h=0;lenth_a=strlen(A[n]);lenth_b=strlen(B[m]);for(i=2;ilenth_a-1;i++){if(A[n][i]!=','){x[g][h]=A[n][i];h++;}else{x[g][h]='\0';h=0;g++;}}x[g][h]='\0';g=0,h=0;for(i=3;ilenth_b-1;i++){if(B[n][i]!=','){y[g][h]=B[n][i];h++;}else{y[g][h]='\0';h=0;g++;}}y[g][h]='\0';for(inti=0;i=g;i++)//置换{if(strlen(x[i])=strlen(y[i])){coutx[i]/y[i]endl;for(j=0;j=r;j++)ReplaceString(B[j],y[i],x[i]);}if(strlen(x[i])strlen(y[i])){couty[i]/x[i]endl;for(j=0;j=k;j++)ReplaceString(A[j],x[i],y[i]);}}for(i=n;i=k;i++)//消去互补对strcpy(A[i],A[i+1]);k=k-1;for(i=m;i=r;i++)strcpy(B[i],B[i+1]);r=r-1;}elseif((A[n][0]=='~')&&(A[n][1]==B[m][0])){charx[N][N],y[N][N];intg=0,h=0;lenth_a=strlen(A[n]);lenth_b=strlen(B[m]);for(i=3;ilenth_a;i++){if(A[n][i]!=','){x[g][h]=A[n][i];h++;}else{x[g][h]='\0';h=0;g++;}}x[g][h]='\0';g=0,h=0;for(i=2;ilenth_b;i++){if(B[n][i]!=','){y[g][h]=B[n][i];h++;}else{y[g][h]='\0';h=0;g++;}}y[g][h]='\0';for(i=0;i=g;i++)//置换{if(strlen(x[i])=strlen(y[i])){coutx[i]/y[i]endl;for(j=0;j=r;j++)ReplaceString(B[j],y[i],x[i]);}if(strlen(x[i])strlen(y[i])){cout&&&&endl;couty[i]/x[i]endl;for(j=0;j=k;j++)ReplaceString(A[j],x[i],y[i]);}for(i=n;i=k;i++)//消去互补对strcpy(A[i],A[i+1]);k=k-1;for(i=m;i=r;i++)strcpy(B[i],B[i+1]);r=r-1;}}elsecontinue;}}//输出结果cout消解后的字句为:endl;for(n=0;n=k;n++)coutA[n]V;for(m=0;mr;m++)coutB[m]V;coutB[m]endl;}voidmain(){chara[N],b[N];cout请输入所要消解的第一个子句:endl;cina;cout请输入所要消解的第二个子句:endl;cinb;xiaojie(a,b);}运行结果: