【问题标题】:How do i make this C++ Calculation Program stay on console? [duplicate]我如何让这个 C++ 计算程序留在控制台上? [复制]
【发布时间】:2018-06-16 13:58:33
【问题描述】:

我最近在尝试一些计算器的代码,我找到了一个可以工作的..

但是无论我尝试什么,这个程序都会在控制台上显示答案后立即关闭。请帮助我,我尽力让它停止。但它不会工作......

我使用Visual Studio进行编码,如果与它相关,请通知我

#include <iostream>
#include <string>
#include <cctype>
#include<conio.h>

    int expression();

char token() {
    char ch;
    std::cin >> ch;
    return ch;
}

int factor() {
    int val = 0;
    char ch = token();
    if (ch == '(') {
        val = expression();
        ch = token();
        if (ch != ')') {
            std::string error = std::string("Expected ')', got: ") + ch;
            throw std::runtime_error(error.c_str());
        }
    }
    else if (isdigit(ch)) {
        std::cin.unget();
        std::cin >> val;
    }
    else throw std::runtime_error("Unexpected character");
    return val;

}

int term() {
    int ch;
    int val = factor();
    ch = token();
    if (ch == '*' || ch == '/') {
        int b = term();
        if (ch == '*')
            val *= b;
        else
            val /= b;
    }
    else std::cin.unget();
    return val;

}

int expression() {
    int val = term();
    char ch = token();
    if (ch == '-' || ch == '+') {
        int b = expression();
        if (ch == '+')
            val += b;
        else
            val -= b;
    }
    else std::cin.unget();

    return val;

}

int main(int argc, char **argv) {
    try {
        std::cout << expression();
    }
    catch (std::exception &e) {
        std::cout << e.what();

    }
    return 0;
}

【问题讨论】:

标签: c++ windows visual-studio


【解决方案1】:

一般来说,最好的方法是从命令解释器运行程序。我使用cmd.exe。在我看来,现在大多数程序员更喜欢 Powershell,但我讨厌它(对我来说就像 COBOL)。您还可以使用 Cygwin,获得类似 bash-shell 的体验。我不推荐在 Windows 10 开发模式下使用 beta bash shell:它不稳定,如果你不是很小心,可能会做坏事。

在 Visual Studio 中只需通过 Ctrl+F5 运行程序,无需调试即可运行。

要在 VS 中运行调试,您可以在 main 的最后一个右花括号上放置一个断点。

【讨论】:

    猜你喜欢
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    相关资源
    最近更新 更多