I hope this is what you had in mind. The enclosed function evaluates a string like 123/3.1 or 12345679*63 and returnes the calculated value. Enter this string in your main() and call the function.
/********************* calc_val.cf ******************************
**
** Parse a calculator string (eg. 5.7/3.2 or 4.2*9.7) for
** the operator, then do the calculation and return the value.
** Include string.h and math.h
** Written in Borland C/C++ by vegaseat 12/11/91
**
*******************************************************************/
double calc_val(char *s1)
{
static char *sp, *ss, *sm, *sd, *s2;
sp = strchr(s1,'+'); /* parse for +, -, *, or / operator */
ss = strchr(s1,'-');
sm = strchr(s1,'*');
sd = strchr(s1,'/');
if (sp && ss && sm && sd) /* all NULL => no calculation needed */
return (atof(s1));
else {
if (sp) { s2 = sp + 1; return (atof(s1) + atof(s2)); }
if (ss) { s2 = ss + 1; return (atof(s1) - atof(s2)); }
if (sm) { s2 = sm + 1; return (atof(s1) * atof(s2)); }
if (sd) { s2 = sd + 1; return (atof(s1) / atof(s2)); }
}
}
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
Offline 5,792 posts
since Oct 2004