P1739 表达式括号匹配

题目描述

假设一个表达式有英文字母(小写)、运算符(+,—,*,/)和左右小(圆)括号构成,以“@”作为表达式的结束符。请编写一个程序检查表达式中的左右圆括号是否匹配,若匹配,则返回“YES”;否则返回“NO”。表达式长度小于255,左圆括号少于20个。

输入格式

一行:表达式

输出格式

一行:“YES” 或“NO”

输入输出样例

输入 #1复制

2*(x+y)/(1-x)@

输出 #1复制

YES

输入 #2复制

(25+x)*(a*(a+b+b)@

输出 #2复制

NO

说明/提示

表达式长度小于255,左圆括号少于20个

 

#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <climits>
#include <stdio.h>
#include <iomanip>
#include <math.h>   
#include <stack>  
using namespace std;

int main(void)
{
	stack<char> mytack;
	string tmp;
	//getline(std::cin, tmp);
	char ch;
	while(1)
	{
		cin >> ch;
		if(ch == '@')
			break;
		else
			tmp.push_back(ch);
	}
	string nums;
	int flag = 0;
	for(int i = 0; i < tmp.size(); i++)
	{
		if(tmp[i] == '(' )
		{
			flag = 1;
			mytack.push(tmp[i]);
		}
		else if(tmp[i] == ')')
		{
			flag = 1;
			if(mytack.size() == 0)
			{
				cout << "NO" << endl;
				return 0;
			}
			mytack.pop();
		}
	}
	if(mytack.size() == 0 && flag == 1)
		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、付费专栏及课程。

余额充值