如何只输出温度转换的正确答案?

时间:2015-06-03 10:47:58

标签: if-statement printing switch-statement

一个简单的问题,但我已经尝试了几个小时,无法计算最后一点。 我怎样才能轻松地让程序“仅”显示正确的答案Celsius或Farenheit,而不是两者?

int farenheit(int x);
int celcius(int x);

int main()
{
    int temperature, converted, converted2;
    int answer;

    printf("Temperature please\n");
    scanf("%d",&temperature);
    printf ("Enter conversion to be completed F/C\n");
    scanf ("%s",&answer);

    converted=farenheit(temperature);
    converted2=celcius(temperature);

    switch(answer)
    {
        case 'f':
            converted;
            break;  

        case 'c':
            converted2;
            break;

    }   

    getch ();
    return 0;
}

int farenheit(int x)
{
    int answer;
    answer = (x-32) * (float)5/9 ;
    printf ("Temperature in farenheit %d",answer);  
}

int celcius(int x)
{
    int ans;
    ans = ((float)9/5*x) + 32;
    printf ("Temperature in celcius %d",ans);
}

1 个答案:

答案 0 :(得分:0)

#include <stdio.h>
void farenheit(int x);
void celcius(int x);
int main()
{
    int temperature,converted,converted2;
    char answer;
    printf("Temperature please\n");
    scanf("%d\n",&temperature);
    printf ("Enter conversion to be completed F/C\n");
    scanf ("%c",&answer);
    switch(answer)
    {
        case 'f':
        case 'F':
            farenheit(temperature);
            break;  

        case 'c':
        case 'C':
            celcius(temperature);
                break;

    }   
        return 0;
}

void farenheit(int x)
{
    int answer;
    answer= (x-32)*(float)5/9 ;
    printf ("Temperature in farenheit %d\n",answer);  
}

void celcius(int x)
{
    int ans;
    ans=((float)9/5*x)+32;
    printf ("Temperature in celcius %d\n",ans);
}