lundi 31 mars 2014

Evaluating math expression given in character array


Vote count:

0




So, I have an input expression which is of the form ....?5+8 ...


I need to parse this and evaluate the result. Since I know that the expression starts after the ? and ends with a ' ', I tried this:



int evaluate(char *buffer){
while(buffer[i] != '+' || buffer[i] != '-' || buffer[i] != '*' || buffer[i] != '/'){
exp[j] = buffer[i];
i++;
j++;
}
exp[j] = '\0';
int number1 = atoi(exp);

char operation = buffer[i];
i++;
j = 0;
while(buffer[i] !=' '){
exp[j] = buffer[i];
i++;
j++;
}
exp[j] = '\0';
int number2 = atoi(exp);

switch(operation)....
}


where j is initialized from 0 and i is initialized to the position after ?.


The function is called using evaluate(buffer);


However, I keep getting the error Segmentation Fault (core dumped).


Any Ideas?



asked 44 secs ago

gary

28





Aucun commentaire:

Enregistrer un commentaire