Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues
Last active July 8, 2016 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CraigRodrigues/5026e4bc2a80bb66d1cd0be1bcc4a542 to your computer and use it in GitHub Desktop.
Save CraigRodrigues/5026e4bc2a80bb66d1cd0be1bcc4a542 to your computer and use it in GitHub Desktop.
[2016-06-27] Challenge #273 [Easy] Getting a degree (with bonus)
/**
* 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;
}
@CraigRodrigues
Copy link
Author

"fk" conversion recursive call doesn't work. Infinite loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment