Hex converter V1.0_Beta

A. Screenshots section

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Second code portions:

Cr2num ()

Action: The character is converted into a corresponding digital
       eg '9' -> 9 ' A' -> 10

int char2num(char ch) {
    if (ch >= '0' && ch <= '9') {
        return ch - '0';
    }
    else {
        return ch - 'A' + 10;
    }
}
char 

Num2cr ()

Action: the digital conversion to the corresponding character
       eg 9 -> '9' 10 -> 'A'

char num2char(int num) {
    if (num >= 0 && num <= 9) {
        return (char)('0' + num);
    }
    else {
        return (char)('A' + num - 10);
    }
}

is_false()

Role: to determine whether the correct input on returns 0, 1 error return

int is_flase(char ch[], int n) {
    int i, len, flag = 0;
    len = strlen(ch);
    for (i = 0; i < len; i++) {
        if (char2num(ch[i]) < 0 || char2num(ch[i]) >= n)
            flag = 1;
    }
    return flag;
}

any2ten()

Role: arbitrary binary to decimal conversion

int any2ten(char str[],int key) {
    int cnt, len, i, sum = 0, item = 1;
    len = strlen(str);
    for (i = 1; i < len; i++) {
        item *= key;
    }
    for (i = 0; i < len; i++) {
        cnt = char2num(str[i]);
        sum += cnt * item;
        item /= key;
    }
    return sum;
}

ten2any ()

Role: decimal into any hex

void ten2any(int n,int key) {
    int i, len = 0;
    char a[10000];
    while (n) {
        a[len] = num2char(n % key);
        n /= key;
        len++;
    }
    switch (key) {
    case 2:printf(" -->二进制是:"); break;
    case 8:printf(" -->八进制是:"); break;
    case 16:printf("    -->十六进制是:"); break;
    }
    for (i = len - 1; i >= 0; i--) {
        printf("%c", a[i]);
    }
    printf("\n");
}

main()

int main() {
    int n, result, flag = 1, now, k;
    char a[10000];
    printf("--------------------------------------------------\n");
    while (flag) {
        printf("--------------------------------------------------\n");
        printf("    请输入当前进制:");
        scanf("%d", &now);
        printf("    请输入你要转换的数:");
        switch (now) {
        case 2:
            getchar();
            gets_s(a);
            if (is_flase(a, now)) {
                printf("        输入错误!请重试!\n");
                goto lab;
            }
            ten2any(any2ten(a, now), 8);
            printf("    -->十进制是:%d\n", any2ten(a, now));
            ten2any(any2ten(a, now), 16);
            break;
        case 8:
            getchar();
            gets_s(a);
            if (is_flase(a, now)) {
                printf("        输入错误!请重试!\n");
                goto lab;
            }
            ten2any(any2ten(a, now), 2);
            printf("    -->十进制是:%d\n", any2ten(a, now));
            ten2any(any2ten(a, now), 16);
            break;
        case 10:
            getchar();
            gets_s(a);
            if (is_flase(a, now)) {
                printf("        输入错误!请重试!\n");
                goto lab;
            }
            ten2any(any2ten(a, now), 2);
            ten2any(any2ten(a, now), 8);
            ten2any(any2ten(a, now), 16);
            break;
        case 16:
            getchar();
            gets_s(a);
            if (is_flase(a, now)) {
                printf("        输入错误!请重试!\n");
                goto lab;
            }
            ten2any(any2ten(a, now), 2);
            ten2any(any2ten(a, now), 8);
            printf("    -->十进制是:%d\n", any2ten(a, now));
            break;
        }
        lab:
        printf("    继续请按1,退出请按0:");
        scanf("%d", &k);
        printf("--------------------------------------------------\n");
        if (k == 0) {
            printf("--------------------------------------------------\n");
            break;
        }
    }
    return 0;
}

III. Mind Mapping

Here Insert Picture Description

IV. Problem Solving Process

1. just started writing a lot of functions are separate, but then I found that I write too long, and in fact, several functions are combined in one, so I re-wrote ten2any () and any2ten () two functions, to solve the problem with this code is too long.
2. Start writing hex that is exactly what is ABCDEF card, think back, to be converted into digital characters, so think of the ASCII code, the type of character cast, another can be achieved through the addition and subtraction digital-to-digital characters and character, but also a few calls, then I'll just write two functions.
3. Finally, see also the requirements of error, think also, that I will direct digital transfer in accordance with the character of the idea, under the n-ary character into a numeric value, if more than n-1, directly error, but error in the main function is implemented.
4. Moreover, on the interface and aesthetic problems, are beginning to write a bunch of numbers, allowing users to choose a few specific to a few decimal notation, and then write out your own with a little, almost spit, so I directly rejected out. Then I thought of a binary input, whether or not use, anyway, put everything else out of the output to the user to use, more convenient, do not turn on several occasions. Then I think every turn once gone, but also to re-open the program again, annoying, so I direct in the outermost sets a while loop, you do not allow users to exit their choice. About messy interface problems .. directly at the end of the beginning of the addition of a dividing line .. already .. plus a behind that is not obvious, another layer, then the interface will be without a mess, which turn what time user It will be a mess.

V. Code peer review

#include <stdio.h>
#include <math.h>
#include <string.h>

void transform_2_8(int n);
int transform_2_10(int n);
void transform_2_16(int n);

void transform_8_2(int n);
int transform_8_10(int n);
void transform_8_16(int n);

void transform_10_2(int n);
void transform_10_8(int n);
void transform_10_16(int n);

void transform_16_2(char *s);
void transform_16_8(char *s);
int transform_16_10(char *s);

The students put the above code were written in the header file

#include "Header.h"
#include "tr2.c"
#include "tr8.c"
#include "tr10.c"
#include "tr16.c"

This makes calling those functions more convenient, but I think we can put those functions concentrated a bit, so there is no need to write so many lines.

VI. Summary

1. The function is a good thing!
2. The function is really great! !
3. Stick a very, very! ! !
1. Function easy programming, debugging convenient, easy to upgrade, and when a procedure needs to be repeated in your program, this time function can play a significant role.
2. When we write a program requires user interaction to talk things need to be considered to the program aesthetics, as well as simple operation for the user's.
3. The function and then blowing it again -

Guess you like

Origin www.cnblogs.com/Sogger/p/11831782.html