int main(int argc, char ** argv) { if (argc < 2) { printf("Usage: rpncalc op1 op2 ...\n"); exit(1); } STACK * s = stack_create(); int i; char * c; for (i = 1; i < argc; i++) { if (is_operand(c = argv[i])) { if (stack_is_empty(s)) { printf("Elements remain in the stack.\n"); exit(1); } else { compute_operation(s, c); } } else { stack_push(s, atof(c)); } } double result = s->top->val; stack_pop(s); if(stack_is_empty(s)) { printf("%f\n", result); } else { printf("Elements remain in the stack.\n"); } exit(0); }
double evaluate_RPN(char *expr, struct stackNode **top) { int i, j = 0; double popVal1; // Stack'ten cikaracaginiz (pop) ilk deger double popVal2; // Stack'ten cikaracaginiz (pop) ikinci deger double pushVal; // Stack'e ekleyeceginiz (push) deger double retval = 0; char Val[20]; /* TODO: Ifadenin sonuna kadar elemanlari gezecek ('\0') bir dongu kurunuz */ for (i = 0; i < strlen(expr); i++) { /* TODO: Eger eleman islenen (operand) ise stack'e ekleyiniz (push) * Soru: Bir karakterin sayi karsiligini nasil buluyorduk? */ if (expr[i] == ' ') { Val[j + 1] = '\0'; j = 0; popVal1 = atof(Val); stack_push(top, popVal1); if (DEBUG) stack_print(*top); } else if (expr[i] == '/' || expr[i] == '*' || expr[i] == '-' || expr[i] == '+') { popVal1 = stack_pop(top); popVal2 = stack_pop(top); pushVal = compute_operation(popVal2, popVal1, expr[i]); stack_push(top, pushVal); i++; if (DEBUG) stack_print(*top); } else { /* TODO: Eger eleman bir islem ise stack'ten iki deger cekiniz (pop) */ Val[j] = expr[i]; j++; if (DEBUG) stack_print(*top); /* TODO: Bu iki deger ile istenen islemi compute_operation() * fonksiyonuna verip donus degeri stack'e push edin. */ if (DEBUG) stack_print(*top); } } /* TODO: Stack'te kalan elemani cekip return edin. */ retval = stack_pop(top); if (!stack_is_empty(*top)) { fprintf(stderr, "UYARI: Stack hala dolu, RPN ifadesi dengesiz veya algoritmayi yanlis kurdunuz.\n"); } return retval; }