本次完成了含有一个未知数的计算,还修复了好多bug(比如计算负数时报错)
#include<iostream> #include<string> using namespace std; // 链栈的存储结构 typedef struct StackNode { string date; struct StackNode* next; } StackNode, * LinkStack; // 链栈的初始化 int InitStack(LinkStack& S) { S = NULL; //将栈顶指针置空 return 1; } int Push(LinkStack& S, string e) { LinkStack p; p = new StackNode; if (!p) exit(0); p->date = e; p->next = S; S = p; return 0; } //出栈 int Pop(LinkStack& S, string& e) { LinkStack p; p = new StackNode; if (S == NULL) return 1; e = S->date; p = S; S = S->next; delete p; return 0; } int Linklength(LinkStack S) { int num = 0; LinkStack q = S; while (q) { num++; q = q->next; } return num; } void countAB(string num1, string num2, int& A, int& B, int& num_2) { //Ax+B int i; num_2 = atoi(num2.c_str()); char num1_1[1000]; strcpy(num1_1, num1.c_str()); int len = strlen(num1_1); for (i = 0; i < len; i++) { if (num1_1[i] == 'x') { break; } } if (i == 0) { A = 1; } if (i == len - 1) { B = 0; } if (i != 0 || i != len - 1) { if (num1_1[0] == '-' && i == 1) { A = -1; } else if (num1_1[0] == '-' && i != 1) { int j = 1; char* A1 = &num1_1[j]; while (true) { if (num1_1[j + 1] >= '0' && num1_1[j + 1] <= '9') { A1 = strcat(A1, &num1_1[j + 1]); j++; } else { break; } } A = atoi(A1); A = -A; } else { int j = 0; string A1(1, num1_1[j]); while (true) { if (j < len && num1_1[j + 1] >= '0' && num1_1[j + 1] <= '9') { string A2(1, num1_1[j + 1]); A1 = A1 + A2; j++; } else { break; } } A = atoi(A1.c_str()); } //计算出B int j = i + 2; char* B1 = &num1_1[j]; while (true) { if (num1_1[j + 1] >= '0' && num1_1[j + 1] <= '9') { B1 = strcat(B1, &num1_1[j + 1]); j++; } else { break; } } B = atoi(B1); if (num1_1[i + 1] == '-') { B = -B; } if (num1_1[0] == 'x') { A = 1; } if (num1_1[len - 1] == 'x') { B = 0; } } } string wuX(string num1, string num2, string oper) { int num_1, num_2, mid1; string mid; num_1 = atoi(num1.c_str()); num_2 = atoi(num2.c_str()); //开始计算最优先的lisp子表达式 if (oper.compare("+") == 0) { mid1 = num_1 + num_2; mid = to_string(mid1); } else if (oper.compare("-") == 0) { mid1 = num_2 - num_1; mid = to_string(mid1); } else if (oper.compare("*") == 0) { mid1 = num_1 * num_2; mid = to_string(mid1); } else if (oper.compare("/") == 0) { mid1 = num_2 / num_1; mid = to_string(mid1); } else { printf("表达式有误(不识别的运算符)"); return false; } return mid; } string youX(string num1, string num2, string oper) { //Ax+B string mid,A_1,B_1; int A, B, num_2; int i; countAB(num1, num2, A, B, num_2); //多项式计算 if (oper.compare("+") == 0) { B = B + num_2; A_1 = to_string(A); B_1 = to_string(B); if (A != 0 && B < 0) { mid = A_1 + "x" + B_1; if (A == 1) { mid = "x" + B_1; } } else if (A != 0 && B > 0) { mid = A_1 + "x" + "+" + B_1; if (A == 1) { mid = "x+" + B_1; } } else if (A != 0 && B == 0) { mid = A_1 + "x"; if (A == 1) { mid = "x"; } } else if (A == 0) { mid = B_1; } } //常数-含x else if (oper.compare("-") == 0) { B = num_2 - B; A_1 = to_string(A); B_1 = to_string(B); if (A != 0 && B < 0) { if (A > 0) { mid = "-" + A_1 + "x" + B_1; if (A == 1) { mid = "x" + B_1; } } else { A_1 = to_string(-A); mid = A_1 + "x" + B_1; } } else if (A != 0 && B > 0) { if (A > 0) { mid = "-" + A_1 + "x" + "+" + B_1; if (A == 1) { mid = "x+" + B_1; } } else { A_1 = to_string(-A); mid = A_1 + "x" + "+" + B_1; } } else if (A != 0 && B == 0) { if (A > 0) { mid = "-" + A_1 + "x"; if (A == 1) { mid = "x"; } } else { A_1 = to_string(-A); mid = A_1 + "x"; } } else if (A == 0) { mid = B_1; } } else if (oper.compare("*") == 0) { A = A * num_2; B = B * num_2; A_1 = to_string(A); B_1 = to_string(B); if (A != 0 && B < 0) { mid = A_1 + "x" + B_1; if (A == 1) { mid = "x" + B_1; } } else if (A != 0 && B > 0) { mid = A_1 + "x+" + B_1; if (A == 1) { mid = "x+" + B_1; } } else if (A != 0 && B == 0) { mid = A_1 + "x"; if (A == 1) { mid = "x"; } } else if (A == 0) { mid = B_1; } } else if (oper.compare("/") == 0) { printf("x的系数不为1,不能计算"); return false; } else { printf("表达式有误(不识别的运算符)"); return false; } return mid; } string chufa(string num1, string num2, string oper) { //Ax+B string mid, A_1, B_1; double A1, B1; int A, B, num_2; countAB(num1, num2, A, B, num_2); A1 = (double)A / (double)num_2; B1 = (double)B / (double)num_2; A_1 = to_string(A1); B_1 = to_string(B1); if (A != 0 && B < 0) { mid = A_1 + "x" + B_1; if (A == 1) { mid = "x" + B_1; } } else if (A != 0 && B > 0) { mid = A_1 + "x+" + B_1; if (A == 1) { mid = "x+" + B_1; } } else if (A != 0 && B == 0) { mid = A_1 + "x"; if (A == 1) { mid = "x"; } } else if (A == 0) { mid = B_1; } return mid; } string jianfa(string num1, string num2, string oper) { string mid, A_1, B_1; int A, B, num_2; countAB(num1, num2, A, B, num_2); B = B - num_2; A_1 = to_string(A); B_1 = to_string(B); if (A != 0 && B != 0) { mid = A_1 + "x" + B_1; if (A == 1) { mid = "x" + B_1; } } else if (A != 0 && B == 0) { mid = A_1 + "x"; if (A == 1) { mid = "x"; } } else if (A == 0) { mid = B_1; } return mid; } int count(LinkStack &numStack, LinkStack& operStack, string &mid) { int length1 = Linklength(numStack); int length2 = Linklength(operStack); string num1, num2, oper; if (length1 <= 1 || length2 == 0) { cout << "error" << endl; } else { Pop(numStack, num1); Pop(numStack, num2); Pop(operStack, oper); if (num1.find("x") == string::npos && num2.find("x") == string::npos) { mid = wuX(num1, num2, oper); } if (num2.find("x") != string::npos && num1.find("x") == string::npos) { if (oper.compare("/") == 0) { //前一个数有x mid = chufa(num2, num1, oper); } else if (oper.compare("-") == 0) { //前一个数有x mid = jianfa(num2, num1, oper); } else { mid = youX(num2, num1, oper); } } if (num1.find("x") != string::npos && num2.find("x") == string::npos) { mid = youX(num1, num2, oper); } } } int main() { LinkStack numStack, operStack; InitStack(numStack); InitStack(operStack); string mid; char str[200]; gets_s(str); if (str[0] == '0') { exit(0); } int length = strlen(str); int flag = 0;//0表示正,1表示负 for (int i = 0; i < length; i++) { if (str[length - 1] != ')') { cout << "error" << endl; break; } if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/') { string str(1, str[i]); Push(operStack, str); } else if (str[i] == '(' || str[i] == ',') { if (str[i + 1] == '-' && str[i + 2] != '(') { i++; flag = 1; } } else if (str[i] == ')') { count(numStack, operStack, mid); int length1 = Linklength(numStack); int length2 = Linklength(operStack); if (length1 <= 1 && length2 == 0) { cout << mid << endl; } else if (i == length - 1) { cout << "error" << endl; break; } else { Push(numStack, mid); } } else if (str[i] >= '0' && str[i] <= '9') { string str1(1, str[i]); while (true) { if (str[i + 1] >= '0' && str[i + 1] <= '9') { string str2(1, str[i + 1]); str1 = str1 + str2; i++; } else { break; } } if (flag == 1) { str1 = "-" + str1; flag = 0; } Push(numStack, str1); } else if (str[i] == 'x') { string str1(1, str[i]); int j = i; while (true) { if (str[j - 1] != ',' && str[j - 1] != '(') { string str2(1, str[j - 1]); str1 = str2 + str1; j--; } else { break; } } while (true) { if (str[i + 1] != ',' && str[i + 1] != ')') { string str2(1, str[i + 1]); str1 = str1 + str2; i++; } else { break; } } if (flag == 1) { str1 = "-" + str1; flag = 0; } Push(numStack, str1); } else { cout << "error" << endl; break; } } }