Ejemplo n.º 1
0
def createGraphProcess(functions, domain, gRange):
    # Format the expressions so they are ready to be graphed
    funcArg = [GraphUtils.formatExpression(f) for f in functions]
    funcArg = GraphUtils.replaceCalls(funcArg)
    funcArg = [GraphUtils.nestFunctions(e[1], funcArg[:e[0]] + funcArg[e[0] + 1:]) for e in enumerate(funcArg)]

    # Open the graphing subprocess
    return subprocess.Popen(["python", sys.path[1] + "/Graphing.py", str(len(funcArg))] + funcArg + [domain,gRange])
Ejemplo n.º 2
0
    def inputExpression():
        while 1:
            while 1:
                print "\nPlease enter expression to evaluate [OR] q to return to the main menu"
                expression = raw_input(">> ").strip()
                if not expression:
                    continue
                break

            if expression.lower() == "q":
                return None

            formatted = GraphUtils.formatExpression(expression)
            if not GraphUtils.validateFunction(formatted):
                print "The expression entered has unmatched brackets.\nPlease try again.\n"
                continue
            return expression
Ejemplo n.º 3
0
    def updateResults(rng):
        formatted = map(lambda frame: GraphUtils.formatExpression(frame[6].get()), fList[rng[0]: rng[1]])
        replaced = GraphUtils.replaceCalls(formatted)
        for i in range(len(replaced)):
            replaced[i] = GraphUtils.nestFunctions(replaced[i], replaced[:i] + replaced[i + 1:])
        for i in range(len(replaced)):
            frame = fList[i + rng[0]]
            eq = replaced[i]

            # Calculate the result of the entered equation
            result = MathCalc.readEquation(GraphUtils.removeDependant(eq))
            resultType = type(result)
            types = [int, long, float]

            # Display the result of calculation below the text box
            if resultType in types:
                frame[7].set("= " + str(result))
                frame[3].grid(columnspan=1000)
            else:
                frame[3].grid_forget()
Ejemplo n.º 4
0
def textInterface():
    # Get user input for expression
    def inputExpression():
        while 1:
            while 1:
                print "\nPlease enter expression to evaluate [OR] q to return to the main menu"
                expression = raw_input(">> ").strip()
                if not expression:
                    continue
                break

            if expression.lower() == "q":
                return None

            formatted = GraphUtils.formatExpression(expression)
            if not GraphUtils.validateFunction(formatted):
                print "The expression entered has unmatched brackets.\nPlease try again.\n"
                continue
            return expression

    # Menu options
    options = ["Evaluate an expression", "Set domain and range", "View a function in text",
                "View a function in the graph window", "Perform calculations from a text file",
                "Exit the program"]
    
    domain, rnge = [-10, 10], [-7.5, 7.5]
    
    # Pattern to match floats
    floatPattern = r"(\-|\+)?[0-9]+(\.[0-9]*)?"

    currentDom, currentRange = "%s,%s" % (str(domain[0]), str(domain[1])), "%s,%s" % (str(rnge[0]), str(rnge[1]))
    graphProcess = None
    running = True
    while running:
        # Display options to the user
        print "What would you like to do?"
        for i, option in enumerate(options):
            print "  %d. %s" % (i + 1, option)

        choice = raw_input(">> ").strip()
        if choice == "1":
            expression = inputExpression()
            if expression == None:
                continue
            formatted = GraphUtils.formatExpression(expression)
            result = MathCalc.readEquation(formatted)
            if result == None:
                print "\nSorry the expression was not evaluated successfully.\n"
            else:
                print "\nResult: %s\n" % str(result)

        elif choice == "2":
            # Display the current domain and range to user
            print "\nCurrent domain and range:"
            print "%s <= x <= %s  %s <= y <= %s\n" % (str(domain[0]), str(domain[1]), str(rnge[0]), str(rnge[1]))

            # Domain entry
            while 1:
                print "Please input the domain [OR] nothing to not alter it"
                print "Format: 'lowerX,upperX'"
                inp = raw_input(">> ").strip()
                if not inp: break

                # Match the user entered domain
                match = re.search(r"(%s) *\, *(%s)" % (floatPattern, floatPattern), inp)
                if match == None:
                    print "Invalid input please try again.\n"
                    continue

                domain = map(lambda s: float(s.strip()), match.group().split(","))
                currentDom = "%s,%s" % (str(domain[0]), str(domain[1]))
                break
            print

            # Range entry
            while 1:
                print "Please input the range [OR] nothing to not alter it"
                print "Format: 'lowerY,upperY'"
                inp = raw_input(">> ").strip()
                if not inp: break

                # Match the user entered range
                match = re.search(r"(%s) *\, *(%s)" % (floatPattern, floatPattern), inp)
                if match == None:
                    print "Invalid input please try again.\n"
                    continue
                    
                rnge = map(lambda s: float(s.strip()), match.group().split(","))
                currentRange = "%s,%s" % (str(rnge[0]), str(rnge[1]))
                break
            print

        elif choice == "3":
            expression = inputExpression()
            if not expression:
                continue
            
            # Format the entered expression and draw the text graph
            expression = GraphUtils.formatExpression(expression)
            Graphing.commandLineDraw(expression, domain, rnge)

        elif choice == "4":
            expression = inputExpression()
            if expression == None:
                continue

            if graphProcess != None:
                if graphProcess.poll() == None:
                    # Terminate previous graphing process
                    graphProcess.terminate()
                    graphProcess = createGraphProcess([expression], currentDom, currentRange)
                else:
                    graphProcess = createGraphProcess([expression], currentDom, currentRange)
            else:
                graphProcess = createGraphProcess([expression], currentDom, currentRange)
            print
        elif choice == "5":
            print
            while 1:
                print "Please input the path to the file [OR] q to return to the main menu"
                inp = raw_input(">> ")
                if inp.lower() == "q":
                    break
                try:
                    fileContents = CLineReadFile.readFile(inp)
                except:
                    print "Sorry", inp, "cannot be found or opened.\n"
                    continue
                expressions = CLineReadFile.parseContents(fileContents)
                results = CLineReadFile.evalExpressions(expressions)

                # Display results
                print "============Results============"
                CLineReadFile.printResults(results)
                break
            print

        elif choice == "6":
            print "Goodbye!"
            running = False