《算法笔记》6.7小节——C++标准模板库(STL)介绍->stack的常见用法详解

目录

问题 A: 简单计算器

问题 B: Problem E


问题 A: 简单计算器

题目描述

读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。

输入

测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。

输出

对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。

样例输入

30 / 90 - 26 + 97 - 5 - 6 - 13 / 88 * 6 + 51 / 29 + 79 * 87 + 57 * 92
0

样例输出

12178.21

题解

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
struct node{
	double num;//操作数 
	char op;//操作符 
	bool flag;//true表示操作数,false表示操作符 
};
string str;
stack<node> s;//操作符栈 
queue<node> q;//后缀表达式序列 
map<char, int> op;
void Change(){//将中缀表达式转换为后缀表达式 
	double num;
	node temp;
	int i;
	for(i = 0; i < str.length(); ){
		if(str[i] >= '0' && str[i] <= '9'){
			temp.flag = true;//标记是操作数
			temp.num = str[i++] - '0';//记录操作数的第一个数位
			while(i < str.length() && str[i] >= '0' && str[i] <= '9'){
				temp.num = temp.num * 10 + (str[i] - '0');//更新操作数
				i++; 
			}
			q.push(temp);
		}
		else{
			temp.flag = false;//标记是操作符
			while(!s.empty() && op[str[i]] <= op[s.top().op]){
				q.push(s.top());
				s.pop();
			}
			temp.op = str[i];
			s.push(temp);
			i++;
		}
	}
	while(!s.empty()){//操作符栈中还有操作符 
		q.push(s.top());
		s.pop();
	}
}
double Cal(){//计算后缀表达式 
	double temp1, temp2;
	node cur, temp;
	while(!q.empty()){
		cur = q.front();//cur记录队首元素
		q.pop();
		if(cur.flag == true){//如果是操作数,直接压入栈 
			s.push(cur);
		}
		else{//如果是操作符 
			temp2 = s.top().num;//弹出第二操作数 
			s.pop();
			temp1 = s.top().num;//弹出第一操作数 
			s.pop();
			temp.flag = true;//临时记录操作数 
			if(cur.op == '+'){
				temp.num = temp1 + temp2;
			}
			else if(cur.op == '-'){
				temp.num = temp1 - temp2;
			}
			else if(cur.op == '*'){
				temp.num = temp1 * temp2;
			}
			else{
				temp.num = temp1 / temp2;
			}
			s.push(temp);
		}
	}
	return s.top().num;//栈顶元素就是后缀表达式的值 
}
int main(){
    op['+'] = op['-'] = 1;//操作符的优先级 
	op['*'] = op['/'] = 2;
	while(getline(cin, str), str != "0"){
		for(string::iterator it = str.end(); it != str.begin(); it--){
			if(*it == ' '){
				str.erase(it);//去掉表达式的空格 
			}
		}
		while(!s.empty()){//初始化栈 
			s.pop(); 
		}
		Change();
		printf("%.2f\n", Cal()); 
	}
    return 0;
}

问题 B: Problem E

题目描述

请写一个程序,判断给定表达式中的括号是否匹配,表达式中的合法括号为”(“, “)”, “[", "]“, “{“, ”}”,这三个括号可以按照任意的次序嵌套使用。

输入

有多个表达式,输入数据的第一行是表达式的数目,每个表达式占一行。

输出

对每个表达式,若其中的括号是匹配的,则输出”yes”,否则输出”no”。

样例输入

4
[(d+f)*{}]
[(2+3))
()}
[4(6]7)9

样例输出

yes
no
no
no

题解

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
stack<char> s;
bool match(string str){
	int i;
	for(i = 0; i < str.length(); i++){
		if(str[i] == '(' || str[i] == '[' || str[i] == '{'){
			s.push(str[i]);
		}
		if(str[i] == ')' || str[i] == ']' || str[i] == '}'){
			if(str[i] == ')'){
				if(!s.empty() && s.top() == '('){
					s.pop();
				}
				else{
					return false;
				}
			}
			else if(str[i] == ']'){
				if(!s.empty() && s.top() == '['){
					s.pop();
				}
				else{
					return false;
				}
			}
			else if(str[i] == '}'){
				if(!s.empty() && s.top() == '{'){
					s.pop();
				}
				else{
					return false;
				}
			}
		}
	}
	if(s.empty()){
		return true;
	}
	return false;
}
int main(){
	int n;
	scanf("%d", &n);
	getchar();
	while(n--){
		string str;
		getline(cin, str);
		while(!s.empty()){
			s.pop();
		}
		if(match(str) == true){
			printf("yes\n");
		}
		else{
			printf("no\n");
		}
	}
	return 0;
}
发布了160 篇原创文章 · 获赞 175 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/wyatt007/article/details/105068940
今日推荐