Here's how I'd do it (or something close):
1) Break the command into seperate commands / entries.
2) Stick them all in a double linked list.
3) Process the list in order of precedence(sp)
Code:
typedef struct node NODE;
NODE {
NODE *prev, *next;
int expression;
int value;
};
NODE *list;
enum { Invalid=-1, Number, Multiplication, Division, Addition, Subtraction };
When you're tokenizing (ie, chopping up the list) you do something like this:
Code:
// pesudo code, not all variables are declared
char chunk[10]={0};
expression = None;
for( x=0; x<10; x++ )
{
chunk[x] = wholeList[runningcount];
if( !isdigit( chunk[x] ) )
{
if( x != 0 )
{
// we've been reading a number, back up!
--x;
else
expression = getExpression( chunk[x] );
break;
}
// otherwise it's a number and we want to keep reading it
}
if( expression == Number )
{
value = atoi( chunk );
stick into a node as a 'value'
link the node
}
else
if( expression == Invalid )
{ we're done reading from our data, it appears to have ended }
else
{
stick into a node as a 'number'
link the node
}
Basicly, what you do next is walk down the node looking for order of precedence.
Check for multiplication. If you find it, multiply the node->prev->value with the node->next->value and then remove both of those nodes from the list, and change this node so that it is now storing a value rather than a expression.
Pretty simple stuff.
Quzah.