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