Leetcode-640 Solve the Equation(求解方程)

本文介绍了一种解决形如ax + b = cx + d形式的一元一次方程的方法。通过解析字符串形式的数学表达式,该方法能够自动计算出方程的解,并能处理诸如无穷解或无解的情况。
  1 class Solution
  2 {
  3     private:
  4         enum op {PLUS,MINUS};
  5         int left_constant_sum = 0;
  6         int left_x_sum = 0;
  7         int right_constant_sum = 0;
  8         int right_x_sum = 0;
  9     public:
 10         void pre_measure(string& s)
 11         {
 12             for(int i = 0;i < s.size();i ++)
 13             {
 14                 if(s[i]=='0'&&s[i+1]=='x'&&(i==0||!isdigit(s[i-1])))
 15                     s[i+1]='0';
 16             }
 17         }
 18         string solveEquation(string equation)
 19         {
 20             enum op OP = PLUS;
 21             int index;
 22             int tmp_store = 0;
 23             int zero_x_warning;
 24             pre_measure(equation);
 25             for(index = 0; equation[index]!='='; index ++)
 26             {
 27                 if(isdigit(equation[index]))
 28                 {
 29                     tmp_store = 10 * tmp_store + equation[index]-'0';
 30                 }
 31                 else if(equation[index]=='x')
 32                 {
 33                     if(OP==PLUS)
 34                     {
 35                         if(tmp_store!=0)
 36                             left_x_sum += tmp_store;
 37                         else
 38                             left_x_sum ++;
 39                     }
 40                     else
 41                     {
 42                         if(tmp_store!=0)
 43                             left_x_sum -= tmp_store;
 44                         else
 45                             left_x_sum --;
 46                     }
 47                     tmp_store = 0;
 48                 }
 49                 else // is '+' or '-'
 50                 {
 51                     if(OP==PLUS)
 52                         left_constant_sum += tmp_store;
 53                     else
 54                         left_constant_sum -= tmp_store;
 55                     if(equation[index]=='+')
 56                         OP = PLUS;
 57                     else
 58                         OP = MINUS;
 59                     tmp_store = 0;
 60                 }
 61             }
 62             if(OP==PLUS)
 63                 left_constant_sum += tmp_store;
 64             else
 65                 left_constant_sum -= tmp_store;
 66             tmp_store = 0;
 67 
 68             for(OP = PLUS,index ++; index < equation.size(); index ++)
 69             {
 70                 if(isdigit(equation[index]))
 71                 {
 72                     tmp_store = 10 * tmp_store + equation[index]-'0';
 73                 }
 74                 else if(equation[index]=='x')
 75                 {
 76                     if(OP==PLUS)
 77                     {
 78                         if(tmp_store!=0)
 79                             right_x_sum += tmp_store;
 80                         else
 81                             right_x_sum ++;
 82                     }
 83                     else
 84                     {
 85                         if(tmp_store!=0)
 86                             right_x_sum -= tmp_store;
 87                         else
 88                             right_x_sum --;
 89                     }
 90                     tmp_store = 0;
 91                 }
 92                 else // is '+' or '-'
 93                 {
 94                     if(OP==PLUS)
 95                         right_constant_sum += tmp_store;
 96                     else
 97                         right_constant_sum -= tmp_store;
 98                     if(equation[index]=='+')
 99                         OP = PLUS;
100                     else
101                         OP = MINUS;
102                     tmp_store = 0;
103                 }
104             }
105             if(OP==PLUS)
106                 right_constant_sum += tmp_store;
107             else
108                 right_constant_sum -= tmp_store;
109             //cout << left_constant_sum << " " << left_x_sum << " " << right_constant_sum << " " << right_x_sum << endl;
110             
111             string result;
112             if(left_constant_sum == right_constant_sum
113             && left_x_sum == right_x_sum)
114                 result = "Infinite solutions";
115             else if(left_x_sum == right_x_sum)
116                 result = "No solution";
117             else
118             {
119                 result = "x=";
120                 char tmp_s[1000];
121                 sprintf(tmp_s,"%d",(right_constant_sum-left_constant_sum)/(left_x_sum-right_x_sum));
122                 result += tmp_s;
123             }
124             return result;
125         }
126 };

 

转载于:https://www.cnblogs.com/Asurudo/p/9493399.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值