C Language Learning Records (3)

Keywords: Programming C less

C Language Learning Records

C Language Practice on Mooc

Read integer

Topic:
Your program reads an integer in the range of [- 100,000,100,000]. Then, each bit of the integer is output in Pinyin.
If input 1234, output:
yi er san si
Note that there is a space between the Pinyin of each word, but there is no space behind the last word. When a negative number is encountered, add "fu" at the beginning of the output. For example, the output of -2341 is:
fu er san si yi
Input format:
An integer with a range of [-100,000,100,000].
Output format:
The Chinese phonetic alphabet for each digit of this integer is separated by spaces, and there is no space at the end.
Input sample:
-30
Output sample:
fu san ling
Time limit: 500ms memory limit: 32000kb

Problem solving:
First analyze the value of i, and note the output when I equals zero. What should I do when I is negative?
Analyse how to get each number of i in positive order.
How to determine the time to add space.
The output is relatively simple, and swith output is used directly after obtaining.

#include <stdio.h>

int main(int argc, char **argv) {
    int i,x,d,mask=1;
    scanf("%d",&i);
    
    if(i==0)
    {
        printf("ling");
    }else if(i>0)
    {
        x=i;
        while(x>9)
        {
            x/=10;
            mask*=10;
        }
        //printf("%d\n",mask);
        do{
            d=i/mask;
            i%=mask;
            mask/=10;
            switch(d){
            case 0:printf("ling");break;
            case 1:printf("yi");break;
            case 2:printf("er");break;
            case 3:printf("san");break;
            case 4:printf("si");break;
            case 5:printf("wu");break;
            case 6:printf("liu");break;
            case 7:printf("qi");break;
            case 8:printf("ba");break;
            case 9:printf("jiu");break;
            }
            if(mask>0)
            {
                printf(" ");
            }
        }while(mask>0);
    }else
    {
        printf("fu ");
        i = -i;
        x=i;
        while(x>9)
        {
            x/=10;
            mask*=10;
        }
        //printf("%d\n",mask);
        do{
            d=i/mask;
            i%=mask;
            mask/=10;
            switch(d){
            case 0:printf("ling");break;
            case 1:printf("yi");break;
            case 2:printf("er");break;
            case 3:printf("san");break;
            case 4:printf("si");break;
            case 5:printf("wu");break;
            case 6:printf("liu");break;
            case 7:printf("qi");break;
            case 8:printf("ba");break;
            case 9:printf("jiu");break;
            }
            if(mask>0)
            {
                printf(" ");
            }
        }while(mask>0);
    }
    return 0;
}

Be careful:
1. When i equals 0, the output should be a fixed value. When i is less than 0, it should be turned into a positive integer first. At the same time, the result should be added a "fu" output.
2. The variable of the condition of adding spaces should be mask.
3. The judgment condition for the end of the second while loop should be a mask variable.

Posted by pagie on Sat, 02 Feb 2019 02:30:16 -0800