新手程式码
写了个函式然后我写了下main试下能不能运行 但是没东西输出
求大佬看看
3.14 那本来写pi 然后也报错。。
#include <stdio.h>
#include <math.h>
float computeArea(int *v1, int *v2, int shape)
{
float area;
while(shape = 3){
area = 3.14 * ((*v1) /2)^2;
}
while(shape = 1){
area = (*v1) * (*v2);
}
while(shape = 2){
area = (*v1) * (*v2) / 2;
return area;
}
}
int main(){
int a=2;
int b=4;
float area = computeArea(&a,&b,1);
printf("%f", area);
return 0;
}
楼主的代码存在很多问题:
1、条件判断应该用 if 而不是 while
2、值判断应该使用 == 而不是 =
3、指数应该用函数 pow() 而不是 ^
应该这么改:
#include <stdio.h>
#include <math.h>
float computeArea(int * v1 , int * v2 , int shape)
{
float area ;
area = 0.0 ;
switch(shape) {
case 1:
area = (*v1) * (*v2) ;
break ;
case 2:
area = (*v1) * (*v2) / 2 ;
break ;
case 3:
area = 3.14 * pow(((*v1) / 2) , 2) ;
break ;
}
return area ;
}
int main(void){
int a = 2 , b = 4 , shape ;
float area ;
for (shape = 1 ; shape < 4 ; shape ++) {
area = computeArea(&a , &b , shape) ;
printf("shape = %d , area = %f\n", shape , area) ;
}
return 0 ;
}
#include <stdio.h>
#include <math.h>
float computeArea(int *v1, int *v2, int shape)
{
float area;
if(shape == 3)
{
area = (float)(3.14 * (*v1 / 2));
area = (float)pow(area, 2);
}
if(shape == 1)
{
area = (float)((*v1) * (*v2));
}
if(shape == 2)
{
area = (float)((*v1) * (*v2) / 2);
}
return area;
}
int main()
{
int a=2;
int b=4;
float area = computeArea(&a,&b,1);
printf("%f", area);
return 0;
}
页:
[1]