【问题标题】:problem with triangle angle in programm language C++ [closed]程序语言C++中的三角角问题[关闭]
【发布时间】:2018-11-05 23:54:01
【问题描述】:

你能帮帮我吗? 我是编程和 C++ 的新手

#include <iostream>
#include <cmath>
#include <math.h>
#include <cstdio>

using namespace std;

double SSS(double a, double b, double c){

    double bot1 = -2*b*c;
    double bot2 = -2*c*a;
    if(bot1==0.0 or bot2==0.0){
        return 0.0;
    }


    double alpha = acos((a*a-b*b-c*c)/bot1);

    const double rad = 0.5729577951308232088;


    double beta = acos((b*b-c*c-a*a)/bot2);
    return alpha*rad;
    return beta*rad;

}

int main(){
    cout << SSS(5, 7, 8)<<endl;
}

我想在我的窗口中获得一个三角形角。不知道哪里出错了..:(

【问题讨论】:

  • 1.57 是预期的输出吗?
  • 我们真的不知道错误是什么,或者您的预期输出是什么。就个人而言,我希望名为 SSS 的函数的输出是三个角度。
  • 我的输出是 0.382132,这是真的,它是 alpha 的输出,但我需要 alpha 和 beta 的输出。我只得到了 alpha 的输出...

标签: c++ triangle-count


【解决方案1】:

你需要转换 int a, b,c 才能浮动。

int SSS(int a, int b, int c){
    return (int) acos((float)(a*a-b*b-c*c)/(-2*b*c));
}

【讨论】:

  • 所以(int) 0.59 (0) 是 OP 正在寻找示例三角形的答案?
  • 感谢您的回答。我试过了,我的输出是 0,但我需要 34°。
  • @KG.Boys 如果您需要以度数为单位的答案,那么您应该在问题中指定。 C++ 数学库使用弧度。
【解决方案2】:

2) 使用 FP 数学而不是整数除法,推荐使用double 变量。

3) 避免除以 0。

double SSS(double a, double b, double c) {
  double bottom = -2 * b * c;
  if (bottom == 0) {
    return 0.0;
  } 
  double alpha = acos((a * a - b * b - c * c) / bottom);
  //return alpha;  // This is in radians
  const double r2d = 57.29577951308232088;
  return alpha * r2d;  // This is degrees
}

参考Law of cosines


如果代码需要提供并返回int

int SSS_int_degrees(int a, int b, int c) {
  int bottom = -2 * b * c;
  if (bottom == 0) {
    return 0;
  } 
  double alpha = acos((1.0 * a * a - 1.0 *b * b - 1.0 *c * c) / bottom);
  const double r2d = 57.29577951308232088;
  return lround(alpha * r2d);  // This is degrees
}

【讨论】:

  • 我更改了我的代码,我想获得 alpha 和 beta。但它没有用:(。我有什么问题吗?
猜你喜欢
  • 2020-07-15
  • 1970-01-01
  • 2015-03-19
  • 2017-07-20
  • 1970-01-01
  • 1970-01-01
  • 2020-10-06
相关资源
最近更新 更多