This function is to calculate expression and output results.
for example, input 3+5 will output 8.0000
and input 3^2 will output 9.0000

I cannot understand it very well on some coding,
my problems are highlighted as comments in the code.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

typedef enum Ops
{
None,
Plus,
Minus,
Multiply,
Divide,
Power
} OPS;

long signif[] = /* what is it mean?? */
{ /* why it is needed ?? */
0,
10,
10,
11,
11,
12
};

long double Evaluate(long start, long stop, char* expr);

int main()
{
char buffer[256];
int c;
do
{
printf("Enter expression to evaluate: ");
scanf("%s", buffer);
printf("Result: %f \n\n", (float)Evaluate(0, strlen(buffer)-1, buffer));

printf("Again? (1/0)");
scanf("%d", &c);
} while(c);

return 0;
}

long double Evaluate(long start, long stop, char* expr)
{
OPS op_found;
OPS op_last = None;
unsigned long op_cursign = 0xFFFFFFFF; /* what is it mean? */
long op_pos;
long parnest, parpair;
long curpos;
int nonwhite;

//Remove enclosing whitespaces paranthesis
while(1) /* why use 1 , is it a endless loop ??? */
{

//Remove enclosing parathesis
parnest = 0;
parpair = 0;
for(curpos = start; curpos <= stop; curpos++)
{
if(expr[curpos] == '(')
{
parnest++;
} else if(expr[curpos] == ')')
{
parnest--;
if(parnest == 0) parpair++;
}
}

if(expr[start] == '(' && expr[stop] == ')' && parpair == 1)
{
start++;
stop--;
} else break; //Break if there are no more paranthesis
}

//Search for operators
nonwhite = 0;
parnest = 0;
op_cursign = 0xFFFFFFFF; /* what is it mean */
op_last = None;
for(curpos = start; curpos <= stop; curpos++)
{
if(parnest == 0) // Are we in paranthesis
{
op_found = None;
if(nonwhite) /* what it means? nonwhite =0 is false */
{ /* if statement willl not run ?? */
switch(expr[curpos])
{
case '+':
op_found = Plus;
break;
case '-':
op_found = Minus;
break;
case '*':
op_found = Multiply;
break;
case '/':
op_found = Divide;
break;
case '^':
op_found = Power;
break;
}
} else /* why use this else statements ?? */
{
switch(expr[curpos])
{
//Interpret the operators as white spaces
case '+':
case '-':
case '*':
case '/':
case '^':

case ' ':
case '\t':
case '\n':
case '\r':
break; //Ignore white spaces
default:
nonwhite = 1; /* assignment it for what use ? */
}
}

if(op_found != None)
{
nonwhite = 0;
if(signif[op_found] <= op_cursign)
{ /* what is this mean ?? */
op_cursign = signif[op_found]; )
op_last = op_found;
op_pos = curpos;
}
}
}

if(expr[ curpos] == '(')
{
parnest++;
} else if(expr[curpos] == ')')
{
parnest--;
}
}

if(parnest != 0)
{
printf("\nError: Parenthesis nesting error!\n");
return 0;
}

//Did we find any operators?
if(op_last != None)
{
long double a, b;

a = Evaluate(start, op_pos - 1, expr); /*why use this */
b = Evaluate(op_pos + 1, stop, expr); /* why use this */

switch(op_last)
{
case Plus:
return a+b;

case Minus:
return a-b;

case Multiply:
return a*b;

case Divide:
return a/b;

case Power:
return pow(a, b);
}
return 1; /* what is this mean */
}

return atof(&expr[start]); /* what is this mean */
}

My problems are a little bit more, I'm happy if you can answer me.
As my c program is not good. ^^

2. You will not learn anything unless you start small, and write your own routine!

I noticed you pasted someone elses code here!

Why don't you try a simpler program and work your way up to something more difficult?

It is as if you were starting to learn architecture and then plan to build a castle for your first project!!

Rediculous!

Popular pages Recent additions