The second blog park job. 2019.11.08

A. Program run shot

II. Functions Introduction

1. function declaration

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
void two(int n);//输入二进制数
void eight(int n);//输入八进制数
void ten(int n);//输入十进制数
void sixteen(int n);//输入十六进制数
void two_to_eight(char num[1000]);//把二进制转换为八进制
void two_to_ten(char num[1000]);//把二进制转换为十进制
void two_to_sixteen(char num[1000]);//把二进制转换为十六进制 
void eight_to_two(char num[1000]);//把八进制转二进制
void eight_to_ten(char num[1000]);//把八进制转十进制
void eight_to_sixteen(char num[1000]);//把八进制转十六进制
void ten_to_two(char num[1000]);//把十进制转二进制
void ten_to_eight(char num[1000]);//把十进制转八进制
void ten_to_sixteen(char num[1000]);//把十进制转十六进制
void sixteen_to_two(char num[1000]);//把十六进制转二进制
void sixteen_to_eight(char num[1000]);//把十六进制转八进制
void sixteen_to_ten(char num[1000]);//把十六进制转十进制
bool judge(int n, char num[1000]);//判断输入的数是否符合
int mypow(int x, int cnt);

Given the limited level of the subject itself, may be more complicated to do, in order to input each decimal as well as hexadecimal conversion, we define a function more, and virtually the entire program by calling the function composition.

2. Referee function

//裁判
bool judge(int n, char num[1000])
{
    //二进制判断
    if (n == 2)
    {
        for (int idx = 0; idx < strlen(num); idx++)
            if (num[idx] != '1' && num[idx] != '0')return false;
        return true;
    }
    //八进制判断
    if (n == 8)
    {
        for (int idx = 0; idx < strlen(num); idx++)
            if (num[idx] < 48 || num[idx] > 55)return false;
        return true;
    }
    //十进制判断
    if (n == 10)
    {
        for (int idx = 0; idx < strlen(num); idx++)
            if (num[idx] < 48 || num[idx] > 57)return false;
        return true;
    }
    //十六进制判断
    if (n == 16)
    {
        for (int idx = 0; idx < strlen(num); idx++)
            if (num[idx] < 48 || num[idx] > 70)return false;
        return true;
    }
}

According to hex, each digit of which appear to traverse the input, once the number of non-compliance found, immediately returns false, otherwise it returns true at the end of traversal. This function returns a Boolean value utilized is true for the referee can also use an int and returns a 0, true and false only because it is relatively straightforward to use it.

3. exponential function

   //指数 
int mypow(int x, int cnt)
{
    int sum = 1;
    for (int idx = 0; idx < cnt; idx++)sum *= x;
    return sum;
}

Simple exponential function for converting a decimal later use.

4. binary input conversion

//二进制
void two(int n)
{
    char str[1000];
    printf("请输入您的数字:");
    scanf("%s", str);
    if (!judge(n, str))printf("ERROR\n");
    else
    {
        two_to_eight(str);
        two_to_ten(str);
        two_to_sixteen(str);
    }
}

The first is the binary input function, binary can be said that this question nasty place, in order to meet binary and conversion of each band, we can not simply use the format control completion of this topic, so I have taken all of the numbers this question stored in an array of ways.

According to the prompts to enter your number, if the referee returns false, you will be prompted to make a mistake, otherwise, will call three decimal conversion function.

