我的c语言作业又出问题了~~帮帮忙啊~~

来源:百度知道 编辑:UC知道 时间:2024/05/28 16:53:45
#include<stdio.h>
#define PI 3.14159

void info (float r,float h,float c,float q){
printf("Please enter the radius of the base in centimeters:\n");
scanf("%f",&r);
printf("please enter the height of the container in centimeters:\n");
scanf("%f",&h);
printf("please enter the cost of each square centimeters:\n");
scanf("%f",&c);
printf("please enter the number of containers:\n");
scanf("%f",&q);
}

float total_surface_area (float r,float h){
return PI*r+2*PI*r*h;
}
void main()
{
float r,h,c,q,spent;
info(r,h,c,q);
spent=total_surface_area(r,h)*c*q;
printf("you will spent %f Yuan.\n",spent);
}
为什么最后算值的时候老算不对呢?谁能给我改改~~感激不尽阿~~~

函数想返回数值供调用者使用有几种方法。
1.使用返回值。适用于只返回一个数值的方法。由于你的info函数需要返回四个数值,这个方法不适用。
2.使用静态变量或全局变量。由于你这个程序不涉及这个问题,不多说。
3.使用参数。如果函数需要通过参数的方式返回数值,那么函数的输出参数需要定义为地址类型。对于info函数,应该定义为
void info (float *r,float *h,float *c,float *q)
并且需要在调用info的函数中预先定义用于存放数值的变量。

针对第三种方式,把代码给你改了一下,结果是否正确没有验证,你自己检查一下。
#include<stdio.h>
#define PI 3.14159

void info (float *r,float *h,float *c,float *q){
printf("Please enter the radius of the base in centimeters:\n");
scanf("%f",r);
printf("please enter the height of the container in centimeters:\n");
scanf("%f",h);
printf("please enter the cost of each square centimeters:\n");
scanf("%f",c);
printf("please enter the number of containers:\n");
scanf("%f",q);
}

float total_surface_area (float r,float h){
return PI*r+2*PI*r*h;
}
void main()
{