简体   繁体   中英

Finding the length of an integer with switch in C or C++

I want to find the length of an integer in C or C++ by using a switch statement I know there are other simple ways but I want use this method for some reasons so here is what I have tried so far:

#include <iostream>
#include <conio.h>

main() {
int a,length;
std::cout << "Please enter a number:\n";
std::cin >> a;
if(a<0)
    a=-a;
switch(a){
case 1<...<10:length==1,break;
case 10<...<100:length==2,break;
case 100<...<1000:length==3,break;
}
std::cout << "It has "<< length <<" numbers\n"; 
getch();    
}

So , I dont know what I can use instead of cases.

If you are using the GCC extension, can be described as follows.

switch(a){
case 1 ... 9     : length = 1; break;
case 10 ... 99   : length = 2; break;
case 100 ... 999 : length = 3; break;
}

It is tempting to use a series of if statements like

if (a >= 10000){
    length = 5;
} else if (/* And so on

But this requires you to make assumptions about the possible maximum size of an int . This is not defined by C or C++, although the minimum possible range is [-32767 to +32767].

Some compilers allow you to use a range-based switch but this is (i) not standard C or C++, and (ii) you'll still encounter the problem with possible range above.

A better approach is to repeatedly divide your int by 10 and count the number of times you can do that until you reach zero. And don't forget to test your work with a negative number and an initial value of 0.

Computing the base-10 logarithm is problematic due to the computation taking place in floating point.

You can use the following approach with the switch statement. Though in my demonstrative program I use only the label default you may include any case label that corresponds to the enumeration

#include <iostream>

int main()
{
    while ( true )
    {        
        enum : unsigned int { One = 1, Two, Three, Four, Five, Six, Seven, Eight, Nine } digits;

        std::cout << "Enter a non-negative number (0-exit): ";

        unsigned int x = 0;
        std::cin >> x;

        if ( !x ) break;

        int len;


        switch ( ( digits = One,   x < 10 ) ||
                 ( digits = Two,   x < 100 ) ||
                 ( digits = Three, x < 1000 ) ||
                 ( digits = Four,  x < 10000 ) ||
                 ( digits = Five,  x < 100000 ) ||
                 ( digits = Six,   x < 1000000 ) ||
                 ( digits = Seven, x < 10000000 ) ||
                 ( digits = Eight, x < 100000000 ) ||
                 ( digits = Nine,  x < 1000000000 ), digits )
        {
                default:
                    len = digits;
                    break;
        }

        std::cout << x << " has " << len << " digits." << std::endl;
    }        
}

The program output might look like

Enter a non-negative number (0-exit): 1
1 has 1 digits.
Enter a non-negative number (0-exit): 11
11 has 2 digits.
Enter a non-negative number (0-exit): 111
111 has 3 digits.
Enter a non-negative number (0-exit): 1111
1111 has 4 digits.
Enter a non-negative number (0-exit): 11111
11111 has 5 digits.
Enter a non-negative number (0-exit): 111111
111111 has 6 digits.
Enter a non-negative number (0-exit): 1111111
1111111 has 7 digits.
Enter a non-negative number (0-exit): 11111111
11111111 has 8 digits.
Enter a non-negative number (0-exit): 111111111
111111111 has 9 digits.
Enter a non-negative number (0-exit): 0

An alternative representation of the switch including other case labels

    switch ( ( digits = One,   x < 10 ) ||
             ( digits = Two,   x < 100 ) ||
             ( digits = Three, x < 1000 ) ||
             ( digits = Four,  x < 10000 ) ||
             ( digits = Five,  x < 100000 ) ||
             ( digits = Six,   x < 1000000 ) ||
             ( digits = Seven, x < 10000000 ) ||
             ( digits = Eight, x < 100000000 ) ||
             ( digits = Nine,  x < 1000000000 ), digits )
    {
        case One:
            std::cout << "You could enter more digits.." << std::endl;
        case Two: case Three: case Four: case Five: case Six: case Seven: case Eight:
            len = digits;
            break;
        case Nine:
            std::cout << "Do you like money?" << std::endl;
            len = digits;
            break;
        default:
            std::cout << "Too big number! It can be never" << std::endl;
            break;
    }

you can't use switch for that purpose.Use if-else statements instead

#include <iostream>
#include <conio.h>

int main() {
    int a,length;
    std::cout << "Please enter a number:\n";
    std::cin >> a;
    if(a<0)
        a=-a;
    if( a >= 1 && a < 10 )
        length = 1;
    else if( a >= 10 && a < 100 )
        length = 2;
    /* .............. */

    std::cout << "It has "<< length <<" numbers\n"; 
    getch();    
}

You cannot use comparisons in switch case. I would recommend a series of if else if in this case with progressively lower limits:

if (a >= 100) length = 3;
else if (a >= 10) length = 2;
else length = 1;

Add as many as needed for the number of digits you want to handle.

Also note you have to assign to length . When you are doing length==1 you are returning a boolean value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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