Git Product home page Git Product logo

refactor's Introduction

Refactor

Introduction

This is the code I wrote for the problem "Expression Tree" on Judgegirl. Here is the link to the question, and I also save the problem description in 65.md for easier access. I'll gradually refactor the code using the techniques taught in class (CSIE1211).

Solution Idea

I can use a stack to store the prefix expression and evaluate it when needed. Luckily, the given expressions have parenthesis, making them easy to parse. I can read the expression from left to right and if I see a (, I can just ignore it. If I see a ), I know I can take out three items (two operands and one operator) to evaluate then push the result to stack. Otherwise, simply push the item to the stack. In the end, the only remaining item in the stack is the answer.

Structure

There are three classes, Item, VariableSet, and Statements.

Item is for the item stored in the stack, whether it be an integer, operator, or variable. I also implement arithmetic in it, since I think it's like an operator takes two operands to operate.

VariableSet is for storing the name and value of the variables, and a method to find them.

Statements is for storing and running the expression. Since the input is separated by spaces, I use stringstream to separate them.

Testing

To check whether my code is correct, I wrote a shell script to download test data and another script to compare the output of my code with the answer. I can run ./get 65 to download test data for this problem and ./test to test the code.

refactor's People

Contributors

leo123457 avatar

Watchers

 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.