void two_to_eight(char num[1000])//把二进制转换为八进制
{
    int str[1000];
    int ans = 1;
    int cnt = 0, sum = 0;
    memset(str, 0, sizeof(str));
    for (int idx = strlen(num) - 1; idx >= 0; idx--)
    {
        cnt++;
        sum = sum + (num[idx] - 48) * mypow(2, cnt - 1);
        if (cnt == 3)
        {
            str[ans++] = sum;
            sum = 0;
            cnt = 0;
        }
    }
    if (sum)str[ans++] = sum;
    printf("八进制:   ");
    for (int idx = ans; idx >= 1; idx--)printf("%d", str[idx]);
    printf("\n");
}
void two_to_ten(char num[1000])//把二进制转换为十进制
{
    int str[1000];
    int sum = 0;
    memset(str, 0, sizeof(str));
    for (int idx = strlen(num) - 1, i = 0; idx >= 0; idx--, i++)
    {
        sum = sum + (num[i] - 48) * mypow(2, idx);
    }
    printf("十进制:   ");
    printf("%d", sum);
    printf("\n");
}
void two_to_sixteen(char num[1000])//把二进制转换为十六进制 
{
    char str[1000];
    int ans = 1;
    int cnt = 0, sum = 0;
    memset(str, 0, sizeof(str));
    for (int idx = strlen(num) - 1; idx >= 0; idx--)
    {
        cnt++;
        sum = sum + (num[idx] - 48) * mypow(2, cnt - 1);
        if (cnt == 4)
        {
            if (sum == 10)str[ans++] = 'A';
            else if (sum == 11)str[ans++] = 'B';
            else if (sum == 12)str[ans++] = 'C';
            else if (sum == 13)str[ans++] = 'D';
            else if (sum == 14)str[ans++] = 'E';
            else if (sum == 15)str[ans++] = 'F';
            else str[ans++] = sum + 48;
            sum = 0;
            cnt = 0;
        }
    }
    if (sum) str[ans++] = sum + 48;
    printf("十六进制: ");
    printf("OX");
    for (int idx = ans - 1; idx >= 1; idx--)printf("%c", str[idx]);
    printf("\n");
}

Very simple function named, is converted to binary octal, decimal and hexadecimal, memset is the array are initialized to zero, store it in

<String.h>, considering an octal number for the binary three-digit, I introduced a counter cnt, every three binary numbers converted to an octal number, and sums them to finally obtain an octal number, then one more output to ensure octal starting with 0, octal achieve correct display

The next metric is a simple multiplication sum bit tired, hexadecimal octal similar transformation, every number is a four hexadecimal, 10 to 15 and you want to be converted to A ~ F, finally, to for the front hex plus OX, it represents the output hexadecimal numbers.

5. octal input and conversion

//八进制
void eight(int n)
{
    char str[1000];
    printf("请输入您的数字:");
    scanf("%s", str);
    if (!judge(n, str))printf("ERROR\n");
    else
    {
        eight_to_two(str);
        eight_to_ten(str);
        eight_to_sixteen(str);
    }
}
void eight_to_two(char num[1000])//把八进制转二进制
{
    int str[1000];
    memset(str, 0, sizeof(str));
    for (int i = strlen(num) - 1; i >= 0; i--)
    {
        int cnt = 2;
        int number = num[i] - 48;
        do {
            if (number % 2)str[i * 3 + cnt] = 1;
            else str[i * 3 + cnt] = 0;
            cnt--;
            number /= 2;
        } while (number > 0);
    }
    printf("二进制:   ");
    for (int i = 0; i < strlen(num) * 3; i++)
    {
        printf("%d", str[i]);
        if ((i + 1) % 3 == 0)printf(" ");
    }
    printf("\n");
}
void eight_to_ten(char num[1000])//把八进制转十进制
{
    int str[1000];
    int  sum = 0;
    memset(str, 0, sizeof(str));
    for (int idx = strlen(num) - 1, i = 0; idx >= 0; idx--, i++)
    {
        sum = sum + (num[i] - 48) * mypow(8, idx);
    }
    printf("十进制:   ");
    printf("%d", sum);
    printf("\n");
}
void eight_to_sixteen(char num[1000])//把八进制转十六进制
{
    char str[1000];
    int  sum = 0, ans = 0;
    memset(str, 0, sizeof(str));
    for (int idx = strlen(num) - 1, i = 0; idx >= 0; idx--, i++)
    {
        sum = sum + (num[i] - 48) * mypow(8, idx);
    }
    do {
        int number = sum % 16;
        if (number == 10)str[ans++] = 'A';
        else if (number == 11)str[ans++] = 'B';
        else if (number == 12)str[ans++] = 'C';
        else if (number == 13)str[ans++] = 'D';
        else if (number == 14)str[ans++] = 'E';
        else if (number == 15)str[ans++] = 'F';
        else str[ans++] = number + 48;
        sum /= 16;
    } while (sum > 0);
    printf("十六进制: ");
    printf("OX");
    for (int idx = ans - 1; idx >= 0; idx--)printf("%c", str[idx]);
    printf("\n");
}

6. decimal conversion input

