C++ 我如何制作这个C++;计算程序留在控制台上?

C++ 我如何制作这个C++;计算程序留在控制台上?,c++,windows,visual-studio,C++,Windows,Visual Studio,我最近在尝试一些计算器的代码,我发现了一个有效的 但无论我尝试了什么,这个程序都会在控制台上显示答案后立即关闭。请帮帮我,我已经尽力让它停下来了。但是它不起作用 我正在使用VisualStudio进行编码,如果与之相关,请通知我 #include <iostream> #include <string> #include <cctype> #include<conio.h> int expression(); char token()

我最近在尝试一些计算器的代码,我发现了一个有效的

但无论我尝试了什么,这个程序都会在控制台上显示答案后立即关闭。请帮帮我,我已经尽力让它停下来了。但是它不起作用

我正在使用VisualStudio进行编码,如果与之相关,请通知我

#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;
}
#包括
#包括
#包括
#包括
int表达式();
字符标记(){
char ch;
标准:cin>>ch;
返回ch;
}
整数因子(){
int-val=0;
char ch=token();
如果(ch=='('){
val=表达式();
ch=令牌();
如果(ch!=')){
std::string error=std::string(“预期”),get:“)+ch;
抛出std::runtime_error(error.c_str());
}
}
否则,如果(isdigit(ch)){
std::cin.unget();
标准:cin>>val;
}
else抛出std::runtime_错误(“意外字符”);
返回val;
}
整型术语(){
int-ch;
int val=因子();
ch=令牌();
如果(ch='*'| | ch=='/')){
int b=术语();
如果(ch=='*')
val*=b;
其他的
val/=b;
}
else std::cin.unget();
返回val;
}
int表达式(){
int val=术语();
char ch=token();
如果(ch='-'| | ch=='+')){
int b=表达式();
如果(ch=='+')
val+=b;
其他的
val-=b;
}
else std::cin.unget();
返回val;
}
int main(int argc,字符**argv){
试一试{

std::cout一般来说,最好的方法是从命令解释器运行程序。我使用
cmd.exe
。在我看来,现在大多数程序员更喜欢Powershell,但我讨厌它(对我来说就像COBOL)。您也可以使用Cygwin,以获得类似bash shell的体验。我不建议在Windows 10开发模式下使用beta bash shell:它不稳定,如果您不非常小心,可能会产生不良后果

在VisualStudio中,只需通过Ctrl+F5运行该程序,该程序无需调试即可运行

要在VS中运行调试,可以在
main
的最后一个右大括号上放置断点,请参见