예제 #1
0
def testNumber():
    nr=Number("100",2)
    assert nr.get_number()=="100"
    assert nr.get_base()==2
    nr.set_number("12")
    assert nr.get_number()=="12"
    nr.set_base(10)
    assert nr.get_base()==10
    nr2=Number("12",10)
    assert nr==nr2
    
    array=nr.digits(nr.get_number())
    assert array==["1","2"]
    
    char='A'
    assert nr.modifyChar(char)==10
    assert nr.stringToInt(char)==10
    
    char="1"
    assert nr.stringToInt(char)==1
예제 #2
0
    def show(self):

        while True:
            print(
                "                                      ~ Monica Olanescu 's application ~"
            )
            print("====Main menu====")
            print("1.Operations")
            print("2.Conversions")
            print("0.Exit\n")
            cmd = input("Give the command:")
            try:
                self.__commValid.validateCommand(cmd, 2)
                cmd = int(cmd)
                if cmd == 0:
                    break
                elif cmd == 1:
                    while True:
                        print("--Operations menu--")
                        print("1.Add")
                        print("2.Decrease")
                        print("3.Multiply with a digit")
                        print("4.Division by a digit")
                        print("0.Back\n")
                        cmd = input("Give the command:")
                        try:
                            self.__commValid.validateCommand(cmd, 4)
                            cmd = int(cmd)
                            if cmd == 0:
                                break
                            elif cmd == 1:
                                print("--Add menu--")
                                print(
                                    "1.Add two numbers in one of number's base"
                                )
                                print("2.Add two numbers in a different base")
                                print("0.Back\n")
                                cmd = input("Give the command:")
                                try:
                                    self.__commValid.validateCommand(cmd, 2)
                                    cmd = int(cmd)
                                    if cmd == 0:
                                        break
                                    elif cmd == 1:
                                        try:
                                            nr = input(
                                                "Give the first number:")
                                            base = input("Give its base:")
                                            nr1 = Number(nr, base)
                                            self.__nrValid.validateNumber(nr1)
                                            nr1.set_base(int(base))
                                            try:
                                                nr = input(
                                                    "Give the another number:")
                                                base = input("Give its base:")
                                                nr2 = Number(nr, base)
                                                self.__nrValid.validateNumber(
                                                    nr2)
                                                nr2.set_base(int(base))
                                                print("The sum is :" +
                                                      self.addTwoNr(nr1, nr2).
                                                      get_number())
                                            except validationExceptions as ex:
                                                print(ex)
                                        except validationExceptions as ex:
                                            print(ex)

                                    elif cmd == 2:
                                        try:
                                            nr = input(
                                                "Give the first number:")
                                            base = input("Give its base:")
                                            nr1 = Number(nr, base)
                                            self.__nrValid.validateNumber(nr1)
                                            nr1.set_base(int(base))
                                            try:
                                                nr = input(
                                                    "Give the second number:")
                                                base = input("Give its base:")
                                                nr2 = Number(nr, base)
                                                self.__nrValid.validateNumber(
                                                    nr2)
                                                nr2.set_base(int(base))
                                                base = input(
                                                    "Give the base on which the operations will be done:"
                                                )
                                                try:
                                                    self.__nrValid.validateBase(
                                                        base)
                                                    base = int(base)
                                                    print("The sum is " + self.
                                                          addTwoNumberDifBase(
                                                              nr1, nr2, base
                                                          ).get_number())
                                                except validationExceptions as ex:
                                                    print(ex)
                                            except validationExceptions as ex:
                                                print(ex)
                                        except validationExceptions as ex:
                                            print(ex)
                                except commandValidationExceptions as ex:
                                    print(ex)

                            elif cmd == 2:
                                while True:
                                    print("--Decrease menu--")
                                    print(
                                        "1.Decrease two numbers in one of number's base"
                                    )
                                    print(
                                        "2.Decrease two numbers in a different base"
                                    )
                                    print("0.Back\n")
                                    cmd = input("Give the command:")
                                    try:
                                        self.__commValid.validateCommand(
                                            cmd, 2)
                                        cmd = int(cmd)
                                        if cmd == 0:
                                            break
                                        elif cmd == 1:
                                            try:
                                                nr = input(
                                                    "Give the first number:")
                                                base = input("Give its base:")
                                                nr1 = Number(nr, base)
                                                self.__nrValid.validateNumber(
                                                    nr1)
                                                nr1.set_base(int(base))
                                                try:
                                                    nr = input(
                                                        "Give the another number:"
                                                    )
                                                    base = input(
                                                        "Give its base:")
                                                    nr2 = Number(nr, base)
                                                    self.__nrValid.validateNumber(
                                                        nr2)
                                                    nr2.set_base(int(base))
                                                    try:
                                                        print(
                                                            "The difference is :"
                                                            + self.decrTwoNr(
                                                                nr1, nr2
                                                            ).get_number())
                                                    except validationExceptions as ex:
                                                        print(ex)
                                                except validationExceptions as ex:
                                                    print(ex)
                                            except validationExceptions as ex:
                                                print(ex)
                                        elif cmd == 2:
                                            try:
                                                nr = input(
                                                    "Give the first number:")
                                                base = input("Give its base:")
                                                nr1 = Number(nr, base)
                                                self.__nrValid.validateNumber(
                                                    nr1)
                                                nr1.set_base(int(base))
                                                try:
                                                    nr = input(
                                                        "Give the second number:"
                                                    )
                                                    base = input(
                                                        "Give its base:")
                                                    nr2 = Number(nr, base)
                                                    self.__nrValid.validateNumber(
                                                        nr2)
                                                    nr2.set_base(int(base))
                                                    base = input(
                                                        "Give the base on which the operations will be done:"
                                                    )
                                                    try:
                                                        self.__nrValid.validateBase(
                                                            base)
                                                        base = int(base)
                                                        try:
                                                            self.__nrValid.decreasesValidation(
                                                                nr1.get_number(
                                                                ),
                                                                nr2.get_number(
                                                                ))
                                                            print(
                                                                "The difference is :"
                                                                + self.
                                                                decrTwoNumberDifBase(
                                                                    nr1, nr2,
                                                                    base
                                                                ).get_number())
                                                        except validationExceptions as ex:
                                                            print(ex)
                                                    except validationExceptions as ex:
                                                        print(ex)
                                                except validationExceptions as ex:
                                                    print(ex)
                                            except validationExceptions as ex:
                                                print(ex)
                                    except commandValidationExceptions as ex:
                                        print(ex)
                            elif cmd == 3:
                                while True:
                                    print("--Multiply menu--")
                                    print(
                                        "1.Multiply a number with a digit in the number's base"
                                    )
                                    print(
                                        "2.Multiply a number with a digit in a different base"
                                    )
                                    print("0.Back\n")
                                    cmd = input("Give the command:")
                                    try:
                                        self.__commValid.validateCommand(
                                            cmd, 2)
                                        cmd = int(cmd)
                                        if cmd == 0:
                                            break
                                        elif cmd == 1:
                                            try:
                                                nr = input("Give the number:")
                                                base = input("Give its base:")
                                                nr = Number(nr, base)
                                                self.__nrValid.validateNumber(
                                                    nr)
                                                nr.set_base(int(base))
                                                try:
                                                    digit = input(
                                                        "Give the digit:")
                                                    self.__nrValid.validateDigitB(
                                                        digit, base)
                                                    print("The product is :" +
                                                          self.multiplyD(
                                                              nr, digit
                                                          ).get_number())
                                                except validationExceptions as ex:
                                                    print(ex)

                                            except validationExceptions as ex:
                                                print(ex)
                                        elif cmd == 2:
                                            try:
                                                nr = input("Give the number:")
                                                base = input("Give its base:")
                                                nr = Number(nr, base)
                                                self.__nrValid.validateNumber(
                                                    nr)
                                                nr.set_base(int(base))
                                                try:
                                                    digit = input(
                                                        "Give the digit:")
                                                    self.__nrValid.validateDigit(
                                                        digit)
                                                    base = input(
                                                        "Give the base on which the operations will be done:"
                                                    )
                                                    try:
                                                        self.__nrValid.validateBase(
                                                            base)
                                                        base = int(base)
                                                        print(
                                                            "The product is :"
                                                            +
                                                            self.multiplyDiff(
                                                                nr, digit, base
                                                            ).get_number())
                                                    except validationExceptions as ex:
                                                        print(ex)

                                                except validationExceptions as ex:
                                                    print(ex)

                                            except validationExceptions as ex:
                                                print(ex)
                                    except commandValidationExceptions as ex:
                                        print(ex)

                            elif cmd == 4:
                                while True:
                                    print("--Division menu--")
                                    print(
                                        "1.Divide a number with a digit in the number's base"
                                    )
                                    print(
                                        "2.Divide a number with a digit in a different base"
                                    )
                                    print("0.Back\n")
                                    cmd = input("Give the command:")
                                    try:
                                        self.__commValid.validateCommand(
                                            cmd, 2)
                                        cmd = int(cmd)
                                        if cmd == 0:
                                            break
                                        elif cmd == 1:
                                            try:
                                                nr = input("Give the number:")
                                                base = input("Give its base:")
                                                nr = Number(nr, base)
                                                self.__nrValid.validateNumber(
                                                    nr)
                                                nr.set_base(int(base))
                                                try:
                                                    digit = input(
                                                        "Give the digit:")
                                                    self.__nrValid.validateDigitB(
                                                        digit, base)
                                                    q, r = self.divideD(
                                                        nr, digit)
                                                    print(
                                                        "The quotient is :" +
                                                        q.get_number() +
                                                        " and the remainder:" +
                                                        r + ".\n")
                                                except validationExceptions as ex:
                                                    print(ex)

                                            except validationExceptions as ex:
                                                print(ex)
                                        elif cmd == 2:
                                            try:
                                                nr = input("Give the number:")
                                                base = input("Give its base:")
                                                nr = Number(nr, base)
                                                self.__nrValid.validateNumber(
                                                    nr)
                                                nr.set_base(int(base))
                                                try:
                                                    digit = input(
                                                        "Give the digit:")
                                                    self.__nrValid.validateDigit(
                                                        digit)
                                                    base = input(
                                                        "Give the base on which the operations will be done:"
                                                    )
                                                    try:
                                                        self.__nrValid.validateBase(
                                                            base)
                                                        base = int(base)
                                                        q, r = self.divideDDiff(
                                                            nr, digit, base)
                                                        print(
                                                            "The quotient is :"
                                                            + q.get_number() +
                                                            " and the remainder:"
                                                            + r + ".\n")
                                                    except validationExceptions as ex:
                                                        print(ex)

                                                except validationExceptions as ex:
                                                    print(ex)

                                            except validationExceptions as ex:
                                                print(ex)
                                    except commandValidationExceptions as ex:
                                        print(ex)
                        except commandValidationExceptions as ex:
                            print(ex)

                elif cmd == 2:
                    while True:
                        print("--Conversions menu--")
                        print("1.Successive division method")
                        print("2.Substitution method")
                        print("3.Fast conversions between 2,4,16 base")
                        print("4.Conversions with an intermediary base")
                        print("0.Back\n")
                        cmd = input("Give the command:")
                        try:
                            self.__commValid.validateCommand(cmd, 4)
                            cmd = int(cmd)
                            if cmd == 0:
                                break
                            elif cmd == 1:
                                print(
                                    "Successive division method works better when the number's base is bigger than the base on which we want to convert!"
                                )
                                try:
                                    nr = input("Give the number:")
                                    base = input("Give its base:")
                                    nr = Number(nr, base)
                                    self.__nrValid.validateNumber(nr)
                                    nr.set_base(int(base))
                                    try:
                                        base = input(
                                            "Give the base in which the number will be converted:"
                                        )
                                        self.__nrValid.validateBase(base)
                                        base = int(base)
                                        self.__methValid.validateDivision(
                                            nr, base)
                                        print("The converted number is " +
                                              self.convertDiv(
                                                  nr, base).get_number() +
                                              "!\n")
                                    except methodExceptions as ex:
                                        print(ex)
                                    except validationExceptions as ex:
                                        print(ex)

                                except validationExceptions as ex:
                                    print(ex)

                            elif cmd == 2:
                                print(
                                    "Substitution method works better when the number's base is bigger than the base on which we want to convert!"
                                )
                                try:
                                    nr = input("Give the number:")
                                    base = input("Give its base:")
                                    nr = Number(nr, base)
                                    self.__nrValid.validateNumber(nr)
                                    nr.set_base(int(base))
                                    try:
                                        base = input(
                                            "Give the base in which the number will be converted:"
                                        )
                                        self.__nrValid.validateBase(base)
                                        base = int(base)
                                        self.__methValid.validateSubstition(
                                            nr, base)
                                        print("The converted number is " +
                                              self.convertSubstitution(
                                                  nr, base).get_number() +
                                              "!\n")
                                    except validationExceptions as ex:
                                        print(ex)
                                    except methodExceptions as ex:
                                        print(ex)
                                except validationExceptions as ex:
                                    print(ex)

                            elif cmd == 3:
                                print(
                                    "Fast conversions works only between 2,4,8 and 16 base!"
                                )
                                print(
                                    "Take care to have every time a base as a power to the other one!\n Ex: 4=2^2 ; 8=2^3 ;16=4^2 \n "
                                )
                                nr = input("Give the number:")
                                base = input("Give the base:")
                                nr = Number(nr, base)
                                try:
                                    self.__nrValid.validateNumber(nr)
                                    base = int(base)
                                    nr.set_base(base)
                                    try:
                                        base = input(
                                            "Give the base in which the number will be converted:"
                                        )
                                        self.__nrValid.validateBase(base)
                                        base = int(base)
                                        self.__methValid.validateBasesFast(
                                            base, nr.get_base())
                                        self.__methValid.validateFast(base)
                                        print("The converted number is " +
                                              self.fastConversions(
                                                  nr, base).get_number() +
                                              "!\n")
                                    except validationExceptions as ex:
                                        print(ex)
                                except validationExceptions as ex:
                                    print(ex)
                                except methodExceptions as ex:
                                    print(ex)
                            elif cmd == 4:
                                nr = input("Give the number:")
                                base = input("Give its base:")
                                nr = Number(nr, base)
                                try:
                                    self.__nrValid.validateNumber(nr)
                                    base = int(base)
                                    nr.set_base(base)
                                    try:
                                        intermediary = input(
                                            "Give the intermediary base:")
                                        self.__nrValid.validateBase(
                                            intermediary)
                                        intermediary = int(intermediary)
                                        base = input(
                                            "Give the base in which the number will be converted:"
                                        )
                                        self.__nrValid.validateBase(base)
                                        base = int(base)
                                        intermediaryNumber, finalNumber = self.conversionsIntermediaryBase(
                                            nr, intermediary, base)
                                        print(
                                            "The converted number in intermediary base is: "
                                            + intermediaryNumber.get_number() +
                                            "!")
                                        print("The converted number is " +
                                              finalNumber.get_number() + "!\n")
                                    except validationExceptions as ex:
                                        print(ex)
                                except validationExceptions as ex:
                                    print(ex)
                                except methodExceptions as ex:
                                    print(ex)
                        except commandValidationExceptions as ex:
                            print(ex)
            except commandValidationExceptions as ex:
                print(ex)