由于当前的连接符变为非显示,所以在有些时候需要考虑当前输入指针所指的位置是否缺少连接符,如果缺少,则将连接符入栈。
1 #include <stdio.h> 2 #include <malloc.h> 3 #include <string.h> 4 //这里默认是没有显示的连接运算符,运算符的优先级为括号、闭包*、连接.、并| 5 //在括号后及闭包后可能缺少显示的连接符,因此需要考虑添加连接符,而由于并操作符的优先级比连接符低 6 //所以就不需要在处理并操作符的时候去考虑是否缺少显示的连接符了 7 int token[100]; 8 int token_pointer; 9 char reg_operator[100]; 10 int reg_operator_pointer; 11 int name_number; 12 int input_pointer; 13 char reg_input[20]; 14 int is_operator(char for_in)//判断输入字符是否是操作符 15 { 16 switch(for_in) 17 { 18 case'(': 19 case')': 20 case'*': 21 case'|': 22 return 1; 23 default: return 0; 24 } 25 } 26 void tackle_or()//处理并操作符 27 { 28 if(reg_operator_pointer!=0)//如果操作符栈中已经有操作符了 29 { 30 if(reg_operator[reg_operator_pointer-1]!='(')//括号另外说 31 { 32 name_number++; 33 if(reg_operator[reg_operator_pointer-1]=='.') 34 //如果前面的优先级比当前的高,则处理前面的优先级 35 { 36 printf("name%d is concat of name%d and name%d ",name_number,token[token_pointer-2],token[token_pointer-1]); 37 } 38 else 39 //这里处理的是相同优先级的情况,其实这里可以与前面的合并的,只不过打印信息不同 40 { 41 printf("name%d is name%d or name%d ",name_number,token[token_pointer-2],token[token_pointer-1]); 42 } 43 token[token_pointer-2]=name_number; 44 token_pointer--; 45 reg_operator[reg_operator_pointer-1]='|'; 46 input_pointer++; 47 } 48 else//对于括号,则直接入栈 49 { 50 reg_operator[reg_operator_pointer++]='|'; 51 input_pointer++; 52 } 53 } 54 else//对于空操作符栈,也是直接入栈 55 { 56 reg_operator[reg_operator_pointer++]='|'; 57 input_pointer++; 58 } 59 } 60 void tackle_cat()//处理连接符,事实上跟前面的or操作符讨论的差不多,差异就在优先级那 61 { 62 if(reg_operator_pointer!=0)//如果操作符栈不为空 63 { 64 if(reg_operator[reg_operator_pointer-1]=='(')//如果前面有括号,则直接入栈 65 { 66 reg_operator[reg_operator_pointer++]='.'; 67 } 68 else//对于前面不是括号的情况下 69 { 70 if(reg_operator[reg_operator_pointer-1]=='.')//优先级相同则输出前面的那个 71 { 72 name_number++; 73 printf("name%d is the concat of name%d and name%d ",name_number,token[token_pointer-2],token[token_pointer-1]); 74 token[token_pointer-1]=0; 75 token[token_pointer-2]=name_number; 76 token_pointer--; 77 } 78 else//否则的话,前面的优先级比当前优先级低,操作符入栈 79 { 80 reg_operator[reg_operator_pointer++]='.'; 81 } 82 } 83 } 84 else//如果操作符栈为空,则入栈 85 { 86 reg_operator[reg_operator_pointer++]='.'; 87 } 88 } 89 void tackle_parenthesis(void)//处理闭括号模式,这里有点复杂 90 { 91 if(reg_operator[reg_operator_pointer-1]=='(')//如果前面那个操作符为开括号,则匹配输出 92 { 93 name_number++; 94 printf("name%d is (name%d) ",name_number,token[token_pointer-1]); 95 token[token_pointer-1]=name_number; 96 input_pointer++; 97 reg_operator[--reg_operator_pointer]='