[Include] matheval.inc
#1

matheval

This is a simple library for evaluating mathematical expressions given as string literals.Currently supported operators are:
  • + - addition
  • - - subtraction
  • ^ - exponential
  • / - division
  • * - product
If you have any issues or improvements to be made in this code just open an issue or pull request

Installation

If you are using sampctl just use

sampctl package install Sreyas-Sreelal/matheval

Otherwise just clone the repository

git clone https://www.github.com/sreyas-sreelal/matheval.git

and add matheval.inc to your pawno/includes folder

Usage

There is only one function

MathEval(expression[])

parameters
  • expression[] - mathmatical expression in string literal
returns
  • NaN if failed
  • if success,value in floating point

For example :


Код:
#include<matheval>
main(){
    printf("(1+3)^(32/3)-1 = %f",MathEval("(1+3)^(32/3)-1"));
}
Testing

To run the tests:

sampctl package run

Dependencies
Repository
https://github.com/Sreyas-Sreelal/matheval
Reply
#2

supports spaces thumbs up
good use of git too thumbs up
Reply
#3

Nice.
Reply
#4

Quote:
Originally Posted by Freshncool
Посмотреть сообщение
supports spaces thumbs up
good use of git too thumbs up
Thumbs down for dumbass.

OT: Good job.
Reply
#5

Quote:
Originally Posted by ******
Посмотреть сообщение
Also, the tests don't actually do anything. They just confirm the answer is the same as itself, which will almost always be the case, they don't check that the answer is correct. So if your code evaluated "2+2" as 5, the test would pass, because "5==5"...
Yeah you are absolutely correct.I never did think of that.I will modify the test.
Quote:
Originally Posted by ******
Посмотреть сообщение
But nice use of a stack machine for evaluation. Though I think the final loop would be better just running until `evalTop==1`, then you return `evalStack[0]`, since if there is only one item left, it must be the result.
Edit: Ehh my method was actually scanning and pushing elements in OperandArray, into evalStack and if an operator is reached pop two elements from evalStack and operate and push the result into evalStack.So i think checking if there is one item left on evalstack will return the first element that is pushed and breaks the loop on first iteration.
Reply
#6

Quote:
Originally Posted by Freshncool
Посмотреть сообщение
supports spaces thumbs up
good use of git too thumbs up
This ^ and also really nice method to evaluate.+Rep
Reply
#7

mashallah brother
Reply
#8

Useful and also i can learn a lot about data structures from this.Thanks
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)