函数返回-1。#IND00 +合并函数

时间:2018-11-22 13:44:07

标签: c function pointers

所以我基本上是在尝试将以下3个功能合并为1个(那是我的老师想要的,但是我无法使它起作用,所以我做了3个)

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define TOTAL_DEG  (180.0)


double berechnung_length(double a, double b, double c, double gamma)
{   
    c = sqrt(a * a + b * b - (2 * a * b * cos(gamma)));
    return c;
}

double berechnung_alpha(double a, double b, double c, double gamma, double alpha)
{
    alpha = gamma * acos((double )(b * b + c * c - a * a) / (2.0 * b * c));
    return alpha;
}

double berechnung_beta(double gamma, double alpha, double beta)
{
    beta = - gamma - alpha + TOTAL_DEG;
     return beta;
}         

soo,如果只有1个函数(指针是一个选项,但我还不太满意),我怎么能让该函数返回值?

感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

  

将以下3个功能合并为1

使用OP的现有函数调用:

void berechnung_all(double a, double b, double c, double gamma,
    double *length, double *alpha, double *beta) {
  *length = berechnung_length(a,b,c,gamma);
  *alpha = berechnung_alpha(a,b,c,gamma, 0);
  *beta = berechnung_beta(a,b,c,gamma, *alpha, 0);
}

更紧密的集成

#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
#endif

#define R2D(r) ((r)*180.0/M_PI)
#define D2R(r) ((r)/180.0*M_PI)

void berechnung_all(double a, double b, double c, double gamma,
    double *length, double *alpha, double *beta) {

  *length = sqrt(a * a + b * b - (2 * a * b * cos(D2R(gamma))));

  // Avoid direct call to `acos()`: prevent `double` math oddities just outside [-1.0...1.0]
  double x = (b * b + c * c - a * a) / (2.0 * b * c);
  if (x > 1.0) x = 1.0;
  if (x < -1.0) x = -1.0;
  *alpha = R2D(gamma * acos(x));

  #define TOTAL_DEG  (180.0)
  *beta = - gamma - alpha + TOTAL_DEG;
}       

答案 1 :(得分:0)

您可以对不同的操作进行枚举,然后打开enum即可在您的多功能功能中执行不同的操作。像这样:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define TOTAL_DEG  (180.0)

enum oper {LENGTH, ALPHA, BETA};

double berechnung_any(double a, double b, double c, double gamma, enum oper op)
{   
    double retVal;
    switch(op) {
       case LENGTH: 
        retVal = sqrt(a * a + b * b - (2 * a * b * cos(gamma)));
       break;
       case ALPHA:
        retVal = gamma * acos((double )(b * b + c * c - a * a) / (2.0 * b * c));
       break;
       case BETA:
        retVal = - gamma - (gamma * acos((double )(b * b + c * c - a * a) / (2.0 * b * c))) + TOTAL_DEG;
       break;
   }
   return retVal;
}

答案 2 :(得分:0)

好的,所以我假设您手中已经有变量a,b,而您没有c,alpha和beta,但是您想在计算后使用它们。 这是代码,我添加了一个主要函数来演示如何使用指针。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define TOTAL_DEG  (180.0)

//here a and b are transferred by values, c, gamma and alpha are just pointers.
double berechnung_func(double a, double b, double* c, double* gamma, double* alpha){

    *c = sqrt(a * a + b * b - (2 * a * b * cos(*gamma)));
    *alpha = (*gamma) * acos((double )(b * b + (*c) * (*c) - a * a) / (2.0 * b * (*c)));

    return (- (*gamma) - (*alpha) + TOTAL_DEG);
}

int main()
{
    double c, gamma, alpha, beta, a=5, b=6;

    // here we are sending c, gamma and alpha **addresses** to the function.
    beta = berechnung_func(a, b, &c, &gamma, &alpha); 
    printf("c = %d\n gamma = %d\n alpha = %d\n beta = %d\n", c, gamma, alpha, beta);

    return 0;
}