美团点评2020校招后台开发方向笔试题

美团点评2020校招后台开发方向笔试题_第1张图片

美团点评2020校招后台开发方向笔试题_第2张图片


#include
using namespace std;
int main()
{

		string s,str;
		stack 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();
		}
}

 

你可能感兴趣的:(算法题)