code:

Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

Terms of Agreement:   

By using this code, you agree to the following terms...   

1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   

2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   

3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 

4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.  

/

//     

/*

File : convert.c

Program : Frogg Temperature Converter

Language : C (but it also works under C++)

Date : 21/11/00

Compiler : Borland Turbo C++ 2.0

Convert temperatures from Fahrenheit, Kelvin, Celsius or Rankine scale.

I see a lot of C/C++ books with the same example: Fahrenheit to Celsius.

I got sick of it and decided to build up a complete program.

The program was designed for you to copy the function convert and use it

in your program, if you want (with a few changes).

The sintaxe of the function is:

int convert(double, int);

where:

double -> you should pass the value of the current temperature

int-> scale in which double is. Valid values:

1 - Celsius

2 - Kelvin

3 - Fahrenheit

4 - Rankine

the function returns 0 on success of 1 if the temperature is invalid.

I made this program so beginners can see how functions works. I tested the

values on the scales, and seems to work fine. if you find any bugs,

contact me by the page where you got this file.

Thanks and HAVE FUN!!!

*/

#include 

int convert(double temp, int op);

    int main() {

    double temp;

    int scale, answer;

    printf("\nFrogg Temperature Converter\n===========================\n");

    printf("\nType in a temperature: ");

    scanf("%lf", &temp);

    printf("\n\nIn which scale is that temperature?\n");

    printf("1.Celsius\n2.Kelvin\n3.Fahrenheit\n4.Rankine");

        do {

        printf("\nOption: ");

        scanf("%d", &scale);

        if ((scale < 1) && (scale > 4))

        printf("Invalid option...");

    } while ((scale < 1) || (scale > 4));

    answer = convert(temp, scale);

        if (answer) {

        printf("Value not accepted.");

        return 1;

    }

    return 0;

}

    int convert(double temp, int op){

    double sca1, sca2, sca3;

    printf("\n\n");

        switch (op) {

        case 1:

        if (temp < -273.15)

          return 1;

        sca1 = temp + 273.15;

        sca2 = (temp * 9)/5 + 32;

        sca3 = (temp * 9)/5 + 492;

        printf("Kelvin: %10.3lf\nFahrenheit: %10.3lf\nRankine: %10.3lf", sca1, sca2, sca3);

        break;

        case 2:

        if (temp < 0)

          return 1;

        sca1 = temp - 273.15;

        sca2 = ((temp - 273.15)*9)/5 + 32;

        sca3 = ((temp - 273.15)*9)/5 + 492;

        printf("Celsius: %10.3lf\nFahrenheit: %10.3lf\nRankine: %10.3lf", sca1, sca2, sca3);

        break;

        case 3:

        if (temp < -459.57)

          return 1;

        sca1 = ((temp - 32)*5)/9;

        sca2 = ((temp - 32)*5)/9 + 273.15;

        sca3 = (temp - 32) + 492;

        printf("Celsius: %10.3lf\nKelvin: %10.3lf\nRankine: %10.3lf", sca1, sca2, sca3);

        break;

        case 4:

        if (temp < 0)

          return 1;

        sca1 = ((temp - 492)*5)/9;

        sca2 = ((temp - 492)*5)/9 + 273.15;

        sca3 = (temp - 492) + 32;

        printf("Celsius: %10.3lf\nKelvin: %10.3lf\nFahrenheit: %10.3lf", sca1, sca2, sca3);

        break;

        default:

        return 1;

        break;

    }

    return 0;

}