Coder Social home page Coder Social logo

rpncalculator's Introduction

RPNCalculator

Functional Programming Assignment 1

This program implements Reverse Polish Notation Calculator which reads the user's input and compute based on RPN's order.

I created a class Calculator which has a compute method gets a new Stack as a parameter. This compute() calls getInput() which returns a string of the user's input, then parse this string by calling parseExpression() which returns a string without characters other than integers, whitespace(" ") and operators. It will now split the parsed string by whitespaces and store it in an array of strings(splittedStr).

At this point, it will test whether splittedstr has an element stored(length will be 0, if the user enters only letters or characters which does not belong to the criteria, checked in parseExpression() and it will just terminate the program with a message of "ERROR: Invalid input"). Otherwise, it will go through the for loop and switch cases that checks each element. When it's done, it will print out the stack that has the operands left which is not yet calculated and the final answer, by simply calling getStack(). It will call the method itself in a recursive way in order to get a new input from the user without losing the elements in the stack. It also uses the Stack's methods: push(), for putting the operands and final answer in the stack and pop(), for getting the elements from the top/last pushed in the stack which will be calculated by the operator.

I also added some simple error checkers/handlers like displaying a string "ERROR: ... ", whenever user tries to enter an invalid input, calculate with only one element in the stack and if the stack is empty.

rpncalculator's People

Contributors

cherose-sem avatar

Watchers

James Cloos 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.