From 79739befc8e17c2c276da7aebac5e261e4879e34 Mon Sep 17 00:00:00 2001 From: Athena Baches Date: Tue, 7 Feb 2023 21:42:52 +0000 Subject: [PATCH 1/2] added cos function to calculator --- scicalc | 85 --------------------------------------------------------- 1 file changed, 85 deletions(-) delete mode 100755 scicalc diff --git a/scicalc b/scicalc deleted file mode 100755 index 1667b1d..0000000 --- a/scicalc +++ /dev/null @@ -1,85 +0,0 @@ -#!/usr/bin/env python -# -# A simple command-line scientific calculator. -# - -import sys, math - -def die_with_usage(message): - print("\n****\n**** error: {}\n****\n".format(message), file=sys.stderr) - print("usage:\n\n {} [arg1 [arg2 [...]]]\n".format(sys.argv[0]), file=sys.stderr) - print(", where can be one of:\n", file=sys.stderr) - for (op, func) in operators.items(): - print(" {}: {}".format(op, func.__doc__), file=sys.stderr) - print("", file=sys.stderr) - exit(-1) - -# -# The operators. -# -# These functions must take the necessary number of arguments, and return -# the result of the operation. They must have a short docstring explaining -# what they do (it will be printed by the die_with_usage() function). -# -# Exceptions: -# - If an incorrect number of arguments is passed, the function must raise a TypeError. -# - If there's a problem with argument values (e.g., a negative number passed to log), -# the function must raise a ValueError. -# - -def add(*args): - """Add a list of numbers""" - - sum = 0.0 - for arg in args: - sum += arg - return sum - -def mul(*args): - """Multiply a list of numbers""" - - prod = 1.0 - for arg in args: - prod *= arg - return prod - -def log10(x): - """Return a base-10 logarithm of x""" - - return math.log10(x) - -# -# The dictionary that maps the command-line name of the operation, -# to the function that performs it. There can be multiple names -# for the same operator (e.g., 'add' and 'sum'). -# -operators = { - 'add': add, - 'sum': add, - 'mul': mul, - 'log10': log10, -} - -if __name__ == "__main__": - # - # Collect and parse arguments - # - try: - op = sys.argv[1] - args = [ float(arg) for arg in sys.argv[2:] ] - except IndexError: - die_with_usage("Insufficient number of arguments.") - except ValueError as e: - die_with_usage(e) - - # - # Run the requested operation, and print the result - # - try: - print(operators[op](*args)) - except KeyError: - die_with_usage("Unknown operator '{}'.".format(op)) - except TypeError as e: - die_with_usage(e) - except ValueError as e: - die_with_usage(e) From 1e81b8be430adf97ccf94806fff1b1e8a2377c18 Mon Sep 17 00:00:00 2001 From: Athena Baches Date: Wed, 8 Feb 2023 00:41:34 +0000 Subject: [PATCH 2/2] fixed file error --- scicalc | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100755 scicalc diff --git a/scicalc b/scicalc new file mode 100755 index 0000000..e7d9761 --- /dev/null +++ b/scicalc @@ -0,0 +1,94 @@ +#!/usr/bin/env python +# +# A simple command-line scientific calculator. +# + +#Cosine(x)# +import sys, math +import numpy as np + + +def die_with_usage(message): + print("\n****\n**** error: {}\n****\n".format(message), file=sys.stderr) + print("usage:\n\n {} [arg1 [arg2 [...]]]\n".format(sys.argv[0]), file=sys.stderr) + print(", where can be one of:\n", file=sys.stderr) + for (op, func) in operators.items(): + print(" {}: {}".format(op, func.__doc__), file=sys.stderr) + print("", file=sys.stderr) + exit(-1) + +def cos(x): + """Return the value of cos(x)""" + return np.cos(x) + + +# +# The operators. +# +# These functions must take the necessary number of arguments, and return +# the result of the operation. They must have a short docstring explaining +# what they do (it will be printed by the die_with_usage() function). +# +# Exceptions: +# - If an incorrect number of arguments is passed, the function must raise a TypeError. +# - If there's a problem with argument values (e.g., a negative number passed to log), +# the function must raise a ValueError. +# + +def add(*args): + """Add a list of numbers""" + + sum = 0.0 + for arg in args: + sum += arg + return sum + +def mul(*args): + """Multiply a list of numbers""" + + prod = 1.0 + for arg in args: + prod *= arg + return prod + +def log10(x): + """Return a base-10 logarithm of x""" + + return math.log10(x) + +# +# The dictionary that maps the command-line name of the operation, +# to the function that performs it. There can be multiple names +# for the same operator (e.g., 'add' and 'sum'). +# +operators = { + 'add': add, + 'sum': add, + 'mul': mul, + 'log10': log10, + 'cos': cos, +} + +if __name__ == "__main__": + # + # Collect and parse arguments + # + try: + op = sys.argv[1] + args = [ float(arg) for arg in sys.argv[2:] ] + except IndexError: + die_with_usage("Insufficient number of arguments.") + except ValueError as e: + die_with_usage(e) + + # + # Run the requested operation, and print the result + # + try: + print(operators[op](*args)) + except KeyError: + die_with_usage("Unknown operator '{}'.".format(op)) + except TypeError as e: + die_with_usage(e) + except ValueError as e: + die_with_usage(e)