# calculater by using reverse polish notation and using a stack

Filed under:
|
##### stack

hello everyone I have a segmentation fault ,can you help please? if i have this operater "3 5 +" that mean 3+5 and like "9 8 * 5 + 4 + sin", "sin(((9*8)+5)+4)" so my idea is check if the first and second are numbers and push theem in the stack then when i have operator i pop the numbers and make the calculation then push the answer again. `

``````typedef struct st_node {
float val;
struct st_node *next;
} t_node;

typedef t_node t_stack;

// a function to allocate memory for a stack and returns the stack
t_stack* fnewCell() {
t_stack* ret;
ret = (t_stack*) malloc(sizeof(t_stack));
return ret;
}
// a function to allocate memory for a stack, fills it with value v and pointer n ,    and returns the stack
t_stack* fnewCellFilled(float v, t_stack* n) {
t_stack* ret;
ret = fnewCell();
ret->val = v;
ret->next =n;
return ret;
}

//function to initialize stack
void initstack(t_stack** stack) {
fnewCellFilled(0,NULL);
}

}

//function to push the value v into the stack s
void push(t_stack **s, float val) {
}

//function to pop a value from the stack and returns it
int pop(t_stack **s) {
t_stack* tmp;
int ret;
tmp = (*s)->next;
ret = (*s)->val;
free(*s);
(*s) = tmp;
return ret;
}

int isempty (t_stack *t) {
return t == NULL;
}

//function to transfer a string(str) to int (value)
//returns -1 when success , i otherwise
int str2int(char *str,int *value) {
int i;
*value = 0;
int sign=(str[0]=='-' ? -1 : 1);
for(i=(str[0]=='-' ? 1 : 0);str[i]!=0;i++) {
if(!(str[i]>=48 && str[i]<=57)) // Ascii char 0 to 9
return i;
*value= *value*10+(str[i]-48);
}
*value = *value * sign;
return -1;
}

//a function that takes a string, transfer it into integer and make operation using a stack
void function(t_stack *stack, char *str)
{
char x[10]=" ";
int y,j,i=0,z;
printf("++\n");
if(str[i] != '\0') {
strcpy(x, strtok(str, " "));
z= str2int(x, &y);
if(z == -1)
{
push(&stack,y);
i=i+2;
}
}
while(str[i] != '\0')
{
strcpy(x, strtok(NULL, " "));
z= str2int(x, &y);
if(z == -1)
{
printf("yes %d",y);
push(&stack,y);
i=i+2;
}
else
{
y=pop(&stack);
j=pop(&stack);

if(x[0] == '+' ) push(&stack,y+j);
else if (x[0] == '-' ) push(&stack,j-y);
else if(x[0] == '*' ) push(&stack,j*y);
else if(x[0] == '/') push (&stack ,j/y);
}
}
}

int main() {
t_stack *s;
initstack(&s);
char *str="3 5 +";
function(s,str);
return 0;
}
``````

`

© Stack Overflow or respective owner

• #### Java: immutability, overuse of stack -- better data structure?

as seen on Stack Overflow - Search for 'Stack Overflow'
I overused hashSets but it was slow, then changed to Stacks, speed boost-up. Poly's reply uses Collections.emptyList() as immutable list, cutting out excess null-checkers. No Collections.emptyStack(). Combining the words stack and immutability, from the last experiences, gets "immutable stack" (probably… >>> More

• #### iTunes 9.0.2 hangs on launch on Mac OS X 10.6.2

as seen on Super User - Search for 'Super User'
My iTunes 9.0.2 hangs on launch in OS X 10.6.2. This doesn't happen all the time, only if I've been running for a while. Then it will recur until I restart. Similarly Safari 4.0.4 will hang in the flash player plugin when about to play a video. If I restart both these problems go away until later… >>> More

• #### How to write Haskell function to verify parentheses matching?

as seen on Stack Overflow - Search for 'Stack Overflow'
I need to write a function par :: String -> Bool to verify if a given string with parentheses is matching using stack module. Ex: par "(((()[()])))" = True par "((]())" = False Here's my stack module implementation: module Stack (Stack, push, pop, top, empty, isEmpty) … >>> More

• #### PSTN Trunk TDM400P Install on Asterisk / Trixbox

as seen on Server Fault - Search for 'Server Fault'
Hey All, I'm trying to get a TDM400P card with FXO module to connect to our PSTN line. The card is correctly detected by Linux: [trixbox1.localdomain asterisk]# lspci 00:09.0 Communication controller: Tiger Jet Network Inc. Tiger3XX Modem/ISDN interface I've run setup-pstn which… >>> More

• #### how to clear stack after stack overflow signal occur

as seen on Stack Overflow - Search for 'Stack Overflow'
In pthread, After reaching yellow zone in stack, signal handler stop the recursive function by making it return however, we can only continue to use extra area in yellow zone, how to clear the rubbish before the yellow zone in the thread stack ? (Copied from "answers"): #include <pthread… >>> More