C中的功能和循环故障

时间:2014-11-09 11:17:41

标签: c arrays loops

此程序希望用户输入一个7位数字(1和0除外),任何数字都有一组相应的字母 (2 = ABC,3 = DEF,4 = GHI,5 = JKL,6 = MNO,7 = PRS,8 = TUV,9 = XYZ,如在美国的电话上所见)。最后,它应该输出所有2187个可能的字母序列。

ex:输入2345678     输出应该是ADGJMPT ADGJMPU ADGJMPV ADGJMRT ADGJMRU ADGJMRV ADGJMST .......... CFILOSV

但我的输出总是AAAAAAA AAAAAAB AAAAAAC .......... CCCCCCC

(我在检查号码方面也遇到了麻烦。我首先设置一个循环和数组,if (che[1] != 1 && che[0] != 1) break;但有时候它不会中断。)

你能解释一下是什么错吗?

#include<stdio.h>

int main(){
    int che[50] = { 0 };

    int a, b, c, d, e, f, g, i, r, q, number, check;

    char word2[7];

    char word1[8][3] = {
            { 'A', 'B', 'C' },
            { 'D', 'E', 'F' },
            { 'G', 'H', 'I' },
            { 'J', 'K', 'L' },
            { 'M', 'N', 'O' },
            { 'P', 'R', 'S' },
            { 'T', 'U', 'V' },
            { 'W', 'X', 'Y' } };


    while (1)
    {
        printf("Enter seven digit(except 0 and 1):");
        scanf("%d", &number);

        check = number;
        for (; number != 0; number /= 10)
        {
            q = number % 10;
            che[q] = 1;
        }
        if (che[1] != 1 && che[0] != 1) break;
    }
    number = check;

    for (i = 6; number == 0; i--)
    {
        r = number % 10;
        if (r == 2){ word2[i] = 0; }
        if (r == 3){ word2[i] = 1; }
        if (r == 4){ word2[i] = 2; }
        if (r == 5){ word2[i] = 3; }
        if (r == 6){ word2[i] = 4; }
        if (r == 7){ word2[i] = 5; }
        if (r == 8){ word2[i] = 6; }
        if (r == 9){ word2[i] = 7; }
        number /= 10;
    }

    for (a = 0; a < 3; a++){

        for (b = 0; b < 3; b++){

            for (c = 0; c < 3; c++){

                for (d = 0; d < 3; d++){

                    for (e = 0; e < 3; e++){

                        for (f = 0; f < 3; f++){

                            for (g = 0; g < 3; g++){
                                printf("%c%c%c%c%c%c%c ",word1[word2[0]][a], word1[word2[1]][b], word1[word2[2]][c], word1[word2[3]][d], word1[word2[4]][e], word1[word2[5]][f], word1[word2[6]][g]);
                            }
                        }
                    }
                }
            }
        }
    }

    return 0;
}

2 个答案:

答案 0 :(得分:2)

主要问题在于:

for (i = 6; number == 0; i--)

循环条件与应该循环条件相反。你希望继续迭代这个数字,直到你达到0(通过连续除以10)。

应该是

for (i = 6; number != 0; i--)

for (i = 6; i >= 0; i--)

另外请注意

    if (r == 2){ word2[i] = 0; }
    if (r == 3){ word2[i] = 1; }
    if (r == 4){ word2[i] = 2; }
    if (r == 5){ word2[i] = 3; }
    if (r == 6){ word2[i] = 4; }
    if (r == 7){ word2[i] = 5; }
    if (r == 8){ word2[i] = 6; }
    if (r == 9){ word2[i] = 7; }

相当于

 if (r >= 2 && r <= 9)
   word2[i] = r - 2;

答案 1 :(得分:1)

我认为这可以按预期工作: 为了方便调试,我更改了一些IO格式:

输入分隔数字2-9除以空格并以-1结束。

输入序列可以是任意长度,只以-1结尾。

例如,输入:2 3 4 2 -1

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char word1[8][3] = {
    { 'A', 'B', 'C' },
    { 'D', 'E', 'F' },
    { 'G', 'H', 'I' },
    { 'J', 'K', 'L' },
    { 'M', 'N', 'O' },
    { 'P', 'R', 'S' },
    { 'T', 'U', 'V' },
    { 'W', 'X', 'Y' } };

void translate(char * headStr,int * pattern,int pos_to_do)
{
    if(pattern[pos_to_do]<0)
    {
        printf("%s\n",headStr);
        return;
    }
    char str[3][20];
    int i;
    for(i=0;i<3;i++)
    {
        strcpy(str[i],headStr);
        char str_this[2];
        str_this[0]=word1[ pattern[pos_to_do] ][i];
        str_this[1]='\0';
        strcat(str[i],str_this);
        translate(str[i],pattern,pos_to_do+1);
    }
    return;
}

int main(){
    int che[20];

    int number, check,len;

    while (1)
    {
    printf("Enter digits(except 0 and 1):");
    len=0;
    scanf(" %d", &number);
    while(number>=2)
    {
        che[len]=number-2;
        len++;
        scanf("%d", &number);
    }
    che[len]=-1;
    char str_start[]="";
    translate(str_start,che,0);
    }
    return 0;
}

测试输出:

Enter digits(except 0 and 1):2 3 4 -1
ADG
ADH
ADI
AEG
AEH
AEI
AFG
AFH
AFI
BDG
BDH
BDI
BEG
BEH
BEI
BFG
BFH
BFI
CDG
CDH
CDI
CEG
CEH
CEI
CFG
CFH
CFI
Enter digits(except 0 and 1):