【问题标题】:Functions should be correct, but why is the final output 0 here?函数应该是正确的,但是为什么这里的最终输出是0呢?
【发布时间】:2021-01-08 21:55:52
【问题描述】:
#include <iostream>
#include <iomanip>
using namespace std;

//Power function
float power (float base, int exp)
{
    if (exp < 0)
    {
        if (base == 0)
            {
                cout << "Base cannot be 0.";
                return -1;
            }
    }

    if (exp == 0)
        return 1;

    if (exp == 1)
        return base;

    return base * power (base, exp - 1);
}

//Factorial function
int facto (int n)
{
    return n <= 0 ? 1 : n * facto (n - 1);
}

//Cos function
float cosCalc (float rad)
{
    float cos = 0;

    int x;
    for (x = 0; x < 10; x++)
        {
            cos += power (-1, x) * power (rad, 2 * x) / facto (2 * x);
        }

    return cos;
}

//Sin function
float sinCalc (float rad)
{
    float sin = 0;

    int x;
    for (x = 0; x < 10; x++)
        {
            sin += power (-1, x) * power (rad, 2 * x + 1) / facto (2 * x + 1);
        }

    return sin;
}

//Main function
int main()
{
    int choice;

    //Title and Menu
    cout << endl << "==============" << endl << " TRIGONOMETRY " << endl << "==============";
    cout << endl << "Select:";
    cout << endl << "1. Calculate Cos and Sin";
    cout << endl << "9. Exit";

    while (true)
    {
        //User Prompt
        cout << endl << endl << "Please enter your choice. => ";
        cin >> choice;
        
        if (choice == 1)
            {
                int angle, anglePh;
                float rad;
                float pi = 3.14159265358979323846264338327950288419716;
                char angleType;
                float cos = 0;
                float sin = 0;

                cout << endl << "Please enter an angle. => ";
                cin >> angle;
                anglePh = angle;
                angle %= 360;
                rad = angle * pi / 180;
                cout << anglePh << " degrees = " << rad << " radian";
                cout << endl << "Calculating Cos...";
                cosCalc (rad);
                cout << endl << "Cos = " << fixed << cos;
                cout << endl << "Calculating Sin...";
                sinCalc (rad);
                cout << endl << "Sin = " << fixed << sin;
            }
        
        if (choice == 9)
            {
                break;
            }
    }
    
}

我正在构建一个根据角度输入计算 Sin 和 Cos 的程序,当我运行它时,它为 Sin 和 Cos 输出 0.000000。我怀疑这与我声明 float cos = 0 和 float sin 有关== 0 在 if 循环中选择 == 1,我尝试弄乱它,但它要么导致程序直接在启动时给我错误,要么我得到相同的输出。

知道我哪里出错了吗?

提前感谢您的洞察力,干杯!

【问题讨论】:

  • float cosCalc (float rad) 函数返回一个float 值,大概是余弦值。但在 main 中,您将其称为 cosCalc (rad); 并忽略该返回值。
  • cos = cosCalc (rad);,同样适用于罪。

标签: c++


【解决方案1】:

您的 cosin 和 sine 函数返回一个浮点数,但为了获得该结果,您仍然必须将它存储在一个变量中。

所以而不是:

cosCalc (rad);

做:

rad = cosCalc (rad);

正弦函数也是如此。

【讨论】:

  • 谢谢!现在它正在输出值,但不幸的是输出仍然是错误的。 :( 如果你知道泰勒级数,你介意检查 Sin 和 Cos 函数吗?否则我会做更多的研究,看看我哪里出错了。再次感谢您的帮助!
  • 一次一个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-21
  • 2020-10-24
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多