I got a parser from Phil Howard's libh, thanx yall for your help
//-----------------------------------------------------------------------------
// Copyright © 2004 - Philip Howard - All rights reserved
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
//-----------------------------------------------------------------------------
// package libh/string
// homepage
http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author Philip Howard
// email libh at ipal dot org
// homepage
http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------
#include <math.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
//-----------------------------------------------------------------------------
// function str_expr_to_d
//
// purpose Convert a string containing a numeric expression into a
// double.
//
// arguments 1 (const char *) pointer to string to convert
// 2 (char * *) pointer to string to store end pointer
//
// returns (double) converted result
//
// note Numeric values that exceed the maximums for this data type
// will result in undefined values being returned.
//-----------------------------------------------------------------------------
double
str_expr_to_d (
const char * arg_str
,
char * * arg_end_ptr
)
{
struct stack_node {
double value ;
int level ;
int oper ;
};
struct stack_node stack_array [64];
struct stack_node * stack_top ;
struct stack_node * stack_end ;
double number ;
char * str_ptr ;
char * end_ptr ;
int erange ;
int nest ;
int op_code ;
int op_level ;
stack_top = stack_array;
stack_top->level = 0;
stack_end = stack_array + 64;
erange = 0;
nest = 0;
op_level = 0;
* (const char * *) & str_ptr = arg_str;
for (;
{
//--------------------
// Expecting a number.
//--------------------
//-- Check for end of string.
if ( * str_ptr == 0 ) {
errno = EINVAL;
number = 0.0;
break;
}
//-- Check for spaces.
if ( isspace( * str_ptr ) ) {
++ str_ptr;
continue;
}
//-- Check for nesting.
if ( * str_ptr == '(' ) {
nest += 8;
++ str_ptr;
continue;
}
//-- Convert a number.
errno = 0;
end_ptr = NULL;
number = strtod( str_ptr, & end_ptr );
if ( ! end_ptr ) {
errno = EINVAL;
number = 0.0;
break;
}
if ( errno == ERANGE ) {
++ erange;
}
str_ptr = end_ptr;
//-----------------------
// Expecting an operator.
//-----------------------
//-- Get the operator code.
for (;
{
op_code = * str_ptr ++;
//-- Skip spaces.
if ( isspace( op_code ) ) {
continue;
}
//-- Check for unnesting.
if ( op_code == ')' && nest >= 8 ) {
nest -= 8;
continue;
}
//-- Operator or not, go with it.
break;
}
//-- Determine level of this operator.
op_level = 0;
if ( op_code == '+' || op_code == '-' ) {
op_level = nest + 2;
}
else if ( op_code == '*' || op_code == '/' ) {
op_level = nest + 3;
}
else if ( op_code == '^' ) {
op_level = nest + 4;
}
//-- Do stacked arithmetic.
while ( stack_top->level > op_level ) {
if ( stack_top->oper == '+' ) {
number = stack_top->value + number;
}
else if ( stack_top->oper == '-' ) {
number = stack_top->value - number;
}
else if ( stack_top->oper == '*' ) {
number = stack_top->value * number;
}
else if ( stack_top->oper == '/' ) {
number = stack_top->value / number;
}
else if ( stack_top->oper == '^' ) {
number = pow( stack_top->value, number );
}
if ( -- stack_top < stack_array ) break;
}
//-- If end of expression.
if ( op_level == 0 ) {
-- str_ptr;
if ( nest > 0 ) {
errno = EINVAL;
number = 0.0;
}
break;
}
//-- Push the new operator into the stack.
if ( ++ stack_top >= stack_end ) {
errno = ENOBUFS;
number = 0.0;
break;
}
stack_top->value = number;
stack_top->oper = op_code;
stack_top->level = op_level;
}
if ( arg_end_ptr ) * arg_end_ptr = str_ptr;
return number;
}
int main(int argc,char *argv[])
{
system("clear");
if(argc != 2)
{
printf("\nEnter the operands surrouded in quotes, ");
printf("eg '1+2(2-6)'\n\n");
exit(1);
}
double d = str_expr_to_d(argv[1],NULL);
printf("%f\n",d);
return 0;
}
EOF