Skip to content

Instantly share code, notes, and snippets.

@HarryZ10
Last active March 18, 2022 04:00
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 HarryZ10/a8f640b52ae3c499648350d208a5a954 to your computer and use it in GitHub Desktop.
Save HarryZ10/a8f640b52ae3c499648350d208a5a954 to your computer and use it in GitHub Desktop.
CSIS 312: Lab 3
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
// Implement Temperature conversion from Celsius to Fahrenheit and Fahrenheit to Celsius
int main(int argc, char const *argv[])
{
double celsius, fahrenheit;
char choice;
cout << "Enter the temperature in Celsius: ";
cin >> celsius;
cout << "Enter the temperature in Fahrenheit: ";
cin >> fahrenheit;
cout << "Enter the conversion type (C to F or F to C): ";
cin >> choice;
if (choice == 'C' || choice == 'c')
{
fahrenheit = (celsius * 9 / 5) + 32;
cout << "The temperature in Fahrenheit is: " << fahrenheit << endl;
}
else if (choice == 'F' || choice == 'f')
{
celsius = (fahrenheit - 32) * 5 / 9;
cout << "The temperature in Celsius is: " << celsius << endl;
}
else
{
cout << "Invalid choice" << endl;
}
return 0;
}
public class Main {
/**
* Convert from F to C and C to F using --f or --c
*/
public static void main(String[] args) {
// If args is empty, exit
if (args.length == 0) {
System.exit(-1);
}
// If args[0] is --f, convert from F to C
if (args[0].equals("--f")) {
if (args.length == 2) {
System.out.println("F: " + args[1]);
System.out.println("C: " + convertFtoC(Double.parseDouble(args[1])));
} else {
System.out.println("Invalid number of arguments");
}
}
// If args[0] is --c, convert from C to F
if (args[0].equals("--c")) {
if (args.length == 2) {
System.out.println("C: " + args[1]);
System.out.println("F: " + convertCtoF(Double.parseDouble(args[1])));
} else {
System.out.println("Invalid number of arguments");
}
}
}
/**
* Convert from F to C
*/
public static double convertFtoC(double f) {
return (f - 32f) * 5f / 9f;
}
/**
* Convert from C to F
*/
public static double convertCtoF(double c) {
return (c * 9f / 5f) + 32f;
}
}
import sys
def ftoc(val):
return float((val - 32) * 0.5556)
def ctof(val):
return float((val * float(1.8)) + float(32))
def main():
args = sys.argv[1:]
if len(args) != 0:
if args[0] == "-f":
print(f"C:{ftoc(float(args[1]))}")
print(f"F:{args[1]}")
elif args[0] == "-c":
print(f"F: {ctof(float(args[1]))}")
print(f"C: {args[1]}")
else:
print("No flags provided.")
else:
print("No args provided")
main()
#!/bin/bash
# temperature convert from celsius to fahrenheit and fahrenheit to celsius
# if there are no arguments, print usage
if [ $# -eq 0 ]
then
# print usage
echo "Usage: $0 [celsius|fahrenheit] [number]"
# exit with error
exit 1
fi
# if the first argument is celsius
if [ $1 = "celsius" ]
then
# convert the second argument to fahrenheit
echo "Fahrenheit: $(($2 * 9 / 5 + 32))"
elif [ $1 = "fahrenheit" ]
then
# convert the second argument to celsius
let var=$2-32
echo "Celsius: $(($var * 5 / 9))"
else
# print usage
echo "Usage: $0 [celsius|fahrenheit] [number]"
# exit with error
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment