2013-07-01 50 views
-1

大家好我有一個分段錯誤,你能幫助嗎?如果我有這個操作符「3 5 +」,這意味着3 + 5 ,並像「9 8 * 5 + 4 +罪」,「罪(((9 * 8)+5)+4)」 所以我的想法檢查第一個和第二個數字是否是數字,然後在堆棧中推送它們,然後當我有操作員時,彈出數字並進行計算,然後再次推送答案。 `通過使用反向波蘭記法和使用堆棧計算器

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); 
} 

// add new cell 
void insrtHead(t_stack** head,float val) { 
    *head = fnewCellFilled(val,*head); 
} 

//function to push the value v into the stack s 
void push(t_stack **s, float val) { 
    insrtHead(s,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; 
} 

`

+1

可以請你把在編譯的代碼中,我們不要「T知道什麼是stack_t,或者是你的push/pop函數的作用。很難幫助調試一段不完整的代碼;或者就此而言,str2int是什麼?等 –

+2

歡迎來到堆棧溢出!爲了讓您的問題更容易回答,請嘗試發佈[簡短,獨立,正確(可編輯),示例](http://sscce.org/)。 –

+1

'罪'運營商應該在這裏實施?似乎沒有任何一元運營商支持。 – Potatoswatter

回答

0

在哪裏,我注意到:

1)

void initstack(t_stack** stack) { 
    fnewCellFilled(0,NULL); 
} 

大概

void initstack(t_stack** stack) { 
    *stack=fnewCellFilled(0,NULL); 
} 

2)

字符串文字是由strtok的

改變
char *str="3 5 +"; 

應該

char str[]="3 5 +";  

3)

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); 
    } 

大概

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); 
     i += 1;//need increment i 
    } 
+0

非常感謝... = D – programmer