Giter VIP home page Giter VIP logo

infix-calculator's Introduction

Infix-Calculator

Algorithm exercise: Module that evaluates a string that contains an arithmetic expression

# Infix Calculator

Welcome to the Infix Calculator project! This Java program allows you to evaluate mathematical expressions written in infix notation. It can handle basic arithmetic operations, parentheses, and negative numbers.

## Table of Contents
- [Introduction](#introduction)
- [Getting Started](#getting-started)
  - [Prerequisites](#prerequisites)
  - [Installation](#installation)
- [Usage](#usage)
- [Examples](#examples)
- [Main Methods](#main-methods)
- [Testing](#testing)
- [Contributing](#contributing)
- [License](#license)

## Introduction

In mathematics, infix notation is a common way of representing mathematical expressions, where operators are placed between operands. The Infix Calculator project takes infix expressions and evaluates them, providing the result.

## Getting Started

### Prerequisites

Make sure you have the following prerequisites installed on your system:

- Java (JDK 8 or higher)

### Installation

1. Clone the repository to your local machine:

   ```bash
   git clone https://github.com/yourusername/infix-calculator.git
  1. Navigate to the project directory:

    cd infix-calculator
  2. Compile the Java code:

    javac EvaluateMathematicalExpression.java

Usage

To use the Infix Calculator, you can create an instance of the EvaluateMathematicalExpression class and call the evaluate method with your mathematical expression as a string. Here's a basic example:

EvaluateMathematicalExpression evaluator = new EvaluateMathematicalExpression();
double result = evaluator.evaluate("5 + 3 * (10 - 2) / 4");
System.out.println("Result: " + result);

Examples

Here are some example expressions you can try:

  • 5 + 3 * (10 - 2) / 4
  • -(4 + 2) * -3
  • 12 * 123 / (-5 + 2)

Main Methods

isOperand(String value)

  • Determines if a given string is a numeric operand.
  • Returns true if the input is a valid numeric value, false otherwise.

isOperator(String value)

  • Checks if a given string is a valid operator.
  • Returns true if the input is one of the supported operators (+, -, *, /), false otherwise.

normalizeElementsTokenizing(String expression)

  • Tokenizes and normalizes a mathematical expression.
  • Returns a list of strings representing the individual elements of the expression with spaces removed and additional spaces added around parentheses and operators.

evaluate(String expression)

  • Evaluates a given infix mathematical expression and returns the result as a double.
  • Takes an infix expression as input and returns the calculated result.

popConnectPush(Stack<ExpressionOperatorNode> opStack, Stack<ExpressionBase> nodeStack, boolean isClosing)

  • Performs the core logic for connecting and pushing operators and nodes.
  • Handles operator precedence, balancing, and parenthesized expressions.
  • Returns true if the operation succeeded, false otherwise.

infixExpressionToTree(String exp)

  • Converts an infix expression into a syntax tree for evaluation.
  • Uses stacks to manage operators and nodes during the conversion.
  • Returns the root of the syntax tree.

Testing

The project includes JUnit tests to ensure the correctness of the calculator. You can run the tests using your preferred IDE or build tool.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.

๐Ÿš€ Happy calculating! ๐Ÿงฎ

infix-calculator's People

Contributors

gmottajr avatar fguerco avatar gerson07jr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.