Advertisement
Guest User

CLI desktop calculator

a guest
Aug 30th, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.46 KB | None | 0 0
  1. def cli():
  2.     print "Welcome to CLI v0.0."
  3.     print "This may be extremely buggy. Enter q to quit."
  4.     stack,functions,prescision=[],{},3
  5.     while True:
  6.         cmd=raw_input('>>')
  7.         if cmd=='':
  8.             pass
  9.         elif cmd.count(':') == 1:
  10.             z=cmd.index(':')
  11.             a,b=cmd[:z],cmd[z+1:]
  12.             m=errorcheck(b,functions)
  13.             if m==1:
  14.                 functions[a]=b
  15.         elif cmd[0]=='p':
  16.             if len(cmd[1:])==0:
  17.                 print "The prescision is currently set to {0}.".format(prescision)
  18.             elif len(filter(lambda x: not x in ' 0123456789',cmd[1:]))==0:
  19.                 prescision=int(cmd[1:])
  20.                 print 'The prescision has been set to {0}.'.format(prescision)
  21.             else:
  22.                 print 'Invalid requested prescision.'
  23.         elif cmd=='q':
  24.             print "Goodbye!"
  25.             break
  26.         elif cmd=='c':
  27.             v=raw_input('Are you sure you want to clear the stack?\n-->')
  28.             if v in ['y','yes']:
  29.                 stack=[]
  30.         else:
  31.             initialstack=stack
  32.             z=run(cmd,stack,prescision,functions)
  33.             if z==0:
  34.                 stack=initialstack
  35.             else:
  36.                 stack=z
  37.         if len(stack)>5:
  38.             print '...'
  39.             for i in range(5):
  40.                 print stack[i+len(stack)-5]
  41.         else:
  42.             for i in stack:
  43.                 print i
  44.  
  45. def run(cmds, stack, prescision, functions):
  46.     l,z=cmds.rsplit(' '),errorcheck(cmds,functions)
  47.     if z==0:
  48.         return 0
  49.     for i in l:
  50.         if stack==0:
  51.             break
  52.         elif i in functions.keys():
  53.             stack = run(functions[i],stack,prescision,functions)
  54.         elif i in '+-*/' or i=='**':
  55.             if len(stack)<2:
  56.                 print 'Stack underflow error'
  57.                 return 0
  58.             else:
  59.                 a=stack.pop()
  60.                 b=stack.pop()
  61.                 stack.append(round(eval(str(b)+i+str(a)),prescision))
  62.         elif i=='$':
  63.             if len(stack)==0:
  64.                 print 'Stack underflow error'
  65.                 return 0
  66.             else:
  67.                 stack.append(stack[-1])
  68.         elif i=='@':
  69.             if len(stack)<3:
  70.                 print 'Stack underflow error'
  71.                 return 0
  72.             else:
  73.                 z=stack[:-3]
  74.                 z.append(stack[-2])
  75.                 z.append(stack[-1])
  76.                 z.append(stack[-3])
  77.                 stack=z
  78.         elif i=='\\':
  79.             if len(stack)<2:
  80.                 print 'Stack underflow error'
  81.                 return 0
  82.             else:
  83.                 a=stack.pop()
  84.                 b=stack.pop()
  85.                 stack.append(a)
  86.                 stack.append(b)
  87.         elif i=='%':
  88.             if len(stack)==0:
  89.                 print 'Stack underflow error'
  90.                 return 0
  91.             else:
  92.                 stack.pop()
  93.         elif len(filter(lambda x: not x in '.0123456789',i))==0:
  94.             stack.append(round(float(i),prescision))
  95.     return stack
  96.  
  97. def errorcheck(cmds, functions):
  98.     l=cmds.rsplit(' ')
  99.     for i in l:
  100.         if i.count('.')>1:
  101.             print 'Invalid decimal placing.'
  102.             return 0
  103.         if not i in '\\$%@+-*/' and i!='**' and not i in functions.keys():
  104.             if len(filter(lambda x: not x in '.0123456789',i))!=0:
  105.                 print "Undefined command {0}".format(i)
  106.                 return 0
  107.     return 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement