【问题标题】:Function and loop trouble in CC中的函数和循环问题
【发布时间】:2014-11-09 11:17:41
【问题描述】:

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

例如:输入 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;
}

【问题讨论】:

  • codeSmellHomework++;

标签: c arrays loops


【解决方案1】:

主要问题在这里:

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;

【讨论】:

  • 非常感谢您的帮助!!!我真的很想提高你的声誉,但我的声誉如此之低,SORRY!!
【解决方案2】:

我认为这可以按您的预期工作: 为了调试方便,我更改了一些 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):

【讨论】:

  • 你太专业了!!!也感谢您的建议。我很高兴学习您的代码。
  • 然后给他答案。如果你不这样做,人们很快就会停止帮助你(做作业)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-15
  • 2014-08-22
  • 1970-01-01
  • 2023-02-13
  • 1970-01-01
  • 2014-12-11
  • 1970-01-01
相关资源
最近更新 更多