Xcode每次运行C++测试都会给出不同的结果
我正在接收一个char数组的程序。在那个char数组中,数字是用空格分隔的。该程序应该收到一个号码并将该号码的回文号码添加到自己。如果结果不是回文,那么我们应该将结果的回文结果添加到结果中,等等,直到结果为回文。例如,如果char数组是“195”,那么195 + 591 = 786. 786不是回文,所以786 + 687 = 1473。1473 + 3741 = 5214。最后5214 + 4125 = 9339,这是一个回文,所以这是答案。然后,程序应该返回它运行的答案的数量,然后回答答案。在这个例子中,程序将返回“4 9339”。Xcode每次运行C++测试都会给出不同的结果
我的程序工作(据我所知),但无论什么原因,每当我运行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 = "123456789";
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;
}
你的问题是在这里:
char* array = NULL;
其实你不分配任何内存来存储阵列,你需要,给它一些内存的方式来初始化此。否则,你只是在系统中选择随机存储器,最终你可能会得到一个SegFault。 这可能就像这样:
char array[100];
或者你可以使用的std :: string代替。这会创建一个包含100个字符的字符数组,您可以根据需要调整大小。
不知道它是唯一的错误,但你不空终止结果:
char* realResult = new char[result.length()];
应该是:
char* realResult = new char[result.length()+1];
realResult [result.length()] = 0;
否则的行为可能是很随机的。
欢迎来到Stack Overflow!这听起来像你可能需要学习如何使用调试器来遍历代码。使用一个好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏离的位置。如果你打算做任何编程,这是一个重要的工具。进一步阅读:** [如何调试小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver