2012-11-17 701 views
1
#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, 

此打印輸出是試運行2 switch語句

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

請只提供相關的代碼。哪條線路導致你的問題? –

+1

在printOutput的第一種情況下,您有兩個輸出的格式字符串,但只有一個參數。 –

+0

我不確定哪條線,因爲我不認爲我所有的打印輸出都是正確的,我正在努力將2個變量放在2個switch語句中,正如您在測試運行的最後一句中看到的一樣,我希望我的打印輸出功能輸出類似的句子,但我不知道如何 –

回答

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