我怎样才能做到这一点?有没有办法优化它?

发布于 2025-01-15 20:54:41 字数 1437 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅忆流年 2025-01-22 20:54:41

在 switch 语句外部定义 top1。您定义的全局 top1 没有您期望的值,因为您在每个 case 语句内定义了一个新的 top1。您的编译器应该对此发出警告(实际上,我认为在这种情况下这应该是一个错误)。

此外,由于 num1num4 都是浮点数,因此您的 top1top2 也应该是 float< /code>s,而不是 ints

有效地,从全局变量中删除 int top1,然后在 main 中:

float top1 = 0;
switch ( operatr1 ) {
    case '+':
        top1 = num1 + num2;
        break;
    // ... continue with other cases here
}

然后,对于第二个 switch语句,top1 将可见并具有正确的值。

Define top1 outside the switch statement. The global top1 you define doesn't have the value you expect, because you define a new top1 inside each case statement. Your compiler should be warning you about this (actually, I think this should be an error in this case).

Further, since num1 through num4 are floats, your top1 and top2 should really also be floats, not ints

Effectively, remove int top1 from your global variables, then in your main:

float top1 = 0;
switch ( operatr1 ) {
    case '+':
        top1 = num1 + num2;
        break;
    // ... continue with other cases here
}

Then, for your second switch statement, top1 will be visible and have the correct value.

橙味迷妹 2025-01-22 20:54:41

您可以使用一个函数:

int Evaluate(const char operator,
             const int  num1,
             const int  num2)
{
    bool is_valid_operator = true;
    int result = 0;
    switch (operator)
    {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case '/':
            //  Note: integer division.
            result = num1 / num2;
            break;
        case '*':
            result = num1 * num2;
            break;
        default:
            result = 0;
            is_valid_operator = false;
            break;
    }
    if (is_valid_operator)
    {
        std::cout << "Result of " << num1 << " " << operator << " " << num2 << "\n";
        std::cout << result << "\n";
    }
    return result;
}

您的 main 将被简化为:

//...
int top1 = Evaluate(operatr1, num1, num2);
int top2 = Evaluate(operatr2, top1, num3);

You could make use of a function:

int Evaluate(const char operator,
             const int  num1,
             const int  num2)
{
    bool is_valid_operator = true;
    int result = 0;
    switch (operator)
    {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case '/':
            //  Note: integer division.
            result = num1 / num2;
            break;
        case '*':
            result = num1 * num2;
            break;
        default:
            result = 0;
            is_valid_operator = false;
            break;
    }
    if (is_valid_operator)
    {
        std::cout << "Result of " << num1 << " " << operator << " " << num2 << "\n";
        std::cout << result << "\n";
    }
    return result;
}

Your main would be simplified to:

//...
int top1 = Evaluate(operatr1, num1, num2);
int top2 = Evaluate(operatr2, top1, num3);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文