2017-02-24 71 views
0

我被給了這個問題,我需要爲它創建一個代碼。因此,我們有一個由用戶輸入的字符串,然後代碼需要檢查句子是否是迴文句(句子中間的對稱詞應該是相同的,但我們應該使用堆棧來實現。 我熟悉函數pop()和push()(甚至認爲我沒有在下面使用它們)。我到現在爲止的想法是,我接收字符串並從這些字符串中創建一堆字,然後使用該字符串檢查句子是迴文。我現在堅持,我真的不能想別的,幫助將非常感激。C中的單詞棧(句子迴文)

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

struct stack 
{ 
    char s[30]; 
    struct stack *next; 
}; 

typedef struct stack STACK; 

struct top 
{ 
    int num; 
    struct stack *top; 
}; 

typedef struct top TOP; 

void create_stack(TOP *s, char str[1000]) 
{ 
    char temp1[30]; 
    int i=0, j=0; 

    STACK *temp; 
    temp=(STACK*)malloc(1*sizeof(STACK)); 

    while(1) 
    { 
     if(str[i]!=' ' && str[i]!='\0') 
     { 
      temp1[j]=str[i]; 
      j++; 
     } 
     else 
     { 
      temp1[j]='\0'; 
      strcpy(temp->s,temp1); 
      printf("%s\n", temp->s); 

      if(s->top==NULL) 
      { 
       s->top=temp; 
       s->num=1; 
      } 
      else 
      { 
       temp->next=s->top; 
       s->top=temp; 
       s->num++; 
      } 
      j=0; 
     } 
     if(str[i]=='\0') 
     { 
      break; 
     } 
     i++; 
    } 
} 

void move_cursor(STACK *cursor, int pos) 
{ 
    while (pos!=0) 
    { 
     cursor=cursor->next; 
     pos--; 
    } 
} 

void compare(TOP *s) 
{ 
    STACK *cursor1, *cursor2; 
    cursor1=s->top; 
    cursor2=s->top; 
    int cursor_move1, cursor_move2, i=0, check=1; 

    if(s->num%2==0) 
    { 
     cursor_move1=s->num/2; 
     cursor_move2=(s->num/2)+1; 

     while (i!=cursor_move1) 
     { 
      cursor1=s->top; 
      cursor2=s->top; 
      move_cursor(cursor1, i); 
      move_cursor(cursor2, cursor_move2); 

      if(strcmp(cursor1->s,cursor2->s)!=0) 
      { 
       check=0; 
       break; 
      } 
      else 
      { 
       i++; 
       cursor_move2++; 
      } 
     } 
    } 

    if(check==0) 
     printf("%d Neg", check); 
    else 
     printf("1Pos"); 
} 

void display(TOP *top) 
{ 
    STACK *cursor; 
    cursor=top->top; 

    while(cursor->next==NULL) 
    { 
     printf("%s pos\n ", cursor->s); 

     cursor=cursor->next; 
    } 
} 

int main() 
{ 
    char input[1000]; 
    TOP top; 
    top.num=0; 
    top.top=NULL; 

    fgets(input, 100, stdin); 

    input[strlen(input)-1]='\0'; 

    create_stack(&top, input); 

    printf("%d \n ", top.num); 

    display(&top); 
    printf("---------------------------------------------------------\n"); 
    compare(&top); 


    return 0; 
} 
+1

我建議你創建正確的'stack'功能(例如'pop'和'push'),這樣你就不需要一個'create_stack'函數將*整個*句子送入堆棧,因此您將擁有更多的靈活性。所以,然後你把你的字符串分成單詞(看看['strtok'](http://en.cppreference.com/w/c/string/byte/strtok)函數btw),把它們中的一半推到棧上,(在句子中增加字數時跳過一個單詞),然後在一個循環中:從棧中彈出一個,並與句子中的下一個比較 - 如果它們不匹配 - 終止它不是迴文。 –

+1

你是否堅持解決這個問題的邏輯或實現? –

+0

@AvantikaSaini我堅持實施。在上面的代碼中,由於某些原因(或者不打印)不會創建堆棧,這就是我卡住的地方。 –

回答

1

有在你的代碼不同的問題。這解釋了最大的一個爲什麼如果你只在開始之前在堆棧中創建一個單獨的元素,那麼你無法提供堆棧g循環,而你顯然需要爲每個單詞分配一個元素。此外,您忘記將top元素的next值初始化爲NULL。順便說一句,你不應該投射malloc。

的create_stack應該成爲:

void create_stack(TOP *s, char str[1000]) 
{ 
    char temp1[30]; 
    int i=0, j=0; 

    STACK *temp; 
    temp=malloc(1*sizeof(STACK)); 
    temp->next = NULL; // must be explicitely NULL for further use 

    while(1) 
    { 
     if(str[i]!=' ' && str[i]!='\0') 
     { 
      temp1[j]=str[i]; 
      j++; 
     } 
     else 
     { 
      temp1[j]='\0'; 
      strcpy(temp->s,temp1); 
      printf("%s\n", temp->s); 

      if(s->top==NULL) 
      { 
       s->top=temp; 
       s->num=1; 
      } 
      else 
      { 
       temp->next=s->top; 
       s->top=temp; 
       s->num++; 
      } 
      j=0; 
      temp=malloc(1*sizeof(STACK)); // time to allocate a new element 
     } 
     if(str[i]=='\0') 
     { 
      free(temp); // last allocated has not been used 
      break; 
     } 
     i++; 
    } 
} 

在顯示你的循環測試是完全錯誤的,它應該是while(cursor!=NULL)

一旦固定,你應該使用調試器來理解,而比較不給預期成績。無論如何,我的意見是,不要反覆移動遊標,你應該爲STACK元素分配一個指針數組,用它的內容饋送它一次,並使用該數組直接在數組元素之間進行比較,即按索引。

0

夥計們我能解決我的問題。我發佈下面的代碼,如果有人需要它知道在哪裏可以找到它。

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

struct stack 
{ 
char s[30]; 
struct stack *next; 
}; 
typedef struct stack STACK; 

struct top 
{ 
int num; 
STACK *top; 
}; 
typedef struct top TOP; 


void push(TOP *top, char str[30]) 
{ 
STACK *temp; 
temp=malloc(1*sizeof(STACK)); 
temp->next = NULL; 
strcpy(temp->s, str); 

if(top->num==0) 
{ 
    top->top=temp; 
    top->num=1; 
} 
else 
{ 
    temp->next=top->top; 
    top->top=temp; 
    top->num++; 
} 
} 

void pop (TOP *top, char s[30]) 
{ 
STACK *temp; 
temp=top->top; 

    temp=temp->next; 
    strcpy(s,top->top->s); 
    free(top->top); 
    top->top=temp; 
    top->num--; 
} 

void create_stack(TOP *s, char str[1000]) 
{ 
char temp1[30]; 
int i=0, j=0; 
while(1) 
{ 
    if(str[i]!=' ' && str[i]!='\0') 
    { 
     temp1[j]=str[i]; 
     j++; 
    } 
    else 
    { 
     temp1[j]='\0'; 
     push(s, temp1); 
     j=0; 
    } 
    if(str[i]=='\0') 
    { 
     break; 
    } 
    i++; 
    } 
    } 


void display(TOP *top) 
    { 
    STACK *cursor; 
    cursor=top->top; 

    while(cursor!=NULL) 
    { 
     printf("%s\n ", cursor->s); 

     cursor=cursor->next; 
    } 
} 

void compare(TOP *top, char *s) 
{ 
char s2[1000]; 
s2[0]='\0'; 
char ret[30]; 
int len; 


pop(top,ret); 
strcpy(s2, ret); 

while(top->top!=NULL) 
{ 
    len=strlen(s2); 
    s2[len]=' '; 
    s2[len+1]='\0'; 
    ret[0]='\0'; 
    pop(top,ret); 
    strcat(s2, ret); 
} 

if(strcmp(s, s2)==0) 
    printf("The sentence is palindromic by words!\n"); 

else 
    printf("The sentence is not palindromic by words!\n"); 

} 

int main() 
{ 
char input[1000]; 
TOP top; 
top.num=0; 
top.top=NULL; 

while(1) 
{ 
fgets(input, 100, stdin); 

input[strlen(input)-1]='\0'; 

if(strcmp(input, "exit")==0) 
    break; 

    create_stack(&top, input); 


    compare(&top, input); 


} 



    return 0; 
} 

嘗試在輸入「像狗和狗喜歡貓的貓」