0
# Welcome To Smart Calculator with AI v1.0.
# Please try this code on PC COMMAND PROMPT OR 'PyCharm' for Better Experience (Recommended).
# Please Leave Comment if anything is wrong in this code OR You have any implementation idea.
# Like if You Enjoy the code : By KEYUR SURELIYA.

# 're' : Use to check whether the string contains the specific characters or not.
# Default Return Value : True
import re

class AdvancedCalc:
    # Defining Variables.
    __x = ""  # This is self.__x Number User Enters.
    __y = ""  # This is self.__y Number User Enters.
    __equation = 0  # This will get entire equation.
    __result = 0  # Result will store here.

    # This is initialization Function.
    def __init__(self):
        print("\n....Welcome to Advanced Calculator v2.0.....\n")
        print('Equation Examples (Possibilities) :-')
        print(' -> With Space :-')
        print('  1. \'1 + 1\'')
        print('  2. \'-1 + -1 \'')
        print('  3. \'1.1 + 1.1 \'')
        print('  4. \'-1.1 + -1.1 \'')
        print(' -> Without Space :-')
        print('  1. \'1+1 \'')
        print('  2. \'-1+-1 \'')
        print('  3. \'1.1+1.1 \'')
        print('  4. \'-1.1+-1.1 \'')
        print(' -> One Sided Space :-')
        print('  1. \'1 +1 \'')
        print('  2. \'1+ 1 \'')
        print('  3. \'-1 +-1 \'')
        print('  4. \'-1+ -1 \'')
        print('  5. \'1.1 + 1.1 \'')
        print('  6. \'1.1+ 1.1 \'')
        print('  7. \'-1.1 +-1.1 \'')
        print('  8. \'-1.1+ -1.1 \'')
        print('Type \'Exit\' to Exit from Program.')
        # This function will get

    # Fetching OR Get Input from User.
    def fetchvalues(self, val):
        self.__equation = input("\nEnter The Equation Here : ")

        if not self.__equation == "exit" or self.__equation == "Exit" or self.__equation == "EXIT":
            # This Conditions & Regular Expression is Exist Only Here.
            while not re.fullmatch('([\-]?\d+[\.]?\d+)(\s?)(\+)?(\-)?(\*)?(\/)?(\s?)([\-]?\d+[\.]?\d+)',
                                   self.__equation):
                if self.__equation == "exit" or self.__equation == "Exit" or self.__equation == "EXIT":
                    print("\nThank You For Visiting...!")
                    exit()
                else:
                    self.__equation = input("Please Enter Valid Equation Here : ")
        else:
            # This will exit the program.
            print("\nThank You For Visiting...!")
            exit()

    # Calculate the Values as User needs.
    def calculate_the_values(self):
        # Get The Equation from User.
        self.fetchvalues(0)

        # Calculate Values Here

        # Get The First Number out of Equation.
        self.__x = re.findall('([\-]?\d+[\.]?\d+)(?:\s?)(?:\+)?(?:\-)?(?:\*)?(?:\/)?(?:\s?)(?:[\-]?\d+[\.]?\d+)',self.__equation)

        # Get The Second Number out of Equation.
        self.__y = re.findall('(?:[\-]?\d+[\.]?\d+)(?:\s)?(?:\+)?(?:\-)?(?:\*)?(?:\/)?(?:\s)?([\+\-]?\d+[\.]?\d+)',self.__equation)

        # Get the Operator out of Equation.
        operator = re.findall('(?:[\-]?\d+[\.]?\d+)(?:\s)?(\+)?(\-)?(\*)?(\/)?(?:\s)?(?:[\-]?\d+[\.]?\d+)',self.__equation)

        # Find The Operator And Do The Math.
        for x in operator[len(operator) - 1]:
            if x == "+":
                self.__result = float(self.__x[0]) + float(self.__y[0])
                break
            elif x == "-":
                self.__result = float(self.__x[0]) + float(self.__y[0])
                break
            elif x == "*":
                self.__result = float(self.__x[0]) * float(self.__y[0])
                break
            elif x == "/":
                self.__result = float(self.__x[0]) / float(self.__y[0])
                break

        # Return The Result Value.
        return self.__result

# Now we call the function here to see this working.
Answer = AdvancedCalc().calculate_the_values().__float__()
print("\nAnswer is : %0.2f" % Answer)

# This will ask user if he/she want to run this again.
choise = input('\nDo you want to Start Again? (Y/N) : ')

# This is to make sure that user inputs only 'Y'/'y'/'n'/N' keys.
while not re.search("[Y][y][n][N]", choise):

    # If user says yes then we will run this.
    if choise == "y" or choise == "Y":
        # NOTE : This Loop will continue Until you Select Exit Option.
        Answer = AdvancedCalc().calculate_the_values().__float__()
        print("\nAnswer is : %0.2f" % Answer)
        choise = input('\nDo you want to Start Again? (Y/N) : ')

    # If User says No then we will say goodbye.
    else:
        print('\nThank You For Visiting...!')
        break

# Complete v1.0.
2
Contributors
1
Reply
18
Views
8 Months
Discussion Span
Last Post by ddanbe
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.