1

「0」から「3」の文字列で機能するこのコードを整数に変換して、より高い数値で機能するようにしようとしています

#include <string>
#include <iostream>
using namespace std;

void permutate(char[], int );
bool recurse(char[], int );

int main()
{
    int strLength;
    cout << "Enter your desired length: ";
    cin >> strLength;
    char strArray[strLength];

    for (int i = 0; i<strLength; i++)
        strArray[i] = '0';

    permutate(strArray, sizeof(strArray));

    return 0;
}

void permutate(char charArray[], int length)
{
    string wait;
    length--;
    bool done = false;

    while(!done)
    {
        for (int i = 0; i <= length; i++)
            cout << charArray[i];
        cout << endl;

        if (charArray[length] == '3')
            done = recurse(charArray, length);
        else
            charArray[length] = (char)(charArray[length]+1);

    }
}

bool recurse(char charArray[], int length)
{
    bool done = false;
    int temp = length;
    if (temp > 1)
    {
        charArray[temp] = '0';
        if (charArray[temp-1] == '3')
        {
            temp--;
            done = recurse(charArray, temp);
        }
        else
            (charArray[temp-1] = (char)(charArray[temp-1] + 1));

    }
    else
    {
        charArray[temp] = '0';
        if (charArray[temp-1] == '3')
            done = true;
        else
            charArray[temp-1] = (char)(charArray[temp-1]+1);
    }
    return done;
}

すべての char を int に変更しました。

  • '0' = 0、'3' = 3 ごと
  • すべて (charArray[temp-1] = (char)(charArray[temp-1] + 1)); to charArray[temp-1]++;

デバッグしようとしましたが、まだ動作させることができません:(

それを修正するために管理しました(高い数値で機能します):

#include <string>
#include <iostream>
using namespace std;

void permutate(int[], int, int );
bool recurse(int[], int, int );

int main()
{
int strLength, nrElem;
cout << "Enter your desired length: ";
cin >> strLength;
cout << "Enter nr elem: ";
cin >> nrElem;
int strArray[strLength];

for (int i = 0; i<strLength; i++)
strArray[i] = 0;

permutate(strArray, strLength, nrElem );
cout << "\nSTOP";
return 0;
}

void permutate(int charArray[], int length, int nrElem)
{

//  length--;
    bool done = false;

    while(!done)
    {

    for (int i = 0; i < length; i++)
    cout << charArray[i] << " ";

    cout << endl;


    if (charArray[length - 1] == nrElem)
        //done = true;
        done = recurse(charArray, length, nrElem);
    else
        charArray[length - 1]++;

}
}

bool recurse(int charArray[], int length, int nrElem)
{
bool done = false;
int temp = length ; 
if (temp > 1)
{
charArray[temp] = 0;
if (charArray[temp-1] == nrElem)
{
temp--;
done = recurse(charArray, temp, nrElem);
}
else
charArray[temp-1]++;

}
else
{
charArray[temp] = 0;
if (charArray[temp-1] == nrElem)
done = true;
else
charArray[temp-1]++;
}
return done;
}
4

3 に答える 3

0

これは同じことを行うための短いコードです(答えではありませんが、コメントフィールドでは正しく見えませんでした)。再帰が必要かどうかはわかりません。そうしない場合、このコードは興味深いかもしれません:

#include <iostream>
#include <sstream>
using namespace std;

string output(int firstIntSize, int secondIntSize)
{
    std::ostringstream oss;
    for (int i = 0; i<firstIntSize; i++)
    {
        for (int j = 0; j< secondIntSize; j++)
        {  
            oss << i << j << " "; 
        }
    }
    return oss.str();
}
int main()
{
    cout << output(2,3);
    return 0;
}
于 2013-10-29T15:34:31.153 に答える