gpt4 book ai didi

嵌入函数中的 C++ 函数反向运行?

转载 作者:行者123 更新时间:2023-11-28 00:04:10 25 4
gpt4 key购买 nike

<分区>

抱歉,如果这看起来是个愚蠢的问题。我相信你们知道为什么会这样,但是在我的 (C++) 函数代码中

int result = calculateResult(getUserInput1(), getMathematicalOperation() , getUserInput2())

首先运行函数“getUserInput2()”而不是“getUserInput1()”。 C++ 中的函数在嵌入另一个函数时向后运行是否正常?

完整代码如下

// SimpleCalculator.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>

int getUserInput1()
{
std::cout << "Please enter an integer 1: ";
int value;
std::cin >> value;
return value;
}

int getUserInput2()
{
std::cout << "Please enter an integer 2: ";
int value;
std::cin >> value;
return value;
}

int getMathematicalOperation()
{
std::cout << "Please enter the operator you wish to use (1 = +, 2 = -, 3 = *, 4 = /): ";
int op;
std::cin >> op;

std::cout << "the operator number you chose is : " << std::endl << op << std::endl;

return op;
}

int calculateResult(int x, int op, int y)
{
if (op == 1)
return x + y;
if (op == 2)
return x - y;
if (op == 3)
return x * y;
if (op == 4)
return x / y;

return -1;
}

void printResult(int result)
{
std::cout << "Your result is : " << result << std::endl;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com