Last active
July 8, 2016 21:01
-
-
Save CraigRodrigues/5026e4bc2a80bb66d1cd0be1bcc4a542 to your computer and use it in GitHub Desktop.
[2016-06-27] Challenge #273 [Easy] Getting a degree (with bonus)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* degree.c | |
* | |
* Reddit Daily Programmer Challenge #273 [EASY] | |
* Getting a degree | |
* | |
* https://www.reddit.com/r/dailyprogrammer/comments/4q35ip/20160627_challenge_273_easy_getting_a_degree/ | |
* | |
*/ | |
#include <cs50.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <math.h> | |
#define pi 3.141592 | |
char units[2]; | |
float num; | |
float conversion; | |
float convert(float* numbers, char* conversion_units); | |
int main(void) | |
{ | |
while (scanf("%f%s", &num, units) == 2) | |
{ | |
float answer = convert(&num, units); | |
if (answer != -1) | |
printf("%2g%c\n", answer, units[1]); | |
} | |
return 0; | |
} | |
float convert(float* numbers, char* conversion_units) | |
{ | |
if (!strcmp(units,"dr")) // degrees to radians | |
{ | |
return *numbers * (pi / 180.0); | |
} | |
else if (!strcmp(units,"rd")) // radians to degrees | |
{ | |
return *numbers * (180.0 / pi); | |
} | |
else if (!strcmp(units,"cf")) // celsius to farenheit | |
{ | |
return *numbers * 1.8 + 32; | |
} | |
else if (!strcmp(units,"fc")) // farenheit to celsius | |
{ | |
return (*numbers - 32) * 0.5556; | |
} | |
else if (!strcmp(units,"ck")) // celsius to kelvin | |
{ | |
return *numbers + 273.15; | |
} | |
else if (!strcmp(units,"kc")) // kelvin to celsius | |
{ | |
return *numbers - 273.15; | |
} | |
else if (!strcmp(units,"fk")) // farenheit to kelvin | |
{ | |
return ((convert(numbers, "fc")) + 273.15); | |
} | |
else if (!strcmp(units,"kf")) // kelvin to farenheit | |
{ | |
return *numbers * 1.8 - 459.67; | |
} | |
else | |
printf("No candidate for conversion\n"); | |
return -1; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"fk" conversion recursive call doesn't work. Infinite loop.