//十进制
void ten(int n)//输入十进制数
{
    char str[1000];
    printf("请输入您的数字:");
    scanf("%s", str);
    if (!judge(n, str))printf("ERROR\n");
    else
    {
        ten_to_two(str);
        ten_to_eight(str);
        ten_to_sixteen(str);
    }
}
void ten_to_two(char num[1000])//把十进制转二进制
{
    int str[1000];
    memset(str, 0, sizeof(str));
    int cnt = 0, sum = 0;
    for (int i = 0; i < strlen(num); i++)sum = sum * 10 + num[i] - 48;
    do {
        int number = sum % 2;
        if (number % 2)str[cnt++] = 1;
        else str[cnt++] = 0;
        sum /= 2;
    } while (sum > 0);
    cnt--;
    printf("二进制:   ");
    for (int i = cnt; i >= 0; i--)printf("%d", str[i]);
    printf("\n");
}
void ten_to_eight(char num[1000])//把十进制转八进制
{
    int str[1000];
    int  sum = 0, ans = 0;
    memset(str, 0, sizeof(str));
    for (int i = 0; i < strlen(num); i++)sum = sum * 10 + num[i] - 48;
    do {
        int number = sum % 8;
        str[ans++] = number;
        sum /= 8;
    } while (sum > 0);
    printf("八进制:   ");
    for (int i = ans; i >= 0; i--)printf("%d", str[i]);
    printf("\n");
}
void ten_to_sixteen(char num[1000])//把十进制转十六进制
{
    char str[1000];
    int  sum = 0, ans = 0;
    memset(str, 0, sizeof(str));
    for (int i = 0; i < strlen(num); i++)sum = sum * 10 + num[i] - 48;
    do {
        int number = sum % 16;
        if (number == 10)str[ans++] = 'A';
        else if (number == 11)str[ans++] = 'B';
        else if (number == 12)str[ans++] = 'C';
        else if (number == 13)str[ans++] = 'D';
        else if (number == 14)str[ans++] = 'E';
        else if (number == 15)str[ans++] = 'F';
        else str[ans++] = number + 48;
        sum /= 16;
    } while (sum > 0);
    printf("十六进制: ");
    printf("OX");
    for (int idx = ans - 1; idx >= 0; idx--)printf("%c", str[idx]);
    printf("\n");
}

7. Enter the hex conversion

//十六进制
void sixteen(int n)//输入十六进制数
{
    char str[1000];
    printf("请输入您的数字:");
    scanf("%s", str);
    for (int i = 0; i < strlen(str); i++)
        if (islower(str[i]))str[i] = toupper(str[i]);
    if (!judge(n, str))printf("ERROR\n");
    else
    {
        sixteen_to_two(str);
        sixteen_to_eight(str);
        sixteen_to_ten(str);
    }
}
void sixteen_to_two(char num[1000])//把十六进制转二进制
{
    int str[1000], number;
    memset(str, 0, sizeof(str));
    for (int i = strlen(num) - 1; i >= 0; i--)
    {
        int cnt = 3;
        if (num[i] == 'A') number = 10;
        else if (num[i] == 'B') number = 11;
        else if (num[i] == 'C') number = 12;
        else if (num[i] == 'D') number = 13;
        else if (num[i] == 'E') number = 14;
        else if (num[i] == 'F') number = 15;
        else number = num[i] - 48;
        do {
            if (number % 2)str[i * 4 + cnt] = 1;
            else str[i * 4 + cnt] = 0;
            cnt--;
            number /= 2;
        } while (number > 0);
    }
    printf("二进制:   ");
    for (int i = 0; i < strlen(num) * 4; i++)
    {
        printf("%d", str[i]);
        if ((i + 1) % 4 == 0)printf(" ");
    }
    printf("\n");
}
void sixteen_to_eight(char num[1000])//把十六进制转八进制
{
    int str[1000];
    int sum = 0, number, ans = 0;
    for (int i = strlen(num) - 1; i >= 0; i--)
    {
        if (num[i] == 'A') number = 10;
        else if (num[i] == 'B') number = 11;
        else if (num[i] == 'C') number = 12;
        else if (num[i] == 'D') number = 13;
        else if (num[i] == 'E') number = 14;
        else if (num[i] == 'F') number = 15;
        else number = num[i] - 48;
        sum = sum + number * mypow(16, strlen(num) - 1 - i);
    }
    do {
        number = sum % 8;
        str[ans++] = number;
        sum /= 8;
    } while (sum > 0);
    ans--;
    printf("八进制:   ");
    printf("0");
    for (int i = ans; i >= 0; i--)printf("%d", str[i]);
    printf("\n");
}
void sixteen_to_ten(char num[1000])//把十六进制转十进制
{
    int sum = 0, number;
    for (int i = strlen(num) - 1; i >= 0; i--)
    {
        if (num[i] == 'A') number = 10;
        else if (num[i] == 'B') number = 11;
        else if (num[i] == 'C') number = 12;
        else if (num[i] == 'D') number = 13;
        else if (num[i] == 'E') number = 14;
        else if (num[i] == 'F') number = 15;
        else number = num[i] - 48;
        sum = sum + number * mypow(16, strlen(num) - 1 - i);
    }
    printf("十进制:   ");
    printf("%d", sum);
    printf("\n");
}

