Siemka, mam pewien problem. Otóż napisałem kalkulator liczący wartość wyrażenia zapisanego w postaci ONP. Analizując kod, wszystko wydaje mi się w porządku, ale prosiłbym was o zerknięcie fachowym okiem. Otóż program za każdym razem zwraca wartość 13.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <locale.h>
struct stack_node
{
int data;
struct stack_node * next;
};
struct stack_node * push (struct stack_node *top , int data){
struct stack_node * new_node = (struct stack_node *)malloc(sizeof(struct stack_node));
if(NULL!=new_node){
new_node->data=data;
new_node->next=top;
top=new_node;
}
return top ;
}
int pop( struct stack_node ** top){
int result=-1;
if ( NULL != *top){
result=(*top)->data;
struct stack_node *tmp=(* top)->next;
free(*top );
*top=tmp;
}
return result ;
}
int peek(struct stack_node *top){
if(NULL!=top)
return top->data ;
fprintf(stderr, " Stack is empty .\n") ;
return top;
}
int umiesc(char a){
struct stack *top = NULL;
int wynik=0;
if(a!='+' && a!='-' && a!='*' && a!='/'){
a=a-'0';
top=push(top, a);
}
else{
switch(a){
case'+':
wynik=pop(&top)+pop(&top);
top = push(top,wynik);
case'-':
wynik=pop(&top)-pop(&top);
top = push(top,wynik);
case'*':
wynik=pop(&top)*pop(&top);
top = push(top,wynik);
case'/':
wynik=pop(&top)/pop(&top);
top = push(top,wynik);
}
}
return pop(&top);
}
int main (){
setlocale(LC_CTYPE, "Polish");
struct stack *top = NULL;
printf("Wprowadź liczby lub znaki żeby obliczyć wartość wyrażenia zapisanego w ONP: \n");
char a;
while(a!='='){
scanf("%c", &a);
umiesc(a);
}
printf("Wynik działania to: %d", umiesc(a));
return 0;
}