-1

你可以帮帮我吗?我是编程和 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;
}

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

4

2 回答 2

1

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
}

参考余弦定律


如果代码需要提供并返回一个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
}
于 2018-11-06T00:03:53.780 回答
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));
}
于 2018-11-06T00:00:49.510 回答