通过C/C++,实现一元一次方程求解

通过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 '*':
 43         // 处理一元一次方程,不考虑二次项
 44         temp_node.a = aa.a * bb.b + aa.b * bb.a;
 45         temp_node.b = aa.b * bb.b;
 46         num.push(temp_node);
 47         break;
 48     case '/':
 49         temp_node.a = aa.a / bb.b;
 50         temp_node.b = aa.b / bb.b;
 51         num.push(temp_node);
 52         break;
 53     }
 54     op.pop();
 55 }
 56 
 57 int main()
 58 {
 59     while (1)
 60     {
 61         cin >>str;
 62 
 63         // 得到str_l, str_r
 64         for (int i = 0; i < str.size(); ++ i)
 65         {
 66             if (str[i] == '=')
 67             {
 68                 str_l = str.substr(0, i);
 69                 str_r = str.substr(i + 1, str.size());
 70             }
 71         }
 72 
 73         // 定义符号栈、数字栈
 74         stack <node> num_l;
 75         stack <node> num_r;
 76         stack <char> op_l;
 77         stack <char> op_r;
 78         // 定义左右两边串的长度
 79         int len_l = str_l.size();
 80         int len_r = str_r.size();
 81 
 82         // 遍历左边的字符串
 83         for (int i = 0; i < len_l; ++ i)
 84         {
 85             if (isdigit(str_l[i]))
 86             {
 87                 node temp_node;
 88                 double temp = atof(&str_l[i]);
 89                 while (isdigit(str_l[i]) || str_l[i] == '.')
 90                     ++ i;
 91                 if (str_l[i] == 'x')
 92                 {
 93                     temp_node.a = temp;
 94                     temp_node.b = 0;
 95                     num_l.push(temp_node);
 96                 }
 97                 else
 98                 {
 99                     temp_node.a = 0;
100                     temp_node.b = temp;
101                     num_l.push(temp_node);
102                     -- i;
103                 }
104             }
105             else if (str_l[i] == 'x')
106             {
107                 node temp_node;
108                 temp_node.a = 1;
109                 temp_node.b = 0;
110                 num_l.push(temp_node);
111             }
112             else if (str_l[i] == '(')
113             {
114                 op_l.push(str_l[i]);
115             }
116             else if (str_l[i] == ')')
117             {
118                 while (op_l.top() != '(')
119                     calc(op_l, num_l);
120                 op_l.pop();
121             }
122             else if (op_l.empty())
123             {
124                 op_l.push(str_l[i]);
125             }
126             else if (priority(op_l.top()) < priority(str_l[i]))
127             {
128                 op_l.push(str_l[i]);
129             }
130             else if (priority(op_l.top()) >= priority(str_l[i]))
131             {
132                 while (!op_l.empty() && priority(op_l.top()) >= priority(str_l[i]))
133                     calc(op_l, num_l);
134                 op_l.push(str_l[i]);
135             }
136         }
137 
138         // 遍历右边的字符串
139         for (int i = 0; i < len_r; ++ i)
140         {
141             if (isdigit(str_r[i]))
142             {
143                 node temp_node;
144                 double temp = atof(&str_r[i]);
145                 while (isdigit(str_r[i]) || str_r[i] == '.')
146                     ++ i;
147                 if (str_r[i] == 'x')
148                 {
149                     temp_node.a = temp;
150                     temp_node.b = 0;
151                     num_r.push(temp_node);
152                 }
153                 else
154                 {
155                     temp_node.a = 0;
156                     temp_node.b = temp;
157                     num_r.push(temp_node);
158                     -- i;
159                 }
160             }
161             else if (str_r[i] == 'x')
162             {
163                 node temp_node;
164                 temp_node.a = 1;
165                 temp_node.b = 0;
166                 num_r.push(temp_node);
167             }
168             else if (str_r[i] == '(')
169             {
170                 op_r.push(str_r[i]);
171             }
172             else if (str_r[i] == ')')
173             {
174                 while (op_r.top() != '(')
175                     calc(op_r, num_r);
176                 op_r.pop();
177             }
178             else if (op_r.empty())
179             {
180                 op_r.push(str_r[i]);
181             }
182             else if (priority(op_r.top()) < priority(str_r[i]))
183             {
184                 op_r.push(str_r[i]);
185             }
186             else if (priority(op_r.top()) >= priority(str_r[i]))
187             {
188                 while (!op_r.empty() && priority(op_r.top()) >= priority(str_r[i]))
189                     calc(op_r, num_r);
190                 op_r.push(str_r[i]);
191             }
192         }
193 
194 
195         while (!op_l.empty())
196             calc(op_l, num_l);
197         while (!op_r.empty())
198             calc(op_r, num_r);
199 
200         double x1 = num_l.top().a, y1 = num_l.top().b;
201         double x2 = num_r.top().a, y2 = num_r.top().b;
202 
203         // cout <<x1 <<" " <<y1 <<" " <<x2 <<" " <<y2 <<endl;
204         printf("%.2lf\n", (y2 - y1) / (x1 - x2));
205     }
206 
207 
208     return 0;
209 }

 

原文链接: https://www.cnblogs.com/GetcharZp/p/10554939.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    通过C/C++,实现一元一次方程求解

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/291691

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 下午1:50
下一篇 2023年2月15日 下午1:52

相关推荐