c - 在理解如何正确使用 C 中的函数时遇到问题

标签 c function

我目前是一名新程序员,我真的不知道为什么这些问题会发生在我身上。我应该用 C 创建一个程序,它将获取以华氏度给出的三个温度并将它们转换为摄氏度(反之亦然)。

这是我的代码:

#include <stdio.h>
#include <math.h>
float calc_celsius(float new1, float new2, float new3);

int main()
{

float c1, c2, c3, f1, f2, f3;
/* float new1, new1, new3;
float newa, newb, newc; */

printf("Please type in three temperatures in fahrenheit");
scanf("%f %f %f", &f1, &f2, &f3);
/* new1 = calc_celsius(f1);
new2 = calc_celsius(f2);
new3 = calc_celsius(f3); */


printf("Please type in three temperatures in celcius");
scanf("%f %f %f", &c1, &c2, &c3);
newa = calc_fah(c1);
newb = calc_fah(c2);
newc = calc_fah(c3);

printf("The converted temperatures are: \t %.2f %.2f %.2f", calc_celsius(new1, new2, new3));
printf("The converted temperatures are: \t %.2f %.2f %.2f", calc_fahr(newa, newb, newc));


return 0;

}

float calc_celsius(float f1, float f2, float f3);

{

float new1 = ((f1 - 32) * .55);
float new2 = ((f2 - 32) * .55);

float new3 = ((f3 - 32) * .55);

return;

}

float calc_fahr(float c1, float c2, float c3)

{

float newa = (c1 * 1.8) + 32;
float newb = (c2 * 1.8) + 32;
float newc = (c3 * 1.8) + 32;

return;

}

我在这里做错了什么?

忘记添加错误日志

C:\Users\Britannia\Documents\assgn3.c||In function 'main':|
C:\Users\Britannia\Documents\assgn3.c|21|error: 'newa' undeclared (first use in this function)|
C:\Users\Britannia\Documents\assgn3.c|21|note: each undeclared identifier is reported only once for each function it appears in|
C:\Users\Britannia\Documents\assgn3.c|22|error: 'newb' undeclared (first use in this function)|
C:\Users\Britannia\Documents\assgn3.c|23|error: 'newc' undeclared (first use in this function)|
C:\Users\Britannia\Documents\assgn3.c|25|error: 'new1' undeclared (first use in this function)|
C:\Users\Britannia\Documents\assgn3.c|25|error: 'new2' undeclared (first use in this function)|
C:\Users\Britannia\Documents\assgn3.c|25|error: 'new3' undeclared (first use in this function)|
C:\Users\Britannia\Documents\assgn3.c|35|error: expected identifier or '(' before '{' token|
C:\Users\Britannia\Documents\assgn3.c|45|error: conflicting types for 'calc_fahr'|
C:\Users\Britannia\Documents\assgn3.c|26|note: previous implicit declaration of 'calc_fahr' was here|
||=== Build failed: 8 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

我的新代码如下,它的功能是:

#include <stdio.h>
#include <math.h>
float calc_celsius( float );
float calc_fahr( float );
float printAll(float f1, float f2, float f3, float c1, float c2, float c3);

int main()
{

float c1, c2, c3, f1, f2, f3;

printf("Please type in three temperatures in fahrenheit \n");
scanf("%f %f %f", &f1, &f2, &f3);
printf("Please type in three temperatures in celsius \n");
scanf("%f %f %f", &c1, &c2, &c3);

printAll(f1, f2, f3, c1, c2, c3);

return 0;

}

float calc_celsius( float fahr )
{
  float celc = (fahr - 32) * .55;
  return celc;
}

float calc_fahr(float celc)

{

return celc * 1.8 + 32;

}

float printAll(float f1, float f2, float f3, float c1, float c2, float c3) {

printf("Fahrenheit \t | Celsius \n");
printf("***************************** \n");
printf("%.2f \t\t %.2f \n", f1, calc_celsius(f1));
printf("%.2f \t\t %.2f \n", f2, calc_celsius(f2));
printf("%.2f \t\t %.2f \n", f3, calc_celsius(f3));

printf("The Temperature Conversions from Celsius to Fahreinheit are \n");
printf("Celsius \t | Fahrenheit \n");
printf("**************************** \n");
printf("%.2f \t\t %.2f \n", c1, calc_fahr(c1));
printf("%.2f \t\t %.2f \n", c2, calc_fahr(c2));
printf("%.2f \t\t %.2f \n", c3, calc_fahr(c3));

}

最佳答案

一个函数只能返回一个值,而不是您期望的三个值。您可以通过多种方式获取"new"变量中的三个值。一种是将它们定义为全局变量,而不是在函数内部,另一种是通过 ref (带有指针)将变量传递给函数,以便能够在函数内部更改它们。对于第一种方法,这是具体代码:

float new1, new1, new3;
float newa, newb, newc; 
int main()
{

float c1, c2, c3, f1, f2, f3;


printf("Please type in three temperatures in fahrenheit");
scanf("%f %f %f", &f1, &f2, &f3);


printf("Please type in three temperatures in celcius");
scanf("%f %f %f", &c1, &c2, &c3);

calc_celsius(f1, f2, f3)
printf("The converted temperatures are: \t %.2f %.2f %.2f", new1, new2, new3 );

calc_fahr(c1, c2, c3))
printf("The converted temperatures are: \t %.2f %.2f %.2f", newa, newb, newc);


return 0;

}

void calc_celsius(float f1, float f2, float f3);

{

new1 = ((f1 - 32) * .55);
new2 = ((f2 - 32) * .55);

new3 = ((f3 - 32) * .55);

}

void calc_fahr(float c1, float c2, float c3)

{

newa = (c1 * 1.8) + 32;
newb = (c2 * 1.8) + 32;
newc = (c3 * 1.8) + 32;

}

请注意 printf 函数如何与值一起使用,而不是与函数一起使用,现在您的函数返回 void,因为我们不需要任何东西。

使用指针应该是这样的:

int main()
{

float c1, c2, c3, f1, f2, f3;


printf("Please type in three temperatures in fahrenheit");
scanf("%f %f %f", &f1, &f2, &f3);


printf("Please type in three temperatures in celcius");
scanf("%f %f %f", &c1, &c2, &c3);

calc_celsius(&f1, &f2, &f3)
printf("The converted temperatures are: \t %.2f %.2f %.2f", f1, f2, f3 );

calc_fahr(&c1, &c2, &c3))
printf("The converted temperatures are: \t %.2f %.2f %.2f", c1, c2, c3);


return 0;

}

void calc_celsius(float *f1, float *f2, float *f3);

{

*f1 = (((*f1) - 32) * .55);
*f2 = (((*f2) - 32) * .55);
*f3 = (((*f3) - 32) * .55);

}

void calc_fahr(float *c1, float *c2, float *c3)

{

*c1 = ((*c1) * 1.8) + 32;
*c2 = ((*c2) * 1.8) + 32;
*c3 = ((*c3) * 1.8) + 32;

}

关于c - 在理解如何正确使用 C 中的函数时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33289993/

相关文章:

控制可能到达非空函数的结尾 [-Wreturn-type]|

c - 我如何扫描一个数字并检查它是否已经在递归数组中?

c - 使用指针时 C 中的奇怪代码行为,其中注释代码的某个不相关部分会影响输出

javascript - Messi jQuery 确认框重定向

matlab - 正确使用波浪号运算符作为输入参数

c - 编译后整数如何存储在二进制文件中?

c - 在函数的结构体中修改指针的正确方法是什么?我做错了吗?

c++ - 在 C++ 函数调用中使用自增运算符是否合法?

PHP:PDO 全局数据库变量?

javascript - javascript 中的回调问题