PTA|基础编程题目集|7-12

解题

根据输入的运算符进行相应的运算,五种之外的运算符给出ERROR即可。

关键点

可使用选择结构进行判断属于哪种运算

参考代码

#include <iostream>
using namespace std;
int main()
{
	int a,b,ret;			//操作数都为整形,所以五种运算的结果必为整形
	char c;
	bool flag=true;
	cin>>a>>c>>b;
	if (c=='+')
	{
		ret=a+b;
	}

	else if(c=='-')
	{
		ret=a-b;
	}

	else if(c=='*')
	{
		ret=a*b;
	}

	else if(c=='/')
	{
		ret=a/b;
	}

	else if(c=='%')
	{
		ret=a%b;
	}

	else
	{
		flag=not flag;
	}

	if (flag)
	{
		cout<<ret<<endl;

	}
	else{
		cout<<"ERROR"<<endl;
	}
	return 0;
}
原文地址:https://www.cnblogs.com/reaptem/p/14000916.html