用ch=getchar()后,缓存区还有个'\n'还要再循环一遍,怎么搞??
查看原帖
用ch=getchar()后,缓存区还有个'\n'还要再循环一遍,怎么搞??
463562
Dreamerlee✅楼主2021/2/21 16:16
#include<iostream>
#include <cstdio>
#include<string>
#include <stack>
using namespace std;
int s = 0, x, y;
stack <int>n;
int main()
{
	char ch;
	do
	{
		ch = getchar();//这里无法清除缓存区
     //为什么题解还有人用getchar()
     //'\n'会再次循环导致报错
		if (ch >= '0' && ch <= '9')
			s = s * 10 + ch - '0';
		else if (ch == '.')
			n.push(s), s = 0;
		else if (ch != '@')
		{
			x = n.top();
			n.pop();
			y = n.top();
			n.pop();
			switch (ch)
			{
			case'+':
				n.push(x + y); break;
			case '-':
				n.push(y - x); break;
			case '*':
				n.push(x * y); break;
			case'/':
				n.push(y / x); break;		
			}
		}
	} while (ch != '@');
	cout << n.top();
	return 0;
}
2021/2/21 16:16
加载中...