c - 使用过程为变量赋值

标签 c function procedure

所以我想计算三角形的角度和面积,但我需要使用程序分配输入和输出的值。我找不到任何关于此的示例,并且已经尝试了一些变体,但使用指针仍然存在问题。

到目前为止我得到了什么

#include <stdio.h>
#include <math.h>
#define pi 3.141592654

// Declaration
void input(void);//get user input for triangle's sides
void calculate(int* x,int* y,int* z);//calculating area and angle

//main program
int main(void){

int x,y,z;
double a,b,c,height;

input(x,y,z);
calculate(x,y,z);
printf("angle a : %.3f degree\n",a);
printf("angle b : %.3f degree\n",b);
printf("angle c : %.3f degree\n",c);
printf("Area : %.3f cm2\n",height);


return 0;
}

//Definition
void input(int* x, int* y, int* z)
{
    printf("insert side x :\n");
    scanf("%d",*&x);
    printf("insert side y :\n");
    scanf("%d",*&y);
    printf("insert side z :\n");
    scanf("%d",*&z);
}
void calculate(int* x,int* y,int* z)
{
    int s
    s=(*x + *y + *z)*0.5;
    *Area=sqrt(s*(s-x)*(s-y)*(s-z));
    *a=acos(((*x * *x)+(*z * *z)-(*y * *y))/2(*x)(*z));
    *b=acos(((*y * *y)+(*z * *z)-(*x * *x))/2(*y)(*z));
    *c=acos(((*x * *x)+(*y * *y)-(*z * *z))/2(*x)(*y));
}

我在扫描用户输入的 x、y、z 并将度数和面积结果分配给面积、a、b、c 时出错

最佳答案

  • 在声明和定义中创建所需的参数。
  • 我认为在不需要的地方使用指针不好。

你的代码应该是这样的:

#include <stdio.h>
#include <math.h>
#define pi 3.141592654

// Declaration
void input(int* x,int* y,int* z);
void calculate(double* a, double* b, double* c, double* Area, int x,int y,int z);

//main program
int main(void){

    int x,y,z;
    double a,b,c,height; /* It maybe good to rename height to Area */

    input(&x,&y,&z);
    calculate(&a,&b,&c,&height,x,y,z);
    printf("angle a : %.3f degree\n",a);
    printf("angle b : %.3f degree\n",b);
    printf("angle c : %.3f degree\n",c);
    printf("Area : %.3f cm2\n",height);

    return 0;
}

//Definition
void input(int* x, int* y, int* z)
{
    printf("insert side x :\n");
    scanf("%d",x);
    printf("insert side y :\n");
    scanf("%d",y);
    printf("insert side z :\n");
    scanf("%d",z);
}
void calculate(double* a, double* b, double* c, double* Area, int x,int y,int z)
{
    double s; /* type of s should be double, not int in this case */
    s=(x + y + z)*0.5;
    *Area=sqrt(s*(s-x)*(s-y)*(s-z));
    *a=acos(((x * x)+(z * z)-(y * y))/(2 * x * z));
    *b=acos(((y * y)+(z * z)-(x * x))/(2 * y * z));
    *c=acos(((x * x)+(y * y)-(z * z))/(2 * x * y));
}

关于c - 使用过程为变量赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35906773/

相关文章:

api - Node.js 阻止函数检查 (toString)

JavaScript - 如何直接从 .js 文件调用函数而不提供其引用

c++ - 具有可变数量参数的派生类

java callable语句帮助使用包过程

function - (SICP) 函数和过程有什么区别?

mysql - 查找序列中的错误

c - 从共享库创建可执行文件的命令差异

c - 这段C语言代码有什么问题?

c++ - (Visual C++) 从线程获取 PID

c - 使用套接字的 IPC 中的意外输出