私はこの問題を与えられました。だから、ユーザが入力した文字列があり、文章が回文かどうかをチェックする必要があります(文の途中の対称単語は同じでなければなりませんが、スタックを使用して実装する必要があります)。 私は関数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;
}
上でそれを試してみてください*全体*文をスタックに送る 'create_stack'関数を使用すると、柔軟性が増します。それであなたの文字列を単語に分割します([strtok'](http://en.cppreference.com/w/c/string/byte/strtok)関数btwを見てください)、それらの半分をスタックにプッシュします、(文中に単語数を加算した場合は1単語をスキップしてください)、ループの中で 'pop'をスタックから取り出し、次の文と比較します - 一致しない場合は終止符で終わりません。 –
あなたはこの問題を解決するためのロジックを実装していますか? –
@AvantikaSaini私は実装に固執しています。上記のコードでは、何らかの理由でスタックが作成されていない(または印刷されていない)ため、スタックされています。 –