三位数字运算计算器

插曲12

问题是,如果程序计算出的第一个动作(取决于操作顺序)是除以0的值(例如7/0 + 3或3 + 7/0),它将打印错误消息,但执行下一个动作。

例子-

输入:7/0 + 2 ----->输出:您不能被零除.7 / 0 + 2 = 2

它应该做什么---->输出:您不能将其除以零。

发生这种情况是因为我无法跳过下一个切换操作。如果您有想法或知道如何解决,请提供帮助。谢谢

PS:如果您知道解决操作顺序问题的更好方法,请提供帮助。

缩短的代码:只需输入类似7 + 4/0或7-4 / 0的代码

#include"stdafx.h"
using namespace System;
#include<iostream>
using namespace std;
int main()
{
    long double num1, num2, num3, res;
    char action1, action2;
    cin >> num1 >> action1 >> num2 >> action2 >> num3;
    if ((action2 == '*' || action2 == '/') && (action1 == '-' || action1 == '+'))  //action2 will be preformed before action1 (Order of operation)
    {
        switch (action2)  //I didn't include the options for '+' or '-' because the if statement requires action2 to be '*' or '/'.
        {
        case('/'):
            if(num3==0)
                cout<< "Error, you can't divide by zero";
            else
                res = num2 / num3;
            break;
            default:
                cout << "Input not recognized";
                break;
            }
            switch (action1)
            {
            case('+'):
                cout << num1 << "+" << num2 << action2 << num3 << "=" << res + num1;
                break;
            case('-'):
                cout << num1 << "-" << num2 << action2 << num3 << "=" << res - num1;
                break;
            default:
                cout << "Input not recognized";
                break;
            }
    }
    cout << "\n\n";
    system("PAUSE");
    return 0;
}

完整代码:

// Three numbers Calculator
/*Known problems: when the first action is divided by zero the programs prints the error message but runs the next action.
Other than that I think most of the stuff works but I need to check*/
#include "stdafx.h"
using namespace System;
#include<iostream>
using namespace std;
int main()
{
    cout << "Enter action as # to exit program" << endl;
    cout << "Possible actions:+,-,*,/\n" << endl;
    int a = 1; //loop veriable
    while (a == 1) //loop
    {
        long double num1, num2, num3, res;
        char action1, action2;
        cin >> num1 >> action1 >> num2 >> action2 >> num3;
        if ((action2 == '*' || action2 == '/') && (action1 == '-' || action1 == '+'))  //action2 will be preformed before action1 (Order of operation)
        {
            switch (action2)  //I didn't include the options for '+' or '-' because the if statement requires action2 to be '*' or '/'.
            {
            case('/'):
                if (num3 == 0)      //The problem I described at the top occurs here and at another place below
                    cout << "You can't divide by zero.";
                else
                    res = num2 / num3;
                break;
            case('*'):
                res = num2*num3;
                break;
            default:
                cout << "Input not recognized";
                break;
            }
            switch (action1)    //I didn't include the options for '*' or '/' because the if statement requires action1 to be '+' or '-'.
            {
            case('+'):
                cout << num1 << "+" << num2 << action2 << num3 << "=" << res + num1;
                break;
            case('-'):
                cout << num1 << "-" << num2 << action2 << num3 << "=" << res - num1;
                break;
            default:
                cout << "Input not recognized";
                break;
            }
        }
        else //action1 will be performed before action2 (Order of operation)
        {
            switch (action1)
            {
            case('+'):
                res = num1 + num2;
                break;
            case('-'):
                res = num1 - num2;
                break;
            case('/'):
                if (num2 == 0)     //The problem I described at the top occurs here and at anothe place above
                    cout << "You can't divide by zero.";
                else
                    res = num1 / num2;
                break;
            case('*'):
                res = num1*num2;
                break;
            case('#'):
                system("PAUSE");
                return 0;
                break;
            default:
                cout << "Input not recognized";
                break;
            }
            switch (action2)
            {
            case('+'):
                cout << num1 << action1 << num2 << "+" << num3 << "=" << res + num3;
                break;
            case('-'):
                cout << num1 << action1 << num2 << "-" << num3 << "=" << res - num3;
                break;
            case('/'):
                if (num3 == 0)
                    cout << "You can't divide by zero.";
                else
                    res = num2 / num3;
                break;
            case('*'):
                res = num2*num3;
                break;
            case('#'):
                system("PAUSE");
                return 0;
                break;
            default:
                cout << "Input not recognized";
                break;
            }
        }
        cout << "\n\n";
    }
}
一些程序员哥们

