Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 每次运行C++;测验_C++_Xcode_Testing - Fatal编程技术网

C++ 每次运行C++;测验

C++ 每次运行C++;测验,c++,xcode,testing,C++,Xcode,Testing,我正在编写一个接收字符数组的程序。在该字符数组中,数字由空格分隔。程序应该接收一个数字并将该数字的回文添加到自身中。如果结果不是回文,那么我们应该将结果的回文添加到结果中,依此类推,直到结果是回文。例如,如果字符数组为“195”,则195+591=786。786不是回文,所以786+687=1473。1473 + 3741 = 5214. 最后是5214+4125=9339,这是一个回文,这就是答案。然后,程序应该返回为得到答案而运行的加法次数,然后是答案。在本例中,程序将返回“49339”

我正在编写一个接收字符数组的程序。在该字符数组中,数字由空格分隔。程序应该接收一个数字并将该数字的回文添加到自身中。如果结果不是回文,那么我们应该将结果的回文添加到结果中,依此类推,直到结果是回文。例如,如果字符数组为“195”,则195+591=786。786不是回文,所以786+687=1473。1473 + 3741 = 5214. 最后是5214+4125=9339,这是一个回文,这就是答案。然后,程序应该返回为得到答案而运行的加法次数,然后是答案。在本例中,程序将返回“49339”

我的程序可以运行(据我所知),但无论出于什么原因,每当我运行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;
}
#包括
#包括
使用名称空间std;
//返回数字的倒数
无符号长反转(无符号长n){
无符号长反转=0;
而(n!=0){
无符号长余数=n%10;
反向=反向*10+余数;
n/=10;
}
反向返回;
}
//返回回文结果是什么
字符串回文(字符串编号字符串、整型和计数器){
计数器++;
//将输入(字符串)转换为int
无符号长编号=std::stol(numberString);
//反转编号并将其指定给numberReversed
无符号长数字反向=反向(数字);
//加上数字及其倒数
无符号长结果=数字+数字相反;
//反转结果并将其指定给resultReversed
无符号长结果反向=反向(结果);
//检查结果和其相反方向是否相等;否则,继续进行,直到它们相等为止
while(result!=resultReversed){
计数器++;
//结果重新分配号码
数字=结果;
//反转编号并将其指定给numberReversed
编号反向=反向(编号);
//加上数字及其倒数
结果=数字+数字相反;
//反转结果并将其指定给resultReversed
结果反向=反向(结果);
}
//返回结果
返回std::to_字符串(结果);
}
//“主要”方法
字符*查找(常量字符*数组){
//Instatnite计数器,稍后将使用
int计数器=0;
//实例化返回的结果字符串
字符串结果=”;
int i=0;
//将用于构造检查为回文的int
字符串currentNumberConstruction=“”;
//遍历数组直到数组结束
while(数组[i]!='\0'){
//如果找到一个空间
if(数组[i]=''){
//调用回文函数并将其添加到以后的结果中
字符串回文数=回文数(currentNumberConstruction,计数器);
结果+=std::to_字符串(计数器);
//添加到结果中,直到找到回文为止有多少个循环
结果+=“”+回文数+“”;
//重置计数器(找到回文之前的循环数)
计数器=0;
//重置currentNumberConstruction(将int检查为回文)
currentNumberConstruction=“”;
//通过数组继续
i++;
}否则{
//将选中的字符添加到currentNumberConstruction(将int作为回文进行检查)
currentNumberConstruction+=数组[i];
//通过数组继续
i++;
}
}
如果(currentNumberConstruction!=“”){
字符串回文数=回文数(currentNumberConstruction,计数器);
结果+=std::to_字符串(计数器);
结果+=“”+回文数;
计数器=0;
currentNumberConstruction=“”;
i++;
}
//将结果从字符串转换为字符*
char*realResult=新字符[result.length()];
for(无符号整数j=0;jchar* array = NULL;
char array[100];
char* realResult = new char[result.length()];
char* realResult = new char[result.length()+1];
realResult [result.length()] = 0;