sphinx2001

Таблица значений функций

Apr 14th, 2021
530
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from math import sqrt
  2.  
  3. class SqrtFun:
  4.   def call(self, x):
  5.     return sqrt(x)
  6.  
  7. class Const:
  8.   def __init__(self, value):
  9.     self.value = value
  10.  
  11.     def call(self, x):
  12.       return self.value
  13.  
  14. class Identity:
  15.   def call(self, x):
  16.     return x
  17.  
  18. class Expression:
  19.   def __init__(self, f1, op, f2):
  20.     self.f1 = f1
  21.     self.op = op
  22.     self.f2 = f2
  23.  
  24.   def call(self, x):
  25.     a = self.f1.call(x)
  26.     b = self.f2.call(x)
  27.     if self.op == '+':
  28.       return a + b
  29.     elif self.op == '-':
  30.       return a - b
  31.     elif self.op == '*':
  32.       return a * b
  33.     elif self.op  == '/':
  34.       return a / b
  35.  
  36. def main():
  37.   functions = {'x': Identity(), 'sqrt_fun': SqrtFun()}
  38.   n = int(input())
  39.   for _ in range(n):
  40.     command, *args = input().split()
  41.     if command == 'define':
  42.       name, f1, op, f2 = args
  43.       if f1.isdigit() or (f1[0] == '-' and f1[1:].isdigit()):
  44.         f1 = Const(int(f1))
  45.       else:
  46.         f1 = functions[f1]
  47.       if f2.isdigit() or (f2[0] == '-' and f2[1:].isdigit()):
  48.         f2 = Const(int(f2))
  49.       else:
  50.         f2 = functions[f2]
  51.       functions[name] = Expression(f1, op, f2)
  52.     elif command == 'calculate':
  53.       name, *x_values = args
  54.       print(' '.join(str(functions[name].call(int(x))) for x in x_values))
  55.  
  56. if __name__ == '__main__':
  57.   main()
  58.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×