在输入之后(和之前if对除以零的情况进行特殊检查,如果是,则继续循环。

所以像

while (...)
{
    ...
    std::cin >> ...
    if (action1 == '/' && num2 == 0 || action2 == '/' && num3 == 0)
    {
        std::cout << "Division by zero\n";
        continue;
    }
    if (...)
    ...
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何检查三位数字是否是回文?

来自分类Dev

给定一个三位数的数字,找到其数字的总和。蟒蛇

来自分类Dev

如何在PHP中获取字符前的最后三位数字

来自分类Dev

快速将三位数的Base64数字转换为Int

来自分类Dev

将div中的数字更改为三位数逗号

来自分类Dev

如何获得前三位数字并进行比较以进行输出?

来自分类Dev

使用startsWith函数识别邮政编码的前三位数字

来自分类Dev

获取一个 Int 的最后三位数字

来自分类Dev

如何保护第一位数字并显示最后三位数字?

来自分类Dev

计算任何三位数字的数字总和(在我的代码循环中每次帮助纠正时都在运行)

来自分类Dev

如何用grep匹配三位数?

来自分类Dev

OBD II中的三位数PID

来自分类Dev

获取每个间隙的前三位数

来自分类Dev

输入列表-双/三位数

来自分类Dev

获取每个间隙的前三位数

来自分类Dev

如何在python中计算和打印以三位数(例如001,002,003,004等)开头的数字?

来自分类Dev

设置Y轴值的格式,以百万为单位的原始数字,只希望显示前三位数字

来自分类Dev

我如何从cpp中任何给定数字的右边获取第三位数字?

来自分类Dev

excel单元格自行将一个三位数的数字更改为整数

来自分类Dev

设置Y轴值的格式,以百万为单位的原始数字,只希望显示前三位数

来自分类Dev

Python:Pandas 数据框根据列中整数的最后三位数字重新排列行

来自分类Dev

循环遍历数据库中的每个表并获取一列的前三位数字

来自分类Dev

格式化JTextField最多接受三位数字,但是最多可以输入1-3位数字

来自分类Dev

量化器仅在Java正则表达式中输出三位数

来自分类Dev

Python排序方法将两位数字字符串视为三位数字字符串

来自分类Dev

Python排序方法将两位数字字符串视为三位数字字符串

来自分类Dev

javascript正则表达式允许数字和前三位数字后只有一个空格

来自分类Dev

获取大数字(例如 1900234)并在每三位数字后放置逗号的最快方法是什么?

来自分类Dev

编写一个Java程序以显示百位数,十位数和一个三位数数字的位数

Related 相关文章

  1. 1

    如何检查三位数字是否是回文?

  2. 2

    给定一个三位数的数字,找到其数字的总和。蟒蛇

  3. 3

    如何在PHP中获取字符前的最后三位数字

  4. 4

    快速将三位数的Base64数字转换为Int

  5. 5

    将div中的数字更改为三位数逗号

  6. 6

    如何获得前三位数字并进行比较以进行输出?

  7. 7

    使用startsWith函数识别邮政编码的前三位数字

  8. 8

    获取一个 Int 的最后三位数字

  9. 9

    如何保护第一位数字并显示最后三位数字?

  10. 10

    计算任何三位数字的数字总和(在我的代码循环中每次帮助纠正时都在运行)

  11. 11

    如何用grep匹配三位数?

  12. 12

    OBD II中的三位数PID

  13. 13

    获取每个间隙的前三位数

  14. 14

    输入列表-双/三位数

  15. 15

    获取每个间隙的前三位数

  16. 16

    如何在python中计算和打印以三位数(例如001,002,003,004等)开头的数字?

  17. 17

    设置Y轴值的格式,以百万为单位的原始数字,只希望显示前三位数字

  18. 18

    我如何从cpp中任何给定数字的右边获取第三位数字?

  19. 19

    excel单元格自行将一个三位数的数字更改为整数

  20. 20

    设置Y轴值的格式,以百万为单位的原始数字,只希望显示前三位数

  21. 21

    Python:Pandas 数据框根据列中整数的最后三位数字重新排列行

  22. 22

    循环遍历数据库中的每个表并获取一列的前三位数字

  23. 23

    格式化JTextField最多接受三位数字,但是最多可以输入1-3位数字

  24. 24

    量化器仅在Java正则表达式中输出三位数

  25. 25

    Python排序方法将两位数字字符串视为三位数字字符串

  26. 26

    Python排序方法将两位数字字符串视为三位数字字符串

  27. 27

    javascript正则表达式允许数字和前三位数字后只有一个空格

  28. 28

    获取大数字(例如 1900234)并在每三位数字后放置逗号的最快方法是什么?

  29. 29

    编写一个Java程序以显示百位数,十位数和一个三位数数字的位数

热门标签

归档