From e83efd3915b411c28307ebcc82b56a1f619c9b74 Mon Sep 17 00:00:00 2001 From: Kato Twofold Date: Thu, 3 Jun 2021 00:29:21 +0300 Subject: [PATCH] + --- .gitignore | 2 ++ README.md | 22 ++++++++++++++++++++++ compile.sh | 1 + main.cpp | 31 +++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 compile.sh create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3e668b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +temp +tempera \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..1d706b4 --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +# About +This is a simple program that can convert temperature from fahrenheit to celsius and vice-versa, the usage is explained bellow and pretty straight-forward. + +The program should be quite light-weighted and easy to use in bigger systems, I personally created it because I Found a need for a program that quickly calculates this for me through CLI for implementation with different scripts and automations, also I found that `C++` is quite a bit faster and much better optimized in doing this than `php` + +# Compilation +To compile the program it's quite straight forward on linux, simply run the `compile.sh` file, which requires the `g++` package, to install it on Debian based systems simply run `sudo apt install g++`. + +# Usage +The package was created expecting pipe-in stdin, you can pipe input straight into it or you can simply run it and type numbers in, simplest usages is: `echo 64 | ./tempera` which will return `17.7778`, you can add this to your `/usr/bin` folder or `/bin` fir ease of use, or simply add it to your path. + +If you wish to convert `Celsius` to `Fahrenheit` simply add in another argument to the program, so when you type `echo 16 | ./tempera 1` it will return `60.8` + +# Examples +```bash +# 🌡 Fahrenheit -> Celsius +echo 64 | ./tempera # Returns 17.7778 + +# 🌡 Celsius -> Fahrenheit +echo 16 | ./tempera 1 # returns 60.8 + +``` \ No newline at end of file diff --git a/compile.sh b/compile.sh new file mode 100644 index 0000000..f760763 --- /dev/null +++ b/compile.sh @@ -0,0 +1 @@ +g++ -o3 main.cpp -o tempera \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..56fed8a --- /dev/null +++ b/main.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +double ftc(double fahrenheit) { + return ( fahrenheit - 32.0 ) * 5.0 / 9.0; +} + +double ctf(double celsius) { + return ( celsius * 9.0 / 5.0 ) + 32; +} + +int main(int argc, char *argv[]) { + + double inp = 0.0; + + // Read input from pipe in + std::cin >> inp; + + // If an argument is passed, then do the reverse ( c -> f instead of f -> c) + if ( argc > 1 ) { + // Cout the temperature in celsius + cout << ctf(inp) << "\n"; + } else { + // Cout the temperature in celsius + cout << ftc(inp) << "\n"; + } + + // Return success to stdout + return 0; +} \ No newline at end of file