回答两个编译器之间的差异

Answer difference between two compiler

本文关键字:之间 编译器 两个      更新时间:2023-10-16

我编译了以下代码,但我遇到了一个严重的问题。当我在Visual Studio 2015中编译此代码时,它运行良好。但是,当我在 Dev C++ 中编译此代码时,我认为它不会打印"是"作为答案。

例如,当我键入诸如

  1. 哈哈
  2. 我看到的是汽车还是猫?
  3. abcdefghiihgfedcba

这些输入必须返回 yes,但在 dev c++ 中返回 no。

为什么会出现此问题?

#include <iostream>
#include <string>
using namespace std;
bool is_palindrome(char input[], int numOfSlots);
int main(void) {
   char text[256], fixed[256];
   cin.getline(text, sizeof(text), 'n');
   for (int i = 0; i < sizeof(text); i++) {
      text[i] = toupper(text[i]);
   }
   int j = 0;
   for (int i = 0; i < sizeof(text); i++) {
      if ((text[i] >= '0' && text[i] <= '9') || (text[i] >= 'A' && text[i] <= 'Z')) {
         fixed[j] = text[i];
         j++;
      }
   }
   fixed[j] = '';
   string s_fixed = fixed;
   if (is_palindrome(fixed, s_fixed.length()) == true) {
      cout << "Yes";
   }
   else {
      cout << "No";
   }
   return 0;
}
bool is_palindrome(char input[], int numOfSlots) {
   int i = 0;
   while (i < numOfSlots / 2)
   {
      if (input[i] != input[(numOfSlots - 1) - i])
         return false;
      i++;
   }
   return true;
}

程序表现出未定义的行为,因为您使用的是未初始化的数据。

你有:

char text[256], fixed[256];

哪些是未初始化的数组。然后你去访问它们:

for (int i = 0; i < sizeof(text); i++) {
   text[i] = toupper(text[i]); // Accessing uninitialized array
}

您可以使用几种方法修复它:

  1. 初始化数组。

    char text[256] = {0}, fixed[256] = {0};
    
  2. 仅访问在调用中填充的元素 getline

    size_t size = strlen(text);
    for (int i = 0; i < size; i++) {
    

但是,更好的解决方法是始终使用第二种方法。这样,您就不会处理不必要的数据。

使用 std::string 代替strlen()是相当奇怪的,当你可以更好地使用它时:

bool is_palindrome( const std::string &input );
int main(void) {
   std::string text;
   getline(cin,text);
   for (size_t i = 0; i < text.length(); i++) {
      text[i] = toupper(text[i]);
   }
   std::string fixed;
   for (size_t i = 0; i < text.length(); i++) {
      if ((text[i] >= '0' && text[i] <= '9') || (text[i] >= 'A' && text[i] <= 'Z')) {
         fixed += text[i];
      }
   }
   if (is_palindrome(fixed)) {
      cout << "Yes";
   }
   else {
      cout << "No";
   }
   return 0;
}
bool is_palindrome(const std::string &input) {
   size_t numOfSlots = input.length();
   int i = 0;
   while (i < numOfSlots / 2)
   {
      if (input[i] != input[(numOfSlots - 1) - i])
         return false;
      i++;
   }
   return true;
}

当然,您的程序可以简化,但我试图使其接近原始程序,以说明为什么最好使用std::string而不是旧式char[] C++

这里使用标准库中的std::string和其他算法的简化版本:

#include <iostream>
#include <string>
#include <algorithm>
bool is_palindrome( std::string str )
{
   if( str.empty() ) return false;
   std::transform( str.begin(), str.end(), str.begin(), []( char c ) { return std::toupper( c ); } );
   str.erase( std::remove_if( str.begin(), str.end(), []( char c ) { return !std::isalnum( c ); } ), str.end() );
   auto len = str.length() / 2 + 1;
   return std::string( str.begin(), std::next( str.begin(), len ) ) ==
          std::string( str.rbegin(), std::next( str.rbegin(), len ) );
}

int main()
{
   std::string text;
   std::getline( std::cin, text );
   std::cout << ( is_palindrome( text ) ? "yes" : "no" ) << std::endl;
   return 0;
}
相关文章: