我正在开发一个接收char数组的程序。在该char数组中是由空格分隔的数字。该程序应该接收一个数字并将该数字的回文添加到自身。如果结果不是回文结果,那么我们应该将结果的回文结果添加到结果中,依此类推,直到结果为回文结果。例如,如果char数组是" 195",则195 + 591 = 786. 786不是回文,因此786 + 687 = 1473. 1473 + 3741 = 5214.最后5214 + 4125 = 9339,这是一个回文,所以这就是答案。然后,程序应该返回它运行的添加次数以获得答案,然后是答案。在此示例中,程序将返回" 4 9339"。
我的程序工作(据我所知),但无论出于何种原因,每当我运行Xcode时,它都会返回不同的结果。例如,有一次我运行它一切都很好,除了案例7.下次我运行它,每一次测试都失败了。如果我再次运行它,除了7和9之外,每个案例都有效。有人知道为什么会这样吗?下面是我的所有代码,包括Xcode运行的测试用例。我也尝试评论该计划中发生的事情。
感谢任何人的帮助!对于C ++来说,我是一个业余爱好者,所以它可能是我忽视的微不足道的东西,或者它可能是更高级的东西 - 我老实说不太确定。提前谢谢大家!
编辑:我使用过Xcode的调试器,当我这样做的时候没有任何失败或看起来不合适,这使我在没有调试的情况下运行测试时失败的原因更加神秘。 编辑2:然后测试用例由我的教授提供,而不是我自己。#include <iostream>
#include <string>
using namespace std;
//returns the reverse of a number
unsigned long reverse(unsigned long n) {
unsigned long reverse = 0;
while(n != 0) {
unsigned long remainder = n%10;
reverse = reverse*10 + remainder;
n /= 10;
}
return reverse;
}
//return what the palindrome result is
string palindrome(string numberInString, int &counter) {
counter++;
//convert input, which is a string, to int
unsigned long number = std::stol(numberInString);
//reverse number and assign it to numberReversed
unsigned long numberReversed = reverse(number);
//add the number and its reverse
unsigned long result = number + numberReversed;
//reverse the result and assign it to resultReversed
unsigned long resultReversed = reverse(result);
//check to see if result and its reverse are equal; otherwise, keep going until they are
while (result != resultReversed) {
counter++;
//reassign number as result
number = result;
//reverse number and assign it to numberReversed
numberReversed = reverse(number);
//add the number and its reverse
result = number + numberReversed;
//reverse the result and assign it to resultReversed
resultReversed = reverse(result);
}
//return result
return std::to_string(result);
}
//the "main" method
char* find(const char* array) {
//instatntite counter, which will be used later
int counter = 0;
//instantiate result string, which is what we are returning
string result = "";
int i = 0;
//will be used to construct int being checked as a palindrome
string currentNumberConstruction = "";
//go through array until end of array
while (array[i] != '\0') {
//if find a space
if (array[i] == ' ') {
//call palindrome function and add it to result later on
string palindromeNumber = palindrome(currentNumberConstruction, counter);
result += std::to_string(counter);
//add to result how many cycles until palindrome found
result += " " + palindromeNumber + " ";
//reset counter (how many cycles until palindrome found)
counter = 0;
//reset currentNumberConstruction (int being checked as a palindrome)
currentNumberConstruction = "";
//continue through array
i++;
} else {
//add char checked to currentNumberConstruction (int being checked as a palindrome)
currentNumberConstruction += array[i];
//continue through array
i++;
}
}
if (currentNumberConstruction != "") {
string palindromeNumber = palindrome(currentNumberConstruction, counter);
result += std::to_string(counter);
result += " " + palindromeNumber;
counter = 0;
currentNumberConstruction = "";
i++;
}
//convert result from string to char*
char* realResult = new char[result.length()];
for (unsigned int j = 0; j < result.length(); j++) {
realResult[j] = result[j];
}
//return char* realResult
return realResult;
}
int main() {
const char* array = NULL;
const char* expected = 0;
for (int i = 0; i < 10; i++) {
switch (i) {
case 0:
array = "195 265 750";
expected = "4 9339 5 45254 3 6666";
break;
case 1:
array = "2 99 4000000000 20 100 1";
expected = "1 4 6 79497 1 4000000004 1 22 1 101 1 2";
break;
case 2:
array = "79 88 97 99";
expected = "6 44044 6 44044 6 44044 6 79497";
break;
case 3:
array = "157 158 166 167 175 188 193 197";
expected = "3 8888 3 11011 5 45254 11 88555588 4 9559 7 233332 8 233332 7 881188";
break;
case 4:
array = "266 273 274 292 365";
expected = "11 88555588 4 5115 4 9559 8 233332 11 88555588";
break;
case 5:
array = "1089 1091 1099";
expected = "4 79497 1 2992 2 11011";
break;
case 6:
array = "19991 2914560 12345678";
expected = "8 16699661 5 47977974 1 99999999";
break;
case 7:
array = "777";
expected = "4 23232";
break;
case 8:
array = "130031 9";
expected = "1 260062 2 99";
break;
case 9:
array = "1234567890123456789";
expected = "2 12222222211222222221";
break;
default:
cout << "we should never get here" << endl;
return -1;
}
char* actual = find(array);
bool equal = strcmp(expected, actual) == 0;
cout << "test " << (i + 1) << ": " << (equal ? "ok" : "failed");
if (!equal) {
cout << " expected [" << expected << "] but was [" << actual << "]";
}
cout << endl;
delete actual;
}
return EXIT_SUCCESS;
}
答案 0 :(得分:0)
你的问题在这里:
char* array = NULL;
你实际上没有分配任何内存来存储数组,你需要以一种给它一些内存的方式初始化它。否则,您只是在系统中选择随机内存,最终可能会获得SegFault。 这可以这样做:
char array[100];
或者您可以使用std :: string代替。这将创建一个包含100个字符的字符数组,您可以根据需要调整大小。
答案 1 :(得分:0)
不确定这是否是唯一的错误,但是你没有终止结果:
char* realResult = new char[result.length()];
应该是:
char* realResult = new char[result.length()+1];
realResult [result.length()] = 0;
否则行为可能非常随机。