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

 #include <bits/stdc++.h>

 using namespace std;
string str, str_l, str_r; struct node
{
// a表示x前面的系数,b表示常数系数
double a, b;
}; // 判断优先级的大小
int priority(char c)
{
if (c == '*' || c == '/')
return ;
if (c == '+' || c == '-')
return ;
return ;
} void calc(stack <char> &op, stack <node> &num)
{
node bb = num.top();
num.pop();
node aa = num.top();
num.pop();
node temp_node; switch (op.top())
{
case '+':
temp_node.a = aa.a + bb.a;
temp_node.b = aa.b + bb.b;
num.push(temp_node);
break;
case '-':
temp_node.a = aa.a - bb.a;
temp_node.b = aa.b - bb.b;
num.push(temp_node);
break;
case '*':
// 处理一元一次方程,不考虑二次项
temp_node.a = aa.a * bb.b + aa.b * bb.a;
temp_node.b = aa.b * bb.b;
num.push(temp_node);
break;
case '/':
temp_node.a = aa.a / bb.b;
temp_node.b = aa.b / bb.b;
num.push(temp_node);
break;
}
op.pop();
} int main()
{
while ()
{
cin >>str; // 得到str_l, str_r
for (int i = ; i < str.size(); ++ i)
{
if (str[i] == '=')
{
str_l = str.substr(, i);
str_r = str.substr(i + , str.size());
}
} // 定义符号栈、数字栈
stack <node> num_l;
stack <node> num_r;
stack <char> op_l;
stack <char> op_r;
// 定义左右两边串的长度
int len_l = str_l.size();
int len_r = str_r.size(); // 遍历左边的字符串
for (int i = ; i < len_l; ++ i)
{
if (isdigit(str_l[i]))
{
node temp_node;
double temp = atof(&str_l[i]);
while (isdigit(str_l[i]) || str_l[i] == '.')
++ i;
if (str_l[i] == 'x')
{
temp_node.a = temp;
temp_node.b = ;
num_l.push(temp_node);
}
else
{
temp_node.a = ;
temp_node.b = temp;
num_l.push(temp_node);
-- i;
}
}
else if (str_l[i] == 'x')
{
node temp_node;
temp_node.a = ;
temp_node.b = ;
num_l.push(temp_node);
}
else if (str_l[i] == '(')
{
op_l.push(str_l[i]);
}
else if (str_l[i] == ')')
{
while (op_l.top() != '(')
calc(op_l, num_l);
op_l.pop();
}
else if (op_l.empty())
{
op_l.push(str_l[i]);
}
else if (priority(op_l.top()) < priority(str_l[i]))
{
op_l.push(str_l[i]);
}
else if (priority(op_l.top()) >= priority(str_l[i]))
{
while (!op_l.empty() && priority(op_l.top()) >= priority(str_l[i]))
calc(op_l, num_l);
op_l.push(str_l[i]);
}
} // 遍历右边的字符串
for (int i = ; i < len_r; ++ i)
{
if (isdigit(str_r[i]))
{
node temp_node;
double temp = atof(&str_r[i]);
while (isdigit(str_r[i]) || str_r[i] == '.')
++ i;
if (str_r[i] == 'x')
{
temp_node.a = temp;
temp_node.b = ;
num_r.push(temp_node);
}
else
{
temp_node.a = ;
temp_node.b = temp;
num_r.push(temp_node);
-- i;
}
}
else if (str_r[i] == 'x')
{
node temp_node;
temp_node.a = ;
temp_node.b = ;
num_r.push(temp_node);
}
else if (str_r[i] == '(')
{
op_r.push(str_r[i]);
}
else if (str_r[i] == ')')
{
while (op_r.top() != '(')
calc(op_r, num_r);
op_r.pop();
}
else if (op_r.empty())
{
op_r.push(str_r[i]);
}
else if (priority(op_r.top()) < priority(str_r[i]))
{
op_r.push(str_r[i]);
}
else if (priority(op_r.top()) >= priority(str_r[i]))
{
while (!op_r.empty() && priority(op_r.top()) >= priority(str_r[i]))
calc(op_r, num_r);
op_r.push(str_r[i]);
}
} while (!op_l.empty())
calc(op_l, num_l);
while (!op_r.empty())
calc(op_r, num_r); double x1 = num_l.top().a, y1 = num_l.top().b;
double x2 = num_r.top().a, y2 = num_r.top().b; // cout <<x1 <<" " <<y1 <<" " <<x2 <<" " <<y2 <<endl;
printf("%.2lf\n", (y2 - y1) / (x1 - x2));
} return ;
}

最新文章

  1. 转载:TypeScript 简介与《TypeScript 中文入门教程》
  2. eclipse 快捷键大全(转载)
  3. 使用karma测试平时写的小demo(arguments为例)
  4. URL编码 utf-8 gb2312的区别
  5. Docker 安装部署
  6. Python 利用pytesser模块识别图像文字
  7. 【bzoj1300】大数计算器
  8. 手把手教你写电商爬虫-第三课 实战尚妆网AJAX请求处理和内容提取
  9. WEB网站常见受攻击方式及解决办法
  10. jEdit应用指南【基础篇】
  11. Ubuntu中Samba的安装配置和使用[图文]
  12. LIBSVM之一
  13. Oracle 学习笔记 17 -- 异常处理(PL/SQL)
  14. Contoso 大学 - 使用 EF Code First 创建 MVC 应用,实例演练
  15. sqlserver查询数据库中有多少个表
  16. virtualenv安装及使用
  17. 查找命令which、whereis、locate
  18. Netty实现的一个异步Socket代码
  19. 使用GO开发ChainCode
  20. python之管道, 事件, 信号量, 进程池

热门文章

  1. 【javascript 伪协议】小结
  2. PHP array_fill_keys
  3. 为什么阿里巴巴Java开发手册中不建议在循环体中使用+进行字符串拼接?
  4. 常用函数-String
  5. 使用css实现导航下方线条随导航移动效果
  6. 【Autofac打标签模式】Component和Autowired
  7. LaTeX常用篇(三)---矩阵与表格
  8. Java IO_003.Reader与Writer--字符流以及编码对数据的操作(读取与写入)
  9. Java并发入门之FutureTask
  10. 记录一些常用的python库、软件或者网址