美團點評2020校招後臺開發方向筆試題


#include<bits/stdc++.h>
using namespace std;
int main()
{

		string s,str;
		stack<string> tmp;
		getline(cin, str);
		stringstream ss(str);
		while (ss >> s)
		{

			if (s == "and" || s == "or")
			{
				if (tmp.empty() || tmp.top() == "and" || tmp.top() == "or")
				{
					cout << "error" << endl;
					while (!tmp.empty())tmp.pop();
					break;
				}
				else tmp.push(s);
			}
			else
			{
				if (tmp.empty())tmp.push(s);
				else if (tmp.top() == "true" || tmp.top() == "false")
				{
					cout << "error" << endl;
					while (!tmp.empty())tmp.pop();
					break;
				}
				else if (tmp.top() == "and")
				{
					tmp.pop();
					string s_1 = tmp.top();
					tmp.pop();
					if (s_1 == "true"&&s == "true")
						tmp.push("true");
					else tmp.push("false");
				}
				else
					tmp.push(s);
			}
		}
		if (!tmp.empty() && (tmp.top() == "or" || tmp.top() == "and"))
		{
			cout << "error" << endl;
			while (!tmp.empty())tmp.pop();
			//break;
		}
		while (!tmp.empty())
		{
			if (tmp.top() == "true")
			{
				cout << "true" << endl;
				break;
			}
			if (tmp.size() == 1 && tmp.top() == "false")
			{
				cout << "false" << endl;
				break;
			}
			tmp.pop();
		}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章