原创:张煜
//====================================================================
大家好,我是张煜.
这周实验室交给我们一个作业,用数据结构中的栈来实现思维运算法则.
并且包括括号的情况
然后我是和组内另外一名成员完成的,我们都对数据结构也是才刚刚了解,也不懂怎样是栈
最后考虑到许多的问题和算法
//====================================================================
//四维运算器
//主要是运用栈来实现
//实现一般表达式转化为后缀表达式
//实现多位数和浮点数的读取和复制
//了解cstrlib头文件的数据操作
//了解栈的操作
//对于栈中优先级的操作我们决定采用一个算法直接将一般表达式转化为后缀表达式
//然后将字符数组转化为实型数组
//copy实型数组到栈之中
//后缀表达式的计算
#include<iostream>
#include<string> //对于字符数组的操控
#include<cstdlib> //包含有将字符型的数组转化为实型数组的atol函数
#include<stack> //栈相关的头文件->包括 出栈压栈提取存放清除的相关函数
using namespace std; //命名空间
int Level(char Symbol) //优先级判读函数
{
switch (Symbol)
{
case '+': case '-': return 1;
case '*': case '/': return 2;
case '(': case ')': return 0; //扩号放在这里是因为害怕影响入栈时对 +-*/的影响
case '#': return -1; //这个'#'符号是用来判断对于长字符型的数判断终结
}
}
int main()
{
//=========栈集========================================================================
stack<float>ArrFlo; //将储存有关于最终将字符转化成的实参型的浮点数
stack<char>ArrSym; //将保存关于符号的栈
//=====================================================================================
string ArrEnter; //开辟一个数组使得输入的字符先全部储存到这个数组之中
string ArrResult;
string ArrFloat = " ";
int top = 0; //栈顶
int top_N = 0;
float NO_1 = 0;
float NO_2 = 0;
float NO_3 = 0;
float NO_4 = 0;
float NO_5 = 0;
float NO_6 = 0;
char C_TOP_ONE;
char C_TOP_TWO;
cout << endl << ">> Please input the symbol just : + - * / ( ) " << endl
<< ">> Thank you for your forgiveness!" << endl << endl
<< "Please enter the expression you want to calculate:" << endl;
cin >> ArrEnter; //输入一个一般表达式的字符串
ArrResult = ArrEnter;
ArrEnter += "#$"; //#将其中的运算符都拿出来
bool Right = 1; //表达式的正确值
while (ArrEnter[top] != '$') //在栈中存放
{
if ((ArrEnter[top] > 47 && ArrEnter[top] < 58) || ArrEnter[top] == 46 ||ArrEnter[top] == 32) //判断是否为数字或者小数点
{
ArrFloat += ArrEnter[top];
for (top_N = top + 1; (ArrEnter[top_N] > 47 && ArrEnter[top_N] < 58) || ArrEnter[top_N] == 46 || ArrEnter[top] == 32; top_N++)
ArrFloat += ArrEnter[top_N];
ArrFlo.push(atof(ArrFloat.c_str())); //c_str()函数
ArrFloat = " ";
top = top_N;
}
//判断字符是否为可用符号
if ((ArrEnter[top] >= 35 && ArrEnter[top] <= 37) || (ArrEnter[top] >= 40 && ArrEnter[top] <= 43) || (ArrEnter[top] == 45 || ArrEnter[top] == 47 ))
{
if (ArrSym.size() == 0 || ArrEnter[top] == '(' || Level(ArrEnter[top]) > Level(ArrSym.top()))
ArrSym.push(ArrEnter[top]); //放在符号栈之中
else
{
if(ArrEnter[top] == ')')
{
while(ArrSym.top() != '(')
{
C_TOP_ONE = ArrSym.top(); //取符号栈栈顶元素
NO_1 = ArrFlo.top();
ArrFlo.pop(); //清空
NO_2 = ArrFlo.top();
ArrFlo.pop();
switch(C_TOP_ONE)
{
case '+':{NO_3 = NO_1 + NO_2;break;}
case '-':{NO_3 = NO_2 - NO_1;break;}
case '*':{NO_3 = NO_1 * NO_2;break;}
case '/':{NO_3 = NO_2 / NO_1;break;}
default:break;
}
ArrFlo.push(NO_3); //将结构放入栈中
ArrSym.pop(); //将栈中'('清楚
}
ArrSym.pop();
}
else
{
//将符号栈中不小于当前符号优先级的符号拿出来,运进行算
while(ArrSym.size() != 0 && Level(ArrEnter[top]) <= Level(ArrSym.top()))
{
C_TOP_TWO = ArrSym.top();
NO_4 = ArrFlo.top();
ArrFlo.pop(); //存放之后立即清除
NO_5 = ArrFlo.top();
ArrFlo.pop();
switch(C_TOP_TWO)
{
case '+':{NO_6 = NO_4 + NO_5;break;}
case '-':{NO_6 = NO_5 - NO_4;break;}
case '*':{NO_6 = NO_4 * NO_5;break;}
case '/':{NO_6 = NO_5 / NO_4;break;}
default:break;
}
ArrFlo.push(NO_6);
ArrSym.pop();
}
ArrSym.push(ArrEnter[top]);
}
}
top++;
}
else
{ //报错
cout << "Error:" << ArrEnter[top] << endl;
Right = 0;
break;
}
}
if (Right)
cout << ArrResult << "=" << ArrFlo.top();
cout << endl;
}
如需转载请声明原创地址:http://blog.youkuaiyun.com/u012485183/article/details/21184927