栈练习

问题 A: 简单计算器
算法分析:
对于这个题,大体思想是这样的:把所有乘除先算出来,再算加减(废话)。
①首先我们把字符串中的空格去掉,不过这里在输入时候记得一定要用getline
②然后用两个栈分别存储数据和运算符,在此之前还要写一个precede函数来判断运算符的优先级,由于栈是先进后出,所以栈顶元素的运算符优先级更高。
③在存储运算符的时候,是否压入栈是根据运算符优先级(precede返回值)判定的,如果当前运算符优先级更低,那么就把前面压入栈的数据计算了;否则就压入栈,继续读取后面的数据。

#include <iostream>
#include <stack>
#include <string>
using namespace std;
bool precede(char a, char b)
{
	if ((a == '*' || a == '/') && (b == '+' || b == '-'))
	{
		return true;
	}
	return false;
}
double operate(char t, double a, double b)
{
	if (t == '+')
	{
		return a + b;
	}
	else if (t == '-')
	{
		return b - a;
	}
	else if (t == '/')
	{
		return b * 1.0 / a;
	}
	else
	{
		return a * b;
	}
}
void calc(stack<double> &numbers, stack<char> &operators)
{
	double a = numbers.top();
	numbers.pop();
	double b = numbers.top();
	numbers.pop();
	numbers.push(operate(operators.top(), a, b));
	operators.pop();
}
int main()
{
	string buffer0;
	while (getline(cin, buffer0))
	{
		if (buffer0.length() == 1 && buffer0[0] == '0')
		{
			break;
		}
		stack<double> numbers;
		stack<char> operators;
		string buffer;
		for (int i = 0; i < buffer0.length(); i++)
		{
			if (buffer0[i] == ' ')
			{
				continue;
			}
			buffer.push_back(buffer0[i]);
		}
		//cout << buffer << endl;
		int i = 0;
		while (i < buffer.length())
		{
			int x = 0;
			if (isdigit(buffer[i]))
			{
				while (isdigit(buffer[i]))
				{
					x = x * 10 + buffer[i] - '0';
					i++;
				}
				numbers.push((double)x);
			}
			if (operators.empty() || precede(buffer[i], operators.top()))
			{
				operators.push(buffer[i]);
				i++;
			}
			else
			{
				calc(numbers, operators);
			}
		}
		while (!operators.empty())
		{
			calc(numbers, operators);
	
		}
		printf("%.2lf\n", numbers.top());
	}
	return 0;
}

问题 B: Problem E
这题其实是模拟栈,把所有的左括号压入栈,一旦遇到右括号,就判断栈顶是否匹配,如果匹配,就弹出当前元素,重复此操作。退出循环后,如果flag = 1,说明所有该匹配的括号都弹出了 ,但是要特别注意,如果栈还不为空,说明有多余的括号,输出“no”,否则输出“yes”;如果flag = 0,输出“no”。

#include<string>
#include<stack>
#include<iostream>
using namespace std;
const int maxn=100;
stack<char> s;
int main()
{
	int n;
	cin >> n;
	while(n--)
	{
		string str;
		cin >> str;
		while (!s.empty())
		{
			s.pop();
		}
		int flag = 1;
		for(int i = 0; i < str.length(); i++)
		{
			if(str[i] == '(' || str[i] == '[' || str[i] == '{')
			{
				s.push(str[i]);//左括号入栈
			}
			else if(str[i] == ')')
			{
				if(!s.empty() && s.top() == '(')
				{
					s.pop();
				}
				else
				{
					flag = 0;
					break;
				}
			
			}
			else if(str[i] == ']')
			{
				if(!s.empty() && s.top() == '[')
				{
					s.pop();
				}
				else
				{
					flag = 0;
					break;
				}	
			}
			else if(str[i] == '}')
			{
				if(!s.empty() && s.top() == '{')
				{
					s.pop();
				}
				else
				{
					flag = 0;
					break;
				}
			}
		}
		if(flag)
		{ 
			if (s.empty())
			{
				printf("yes\n");
			}
			else
			{
				printf("no\n");
			}
		}
		else
		{
			printf("no\n");
		}
	}
	return 0;
}

PAT A1051
思路分析:按顺序1~n把数字进栈,每进入一个数字,判断有没有超过最大范围,超过了就break。如果没超过,设cur = 1,从数组的第一个数字开始,看是否与栈顶元素相等,while相等就一直弹出栈。循环退出后,如过栈为空就输出YES,否则输出NO。

#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int main()
{
	int m, n, k;
	scanf("%d%d%d", &m, &n, &k);
	while (k--)
	{
		stack<int> s;
		vector<int> v(n + 1);
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &v[i]);
		}
		int cur = 1;
		for (int i = 1; i <= n; i++)
		{
			s.push(i);
			if (s.size() > m)
			{
				break;
			}
			while (!s.empty() && v[cur] == s.top())
			{
				s.pop();
				cur++;
			}
		}
		if (s.empty())
		{
			cout << "YES" << endl;
		}
		else
		{
			cout << "NO" << endl;
		}
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值