顺序栈——逆波兰表达式

实验目的:

设计一个顺序栈程序,实现对逆波兰表达式的求值。

实验要求:

1、每个栈元素是一个union类型,例如:

union unData //栈元素的数据类型为Union,Union共用同一块存储空间
{
int d;
char c;
};
提示:union结构体中的变量共用同一个存储空间,即d和c变量的地址码相同

2、顺序栈的类型定义如下:

typedef unData datatype; //栈元素类型,修改为混合型
const int maxsize = 100; //栈容量
typedef struct {
datatype data[maxsize];
int top;
} sqstack; //顺序栈类型
3、自定义输入合法的逆波兰表达式

实验运行示例:(4+1)*5=25

image.png

输入
程序启动后,依次input的信息如下:

1、输入一个合法的逆波兰表达式

提示:表达式非法时,应输出“illegal input!”

输出
完成上述所有输入信息后,程序依次output的信息如下:

1、输出合法的逆波兰表达式的结果

输入样例 1

6 8 4 - - 8 1 + * 2 1 / -
输出样例 1

16
输入样例 2

1.2 8 3 - * 10 2 3 + / -
输出样例 2

4
输入样例 3

6 8 4 - - -
输出样例 3

illegal input!
输入样例 4

6 8 4 - - 8
输出样例 4

illegal input!

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
union unData             //栈元素的数据类型为Union,Union共用同一块存储空间
{
    int d;
    char c;
};
typedef unData datatype; //栈元素类型,修改为混合型
const int maxsize = 100; //栈容量
typedef struct {
    datatype data[maxsize];
    int top;
} sqstack;   
bool isOper(char ch){
	return ch == '+' || ch == '-' || ch == '*' || ch =='/';
}
string doubleTranStr(double num){
	 stringstream str;
     str << num;
     return str.str();
}
double strTranDouble(string str){
	 stringstream ss;
     double num;

     ss << str;
     ss >> num;

     return num;
}
int pop_stack(sqstack *stack , double *num){
	string str = "";
	char ch;
	if(stack->data[stack->top].c == ' '){
		stack->top--;
	}
	if(stack->top <= -1){
		return 0;
	}
	while(1){
		ch = stack->data[stack->top--].c;
		if(ch == ' ' || stack->top < -1){
			stack->top++;
			break;
		}else{
			str.insert(0,1,ch);
		}
	}
	*num = strTranDouble(str);
	return 1;
}
int cal(sqstack *stack, double *sul){
	stack->top = -1;
	double num1 = 0 ,num2 = 0;
	string str;
	getline(cin, str);
	char ch;
	for(size_t i = 0 ; i < str.size();  i++){
		ch = str[i];
		if(isOper(ch)){
			if(pop_stack(stack,&num2) && pop_stack(stack,&num1)){
				double value;
				switch(ch){
					case '+':value = num1 + num2;break;
			        case '-':value = num1 - num2;break;
			        case '*':value = num1 * num2;break;
			        case '/':value = num1 / num2;break;
				}
				string newstr = doubleTranStr(value);
				for(size_t x = 0 ; x < newstr.size() ; x++){
					stack->data[++stack->top].c = newstr[x];
				}
			}else{
				return 0;
			}
		}else{
			stack->data[++stack->top].c = ch;
		}
	}
	if(pop_stack(stack,sul) && stack->top != -1){
		return 0;
	}
	return 1;
}
int main(){
	sqstack * stack = (sqstack*)malloc(sizeof(sqstack));
	double sul;
	 if (cal(stack,&sul))
		 cout << sul << endl;
     else
         cout << "illegal input!" << endl;
	return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值