-
大小: 2.14MB文件类型: .rar金币: 2下载: 0 次发布日期: 2023-08-12
- 语言: C/C++
- 标签: c++ mid_to_suffi
资源简介
这是一个运行于vs2015的项目,代码也就200+行,实现的功能有基础的四则运算以及math里面cos,sin,pow函数的,有这几个函数为样本添加其他函数也很简单,代码实现简单,中缀转后缀,只用了一个栈,附加提醒,若是使用函数,一定要加括号,如pow(1+2,sin(3+4))这样的可行。

代码片段和文件信息
#include“cal.h“
cal::cal(string ans)
{
str = ans;
}
cal::~cal()
{
}
int cal::compareNumber(char x)//用来比较符号的优先级
{
if (x == ‘#‘||x==‘(‘ || x == ‘‘)
return -1;
if (x == ‘+‘ || x == ‘-‘)
return 0;
if (x == ‘*‘ || x == ‘/‘)
return 1;
return 2;
}
void cal::change(string mid string suxfix)//中缀表达式转换为后缀表达式
{
int i = 0 len = mid.size() mid_index = 0;
char c;
stackintElements;// int数据集
intElements.push(‘#‘);// 特殊符号用来区分元素
while (i < len)
{
if (mid[i] == ‘(‘)
{
intElements.push(mid[i]);
i++;
}
else if (mid[i] == ‘)‘)
{
while (intElements.top() != ‘(‘)
{
suxfix+= intElements.top();
suxfix+= ‘ ‘;
intElements.pop();
}
intElements.pop();
i++;
}
else if (mid[i] == ‘‘)
{
while (intElements.top() != ‘(‘)
{
suxfix += intElements.top();
suxfix += ‘ ‘;
intElements.pop();
}
i++;
}
else if (mid[i] == ‘+‘ || mid[i] == ‘-‘ || mid[i] == ‘/‘ || mid[i] == ‘*‘)
{
while (compareNumber(intElements.top()) >= compareNumber(mid[i]))
{
suxfix+= intElements.top();
suxfix+= ‘ ‘;
intElements.pop();
}
intElements.push(mid[i]);
i++;
}
else if (mid[i] >= ‘0‘&&mid[i] <= ‘9‘)
{
while (mid[i] >= ‘0‘&&mid[i] <= ‘9‘)
{
suxfix+= mid[i];
i++;
}
suxfix+= ‘ ‘;
}
else if (mid[i] >= ‘a‘&&mid[i] <= ‘z‘)
{
string count;
for (; mid[i] != ‘(‘; i++)
count += mid[i];
if (count.compare(“sin“) == 0)
intElements.push(‘a‘);
else if (count.compare(“pow“) == 0)
intElements.push(‘b‘);
else if (count.compare(“cos“) == 0)
intElements.push(‘c‘);
}
else if(mid[i]==‘‘)
{
i++;
}
}
while (intElements.top() != ‘#‘) {
suxfix+= intElements.top();
suxfix+= ‘ ‘;
intElements.pop();
}
printf(“%lf“countSuxfix(suxfix));
}
double cal::countSuxfix(string suxfix)//计算后缀表达式 suxfix后缀表达式
{
int len = suxfix.size();
double x;
stack intElements;
int i = 0;
while (i if (suxfix[i] == ‘ ‘)// 用于表达式有效字符集用完跳出循环
{
i++;
continue;
}
switch (suxfix[i])
{
case ‘+‘:
x = intElements.top();//根据原理图看
intElements.pop();
x = intElements.top() + x;
intElements.pop(); i++;
break;
case ‘-‘:
x = intElements.top();
intElements.pop();
x = intElements.top() - x;
intElements.pop();
i++;
break;
case ‘*‘:
x = intElements.top();
intElements.pop();
x = intElements.top()*x;
intElements.pop();
i++;
break;
case ‘a‘:
x = intElements.top();
intElements.pop();
x = sin(x);
i++;
break;
case ‘b‘:
{
int a = intElements.top();
intElements.pop();
int b = intElements.top();
intElements.pop();
x = 1;
for (int i = 0; i < a; i++)
{
x *= b;
}
i++;
break;
}
case ‘c‘:
x = intElements.top();
intElements.pop();
x = cos(x);
i++;
break;
case ‘/‘:
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
..A..H. 35840 2019-01-16 18:01 Project1\.vs\Project1\v14\.suo
文件 118272 2019-01-16 17:41 Project1\Debug\Project1.exe
文件 685788 2019-01-16 17:41 Project1\Debug\Project1.ilk
文件 1134592 2019-01-16 17:41 Project1\Debug\Project1.pdb
文件 3437 2019-01-16 17:41 Project1\Project1\cal.cpp
文件 434 2019-01-16 11:52 Project1\Project1\cal.h
文件 346536 2019-01-16 17:41 Project1\Project1\Debug\cal.obj
文件 1086 2019-01-16 14:47 Project1\Project1\Debug\Project1.Build.CppClean.log
文件 716 2019-01-16 17:41 Project1\Project1\Debug\Project1.log
文件 1326 2019-01-16 17:41 Project1\Project1\Debug\Project1.tlog\CL.command.1.tlog
文件 27078 2019-01-16 17:41 Project1\Project1\Debug\Project1.tlog\CL.read.1.tlog
文件 2030 2019-01-16 17:41 Project1\Project1\Debug\Project1.tlog\CL.write.1.tlog
文件 1530 2019-01-16 17:41 Project1\Project1\Debug\Project1.tlog\li
文件 3520 2019-01-16 17:41 Project1\Project1\Debug\Project1.tlog\li
文件 834 2019-01-16 17:41 Project1\Project1\Debug\Project1.tlog\li
文件 228 2019-01-16 17:41 Project1\Project1\Debug\Project1.tlog\Project1.lastbuildstate
文件 486400 2019-01-16 17:41 Project1\Project1\Debug\vc140.idb
文件 471040 2019-01-16 17:41 Project1\Project1\Debug\vc140.pdb
文件 166206 2019-01-16 14:48 Project1\Project1\Debug\源.obj
文件 5807 2019-01-14 15:18 Project1\Project1\Project1.vcxproj
文件 1147 2019-01-14 15:18 Project1\Project1\Project1.vcxproj.filters
文件 192 2019-01-16 10:58 Project1\Project1\源.cpp
文件 1306 2019-01-14 14:09 Project1\Project1.sln
文件 8474624 2019-01-16 18:01 Project1\Project1.VC.db
目录 0 2019-01-14 14:09 Project1\.vs\Project1\v14
目录 0 2019-01-16 17:41 Project1\Project1\Debug\Project1.tlog
目录 0 2019-01-14 14:09 Project1\.vs\Project1
目录 0 2019-01-16 17:41 Project1\Project1\Debug
...D.H. 0 2019-01-14 14:09 Project1\.vs
目录 0 2019-01-16 14:53 Project1\Debug
............此处省略5个文件信息
相关资源
- 国际象棋的qt源代码
- C++中头文件与源文件的作用详解
- C++多线程网络编程Socket
- VC++ 多线程文件读写操作
- 利用C++哈希表的方法实现电话号码查
- 移木块游戏,可以自编自玩,vc6.0编写
- C++纯文字DOS超小RPG游戏
- VC++MFC小游戏实例教程(实例)+MFC类库
- 连铸温度场计算程序(C++)
- 6自由度机器人运动学正反解C++程序
- Em算法(使用C++编写)
- libstdc++-4.4.7-4.el6.i686.rpm
- VC++实现CMD命令执行与获得返回信息
- 白话C++(全)
- C++标准库第1、2
- 大数类c++大数类
- C++语言编写串口调试助手
- c++素数筛选法
- C++ mqtt 用法
- 商品库存管理系统 C++ MFC
- c++ 多功能计算器
- C++17 In Detail
- 嵌入式QtC++编程课件
- 颜色识别形状识别STM103嵌入式代码
- c++ 邮件多附件群发
- c++ 透明代理(hookproxy)
- mfc 调用redis
- FTP客户端源码(c++)
- c++ 画图(14Qt-XPS)
- c++多边形交并差运算
评论
共有 条评论