A simple calculator which supports brackets and various operators. Mainly used Shunting-Yard algorithm. You can also add more operators easily.
+(addition)
-(subtraction)
*(multiplication)
/(division)
%(modulo)
^(exponentiation)
!(fractorial)
Imitated the exception handling form of C++ using setjmp
and longjmp
:
TRY(LABEL)
{
THROW(exc);
...
}
CATCH()
{
e=CATCH_var(LABEL);
...
}
Exception definitions:
{1000,"Failed to create the stack."},
{1001,"The bracket does not match."},
{1002,"Invalid expression."},
{1003,"Unexpected operator in converted postfix expression."},
{1004,"Invalid converted postfix expression info."},
{1005,"Invalid converted postfix expression."},
{1006,"The expression is too long.(Longer than 384 characters)"},
{1007,"Failed to allocate memory."},
{1008,"Cannot divide by 0."},
{1009,"Factorial operator cannot operate a negative number."},
{1010,"Factorial operator cannot operate a fraction."}
Just add all files to an project in Visual Studio or other IDEs, then compile.
Use gcc or other compilers compile all files and link the object files.
Enter an expression which only consists of numbers and operators mentioned above, press ENTER.
There is an instance below.
3!+4*2%10/(1-5)^2
Value=6.5
P.S. The comments inside the code files are in Chinese, and the file encoding is UTF8. This is just a beginner's assignment. :)