通过C/C++,实现一元一次方程求解:
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 string str, str_l, str_r; 5 6 struct node 7 { 8 // a表示x前面的系数,b表示常数系数 9 double a, b; 10 }; 11 12 // 判断优先级的大小 13 int priority(char c) 14 { 15 if (c == '*' || c == '/') 16 return 2; 17 if (c == '+' || c == '-') 18 return 1; 19 return 0; 20 } 21 22 void calc(stack <char> &op, stack <node> &num) 23 { 24 node bb = num.top(); 25 num.pop(); 26 node aa = num.top(); 27 num.pop(); 28 node temp_node; 29 30 switch (op.top()) 31 { 32 case '+': 33 temp_node.a = aa.a + bb.a; 34 temp_node.b = aa.b + bb.b; 35 num.push(temp_node); 36 break; 37 case '-': 38 temp_node.a = aa.a - bb.a; 39 temp_node.b = aa.b - bb.b; 40 num.push(temp_node); 41 break; 42 case