信息学奥赛一本通 1057:简单计算器 | OpenJudge NOI 1.4 19:简单计算器

【题目链接】

ybt 1057:简单计算器
OpenJudge NOI 1.4 19:简单计算器

【题目考点】

1. switch语句
2. if…else if…else语句
3. 函数

【题解代码】

解法1:使用switch语句
#include<bits/stdc++.h>
using namespace std; 
int main()
{
    int x, y;//声明两个整型变量,表示参与运算的数字
    char c;//声明字符型变量,表示运算符
    cin >> x >> y >> c;//输入变量
    switch(c)//switch选择语句,看变量c与哪个case后面的常量相等
    {
        case '+'://如果c是'+'
            cout << x + y;//输出x+y的结果
            break;
        case '-'://如果c是'-'
            cout << x - y;//输出x-y的结果
            break;
        case '*'://如果c是'*'
            cout << x * y;//输出x*y的结果
            break;
        case '/'://如果c是'/'
            if (y == 0)//如果除数是0
                cout << "Divided by zero!";//输出:除0,这是非法运算
            else
                cout << x / y;//输出x/y的结果
            break;
        default://如果运算符不是 + - * /
            cout << "Invalid operator!";//输出"非法运算符"
    }
    return 0;
}
解法2:使用if…else if…else语句
#include<bits/stdc++.h>
using namespace std; 
int main()
{
    int x, y;//声明两个整型变量,表示参与运算的数字
    char c;//声明字符型变量,表示运算符
    cin >> x >> y >> c;
    if(c == '+')
        cout << x + y;
    else if(c == '-')
        cout << x - y;
    else if(c == '*')
        cout << x * y;
    else if(c == '/')
    {
        if (y == 0)//如果除数是0
            cout << "Divided by zero!";//输出:除0,这是非法运算
        else
            cout << x / y;
    }
    else
        cout << "Invalid operator!";//输出"非法运算符"
    return 0;
}
解法3:写函数
#include<bits/stdc++.h>
using namespace std;
int calc(int a, int b, char c)//已知a,b,c合法 
{
	switch(c)
	{
		case '+':
			return a+b;
		case '-':
			return a-b;
		case '*':
			return a*b;
		case '/':
			return a/b;
	}
}
int main()
{
	int a, b;
	char c;
	cin >> a >> b >> c;
	if(c != '+' && c != '-' && c != '*' && c != '/')
		cout << "Invalid operator!";
	else if(c == '/' && b == 0)
		cout << "Divided by zero!";
	else
		cout << calc(a, b, c);
	return 0;
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
信息学奥赛一本通1255:迷宫问题是一个关于迷宫的问题。这个问题要求过广搜算法来解决迷宫问题,找到走出迷宫的路径。具体来说,迷宫可以看成是由n×n的格点组成,每个格点只有两种状态, "." 和 "#" 。其中 "." 代表可行的路径,"#" 代表不可行的墙壁。过广搜算法,我们可以搜索从起点到终点的路径,找到一条合法的路径即可。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [信息学奥赛一本通 1255:迷宫问题 | OpenJudge NOI 2.5 7084:迷宫问题](https://blog.csdn.net/lq1990717/article/details/124721407)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [c++信息学奥赛一本通1215题解](https://download.csdn.net/download/Asad_Yuen/87357807)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [信息学奥赛一本通(1255:迷宫问题)](https://blog.csdn.net/lvcheng0309/article/details/118879231)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值