示例#1
0
def showMenu():

    mytree = BST()
    answer = 0

    contacto1 = Contact("Maria", 0)
    contacto2 = Contact("Pedro", -2)
    contacto3 = Contact("Juan", -3)
    contacto4 = Contact("Carlos", -1)
    contacto5 = Contact("Jose", 2)
    contacto6 = Contact("Jazmin", 1)
    contacto7 = Contact("Daniel", 3)

    mytree.add(contacto1)
    mytree.add(contacto2)
    mytree.add(contacto3)
    mytree.add(contacto4)
    mytree.add(contacto5)
    mytree.add(contacto6)
    mytree.add(contacto7)
    '''
    mytree.searchContact(0)
    mytree.remove(123)
    mytree.remove(0)
    mytree.searchContact(0)
    mytree.searchContact(3)
    '''

    while (answer != 4):
        print("\n-------MENU-------")
        print(
            "1. Añadir Contacto\n2. Buscar Contacto\n3. Eliminar Contacto\n4. Salir\n"
        )
        answer = int(input("Escoge una opcion\n"))

        if (answer == 1):
            name = input("\nAgregar nombre del contacto que desea agregar\n")
            number = int(input("\nAgregar numero del usuario\n"))
            newContact = Contact(name, number)
            mytree.add(newContact)

        elif (answer == 2):
            number = int(
                input("\nAgregar numero del usario para hacer la busqueda\n"))
            mytree.searchContact(number)

        elif (answer == 3):
            number = int(
                input("\nAgregar numero del usario que desea eliminar\n"))
            mytree.remove(number)

        elif (answer == 4):
            pass

        else:
            print("\nOpcion no disponible\n")
示例#2
0
class Scanner:
    def __init__(self, fileName):
        self.tokens = []
        self.pif = []
        self.st = BST()
        self.fileName = fileName
        self.pifout = open("pif.txt", 'w')
        self.stout = open("st.txt", 'w')

    def readTokens(self):
        f = open("tokens")
        f.readline()
        f.readline()
        for line in f:
            line = line.split()
            self.tokens.append(line[0])

    def isIdentifier(self, token):
        x = re.findall("^[a-z]+[0-9]*$", token)
        if len(x):
            return True
        return False

    def isReservedWord(self, token):
        try:
            idx = self.tokens.index(token)
        except ValueError:
            return False
        if idx >= 22:
            return True
        return False

    def isOperator(self, token):
        try:
            idx = self.tokens.index(token)
        except ValueError:
            return False
        idx = self.tokens.index(token)
        if idx >= 0 and idx <= 12:
            return True
        return False

    def isSeparator(self, token):
        if token == ' ' or token == '\n':
            return True
        try:
            idx = self.tokens.index(token)
        except ValueError:
            return False
        idx = self.tokens.index(token)
        if idx >= 13 and idx <= 21:
            return True
        return False

    def isConstant(self, token):
        x = re.findall("^[\+-]?[0-9]*$", token)
        if len(x):
            return True
        return False

    def genPif(self, token, val):
        self.pifout.write(token + " " + str(val) + '\n')

    def run(self):
        f = open(self.fileName)
        self.readTokens()
        file = f.read()
        lastSep = -1
        #         for i in range(len(file)):
        #             if not file[i].isalpha() and not file[i].isdigit():
        #                 token = file[lastSep + 1 : i]
        #                 lastSep = i
        file.replace('\n', ' ')
        file = file.split()

        for token in file:
            print(token)
            if token == '#':
                token = '#'
            if self.isReservedWord(token) or self.isOperator(
                    token) or self.isSeparator(token):
                self.genPif(token, 0)
            elif self.isConstant(token) or self.isIdentifier(token):
                self.st.add(token)
                idx = self.st.contains(token)
                self.genPif(token, idx)
            else:
                print('Lexical error')

        self.stout.write(str(self.st))