Coder Social home page Coder Social logo

calculator's Introduction

Computes any expression with operations such as addition, subtraction, multiplication, division

Allowed characters:

' ' '.' '+' '-' '/' '*' '(' ')' '0' '1' '2' '3' '4' '5' '6' '7' '8' '9'

Use example

std::string str = "(1+23*51+(((913--13/4+8)*4*16))+-7*-11)";
Result res = Calculator().getResult(str);
if (res.ok)
	std::cout << "result: " << res.value << "\n"; // (double) result number
else
	std::cout << "error: " << res.error << "\n"; // (string) error message

Expression grammar

Expression:
    Term
    Expression "+" Term // addition
    Expression "โ€“" Term // subtraction
Term:
    Primary
    Term "*" Primary    // multiplication
    Term "/" Primary    // division
Primary:
    Number
    "(" Expression ")"  // grouping
Number:
    floating-point-literal

Other examples:

"-1-2" // -3
"2 - -3" // 5
"-2" // -2
"(1+23)" // 24
"(1+2*5+(((91-( -13 / -1 + (23.1*1)-(9/2))+-8+23)*4*16))+7*-11)" // 4695.6
"1.2-2" // -0.8
"(1+23*51+(((913--13/4+8)*4*16))+7*11)" // 60403
"0" // 0
"-12 + 2" // -10
"(123/32)" // 3.84375
"123 - (21 - 12" // "')' expected"
") 123 + 2" // "incorrect input expression"
"-----++++" // "multiple sign"
"123//2" // "incorrect input expression"
" 90 = 9" // "incorrect input expression"
" 90 9" // "incorrect input expression"
"(123123)" // 123123
"()" // "incorrect input expression"
"-2222 + 8888 - (232 *    2)" // 6202
"*" // "incorrect input expression"
"2 * 6 *" // "incorrect input expression"
"3 / (23 - 10 - 13)" // "division by zero"
"3 / (2)" // 1.5
"23.123.32" // "incorrect number format"
"." // "incorrect number format"
".4" // 0.4
"4." // 4
".4" // 0.4
"" // "incorrect input expression"
"12 + 3" // 15
"1 ~ +3" // "incorrect input expression"
"-1-1-" // "incorrect input expression"
"-33-0-0-0" // -33
"-33" // -33
"+33-+-23" // "multiple sign"
"+2" // 2
"-(" // "incorrect input expression"
"+33-0-0" // 33
"-+33" // "multiple sign"
"+-4" // "multiple sign"
"23 - + 34" // -11
"4 + - 34" // -30
"- 2 + 4" // 2
"--2 + 4" // "multiple sign"
" 2 - - 5" // 7
"5 + + 7" // 12
"5++7" // 12
"+5++7" // 12
"-34.+-2" // -36

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.