#include <stdio.h>
#include <math.h>
void getInput( int *metricUnit, int *englishUnit, int *metricNum, int *englishNum, int *valueConvert, int convert);
int getMetricUnit(int *metricNum);
int getEnglishUnit (int *englishNum);
int getValueConvert(int convert, int *metricUnit);
double convertLiquid(int metricUnit, int englishUnit, int convert, double converted);
void printOutput(int metricUnit, int englishUnit, int convert, double converted);
int main (void)
{
//Declarations
int metricUnit;
int englishUnit;
int convert;
int metricNum;
int englishNum;
int valueConvert;
int converted;
//Statements
getInput(&metricUnit, &englishUnit, &metricNum, &englishNum, &valueConvert, convert);
convertLiquid(metricUnit, englishUnit, convert, converted);
printOutput(metricUnit, englishUnit, convert, converted);
return 0;
}
void getInput( int *metricUnit, int *englishUnit, int *metricNum, int *englishNum, int *valueConvert, int convert) //Function 0
{
*metricUnit = getMetricUnit(metricNum);
*englishUnit = getEnglishUnit(englishNum);
*valueConvert = getValueConvert(convert, metricUnit);
}
int getMetricUnit(int *metricNum) //Subfunction 1 of getInput
{
printf("Metric Units (Input)\n");
printf("1 - Milliliters \n");
printf("2 - Centiliters \n");
printf("3 - Deciliters \n");
printf("4 - Liters \n");
printf("Enter the number of metric unit: ");
scanf("%d", metricNum);
printf("\n");
return *metricNum;
}
int getEnglishUnit (int *englishNum) //Subfunction 2 of getInput
{
printf("English Units (Output) \n");
printf("1 - Fluid Ounce \n");
printf("2 - Cup \n");
printf("3 - Pint \n");
printf("4 - Quart \n");
printf("5 - Gallon \n");
printf("Enter the number of the English unit: ");
scanf("%d", englishNum);
return *englishNum;
}
int getValueConvert(int convert, int *metricUnit)
{
switch (*metricUnit)
{
case 1: printf("Enter the milliliter value to convert: ");
scanf("%d", &convert); break;
case 2: printf("Enter the centiliter value to convert: ");
scanf("%d", &convert); break;
case 3: printf("Enter the deciliter value to convert: ");
scanf("%d", &convert); break;
case 4: printf("Enter the liter value to convert: ");
scanf("%d", &convert); break;
default: printf("Enter the milliliter value to convert: ");
scanf("%d", &convert); break;
}
if (convert > 0) return convert;
else return 0;
}
double convertLiquid(int metricUnit, int englishUnit, int convert, double converted)
{
double milliliter;
double centiliter;
double deciliter;
double liter;
double fluidOunce;
double cup;
double pint;
double quart;
double gallon;
switch (metricUnit)
{
case 1: metricUnit = milliliter; convert = milliliter ;break;
case 2: metricUnit = centiliter; convert = milliliter * 10 ;break;
case 3: metricUnit = deciliter ; convert = milliliter * 100 ;break;
case 4: metricUnit = liter ; convert = milliliter * 1000;break;
default: metricUnit = milliliter; convert = milliliter ;break;
} //end swtich1
fluidOunce = ((double)convert) * 29.537;
switch (englishUnit)
{
case 1: englishUnit = fluidOunce; converted = fluidOunce ; break;
case 2: englishUnit = cup; converted = fluidOunce * 8 ; break;
case 3: englishUnit = pint; converted = fluidOunce * 16; break;
case 4: englishUnit = quart; converted = fluidOunce * 32; break;
case 5: englishUnit = gallon; converted = fluidOunce * 128; break;
default: englishUnit = fluidOunce; converted = fluidOunce ; break;
} //end switch2
return converted;
}
void printOutput(int metricUnit, int englishUnit, int convert, double converted)
{
switch (metricUnit)
{
case 1: printf("%lf milliliter(s) = %lf ", (double)convert);
case 2: printf("%lf centiliter(s) = ", (double)convert);
case 3: printf("%lf deciliter(s) = ", (double)convert);
case 4: printf("%lf liter(s) = ", (double)convert);
default: printf("%lf milliliter(s) = ", (double)convert);
}
}
Im having the trouble with the printOutput function,
这是试运行
Metric Units (Input)
1: Milliliter
2: Centiliter
3: Deciliter
4: Liter
Enter the number of the metric unit: 5
English Units (Output)
1: Fluid Ounce
2: Cup
3: Pint
4: Quart
5: Gallon
Enter the number of the English unit: 0
Enter the milliliter value to convert: 300
300.000 milliliter(s) = 10.144 fluid ounce(s)
我只是想弄清楚如何获得此测试运行的最后一行。最后一行显示了2个变量,即毫升和液体盎司,我如何在2个开关语句中放入2个变量?或者我如何使最后一行以与测试运行相同的方式显示。
提前谢谢
答案 0 :(得分:0)
我对你提出的确切问题感到有些困惑,但你可以在交换机中嵌套一个开关。
void printOutput(int metricUnit, int englishUnit, int convert, double converted)
{
switch (metricUnit)
{
case 1: printf("%lf milliliter(s) = ", (double)convert);
switch (englishUnit)
{
case 1: printf("%lf fluid ounce(s)\n", (double)converted); break;
case 2: printf("%lf cup(s)\n", (double)converted); break;
case 3: printf("%lf pint(s)\n", (double)converted); break;
case 4: printf("%lf quart(s)\n", (double)converted); break;
case 5: printf("%lf gallons(s)\n", (double)converted); break;
}
break;
case 2: printf("%lf centiliter(s) = ", (double)convert);
switch (englishUnit)
{
case 1: printf("%lf fluid ounce(s)\n", (double)converted); break;
case 2: printf("%lf cup(s)\n", (double)converted); break;
case 3: printf("%lf pint(s)\n", (double)converted); break;
case 4: printf("%lf quart(s)\n", (double)converted); break;
case 5: printf("%lf gallons(s)\n", (double)converted); break;
}
break;
case 3: printf("%lf deciliter(s) = ", (double)convert);
switch (englishUnit)
{
case 1: printf("%lf fluid ounce(s)\n", (double)converted); break;
case 2: printf("%lf cup(s)\n", (double)converted); break;
case 3: printf("%lf pint(s)\n", (double)converted); break;
case 4: printf("%lf quart(s)\n", (double)converted); break;
case 5: printf("%lf gallons(s)\n", (double)converted); break;
}
break;
case 4: printf("%lf liter(s) = ", (double)convert);
switch (englishUnit)
{
case 1: printf("%lf fluid ounce(s)\n", (double)converted); break;
case 2: printf("%lf cup(s)\n", (double)converted); break;
case 3: printf("%lf pint(s)\n", (double)converted); break;
case 4: printf("%lf quart(s)\n", (double)converted); break;
case 5: printf("%lf gallons(s)\n", (double)converted); break;
}
break;
default: printf("%lf milliliter(s) = ", (double)convert);
switch (englishUnit)
{
case 1: printf("%lf fluid ounce(s)\n", (double)converted); break;
case 2: printf("%lf cup(s)\n", (double)converted); break;
case 3: printf("%lf pint(s)\n", (double)converted); break;
case 4: printf("%lf quart(s)\n", (double)converted); break;
case 5: printf("%lf gallons(s)\n", (double)converted); break;
}
break;
}
}
但是如果可以的话,这种方法有点费力,我会将englishUnit和metricUnit更改为数组,这样就可以用一个printf()打印输出。当然,您必须重写几乎所有程序。
char metricUnit[20];
char englishUnit[20];
printf("%lf %s(s) = %lf %s(s)\n", (double)convert, metricUnit, (double)converted, enlgishUnit);