简体   繁体   English

如果没有 switch case 循环,我该怎么做(例如计数器)

[英]how can i do it without switch case loop(counter for example)

I wrote this program that asks for the number of characters wish is alphabet in a single line and then calculates the possible configurations through that number..but the options are limited..so how can I make the selections unlimited in a domaine like for exp : you have maximum 4 characters in a line?`我编写了这个程序,它要求在一行中输入希望是字母表的字符数,然后通过该数字计算可能的配置...... : 一行最多 4 个字符?`

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[])
 {
    int Nb_Alpha,P_Kida = 1,i,j,k,h,Power = 1,nb,P_lvl,counter = 1;
    printf("enter the number of alphabet in one line :\t\n");
    scanf("%d",&Nb_Alpha);
    for(i=1;i<=Nb_Alpha;i++)//this is the Exponent made by me
    {
        P_Kida *= 26;
     }
  
        switch(Nb_Alpha)
        {
            case 1: 
            {
                
                for(i=65;i<=90;i++)
                        {
                            printf("%c\n",i);
                        }
            
             }  
            break;
             
             case 2 : {
                
                printf("Double probabilytis !\n");
                for(i=65;i<=90;i++)
                {
                    for(j=65;j<=90;j++)
                    {
                        printf("%c%c\n",i,j);
                    }
                }
             }
             break;
            
            case 3: 
            {
                    printf("Triple probabilytis !\n");
                    for(i=65;i<=90;i++)
                    {
                        for(j=65;j<=90;j++)
                        {
                          for(k=65;k<=90;k++)
                          {
                                printf("%c%c%c\n",i,j,k);
                          }
                        }
                    }
            
            }
            break;
            
                case 4: 
            {
                    printf("Quadra probabilytis !\n");
                    for(i=65;i<=90;i++)
                    {
                        for(j=65;j<=90;j++)
                        {
                          for(k=65;k<=90;k++)
                          {
                            for(h=65;h<=90;h++)
                            {
                                printf("%c%c%c%c\n",i,j,k,h);
                              }
                                
                          }
                        }
                    }
            
            }
            break;
            
        }
        
        
        
            
        printf("that is the %d probabilytis that u can make with %d alphabets\n",P_Kida,Nb_Alpha);
    return 0;
}`

One way to solve this is to use recursion.解决这个问题的一种方法是使用递归。

For instance:例如:

#define MAX 32

void rec(unsigned int nb, char* s, size_t len)
{
    if (nb == 0)
    {
        puts(s);
        return;
    }
    
    for(int i = 'A'; i <= 'Z'; i++)
    {
        s[len] = i;
        rec(nb - 1, s, len + 1);
    }
    s[len] = '\0';
}


void foo(unsigned int nb)
{
    assert(nb <= MAX);
    char x[MAX + 1] = "";
    
    for(int i = 'A'; i <= 'Z'; i++)
    {
        x[0] = i;
        rec(nb - 1, x, 1);
    }
}

int main(void)
{
    foo(6);
    return 0;
}

Recursion seems like an overkill here.递归在这里似乎有点矫枉过正。 Think of your configurations as numbers.将您的配置视为数字。

How would you list all the k-digit numbers?你会如何列出所有的 k 位数字? Obviously, start with 0...0 , and increment until it becomes 9...9 .显然,从0...0开始,然后递增直到它变成9...9

Your problem is exactly the same.你的问题完全一样。 Start with a...a , and increment until it becomes z...z .a...a开始,然后递增直到它变成z...z Now the question is how to increment a string?现在的问题是如何增加一个字符串? Same way as a number.与数字相同的方式。 If the last character is not yet z , increment it;如果最后一个字符还不是z ,则增加它; if it is make it a , and try to increment the previous one, etc.如果是使它成为a ,并尝试增加前一个,等等。

I hope you can take it from here.我希望你能从这里拿走它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM