簡體   English   中英

在了解如何正確使用C-中的功能時遇到問題

[英]having issues understanding how to properly use functions in C-

我目前是一名新程序員,我真的不知道為什么這些問題對我來說會發生。 我應該用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));

}

那么一個函數只能返回一個值,而不是您在此處期望的三個值。 您可以通過多種方法來獲取“新”變量中的三個值。 一種是將它們定義為全局變量,而不是在函數內部,另一種是通過ref(帶有指針)將變量粘貼到函數,以便能夠在函數內部進行更改。 對於第一種方法,這是especific代碼:

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,因為我們不需要使用anthing。

帶指針應該是這樣的:

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;

}

您在這里的路線正確:

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); */

您想要像在此最初為每個輸入溫度調用calc_celcius函數。 該函數的定義為

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

或更簡單地說,

float calc_celcius( float fahr )
{
  return (fahr - 32) * .55;
}

calc_fahr的定義類似:

float calc_fahr( float celc )
{
  return celc * 1.8 + 32;
}

因此,您的代碼基本上是:

printf("Please type in three temperatures in fahrenheit");
scanf("%f %f %f", &f1, &f2, &f3);
printf("The converted temperatures are: \t %.2f %.2f %.2f", 
          calc_celsius(f1),  // no real need to store any of these 
          calc_celcius(f2),  // results since you just print them
          calc_celcius(f3)); // out

從華氏溫度到攝氏溫度的計算具有相同的一般結構。

有關C函數的一些基本知識:

  1. 至少必須先聲明一個函數,然后才能對其進行調用。 如果函數及其調用者都在同一個源文件中,則我想在函數調用者之前定義它。 在這種情況下,將如下所示:
     float calc_celcius( float fahr ) { return (fahr - 32) * .55; } float calc_fahr( float celc ) { return celc * 1.8 + 32; } int main( void ) { // body of main, including calls to calc_celcius and calc_fahr } 
  2. 函數聲明將指定函數的返回類型以及參數1的數量和類型。 函數聲明不需要指定參數名稱(盡管這樣做通常是個好主意)。 轉換函數的聲明如下所示:
     float calc_celcius( float ); float calc_fahr( float ); 
    因此,如果您願意,可以將代碼編寫為:
     float calc_celcius( float ); int main( void ) { ... x = calc_celsius( y ); ... } float calc_celcius( float fahr ) { return (fahr - 32) * .55; } 
    如果您想將函數定義放在main之后。
  3. 函數本地聲明的變量不能被其他函數訪問; 屍體calc_celciuscalc_fahr無法更新new1new2new3 ,等等,這些都是局部變量main 同樣,在函數中本地聲明的變量僅在該函數的生存期內存在。 在文件作用域(任何函數之外)聲明的變量對於同一轉換單元中的所有函數都是可見的。 理想情況下,您要避免使用全局變量,而讓函數通過參數和返回值進行排他性通信。 它使您的代碼更易於維護和重用。
  4. 確保函數聲明,函數調用,函數定義和函數返回所有行。 也就是說,如果函數定義采用三個參數並返回單個float,則函數中的return語句需要返回浮點值,函數調用需要傳遞三個參數並將結果分配給float對象,並且聲明和定義之間的返回類型和數量以及參數類型是否匹配(如果其中任何一個不正確,編譯器都會抱怨)。


1.這種形式的函數聲明稱為原型語法 在1989年版標准之前,聲明僅指定函數的返回類型,因此編譯器無法驗證函數調用中參數的數量和類型。 非原型函數聲明和定義是過時的,並且(通常)在新開發中不使用,但是它們仍然合法,可以在某些舊代碼庫中找到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM