Das ganze ist sehr einfach, wenn der Ausdruck erst mal in Postfix Notation vorliegt. Natürlich wird kein normaler Mensch deinen Taschenrechner in Postfix Notation bedienen, also musst du einfach eine Infox2Postfix Algorithmus implementieren; den Postfix Ausdruck dann auszuwerten ist sehr einfach, ein bisschen Wissen über Stacks reicht da aus. Ich habe so was schon mal gemacht als ich einen Assembler auf dem C64 programmiert habe 😉 und war überrascht, wie einfach sdas eigentlich, wenn man es mal kapiert hat, ist. Also es lohnt sich, sich einfach mal mit der Postfix Geschichte auseinander zusetzen, du seolltest auch massig Stoff im Netz dazu finden, eine kurze Suche brachte mich z. Bsp. auf folgenden Link:
Infiix2Postfix Conversion:
http://scriptasylum.com/tutorials/infix_postfix/algorithms/infix-postfix/index.htm
Postfix Evaluation:
http://scriptasylum.com/tutorials/infix_postfix/algorithms/postfix-evaluation/index.htm
Habe die Details jetzt gerade auch nicht mehr im Kopf aber schaus dir doch einfach mal an und versuche das mal selbst zu implementieren; ist gar nicht so schwierig wie es vllt. am Anfang aussieht.
Und man lernt eine Menge dabei! Also bevor du versuchst, hier irgendwie deine Strings zu zerlegen und das dann irgendwie "notdürftig" zusammen frickelst - mach es gleich richtig und eigne Dir (Informatik-)Grundwissen an.
EDIT: Hat mich grad nochmal gebizzelt das Thema und ich habe mal einen ganz einfachen Expression Evaluator geschrieben (in C), liest Integer und +,-,* und /:
Der Witz ist, dass es jetzt recht einfach ist weiter zu machen; z. Bsp. Klammerung zu implementieren ist auch nur noch eine Frage des richtigen push/pop zur rechten Zeit usw.
#include <stdio.h>
enum {
OPERATOR,
OPERAND,
END
};
int stack[512];
unsigned int stack_pointer;
/* The Postfix Expression will hold Operands and Operators so we need a Data structure to
* differenciate between them */
struct postfix_element {
unsigned type;
int value;
} postfix[512];
/* push a value to stack */
void push(char x){
stack[stack_pointer++] = x;
}
/* pop a value off the stack */
char pop(void){
if (stack_pointer > 0){
return stack[--stack_pointer];
}
else{
return (-1);
}
}
/* peek at the uppermost value on stack without popping it*/
char peek(void){
if (stack_pointer > 0){
return (stack[stack_pointer - 1]);
}
else{
return (-1);
}
}
/* get priority of a operator */
int priority(char operator){
switch (operator){
case '+':
case '-':
return (1);
case '*':
case '/':
return (2);
}
}
void infix2postfix(char *infix){
unsigned int i = 0;
char operator;
int operand;
char c;
int v;
stack_pointer = 0;
while (c = *infix){
switch (c){
case '+':
case '-':
case '*':
case '/':
/* if the scanned character is an operator and the stack is empty, push it to the stack */
if (stack_pointer == 0){
push(c);
}
/* if stack is not empty... */
else{
/* repeat while stack is not empty... */
while (stack_pointer > 0){
/* if precedence of stacked operator is higher than current one, then pop and add to postfix */
if (priority(peek()) > priority(c)){
postfix[i].type = OPERATOR;
postfix[i].value = pop();
i++;
}
/* else break */
else{
break;
}
}
/* finally push the current operator on stack */
push(c);
}
/* proceed with next infix character */
infix++;
default:
/* if the scanned character is an operand, add it to postfix */
v = 0;
/* read (decimal) integer */
while (isdigit(*infix)){
v = 10 * v + (*infix++ - '0');
}
/* and add to postfix */
postfix[i].type = OPERAND;
postfix[i].value = v;
i++;
}
}
/* when all infix characters are read, pop everything from stack and add to postfix */
while (stack_pointer > 0){
postfix[i].type = OPERATOR;
postfix[i].value = pop();
i++;
}
/* mark postfix end. */
postfix[i].type = END;
}
void print_postfix(){
unsigned int i = 0;
while (postfix[i].type != END){
if (postfix[i].type == OPERATOR){
printf("%c", postfix[i].value);
}
else{
printf("[%d]", postfix[i].value);
}
i++;
}
printf("\n");
}
int evaluate_postfix(void){
unsigned int op1, op2, r, i = 0;
print_postfix(postfix);
/* iterate over postfix expression... */
while (postfix[i].type != END){
if (postfix[i].type == OPERAND){
/* Operands are pushed */
push(postfix[i].value);
}
else{
/* If we encounter an operator, then pop two operands from stack
* (in reverse order!) and operate on them according to the operator,
* then push the result back on stack */
op2 = pop();
op1 = pop();
switch (postfix[i].value){
case '+': r = op1 + op2; break;
case '-': r = op1 - op2; break;
case '*': r = op1 * op2; break;
case '/': r = op1 / op2; break;
}
push(r);
}
i++;
}
/* the last element on stack is the expression's value */
return (pop());
}
int main(int argc, char **argv){
if (argc == 2){
infix2postfix(argv[1]);
printf("result = %d\n", evaluate_postfix());
}
}