所以这个程序应该在给出每日最高、最低和预计最低的小时后估计一天中的每小时温度。我在调用主函数中的函数时遇到问题。我真的不明白我应该如何从函数中获取特定信息,并用它代替我的主要函数中的变量。我也很难理解参数和参数的概念。我确定我在这里不止一个地方搞砸了,但我现在最关心的是功能。任何帮助将不胜感激。
#include <stdio.h>
#include <math.h>
#define PI 3.14159
double getFahrTemp(double high, double low, int hour);
double fahr2cels( double fahr );
double cels2kelv( double cels );
double fahr2rank( double fahr );
double getDailyHigh()
{
int high;
printf("Enter the daily high temperature <F>:\n");
scanf("%d",&high);
return high;
}
double getDailyLow()
{
int low;
printf("Enter the daily low temperature <F>:\n");
scanf("%d",&low);
return low;
}
int getLowHour()
{
int lowHour;
printf("Enter the time of the daily low temperature:\n");
scanf("%d",&lowHour);
return lowHour;
}
double getFahrTemp(double high, double low, int hour)
{
return (high-low)/2 * sin(2*PI/24 * hour + 3.0/2.0 * PI) + (high+low)/2;
}
double fahr2cels( double fahr )
{
double cels;
cels = fahr - 32 / 1.8;
}
double cels2kelv( double cels )
{
double kelv;
kelv = cels + 273;
}
double fahr2rank ( double fahr )
{
double rank;
rank = fahr + 459.67;
}
int main(getDailyHigh, getDailyLow, getLowHour, getFahrTemp)
{
int hour, time;
printf ("Temperature Scale Conversion Chart:\n")
printf ("TIME FAHR CELSIUS KELVIN RANKINE")
getDailyHigh();
getDailyLow();
getLowHour();
do
{
int time, hour=1;
time = (hour + lowHour) % 12;
getFahrTemp(getDailyHigh(), getDailyLow(), hour)
fahr2cels
cels2kelv
fahr2rank
printf ("%d:00 %2.2d %2.2d %3.2d %3.2d\n", time, fahr, cels, kelv, rank;
hour = hour++;
}
while (hour <= 24);
}
最佳答案
I don't really understand how I am supposed to get specific information from the functions, and use it in place of a variable in my main function. I'm also having trouble grasping the idea of parameters and arguments.
你了解数学中函数的概念吗?例如,将摄氏度转换为华氏度的公式为:
°C = (°F − 32) × 5⁄9
可以将其写成数学函数:
f(x) = (x - 32) × 5⁄9
函数 f
接受一个名为 x
的参数并返回 (x - 32) × 5⁄9
。要“使用”该功能,您可以这样写:
y = f(x)
给定一个变量x
,您可以将函数f
的结果赋值给一个变量y
。
一旦你理解了这一点,你就可以很容易地看到它是如何转化为编程的:
double fahr2cels(double f)
{
return (f - 32) * 5 / 9;
}
调用函数甚至看起来像“数学是如何完成的”:
double celcius = fahr2cels(fahrenheit);
就像您可以在数学中拥有多变量函数一样,您可以拥有接受多个参数的函数。您甚至可以拥有不接受任何参数的函数!
double getFahrTemp(double high, double low, int hour)
{
return (high-low)/2 * sin(2*PI/24 * hour + 3.0/2.0 * PI) + (high+low)/2;
}
调用函数的语法相当一致:
// Call the function getFahrTemp(), passing three parameters.
// The variable fahrtemp receives the result of the function call.
double fahrtemp = getFahrTemp(high, low, hour);
在这个数学类比中,我必须注意一些重要的差异 - C 中的函数可能有副作用(它们以某种方式在函数之外影响程序状态)。此外,您传递的参数总是被复制。
关于C函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3993120/