#include<stdio.h> void getch(char a[],int n,char c1[],char c2[],char c3[],char f1[],char f2[]) { int i,i1=0,i2=0; for(i=0;i<n;i++) { if(a[i]==‘*‘) { c2[i-1]=a[i-1]; } } for(i=0;i<n;i++) { if(a[i]==‘|‘) { c1[i]=a[i+1]; } } for(i=0;i<n;i++) { if(a[i]==‘|‘) { i=i+1; } else if(a[i+1]==‘*‘) { i=i+1; } else if(a[i]==‘*‘) { } else { c3[i]=a[i]; } } for(i=0;i<n;i++) { if(c3[i]!=‘0‘ && c2[i+1]==‘0‘) { f1[i1]=c3[i]; i1=i1+1; } else if(c3[i]!=‘0‘ && c1[i]==‘0‘ && c2[i+1]!=‘0‘) { f1[i1]=c3[i1]; i1=i1+1; f1[i1]=‘&‘; f1[i1+1]=c2[i+1]; f1[i1+2]=‘&‘; i1=i1+3; } else if(c3[i]==‘0‘ && c1[i]==‘0‘ && c2[i+1]!=‘0‘) { f1[i1]=‘&‘; f1[i1+1]=c2[i+1]; f1[i1+2]=‘&‘; i1=i1+3; } else if(c3[i]==‘0‘ && c1[i]==‘0‘ && c2[i1]!=‘0‘) { f1[i1]=‘&‘; f1[i1+1]=c2[i]; f1[i1+2]=‘&‘; i1=i1+3; } else if(c1[i]!=‘0‘ && c2[i+1]==‘0‘) { if(c2[i-2]!=‘0‘ && c1[i-3]==‘0‘) { i2=i1-3; f2[i2]=c1[i]; i2=i2+1; } else { i2=i1-1; f2[i2]=c1[i]; i2=i2+1; } } else if(c1[i]!=‘0‘ && c2[i+1]!=‘0‘) { if(c1[i-1]==‘0‘&&c1[i-3]==‘0‘) { i2=i1; i2=i2-3; f2[i2]=‘&‘; f2[i2+1]=c2[i+1]; f2[i2+2]=‘&‘; i2=i2+1; } else { i2=i1; i2=i2-1; f2[i2]=‘&‘; f2[i2+1]=c2[i+1]; f2[i2+2]=‘&‘; i2=i2+1; } } else { } } } void printfc(char f1[],char f2[],int n) { int i=0,i1=0,s=0,i3=0,k; int x[100]; for(i=0;i<2*n;i++) { x[i]=i; } i=0; while(f1[i]!=‘0‘) { if(f2[i]==‘0‘) { if(f1[i]==‘&‘ && f1[i+2]==‘&‘) { if(s!=0) { k=k+1; printf("f(%d,%c)=%d\n",x[i],f1[i],x[k]); printf("f(%d,%c)=%d\n",x[k],f1[i+1],x[k]); i=i+1; i1=i1+1; k=k+1; s=s-1; } else { printf("f(%d,%c)=%d\n",x[i1],f1[i],x[i1+1]); printf("f(%d,%c)=%d\n",x[i1+1],f1[i+1],x[i1+1]); i=i+1; i1=i1+1; } } else { if(s!=0) { printf("f(%d,%c)=%d\n",x[i],f1[i],x[k]); k=k+1; s=s-1; } else { printf("f(%d,%c)=%d\n",x[i1],f1[i],x[i1+1]); i1=i1+1; } } } else { i3=i1=i; while(f2[i3]!=‘0‘) { if(f2[i3]==‘&‘ && f2[i3+2]==‘&‘) { printf("f(%d,%c)=%d\n",x[i1],f2[i3],x[i1+1]); printf("f(%d,%c)=%d\n",x[i1+1],f2[i3+1],x[i1+1]); f2[i3]=‘0‘; f2[i3+1]=‘0‘; i3=i3+2; i1=i1+1; } else { printf("f(%d,%c)=%d\n",x[i1],f2[i3],x[i1+1]); f2[i3]=‘0‘; i1=i1+1; i3=i3+1; k=i1; s=s+1; } } i=i-1; // printf("f(%d,%c)=%d\n",x[i],f1[i],x[i+1]); //printf("f(%d,%c)=%d\n",x[i],f2[i],x[i+1]); } i=i+1; } } main() { char a[100]; char c1[100],c2[100],c3[100],f1[100],f2[100]; int m, n,i,j; printf("请你输入字符串在X[i]数组中"); gets(a); n=strlen(a); for(i=0;i<2*n;i++) { f1[i]=f2[i]=c1[i]=c2[i]=c3[i]=‘0‘; } getch(a,n,c1,c2,c3,f1,f2); printfc(f1,f2,n); //ZDZHJ(a,n); }
原文:http://www.cnblogs.com/wwc000/p/6096797.html