A simple command line calculator
Examples
C:>calc 5+5*5 30.000000 c:>calc 0x30 48.000000 c:>calc (123456 % 51)/12 3.000000
Thank you again, Steve Hanov!
"Apparently you've forgotten to remove debug output:
C:>calc 10*4
parse_expr()
parse_term()
parse_factor()
parse_num_op()
parse_rest_term()
parse_factor()
parse_num_op()
parse_rest_term()
parse_rest_expr()
40.000000
"
C:>calc 10*4
parse_expr()
parse_term()
parse_factor()
parse_num_op()
parse_rest_term()
parse_factor()
parse_num_op()
parse_rest_term()
parse_rest_expr()
40.000000
calc() {
python -c "from math import *; print $*"
}
Example:
$ calc pi - 3
0.14159265359
This, or one of the ipython shells I have lying around.
it seems that the replacement i did have effect on A ^ 9 expression...
the replacement should be :
resolve_variable(val);
if ( match_char( '^' ) ){
val_t val2;
parse_num_op( &val2 );
val->d.fval = pow( val->d.fval, val2.d.fval );
parse_rest_num_op( val );
}
parse_rest_expr( val );
myVariable + 1
This is due to a bug in the void parse_rest_var function !
The following code solve this problem and seems to not have boundary effect !
void parse_rest_var( val_t* val )
{
if ( match_char( '=' ) ) {
val_t vexp;
parse_expr( &vexp );
if ( vexp.type != TYPE_FLOAT ) {
printf("Error: Tried to assign non-number to %s.\n", val->d.variable );
longjmp( env, 1 );
}
printf("Assigned to %s: ", val->d.variable );
map_add( val->d.variable, vexp.d.fval );
*val = vexp;
} else {
// -----------------------replace
// parse_rest_num_op( val );
// -- by
resolve_variable(val);
parse_rest_expr( val );
// ------------------End of modif !
}