These types of binary conversion is much the same, in order to achieve the main function of simplicity, the establishment of a relatively large number of functions.

In particular, I use hexadecimal input in the function <ctype.h> is the lowercase letters input by the user converted all capital letters, to ensure the correct identification of hexadecimal numbers.

Shows three .main function

int main()
{
    int n;
    while (1)
    {
        printf("请选择您所要输入的进制:");
        scanf("%d", &n);
        if (!n) break;
        switch (n)
        {
        case 2:two(n); break;
        case 8:eight(n); break;
        case 10:ten(n); break;
        case 16:sixteen(n); break;
        default:printf("ERROR\n"); break;
        }
        printf("-------------------------------\n");
    }
    return 0;
}

A while to do an infinite loop only when the user enters 0 will exit, enter the conversion page for binary input according to user-entered digits, at the same time, the number does not belong to the band of 2,8,10,16 You will be prompted ERROR, and re-enter.

IV. Mind Mapping

Five problems and solutions:

Question: had wanted to be lazy instead of using macros for circulation, reduce my workload, but later found the program appear BUG, ​​will output a wrong answer.

int idx;

#define myfor(n) for(idx = 0;idx<n;idx++)

Solution: I found is a global variable set of macro and re-use when calling the function, resulting in the value of a global variable bias, triggered a mistake, so I deleted the global variables and macros.

VI. Code peer review

Just some students have submitted work, I looked at their function, I have a feeling of inspiration, look to-one comparisons.

1. Network 1913 Tao

int Tennum(char a[],int b)//将输入的数字转换为10进制数
{
    int len, i, num;
    int sum = 0;
    len = strlen(a);//求字符长度
    for (i = 0; i < len; i++) 
    {
        if (a[i] >= '0' && a[i] <= '9')
        {
            num = a[i] - '0';
        }
        else if (a[i] >= 'A' && a[i] <= 'F')
        {
            num = a[i] - 'A' + 10;
        }
        sum = sum * b + num;
    }
    return sum;
}

I feel this is a great idea, you can perfect the various types of binary to decimal format, saving a lot of space, and I was for each band have established a convert to decimal function, in fact, not necessary.

2. Network 1914 Lin Jieying

int binaryconversion(int number){
 if(number/2==0){
     return number%2;
 }
 else{
     return number%2+binaryconversion(number/2)*10;
 }
}

Recursive bright spots, I came to understand the original can be written in this way, compared to my own binary conversion, my code looks a little cumbersome, but, recursive, then will likely stack when dealing with a large number of overflow, I tried it will get the wrong value is about to enter six digits.

3. Network 1913 Chen Hanyu

void TenToSixteen()
{
    char ai[] = "0123456789ABCDEF";
    char h[16];
    int i = 0;
    int j = 0;
    int num = 0, g;
    printf("输入一个整数:");
    scanf_s("%d", &num);
    g = num;
    if (num < 0)
        num = -num;
    while (num)
    {
        h[i++] = ai[num % 16];   //对十进制数求余并最终与h数组中的字符匹配
        num = num / 16;
    }
    printf("十进制数转换成十六进制数是:");
    if (g < 0)
        printf("-");
    for (j = i - 1; j >= 0; --j)
        printf("%c", h[j]);
    printf("\n");
}

Is a very great idea, a method using play table, concise finished hex conversion.

VII. Summary

1. Harvest : compared what students submitted code, I wrote a bit cumbersome, and in these, I learned some great idea, at the same time, the job is completed in this process, deepened my application binary conversion, but also tried some interesting ideas, such as the main function to do very little, relying on the basic call functions to achieve program

2 understand: the function itself exists to make the code looked more clear and easier for people to read the program, as a function of taking their good name, the right call, you can make your code more concise, more interesting.

Guess you like

Origin www.cnblogs.com/JMU718/p/11817188.html