diff --git a/math-replay/.gitignore b/math-replay/.gitignore index d9766f8..0038a65 100644 --- a/math-replay/.gitignore +++ b/math-replay/.gitignore @@ -2,3 +2,4 @@ *.log *.pdf *.html +temperatures diff --git a/math-replay/Makefile b/math-replay/Makefile index 918cb2d..d5f6778 100644 --- a/math-replay/Makefile +++ b/math-replay/Makefile @@ -1,5 +1,8 @@ +CFLAGS += -g -Wall -Wextra + ALL := $(patsubst %.haml,%.html,$(shell find . -name '*.haml')) ALL += $(patsubst %.tex,%.pdf,$(shell find . -name '*.tex')) +ALL += temperatures %.html:%.haml diff --git a/math-replay/temperatures.c b/math-replay/temperatures.c new file mode 100644 index 0000000..1e6f375 --- /dev/null +++ b/math-replay/temperatures.c @@ -0,0 +1,47 @@ +#include +#include +#include + + +double c2f(double celsius) { + return ((9.0 / 5.0) * celsius) + 32.0; +} + + +double f2c(double fahrenheit) { + return (5.0 / 9.0) * (fahrenheit - 32.0); +} + + +int die_usage(const char *msg) { + printf("Usage: temperatures [FC]\n"); + printf("%s\n", msg); + exit(1); +} + + +int main(int argc, char * argv[]) { + if (argc < 2) { + die_usage(""); + } + + char msg[255]; + double temp; + const char * temp_string = argv[1]; + + size_t temp_len = strlen(temp_string); + + if (strncmp(temp_string, "F", temp_len) > -1) { + sscanf(temp_string, "F%lf", &temp); + printf("%.2lf\n", f2c(temp)); + } else if (strncmp(temp_string, "C", temp_len) > -1) { + sscanf(temp_string, "C%lf", &temp); + printf("%.2lf\n", c2f(temp)); + } else { + sprintf(msg, "Argument '%s' does not match '[FC]'", + temp_string); + die_usage(msg); + } + + return 0; +}