1
所以我只是写了一个简单的计算器使用函数,我想尽可能容易使用。我基本上希望在1行代码中可以完成询问数字和计算以及计算结果的整个过程。但我有我的问题。在下面的代码中,当我在主函数 “cout < < calculate(askCalculation(),askNumber1(),askNumber2())< < endl;” 和我运行该程序,然后我想它首先要求计算,然后第一个数字,然后第二个数字。但事实并非如此,实际上它是以excact的方式进行的。是有原因的,我该如何解决这个问题?C++:为什么它总是先做最后一个参数?
噢,请我知道你可以只把三问funtion在1类使其更加简单,但有什么办法可以把计算funtion在同一个班?
#include <iostream>
using namespace std;
int calculate(int calculation, int firstNumber, int lastNumber)
{
switch(calculation)
{
case 1:
return firstNumber + lastNumber;
break;
case 2:
return firstNumber - lastNumber;
break;
case 3:
return firstNumber * lastNumber;
break;
case 4:
return firstNumber/lastNumber;
break;
}
}
int askNumber1()
{
int a;
cout << "Give the first number" << endl;
cin >> a;
return a;
}
int askNumber2()
{
int b;
cout << "Give the second number" << endl;
cin >> b;
return b;
}
int askCalculation()
{
int c;
cout << "what calculation do you want to do? add (1) subtract (2) multiplication (3) divide (4)" << endl;
cin >> c;
return c;
}
int main()
{
cout << calculate(askCalculation(), askNumber1(), askNumber2()) << endl;
return 0;
}