public static int Check(int[] puzzle, int puzzle_len, int puzzle_num, int num_type) //判断题目是否重复 { int i, j, L = 1, s; fraction FA, FB, Ft; Stack <int> stack_integer = new Stack <int>(); Stack <fraction> stack_fraction = new Stack <fraction>(); Stack <int> stack_operator = new Stack <int>(); int[] order = new int[400]; /********整数表达式*******/ if (num_type == 0) { for (i = 0; i < puzzle_len; i++) { if (puzzle[i] < 0) { continue; } if (puzzle[i] < 100 && puzzle[i] >= 0) //遇到数字,数字直接进入stack_integer栈 { fraction f = IntegerToFraaction(puzzle[i]); stack_fraction.Push(f); } else { if (puzzle[i] == 106) //遇到左括号,左括号直接入stack_operator栈 { stack_operator.Push(106); } else if (puzzle[i] == 107) //遇到右括号,弹出所有运算符,直至遇到左括号 { if (stack_operator.Count() != 0) { while (stack_operator.Count() != 0 && stack_operator.Peek() != 106) { s = stack_operator.Peek(); //运算符出栈 stack_operator.Pop(); FB = stack_fraction.Peek(); //操作数B出栈 stack_fraction.Pop(); FA = stack_fraction.Peek(); //操作数A出栈 stack_fraction.Pop(); if (s == 100 || s == 102) //+,*法,统一按照大数在前,小数在后方式运算 { if (FA < FB) { Ft = FA; FA = FB; FB = Ft; } } order[L++] = FA.GetNumerator(); order[L++] = 1030; order[L++] = FA.GetDenominator(); order[L++] = s; order[L++] = FB.GetNumerator(); order[L++] = 1030; order[L++] = FB.GetDenominator(); if ((s == 103 && FB.GetNumerator() == 0) || ((s == 104 || s == 105) && FA.GetNumerator() == 0 && FB.GetNumerator() == 0)) { return(-1); } stack_fraction.Push(FractionPartialResult(FA, FB, s)); //运算结果入栈 } if (stack_operator.Count() != 0) { stack_operator.Pop(); //弹出左括号 } } } else { if (puzzle[i] == 104 || puzzle[i] == 105) //遇到乘方,乘方优先级最高,乘方直接进运算符栈 { stack_operator.Push(puzzle[i]); } else if (puzzle[i] >= 100 && puzzle[i] <= 107) { //遇到运算符,且运算符栈为空或者栈顶运算符优先级小于当前运算符优先级,运算符直接进运算符栈 if (stack_operator.Count() == 0 || (stack_operator.Count() != 0 && priority[stack_operator.Peek() - 100] < priority[puzzle[i] - 100])) { stack_operator.Push(puzzle[i]); } else { while (stack_operator.Count() != 0 && priority[stack_operator.Peek() - 100] >= priority[puzzle[i] - 100]) { s = stack_operator.Peek(); //运算符出栈 stack_operator.Pop(); FB = stack_fraction.Peek(); //操作数B出栈 stack_fraction.Pop(); FA = stack_fraction.Peek(); //操作数A出栈 stack_fraction.Pop(); if ((s == 100 || s == 102) && FA < FB) //+,*法,统一按照大数在前,小数在后方式运算 { Ft = FA; FA = FB; FB = Ft; } order[L++] = FA.GetNumerator(); order[L++] = 1030; order[L++] = FA.GetDenominator(); order[L++] = s; order[L++] = FB.GetNumerator(); order[L++] = 1030; order[L++] = FB.GetDenominator(); if ((s == 103 && FB.GetNumerator() == 0) || ((s == 104 || s == 105) && FA.GetNumerator() == 0 && FB.GetNumerator() == 0)) { return(-1); } stack_fraction.Push(FractionPartialResult(FA, FB, s)); //运算结果入栈 } stack_operator.Push(puzzle[i]); //运算符进运算符栈 } } } } } if (stack_operator.Count() != 0) { while (stack_operator.Count() != 0) { s = stack_operator.Peek(); //运算符出栈 stack_operator.Pop(); FB = stack_fraction.Peek(); //操作数B出栈 stack_fraction.Pop(); FA = stack_fraction.Peek(); //操作数A出栈 stack_fraction.Pop(); if ((s == 100 || s == 102) && FA < FB) //+,*法,统一按照大数在前,小数在后方式运算 { Ft = FA; FA = FB; FB = Ft; } order[L++] = FA.GetNumerator(); order[L++] = 1030; order[L++] = FA.GetDenominator(); order[L++] = s; order[L++] = FB.GetNumerator(); order[L++] = 1030; order[L++] = FB.GetDenominator(); if ((s == 103 && FB.GetNumerator() == 0) || ((s == 104 || s == 105) && FA.GetNumerator() == 0 && FB.GetNumerator() == 0)) { return(-1); } stack_fraction.Push(FractionPartialResult(FA, FB, s)); //运算结果入栈 } if (stack_fraction.Count() != 0) { Ft = stack_fraction.Peek(); //运算结果出栈 stack_fraction.Pop(); order[L++] = Ft.GetNumerator(); order[L++] = 1030; order[L++] = Ft.GetDenominator(); } } else { Ft = stack_fraction.Peek(); //运算结果出栈 stack_fraction.Pop(); order[L++] = Ft.GetNumerator(); order[L++] = 1030; order[L++] = Ft.GetDenominator(); } order[0] = L; } /****分数表达式****/ else { for (i = 0; i < puzzle_len; i++) { if (puzzle[i] < 0) { continue; } if (puzzle[i] < 100 && puzzle[i] >= 0) //遇到分数,分数直接进入stack_fraction栈 { fraction f = new fraction(puzzle[i], puzzle[i + 2]); stack_fraction.Push(f); i += 2; } else { if (puzzle[i] == 106) //遇到左括号,左括号直接入stack_operator栈 { stack_operator.Push(106); } else if (puzzle[i] == 107) //遇到右括号,弹出所有运算符,直至遇到左括号 { s = stack_operator.Peek(); //运算符出栈 while (stack_operator.Count() != 0 && (s = stack_operator.Peek()) != 106) { stack_operator.Pop(); FB = stack_fraction.Peek(); //操作数FB出栈 stack_fraction.Pop(); FA = stack_fraction.Peek(); //操作数FA出栈 stack_fraction.Pop(); if (s == 100 || s == 102) //+,*法,统一按照大数在前,小数在后方式运算 { if (FA < FB) { Ft = FA; FA = FB; FB = Ft; } } order[L++] = FA.GetNumerator(); order[L++] = 1030; order[L++] = FA.GetDenominator(); order[L++] = s; order[L++] = FB.GetNumerator(); order[L++] = 1030; order[L++] = FB.GetDenominator(); if (FB.GetNumerator() == 0 && s == 103) { return(-1); } fraction ff = FractionPartialResult(FA, FB, s); stack_fraction.Push(ff); //运算结果入栈 } stack_operator.Pop(); //弹出左括号 } else if (puzzle[i] >= 100 && puzzle[i] <= 107) { //遇到运算符,且运算符栈为空或者栈顶运算符优先级小于当前运算符优先级,运算符直接进运算符栈 if (stack_operator.Count() == 0 || (stack_operator.Count() != 0 && priority[stack_operator.Peek() - 100] < priority[puzzle[i] - 100])) { stack_operator.Push(puzzle[i]); } else { while (stack_operator.Count() != 0 && priority[stack_operator.Peek() - 100] >= priority[puzzle[i] - 100]) { s = stack_operator.Peek(); //运算符出栈 stack_operator.Pop(); FB = stack_fraction.Peek(); //操作数B出栈 stack_fraction.Pop(); FA = stack_fraction.Peek(); //操作数A出栈 stack_fraction.Pop(); if ((s == 100 || s == 102) && FA < FB) //+,*法,统一按照大数在前,小数在后方式运算 { Ft = FA; FA = FB; FB = Ft; } order[L++] = FA.GetNumerator(); order[L++] = 1030; order[L++] = FA.GetDenominator(); order[L++] = s; order[L++] = FB.GetNumerator(); order[L++] = 1030; order[L++] = FB.GetDenominator(); if (FB.GetNumerator() == 0 && s == 103) { return(-1); } fraction ff = FractionPartialResult(FA, FB, s); stack_fraction.Push(ff); //运算结果入栈 } stack_operator.Push(puzzle[i]); //运算符进运算符栈 } } } } if (stack_operator.Count() != 0) { while (stack_operator.Count() != 0) { s = stack_operator.Peek(); //运算符出栈 stack_operator.Pop(); FB = stack_fraction.Peek(); //操作数B出栈 stack_fraction.Pop(); FA = stack_fraction.Peek(); //操作数A出栈 stack_fraction.Pop(); if ((s == 100 || s == 102) && FA < FB) //+,*法,统一按照大数在前,小数在后方式运算 { Ft = FA; FA = FB; FB = Ft; } order[L++] = FA.GetNumerator(); order[L++] = 1030; order[L++] = FA.GetDenominator(); order[L++] = s; order[L++] = FB.GetNumerator(); order[L++] = 1030; order[L++] = FB.GetDenominator(); if (FB.GetNumerator() == 0 && s == 103) { return(-1); } fraction ff = FractionPartialResult(FA, FB, s); stack_fraction.Push(ff); //运算结果入栈 } if (stack_fraction.Count() != 0) { Ft = stack_fraction.Peek(); //运算结果出栈 stack_fraction.Pop(); order[L++] = Ft.GetNumerator(); order[L++] = 1030; order[L++] = Ft.GetDenominator(); } } else { Ft = stack_fraction.Peek(); //运算结果出栈 stack_fraction.Pop(); order[L++] = Ft.GetNumerator(); order[L++] = 1030; order[L++] = Ft.GetDenominator(); } order[0] = L; } /**********判断与之前的题目是否重复************/ int flag = 0, label = 0; for (i = 0; i < puzzle_num; i++) { if (labels[i] != num_type || order[0] != stack_order[i, 0]) { continue; } else { flag = 0; for (j = 1; j < stack_order[i, 0]; j++) { if (stack_order[i, j] != order[j]) { flag = 1; break; } } if (flag == 0) { label = 1; break; } } } if (label == 1) { return(-1); //有重复 } else { for (j = 0; j < order[0]; j++) //登记到题目记录里 { stack_order[puzzle_num, j] = order[j]; } MainGUI.answer_int[puzzle_num, 0] = stack_order[puzzle_num, stack_order[puzzle_num, 0] - 3]; MainGUI.answer_int[puzzle_num, 1] = stack_order[puzzle_num, stack_order[puzzle_num, 0] - 2]; MainGUI.answer_int[puzzle_num, 2] = stack_order[puzzle_num, stack_order[puzzle_num, 0] - 1]; return(1); //无重复 } }