之前写过一次,因为对C++理解不深,没有使用stack<自定义数据结构>,自定义数据结构的栈实现是自己编写的,这次重新写了一下,使用内置栈对自定义数据结构进行处理。
附一个上次写的帖子链接:
(21条消息) 3-8 求前缀表达式的值 (20分)_HBU_赵文熙的博客-CSDN博客
新代码:
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stack>
using namespace std;
typedef struct Node
{
int type;//1为操作符,2为数字
float num;
char operate;
Node() :type(),num(),operate(){}
Node(int type,float num,char operate) :type(type),num(num),operate(operate){}
Node(char operate) :type(1),num(0.0),operate(operate){}
Node(float num) :type(2),num(num),operate('\0'){}
}sNode;
typedef sNode *sp;
float calculation(char a,float b,float c)
{
//printf("计算%f/%f=%f",b,c,b/c);
switch(a)
{
case '+':return b+c;
case '-':return b-c;
case '*':return b*c;
case '/':return b/c;
}
}
int main()
{
string s;
getline(cin,s);
s.append(1,' ');
stack<sNode> stackp;
int D=0;
for(int i=0;i<s.length()-1;i++)
{
if(strchr("+-*/",s[i])&&s[i+1]==' ')//单个操作符号
{
stackp.push(sNode(s[i]));
i++;//两次i++跳过空格
}
else if(strchr("0123456789",s[i])&&s[i+1]==' ')//单个数字
{
stackp.push(sNode((float)((int)s[i]-(int)'0')));
i++;//两次i++跳过空格
}
else //组合数字(需要判断正负号、多位数字)
{
int is=0;float sum=0.0;int notd=1;int wei=-1;
if(strchr("+-",s[i]))//第一位是符号位
{
if(s[i]=='-') is=1;
i++;
//printf("判断符号\n");
}
while(s[i]!=' ')
{
if(s[i]=='.')
notd=0;
else if(notd==1)
sum=sum*10+(int)s[i]-(int)'0';//最后s[i]的位置在空格,再加1,跳过空格
else
sum=sum+((int)s[i]-(int)'0')*pow(10,wei--);
i++;
//printf("S");
}
if(is) sum=-sum;
stackp.push(sNode(sum));
}
D++;
}//将数据存入栈中
if(D==1)
{
printf("%.1f",stackp.top().num);return 0;
}
stack<float> stack2;//2栈只会存放数字
for(int i=0;i<D;i++)
{
if(stackp.top().type==2)
{
stack2.push(stackp.top().num);
stackp.pop();
//cout<<stack2.top()<<endl;
}
else//计算
{
char operate=stackp.top().operate;
stackp.pop();
//cout<<"operate="<<operate<<endl;
float temp[3];
for(int j=0;j<2;j++)
{
if(!stack2.empty())
{
temp[j]=stack2.top();
//cout<<"temp="<<temp[j]<<endl;
stack2.pop();
}
else
{
printf("ERROR");
return 0;
}
}
if(abs(temp[1])<1e-6&&operate=='/')
{
printf("ERROR");
return 0;
}
temp[2]=calculation(operate,temp[0],temp[1]);
stack2.push(temp[2]);
//cout<<stack2.top()<<endl;
}
}
float out=stack2.top();
stack2.pop();
if(stack2.empty())
printf("%.1f",out);
else
printf("ERROR");
return 0;
}
//char
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stack>
using namespace std;
typedef struct Node
{
int type;//1为操作符,2为数字
float num;
char operate;
Node() :type(),num(),operate(){}
Node(int type,float num,char operate) :type(type),num(num),operate(operate){}
Node(char operate) :type(1),num(0.0),operate(operate){}
Node(float num) :type(2),num(num),operate('\0'){}
}sNode;
typedef sNode *sp;
float calculation(char a,float b,float c)
{
//printf("计算%f/%f=%f",b,c,b/c);
switch(a)
{
case '+':return b+c;
case '-':return b-c;
case '*':return b*c;
case '/':return b/c;
}
}
int main()
{
string s;
getline(cin,s);
s.append(1,' ');
stack<sNode> stackp;
int D=0;
for(int i=0;i<s.length()-1;i++)
{
if(strchr("+-*/",s[i])&&s[i+1]==' ')//单个操作符号
{
stackp.push(sNode(s[i]));
i++;//两次i++跳过空格
}
else if(strchr("0123456789",s[i])&&s[i+1]==' ')//单个数字
{
stackp.push(sNode((float)((int)s[i]-(int)'0')));
i++;//两次i++跳过空格
}
else //组合数字(需要判断正负号、多位数字)
{
int is=0;float sum=0.0;int notd=1;int wei=-1;
if(strchr("+-",s[i]))//第一位是符号位
{
if(s[i]=='-') is=1;
i++;
//printf("判断符号\n");
}
while(s[i]!=' ')
{
if(s[i]=='.')
notd=0;
else if(notd==1)
sum=sum*10+(int)s[i]-(int)'0';//最后s[i]的位置在空格,再加1,跳过空格
else
sum=sum+((int)s[i]-(int)'0')*pow(10,wei--);
i++;
//printf("S");
}
if(is) sum=-sum;
stackp.push(sNode(sum));
}
D++;
}//将数据存入栈中
if(D==1)
{
printf("%.1f",stackp.top().num);return 0;
}
stack<float> stack2;//2栈只会存放数字
for(int i=0;i<D;i++)
{
if(stackp.top().type==2)
{
stack2.push(stackp.top().num);
stackp.pop();
//cout<<stack2.top()<<endl;
}
else//计算
{
char operate=stackp.top().operate;
stackp.pop();
//cout<<"operate="<<operate<<endl;
float temp[3];
for(int j=0;j<2;j++)
{
if(!stack2.empty())
{
temp[j]=stack2.top();
//cout<<"temp="<<temp[j]<<endl;
stack2.pop();
}
else
{
printf("ERROR");
return 0;
}
}
if(abs(temp[1])<1e-6&&operate=='/')
{
printf("ERROR");
return 0;
}
temp[2]=calculation(operate,temp[0],temp[1]);
stack2.push(temp[2]);
//cout<<stack2.top()<<endl;
}
}
float out=stack2.top();
stack2.pop();
if(stack2.empty())
printf("%.1f",out);
else
printf("ERROR");
return 0;
}
//char