所以我想计算三角形的角度和面积,但我需要使用过程分配输入和输出的值。我找不到任何关于这个的例子,并且已经尝试了一些变化,但仍然有使用指针的问题。
到目前为止我得到了什么
#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
答案 0 :(得分:1)
您的代码应该是这样的:
#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));
}