我想创建一个exponent函数,它似乎没有按预期工作。对不起,如果我不明白一些基本的东西,我只是在互联网上学习点点滴滴。功能不正常?
float x;
float y;
float z;
int h;
int j;
float exponent (float a, float b)
{
float r;
while(b > 1)
{
r = a * a;
b = b - 1;
}
return (r);
}
^带有变量的函数的片段。
cout << "EXPONENT MODE\n\n";
cout << "Please enter a number: ";
cin >> x; system("CLS");
cout << "Please enter another number as the exponent for the first: ";
cin >> y;
z = exponent(x, y);
cout << "Calculating the answer, please wait";
Sleep(1000);
cout << ".";
Sleep(1000);
cout << ".";
Sleep(1000);
cout << ".";
Sleep(1000);
cout << "\n\nYour answer is : ";
cout << r;
Sleep(5000);
system("CLS");
cout << "Would you like to calculate another set of numbers? (yes = 1, no = 2) : ";
cin >> h;
system("CLS");
^第I部分要在控制台上执行。(只是代码)
基本上,我希望用户输入2号,第一(X)作为基数,所述第二(Y )是指数。程序应该输入x作为a和y作为b并运行该函数。发生了什么事:输入1:5,输入2:3,预期:125,收到:25.我在考虑改变while(b> 0)。如果你们能帮上忙,那就太棒了! (也不要判断我在system("CLS")
的代码)
你的指数函数将只计算正方形('a * a')。 – congusbongus
...重复。 *相同*正方形。而当你修复它时,你最好在将'r'初始化为'1.0'。当然,这不会使用分数指数,所以'b = 1.5'就会失败。 – WhozCraig
我看到了我的数学错误。但是如果我输入5,3不应该输出为25 * 25,因为5 * 5 = 25? –