【问题标题】:how can i code a program in turbo c++ which will tell the value of trignometric function(like sin30,sin45)?我如何在 turbo c++ 中编写一个程序来告诉三角函数的值(如 sin30、sin45)?
【发布时间】:2020-04-02 03:18:40
【问题描述】:

我知道这听起来很愚蠢,但我怎样才能制作一个程序,使用 int 函数给出三角函数(如 sin30、sin45)的值。我有这个想法,但我不知道它是否可能。如果有人可以尝试它,它真的很有帮助。我目前已经尝试过这样的事情......而且我当然知道它非常错误。我对这个编码很陌生,所以不知道该怎么做

【问题讨论】:

  • Turbo C++ 刚问世时非常棒,但它已被 Windows 上的其他工具(如 MSVC)取代。是的,可以构建自己的三角函数,尽管与生产函数相匹配并非易事。
  • 如果你想使用 int 那么你需要以一定的精度相乘,因为 int 1/2 为零,3 位小数 1000/2 是 500。
  • google:定点精度、CORDIC、泰勒级数、切比雪夫级数。定点将您的非整数值转换为整数值(如果您坚持使用整数),其余部分为您提供算法和方程式,以仅使用 +,-,*,/ 等基本运算符计算基本三角函数。我建议从sin(x) 的泰勒系列开始......这里的例子Algorithm for calculating trigonometry, logarithms or something like that. ONLY addition-subtraction 但对于新手来说可能太多了
  • 也不要发布源代码的图片!发布代码本身(作为代码块文本),以便任何人都可以直接复制使用/测试它而不是重新输入它......

标签: c++ turbo-c++


【解决方案1】:

使用查找表:

#include <iostream.h>

void main() {
    const double sin30 = 0.5;
    const double sin45 = 0.707107;
    const double sin60 = 0.866025;
    const double sin75 = 0.965926;
    const double sin90 = 1;

/*
    const int sin30 = 0;
    const int sin45 = 0;
    const int sin60 = 0;
    const int sin75 = 0;
    const int sin90 = 1;
*/

    cout << "sin 30: " << sin30
         << "\nsin 45: " << sin45
         << "\nsin 60: " << sin60
         << "\nsin 75: " << sin75
         << "\nsin 90: " << sin90 << "\n";
    return 0;
}

我使用了double,因为除sin90 之外的所有值都是0 作为int

【讨论】:

  • 嘿,我试图编译这个程序,但它显示这样的错误 "$g++ -o main *.cpp main.cpp: In function 'int main()': main.cpp:10: 5: 错误: 'cout' 没有在这个范围内声明 cout
  • @AdityaBorkar 这是 Turbo C++ 的答案,因为您询问了 Turbo C++。对于 gcc,您必须改用 std::cout,并且必须将 iostream.h 替换为 iostreamvoid main 在现代(1998 年之后)C++ 中是不允许的。它必须是int main
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-30
  • 2018-03-11
  • 2011-07-24
  • 1970-01-01
相关资源
最近更新 更多