Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues CraigRodrigues/degree.c
Last active Jul 8, 2016

Embed
What would you like to do?
[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

This comment has been minimized.

Copy link
Owner Author

commented Jul 8, 2016

"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
You can’t perform that action at this time.