我正在尝试将Switch组合成Void Function以将Celsius转换为Fahrenheit,反之亦然。我不确定将Switch和Void功能组合在一起的正确方法是什么。 我的代码在下面。任何建议对我都有价值。 谢谢。
#include<stdio.h>
void fahtocel(void);
void celtofah(void);
int main(void)
{
int tem;
printf("Please select your choice\n");
printf("Enter 1 if you need to convert Fahrenheit to Celsius\n");
printf("Enter 2 if you need to convert Celsius to Fahrenheit\n");
scanf("%d", &tem);
switch(tem) {
case 1:
fahtocel();
break;
case 2:
celtofah();
break;
}
return 0;
}
void fahtocel()
{
float fahr;
printf("Please input Fahrenheit: ");
scanf("%2.f", &fahr);
printf("%3f Fahrenheit is equal %3.0f Celsius \n",fahr,(5.0/9.0)*(fahr - 32)*5/9;
}
void celtofah()
{
float celsius;
printf("Please input celsius: ");
scanf("%2.f", &celsius);
printf("%2.f Celsius is equal %2.f Fahrenheit\n",celsius, (9*celsius/5)+32);
}
答案 0 :(得分:0)
您必须遇到编译器问题。将一个括号放在printf的末尾,并将%2.f
更改为scanfs中的%f。
此代码有效......
#include<stdio.h>
void fahtocel(void);
void celtofah(void);
int main(void)
{
int tem;
printf("Please select your choice\n");
printf("Enter 1 if you need to convert Fahrenheit to Celsius\n");
printf("Enter 2 if you need to convert Celsius to Fahrenheit\n");
scanf("%d", &tem);
switch (tem) {
case 1:
fahtocel();
break;
case 2:
celtofah();
break;
}
return 0;
}
void fahtocel()
{
float fahr;
printf("Please input Fahrenheit: ");
scanf("%f", &fahr);
printf("%3f Fahrenheit is equal %3.0f Celsius \n", fahr, (5.0 / 9.0) * (fahr - 32) * 5 / 9);
}
void celtofah()
{
float celsius;
printf("Please input celsius: ");
scanf("%f", &celsius);
printf("%2.f Celsius is equal %2.f Fahrenheit\n", celsius, (9 * celsius / 5) + 32);
}