/*fahrcels.c (several versions) - use a conditional statement to select one of two calculations. Copyright (c) 1998 Matthew Belmonte.*/ #include int main() { double fahr, cels; int scale; /*'C' for Celsius input, 'F' for Fahrenheit*/ /*get input type*/ printf("Type C to convert Celsius to Fahrenheit, or F to go the other way: "); scale = getchar(); /*get input, and do the appropriate calculation*/ printf("degrees? "); if(scale == 'C') { scanf("%lf", &cels); fahr = 9.0*cels/5.0+32.0; } else { scanf("%lf", &fahr); cels = 5.0*(fahr-32.0)/9.0; } printf("%.3f degrees Fahrenheit is %.3f degrees Celsius\n", fahr, cels); return 0; } /*uses logical OR to handle lowercase*/ #include int main() { double fahr, cels; int scale; /*'C' for Celsius input, 'F' for Fahrenheit*/ /*get input type*/ printf("Type C to convert Celsius to Fahrenheit, or F to go the other way: "); scale = getchar(); /*get input, and do the appropriate calculation*/ printf("degrees? "); if((scale == 'C') || (scale == 'c')) { scanf("%lf", &cels); fahr = 9.0*cels/5.0+32.0; } else { scanf("%lf", &fahr); cels = 5.0*(fahr-32.0)/9.0; } printf("%.3f degrees Fahrenheit is %.3f degrees Celsius\n", fahr, cels); return 0; } /*uses bitwise AND to convert case*/ #include int main() { double fahr, cels; int scale; /*'C' for Celsius input, 'F' for Fahrenheit*/ /*get input type*/ printf("Type C to convert Celsius to Fahrenheit, or F to go the other way: "); scale = getchar()&0x5f; /*get input, and do the appropriate calculation*/ printf("degrees? "); if(scale == 'C') { scanf("%lf", &cels); fahr = 9.0*cels/5.0+32.0; } else { scanf("%lf", &fahr); cels = 5.0*(fahr-32.0)/9.0; } printf("%.3f degrees Fahrenheit is %.3f degrees Celsius\n", fahr, cels); return 0; } /*uses toupper() to convert case*/ #include #include int main() { double fahr, cels; int scale; /*'C' for Celsius input, 'F' for Fahrenheit*/ /*get input type*/ printf("Type C to convert Celsius to Fahrenheit, or F to go the other way: "); scale = toupper(getchar()); /*get input, and do the appropriate calculation*/ printf("degrees? "); if(scale == 'C') { scanf("%lf", &cels); fahr = 9.0*cels/5.0+32.0; } else { scanf("%lf", &fahr); cels = 5.0*(fahr-32.0)/9.0; } printf("%.3f degrees Fahrenheit is %.3f degrees Celsius\n", fahr, cels); return 0; } /*uses a conditional expression as well as a conditional statement*/ #include #include int main() { double fahr, cels; int scale; /*'C' for Celsius input, 'F' for Fahrenheit*/ /*get input type*/ printf("Type C to convert Celsius to Fahrenheit, or F to go the other way: "); scale = toupper(getchar()); /*get input, and do the appropriate calculation*/ printf("degrees? "); scanf("%lf", (scale=='C')? &cels: &fahr); if(scale == 'C') fahr = 9.0*cels/5.0+32.0; else cels = 5.0*(fahr-32.0)/9.0; printf("%.3f degrees Fahrenheit is %.3f degrees Celsius\n", fahr, cels); return 0; } /*uses shorthand operators*/ #include #include int main() { double fahr, cels; int scale; /*'C' for Celsius input, 'F' for Fahrenheit*/ /*get input type*/ printf("Type C to convert Celsius to Fahrenheit, or F to go the other way: "); scale = getchar(); scale &= 0x5f; /*get input, and do the appropriate calculation*/ printf("degrees? "); scanf("%lf", (scale=='C')? &cels: &fahr); if(scale == 'C') { fahr = cels; fahr *= 9.0/5.0; fahr += 32.0; } else { cels = fahr; cels -= 32.0; cels *= 5.0/9.0; } printf("%.3f degrees Fahrenheit is %.3f degrees Celsius\n", fahr, cels); return 0; } /*THIS ONE IS FULL OF BUGS: FIND ALL OF THEM, WITHOUT LOOKING BACK!*/ #include int main(); { double fahr, cels; char scale; printf("Type C to convert Celsius to Fahrenheit, or F to go the other way: "); scale = toupper(getchar()); printf("degrees? "); scanf("%f", &((scale='C')? cels: fahr)); if(scale = 'C') fahr = 9.0*cels/5.0+32.0; else cels = 5.0 * fahr-32.0 / 9.0; printf("%d degrees Fahrenheit is %d degrees Celsius", fahr, cels); return 0; } /******* ANSWERS: 0. ctype.h not included, so no prototype for toupper() 1. semicolon after function definition 2. getchar() squeezed into a char, instead of an int 3. scanning into a double with a float specifier 4. operand of reference is not an lvalue 5. assignment operator used instead of equality 6. order of operations 7. doubles as arguments for %d conversion 8. no newline 9. no comments *******/