数据结构——整数的四则运算
2015-10-19 10:47
357 查看
栈的应用——整数的四则运算:
例如计算:(1+2)*3-4/4;
代码如下:/************************************************************************/
/* 整数加减乘除四则运算,先要完成后续排列,然后完成数据进出栈 */
/************************************************************************/
#include<iostream>
#include<string>
#include <stack>
using namespace std;
int fh_uxj(char c)
{
switch(c)
{
case '+':return 0;
case '-':return 0;
case '*':return 1;
case '/':return 1;
default: break;
}
}
float szys(float a,float b,char c)
{
switch (c)
{
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':return a/b;
}
}
int main()
{
stack <char> stack1;
stack <float> stack2;
string s_input,s_last,s_comput;
cout<<"please input your data...."<<'\n';
getline(cin,s_input);
int j=0,last=-1;
for (int i=0;i<s_input.size();i++)
{
char temp=s_input.at(i);
if (s_input.at(i)>='0'&&s_input.at(i)<='9')
{
if (i-last!=1)
{
s_last.push_back(' ');
}
s_last.push_back(s_input.at(i));
last=i;
}
else
{
if(stack1.empty())
{
stack1.push(s_input.at(i));
}
else
{
if (s_input.at(i)==')')
{
while (stack1.top()!='(')
{
s_last.push_back(stack1.top());
stack1.pop();
}
stack1.pop();
}
else if (s_input.at(i)=='(')
{
stack1.push(s_input.at(i));
}
else
{
char flag=1;
while (stack1.top()!='(')
{
if (fh_uxj(stack1.top())<fh_uxj(s_input.at(i)))
{
flag=0;
stack1.push(s_input.at(i));
break;
}
else
{
s_last.push_back(stack1.top());
stack1.pop();
if (stack1.empty())
break;
}
}
if (flag) stack1.push(s_input.at(i));
}
}
}
}
while (!stack1.empty())
{
s_last.push_back(stack1.top());
stack1.pop();
}
for (int i=0;i<s_last.size();i++)
{
if (s_last.at(i)>='0'&&s_last.at(i)<='9')
{
float f_temp=0;
while(s_last.at(i)>='0'&&s_last.at(i)<='9')
{
f_temp=f_temp*10;
f_temp=f_temp+(float)(s_last.at(i)-48);
i++;
}
stack2.push(f_temp);
i--;
}
else if (s_last.at(i)==' ')
continue;
else
{
float a=(float)stack2.top();
stack2.pop();
float b=(float)stack2.top();
stack2.pop();
float c=szys(b,a,s_last.at(i));
stack2.push(c);
}
}
cout<<stack2.top();
return 0;
}
例如计算:(1+2)*3-4/4;
代码如下:/************************************************************************/
/* 整数加减乘除四则运算,先要完成后续排列,然后完成数据进出栈 */
/************************************************************************/
#include<iostream>
#include<string>
#include <stack>
using namespace std;
int fh_uxj(char c)
{
switch(c)
{
case '+':return 0;
case '-':return 0;
case '*':return 1;
case '/':return 1;
default: break;
}
}
float szys(float a,float b,char c)
{
switch (c)
{
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':return a/b;
}
}
int main()
{
stack <char> stack1;
stack <float> stack2;
string s_input,s_last,s_comput;
cout<<"please input your data...."<<'\n';
getline(cin,s_input);
int j=0,last=-1;
for (int i=0;i<s_input.size();i++)
{
char temp=s_input.at(i);
if (s_input.at(i)>='0'&&s_input.at(i)<='9')
{
if (i-last!=1)
{
s_last.push_back(' ');
}
s_last.push_back(s_input.at(i));
last=i;
}
else
{
if(stack1.empty())
{
stack1.push(s_input.at(i));
}
else
{
if (s_input.at(i)==')')
{
while (stack1.top()!='(')
{
s_last.push_back(stack1.top());
stack1.pop();
}
stack1.pop();
}
else if (s_input.at(i)=='(')
{
stack1.push(s_input.at(i));
}
else
{
char flag=1;
while (stack1.top()!='(')
{
if (fh_uxj(stack1.top())<fh_uxj(s_input.at(i)))
{
flag=0;
stack1.push(s_input.at(i));
break;
}
else
{
s_last.push_back(stack1.top());
stack1.pop();
if (stack1.empty())
break;
}
}
if (flag) stack1.push(s_input.at(i));
}
}
}
}
while (!stack1.empty())
{
s_last.push_back(stack1.top());
stack1.pop();
}
for (int i=0;i<s_last.size();i++)
{
if (s_last.at(i)>='0'&&s_last.at(i)<='9')
{
float f_temp=0;
while(s_last.at(i)>='0'&&s_last.at(i)<='9')
{
f_temp=f_temp*10;
f_temp=f_temp+(float)(s_last.at(i)-48);
i++;
}
stack2.push(f_temp);
i--;
}
else if (s_last.at(i)==' ')
continue;
else
{
float a=(float)stack2.top();
stack2.pop();
float b=(float)stack2.top();
stack2.pop();
float c=szys(b,a,s_last.at(i));
stack2.push(c);
}
}
cout<<stack2.top();
return 0;
}
相关文章推荐
- 数据结构——查找之二叉排序树
- c++数据结构——模板类重写二叉树
- 优先级队列(PriprityQueue)是一种什么样的数据结构
- 各种数据结构的复杂度
- 数据结构 (五)c++函数和参数传递 按值传递 按引用传递
- 数据结构:字典树的基本使用
- 数据结构:字典树的基本使用
- Hadoop集群管理 Namenode的目录数据结构
- 数据结构之顺序表
- [题解+总结]20151017数据结构
- 数据结构与算法80道
- 第六周--数据结构之自建算法库之迷宫问题(用队列)
- 数据结构之图
- 第六周--数据结构之自建算法库之迷宫问题(用栈结构)
- 数据结构基本概念
- 中国大学MOOC-陈越、何钦铭-数据结构 列出连通集
- 第六周--数据结构之自建算法库之表达式求值(用栈结构)
- 【ShancoLove】带你看数据结构——第八课:二叉树概念
- 第六周--数据结构之自建算法库之链式队列
- 第六周--数据结构 项目之顺序环形队列