2017-02-24 1 views
0

私はこの問題を与えられました。だから、ユーザが入力した文字列があり、文章が回文かどうかをチェックする必要があります(文の途中の対称単語は同じでなければなりませんが、スタックを使用して実装する必要があります)。 私は関数pop()とpush()に精通しています(私も以下でそれらを使っていないと思っていました)。私が今まで考えてきたことは、文字列を取り出してこの文字列から単語スタックを作り、文が回文であるかどうかを確認する。私は今、立ち往生していると私は本当に何かを考えることはできません。ヘルプははるかに高く評価されるだろう。C(Sentence Palindrome)の単語のスタック

#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

上でそれを試してみてください*全体*文をスタックに送る 'create_stack'関数を使用すると、柔軟性が増します。それであなたの文字列を単語に分割します([strtok'](http://en.cppreference.com/w/c/string/byte/strtok)関数btwを見てください)、それらの半分をスタックにプッシュします、(文中に単語数を加算した場合は1単語をスキップしてください)、ループの中で 'pop'をスタックから取り出し、次の文と比較します - 一致しない場合は終止符で終わりません。 –

+1

あなたはこの問題を解決するためのロジックを実装していますか? –

+0

@AvantikaSaini私は実装に固執しています。上記のコードでは、何らかの理由でスタックが作成されていない(または印刷されていない)ため、スタックされています。 –

答えて

1

あなたのコード内の別の問題がある。説明して最大のもの何故ならば、あなたは最初にスタック内に1つの要素を作成するだけですあなたは明らかに単語ごとに1つの要素を割り当てる必要があります。また、先頭の要素のnextの値をNULLに初期化することも忘れてしまいます。 Cで、あなたは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要素へのポインタの配列を割り当て、スタックの内容で1回フィードし、その配列を配列要素間の直接的に、つまりインデックスで比較する必要があります。

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

あなたが必要ないように、私は適切な `stack`機能(例えば、` pop`と `push`)を作成することをお勧めします入力「猫のような犬と犬のような猫」