1.2 switch实现两个数的四则运算

注意:

1、每一个case后面要有break

2、/运算的时候注意分母不能为0

int a, b;
	char c;
	cin>>a>>b>>c;
	switch (c)
	{
	case '+':
		cout << a << '+' << b << '=' << a + b << endl;
		break;
	case '-':
		cout << a << '-' << b << '=' << a - b << endl;
		break;
	case '*':
		cout << a << '*' << b << '=' << a * b << endl;
		break;
	case '/':
		if (b == 0)
		{
			cout << "分母不能为0" << endl;
		}else
		cout << a << '/' << b << '=' << a /b << endl;
		break;
	default:
		cout << "表达式错误" << endl;
	}

你可能感兴趣的:(C++基础代码,c++,算法,开发语言)