我有一个转换器,但它转换了program.ow中的所有单元。我可以让打开项目的用户选择要转换的单元类型吗?
#include<stdio.h>
#include<conio.h>
int main()
{
float m, f, l, g, cm, inch;
printf("Type meter : ");
scanf("%f",&m);
f = 3.2808399 * m;
printf("feets: %f",f);
printf("\nType gallons : ");
scanf("%f",&g);
l = 3.78541178 * g;
printf("litres: %f",l);
printf("\ninches : ");
scanf("%f", &inch);
cm = 2.54 * inch;
printf("cm: %f", cm);
return 0;
}
答案 0 :(得分:1)
下面的代码在复杂性,可移植性,优化(内存/时间)和编程的许多其他方面绝对不是最好的,但它应该让你前进。
我添加了注释来解释代码。几乎所有那些printf
的代码都是不言自明的。
#include<stdio.h>
int main(void)
{
// We need just 3 variables
// This one is for getting the user option
int choice = 0;
// We need these to float variables for user input and an output
float Input = 0.0, Output = 0.0;
// Following code till `while(1)` is optional.
printf("\nThis is a converter with a fixed set of functions.");
printf("\nNote: This converter does support floating point inputs.");
printf("\nNote: Floating point inputs and outputs are truncated to 2 digits after decimal place.");
printf("\nNote: Press any key to acknowledge!");
getchar();
// To get user input multiple times, you'll need to loop
while(1)
{
printf("\n\nFollowing functions are supported, enter a suitable choice form the list below.");
printf("\nPress `1` for Converting Metres to Feet.");
printf("\nPress `2` for Converting Gallons to Litres.");
printf("\nPress `3` for Converting Inches to Centimetres.");
printf("\nPress `0` for Exiting the program.");
printf("\nEnter your Option : ");
scanf("%d", &choice);
// Lets implement a switch-case statement to get the job done
switch(choice)
{
case 1:
printf("Enter input value (in Metres) : ");
scanf("%f",&Input);
Output = 3.2808399 * Input;
printf("%0.2f Metres is equal to %0.2f Feets", Input, Output);
break;
case 2:
printf("Enter input value (in Gallons) : ");
scanf("%f",&Input);
Output = 3.78541178 * Input;
printf("%0.2f Gallons is equal to %0.2f Litres", Input, Output);
break;
case 3:
printf("Enter input value (in Inches) : ");
scanf("%f",&Input);
Output = 2.54 * Input;
printf("%0.2f Inches is equal to %0.2f Centimetres", Input, Output);
break;
case 0:
printf("Thank you. The program will exit now!\n\n");
return 0;
break;
// This default case should take care of the invalid set of choices entered by user
default:
printf("Option you entered is either invalid or is not supported as of now!");
}
}
return 0;
}
实现此目的的另一种方法是使用if-else if-else
阶梯。
因此,您可以从以下代码中删除swtich-case
语句,并将其替换为以下内容:
if(choice == 0)
{
printf("Thank you. The program will exit now!\n\n");
return 0;
}
else if(choice == 1)
{
printf("Enter input value (in Metres) : ");
scanf("%f",&Input);
Output = 3.2808399 * Input;
printf("%0.2f Metres is equal to %0.2f Feets", Input, Output);
}
else if(choice == 2)
{
printf("Enter input value (in Gallons) : ");
scanf("%f",&Input);
Output = 3.78541178 * Input;
printf("%0.2f Gallons is equal to %0.2f Litres", Input, Output);
}
else if(choice == 3)
{
printf("Enter input value (in Inches) : ");
scanf("%f",&Input);
Output = 2.54 * Input;
printf("%0.2f Inches is equal to %0.2f Centimetres", Input, Output);
}
else
{
// This should take care of the invalid set of choices entered by user
printf("Option you entered is either invalid or is not supported as of now!");
}
答案 1 :(得分:0)
char unit;
printf("Which unit do you want to convert (input:m,g,i): ");
scanf("%c",&unit);
if(unit == 'm') ...
else if( if(unit == 'g'))....
else if( if(unit == 'i'))....
else printf("wrong input ! \n");