Hello,
I'm writing a simple calculator in C. The main problem is I need to parse string to evaluate for example 1+2*(3-1)/2.5.
However writing such parses is a very hard task.
This is a trivial thing Python using this code:
s = raw_input('Please enter expression:')
try:
x = eval(s)
except NameError:
print 'Unknown elements in the expression!'
except ZeroDivisionError:
print 'Division with zero attempted!'
except:
print 'Some other kind of error!'
else:
print 'Expression is evaluated correctly'
I saw that Python has C API so I assume that Python functions can be called from C.
Is it possible to use Python function eval() to evaluate user string in C program? In other words is it possible to make wrapper to this Python code and evaluate expression in C?
I tried to find answer in Python manual but I'm a complete jackass beginer.
For example I have Test.c
#include <stdio.h>
char buff[BUFSIZ];
int main(void)
{
char * p;
double res;/* to store result of expression evaluation*/
printf ("Please enter expression: ");
fgets(buff, sizeof buff, stdin);
if (p = strrchr(buff,'\n'))
{
*p = '\0';
}
/* now wrapper to python function eval()-....? */
}
and from that c file I'd like to call eval and if success to store result to variable res.
I'm using MSCV++ .NET with Win XP and I assume I'll need to use some kind of Python libraray...
There is explanation in Pythion doc, but there is no complete testing source file and my english is not very good to understand all the described steps.
Is this possible at all?
Please, can you help me?
Thanks in advance!