回文 - 删除 goto

发布于 2024-10-08 00:32:26 字数 1718 浏览 3 评论 0原文

再会! 我们的老师要求我们确定一个单词或一系列数字是否是回文或不使用堆栈。我已经做完了。但我想更多地练习,所以现在我试图通过删除空格和其他不相关的字符来确定一个句子是否是回文(注意:不再是我的作业的一部分)我的代码已经可以工作(希望如此),但我发现很乱。所以我想改进它。我想删除 goto 功能,因为我的老师建议我不要使用它。如何跳出 if 语句而不使用 goto 函数?先感谢您。还有其他方法可以检查一个句子是否是回文,因为我的代码是用暴力方法完成的。我的代码如下: 注意(我没有在此处包含/粘贴结构以及弹出和推送功能)

int main(){
   char word[11];
   char temp[11];
   char value;
   int i=0, x=0, n=0, length=0; 
   Stack*head = NULL;
   printf("Please type the word: ");
   gets(word);
   length = strlen(word);
   while(i<length){
       if(isspace(word[i]) || !isalpha(word[i]))  {
           if(isdigit(word[i])) goto NEXT;  // i used the goto function here
           i++; 
           continue;
       }
       NEXT:
       temp[n]=word[i];
       push(&head, word[i]);
       i++;
       n++;
   }
   temp[n]='\0';
   while(x<n){
       value = pop(&head);         
       if (value==temp[x]){ 
           x++; 
           continue;
       }
       break;
   }    
   if(x==n) printf("Yehey! It is a palindrome.");
   else printf("Sorry, It is not a palindrome.");
   getch();
}

基于您的建议。这是我改进的代码:

int main(){
   char word[11];
   char temp[11];
   int i=0, n=0; 
   int flag = 1;
   Stack*head = NULL;
   printf("Please type the word: ");
   fgets(word, 11, stdin);
   for(i = 0; word[i]!='\0' ; i++){
       if(isalnum(word[i])) {
           temp[n]=word[i];
           push(&head, word[i]);
           n++;
       }
   }
   temp[n]='\0';
   for(i=0; temp[i]!='\0'; i++){
       if (pop(&head)!=temp[i]){ 
          flag = 0;
          break;
       }
   }    
   if (flag==1) printf("Yehey! It is a palindrome.");
   else printf("Sorry, It is not a palindrome.");
   getch();
}

Good day!
Our teacher required us to determine if a word or a series of number is a palindrome or not using stacks. I already finished doing that. But I want to practice more so right now I am trying to determine if a sentence is a palindrome or not by removing whitespaces and other irrelevant characters (Note: Not part of my homework anymore) My code is already working (hopefully) but I find it messy. So i want to improve it. I want to remove the goto function because my teacher advised me not to use it. How can i go out of the if statement w/o using the goto function? Thank you in advance. Also are there other ways to do check if a sentence is a palindrome or not because my code is done in a brute force method. My code is as follows: NOTE(I did not include/pasted the struct and the pop and push function here)

int main(){
   char word[11];
   char temp[11];
   char value;
   int i=0, x=0, n=0, length=0; 
   Stack*head = NULL;
   printf("Please type the word: ");
   gets(word);
   length = strlen(word);
   while(i<length){
       if(isspace(word[i]) || !isalpha(word[i]))  {
           if(isdigit(word[i])) goto NEXT;  // i used the goto function here
           i++; 
           continue;
       }
       NEXT:
       temp[n]=word[i];
       push(&head, word[i]);
       i++;
       n++;
   }
   temp[n]='\0';
   while(x<n){
       value = pop(&head);         
       if (value==temp[x]){ 
           x++; 
           continue;
       }
       break;
   }    
   if(x==n) printf("Yehey! It is a palindrome.");
   else printf("Sorry, It is not a palindrome.");
   getch();
}

Based from your suggestions. Here is my improved code:

int main(){
   char word[11];
   char temp[11];
   int i=0, n=0; 
   int flag = 1;
   Stack*head = NULL;
   printf("Please type the word: ");
   fgets(word, 11, stdin);
   for(i = 0; word[i]!='\0' ; i++){
       if(isalnum(word[i])) {
           temp[n]=word[i];
           push(&head, word[i]);
           n++;
       }
   }
   temp[n]='\0';
   for(i=0; temp[i]!='\0'; i++){
       if (pop(&head)!=temp[i]){ 
          flag = 0;
          break;
       }
   }    
   if (flag==1) printf("Yehey! It is a palindrome.");
   else printf("Sorry, It is not a palindrome.");
   getch();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

删除→记忆 2024-10-15 00:32:26

您可以进行的最简单的更改如下:

   ...
   if(isspace(word[i]) || !isalpha(word[i]))  {
       if(!isdigit(word[i])) {
           i++; 
           continue;
        }
   }
   temp[n]=word[i];
   ...

您还可以执行一些其他操作来整理代码(例如,合并 if 语句、删除 isspace因为 !isalpha 涵盖了这一点等等)。

The simplest change you can make is the following:

   ...
   if(isspace(word[i]) || !isalpha(word[i]))  {
       if(!isdigit(word[i])) {
           i++; 
           continue;
        }
   }
   temp[n]=word[i];
   ...

There are a few other things you can do to tidy up the code (e.g. combine the if statements, get rid of the isspace since !isalpha covers that and so on).

拍不死你 2024-10-15 00:32:26

我喜欢你的态度。

首先,您在这里尝试做的是嵌套两个本质上是一个的逻辑语句。您还使用了错误的函数来确定字符类型:

如果 isspace(word[i]) 那么您可以保证 !isalpha(word[i])。两种陈述总是同时为真或为假,因此其中之一是多余的。你真正要做的只是推送字母数字字符,对吧?因此,与其使用 if 语句来确定是否要跳过某个字符,不如使用 if 语句来确定是否要推送该字符。我认为 isalnum() 可能就是您想要的。

其次,不要执行 strlen() 迭代字符串并使用返回值迭代字符串(这会产生两次),而是尝试:

while('\0' != word[i])

或者甚至更好:

for(i = 0; '\0' != word[i]; i++)

最后,您对回文的测试可以稍微简化一下。在循环之后测试循环值在所有情况下都有效,但有点难看。它也不乐意受愚弄。在专业环境中,有很多人(其中一些人不太认真)编辑代码并在循环后使用循环值可能会有风险。也许有一个名为“match”的布尔值并将其初始化为 true,然后循环直到堆栈末尾或“match”变为 false,如果堆栈上的字符与“match”不“匹配”,则将“match”设置为 false预期值。这也会更加高效。


当原来的问题显然被删除时,我正在撰写这个答案。

如果您希望我发布代码示例,我很乐意这样做,但我认为如果我不这样做,您可能会学到更多。如果您想要一个代码示例,或者希望我看看您在这个答案之后得到的结果,请随意。

I like your attitude.

Firstly, what you are trying to do here is nest two logic statements that are essentially one. You are also using the wrong functions to determine the character type:

if isspace(word[i]) then you can guarantee that !isalpha(word[i]). Both statements will always be true or false at the same time so one of them is redundant. What you are really doing is only pushing characters if the are alphanumeric, right? So rather than having an if statement to determine if you want to skip a character you ought to be doing an if statement to determine if you want to push the character. I think isalnum() might be what you want.

Secondly, rather than doing strlen() which iterates over the string and using the return value to iterate over the string (that makes twice) try:

while('\0' != word[i])

or even better:

for(i = 0; '\0' != word[i]; i++)

Lastly, your test for a palindrome could be neatened up a bit. Testing a loop value after the loop works in all cases but is a bit ugly. It also does not suffer fools gladly. In a professional environment you get many people, some not so conscientious, editing the code, and using loop values after a loop can be risky. Maybe instead have a bool called something like "match" and initialise it to true, then loop until end of the stack or "match" turns false and set "match" to false if the character on the stack doesn't "match" the expected value. This will also be more efficient.


I was in the middle of composing this answer when the original question apparently got deleted.

If you want me to post a code example I'm happy to do so, but I think you might learn more if I don't. If you want a code example, or want me to have a look at what you come up with after this answer, feel free.

指尖凝香 2024-10-15 00:32:26

我只是扫了一眼..可能是误解:

while(i<length){
   if(isalnum(word[i]))  {
       temp[n]=word[i];
       push(&head, word[i]);
       n++;

   }
   i++;

}

I just glanced over..might be misunderstanding:

while(i<length){
   if(isalnum(word[i]))  {
       temp[n]=word[i];
       push(&head, word[i]);
       n++;

   }
   i++;

}

傻比既视感 2024-10-15 00:32:26

对于如此短的跳转,重写以消除问题是微不足道的。

while(i<length){
   if(isspace(word[i]) || !isalpha(word[i]))  {
       if(!isdigit(word[i])) {
           i++;
           continue;
       }
   }
   temp[n]=word[i];
   push(&head, word[i]);
   i++;
   n++;
}

For such a short jump, it's trivial to re-write to remove the problem.

while(i<length){
   if(isspace(word[i]) || !isalpha(word[i]))  {
       if(!isdigit(word[i])) {
           i++;
           continue;
       }
   }
   temp[n]=word[i];
   push(&head, word[i]);
   i++;
   n++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文