2

我一直在尝试用 C++ 创建一个程序来尝试完成这个伪代码:

get argv[1] into int

get int’s digits into array[int length]

for int i = array length; i >= 0;

gen random number into check

if check == array[i]
    i
    say Number i was check
end if

我认为我真正挣扎的部分是

get argv[1] into int

get int’s digits into array[int length]

部分。在我的完整代码中甚至没有尝试,因为我尝试过的任何东西都不起作用。我得到的最多的错误是代码可以编译,但每次它尝试时cout << "Number 1:" << number我都会得到Number 1: 0无论我输入的实际数字。当0 == 0代码甚至没有注意到时。

我破坏的可能违反约定的代码如下:

#include <iostream>
#include <string>

int main (int argc, char **argv) {

    if (argc == 1 || argc == 3) {
        std::cout << "Argument count does not match (one argument expected)\n";
        return(-1);
    }

    std::cout << "Input: " << argv[1] << "\n";

    const char* text = argv[1];
    int number = atoi(text);

        int check = rand() % 10;

        std::cout << "Check 1: " << check << "\nNumber 1: " <<  number << "\n";

        if (check == array[i]) {
            i++;
            std::cout << "Success! Number " << i << " was " << check << ".\n";
        }
    }
}

TL; DR:我的“某种”数字破解者不想将 argv 1放入一个 int 中,而该 int 的数字稍后会放入一个数组中。

随意让我感到愚蠢。希望问题不要太具体。我将按要求详细说明。

编辑:这是转换的早期尝试:

    int array[];
    for (int i = strlen(text); i >= 0; i--) {
        array[i] = number % 10;
        number /= 10;
    }

EDIT2:这么多的回应,没有解决方案。谢谢你试图一次解释这个新手这么多事情。顺便说一句:Git

4

3 回答 3

2

早期的尝试几乎是好的:只是你必须为数组实际分配空间,如下所示:

int array[strlen(text)];

如果您的编译器支持可变长度数组作为扩展,并且

std::vector<int> array;
array.resize(strlen(text));

如果您想坚持使用标准 C++ 并遵循一些好的做法。

但是,如果您想变得棘手,您甚至不需要将参数转换为数字:

if (argv[1][i] == check % 10 + '0')

也可以。总而言之,完整的程序如下所示:

#include <iostream>
#include <cstdlib>


int main(int argc, char *argv[])
{
    int check = std::rand();
    std::cout << check << std::endl;

    char *p = argv[1] + strlen(argv[1]);
    while (p - argv[1] >= 0) {
        if (*--p == '0' + check % 10)
            std::cout << "guessed " << p - argv[1] << "th digit" << std::endl;

        check /= 10;
    }

    return 0;
}
于 2013-07-16T13:35:02.297 回答
1

您的代码相对接近正确。您正在为数组的声明而苦苦挣扎(您必须为其指定大小)。32位int不能超过十位,所以声明

int array[10];

应该足够了。

在将数字转换为数字数组之前,检查它是否为负数,如果为负数则翻转其符号:

if (number < 0) {
    number = -number;
}

否则,你的number%10把戏是行不通的。

当您进行转换时,请计算您有多少位数。将结果放入actualCount变量中:您可能不会用完数组中的所有数字,所以

int check = rand() % 10; // 10 is the max, not the actual digit count

应该

int check = rand() % actualCount;

你的参数检查也需要改进:想想如果用户传递五个参数会发生什么?如果你期望只有一个论点,你应该写

if (argc != 2) {
    std::cout << "Argument count does not match (one argument expected)\n";
    return(-1);
}
于 2013-07-16T13:36:39.950 回答
0

为了一次只从一个数字中提取一个数字,您有几个选择。

为方便起见,您可以使用 a std::string,将原始字符串 ( argv[1]) 插入其中,然后一次提取一个char

#include <string>

...    

// put the input in a string
std::string text = argv[1];

for (unsigned i = 0; i < text.size(); i++)
{
   // extract only one char, a digit
   char ch = text.at(i);
   // convert that char in a number
   int n = ::atoi(& ch);

   // use n
   ...
}

如果您不想使用std::string,您可以随时使用类似 c 的数组(argv[1]本身):

#include <cstring>

...    

for (unsigned i = 0; i < strlen(argv[1]); i++)
{
   // extract only one char, a digit
   char digit = argv[1][i];
   // convert that char in a number
   int num = ::atoi(& digit);

   // use n
   ...
}
于 2013-07-16T13:37:50.890 回答