将base16数转换为base2并求和

时间:2018-03-16 23:53:51

标签: c char

我有一个程序控制16个基数并将其转换为基数2.然后,它使用二进制查找SUM和Cout。如您所知,A ... F是16号基数的有效数字。因此,我需要使用Char来扫描它们。这就是我使用“char”的原因。不允许使用字符串和函数。提前谢谢!

#include<stdio.h>
int main() {
    char numsix;
    int choice, base;
    int i,length;
    int num=0, decisum, bin=0;
    int a, b, c , d ;
    int rm, location = 1;
    int sum, cout, part1, part2, part3;         //Part1 is (A AND B) ,Part2 is (C_IN AND (A XOR B)) ,Part3 is  (A XOR B)
    printf("Welcome to Full-Adder! \n");
    printf("(1)Compute and display the outputs");
    printf("\n(2)Quit");
    printf("\nYou choose:");
    scanf("%d", &choice);

    while (choice != 2) {                                           //instructor's suggest
        printf("You have chosen option 1");
        printf("\n Which base will you use to enter data lines (base 16/8/2)?");
        scanf("%d", &base);
        if (base == 16) {                                           //User is not allowed to enter integers greater than 7.Integers less than 7 have same value in 8 16 and 10.
            printf("Please enter input");
            do {
                scanf("%c",&numsix);
                printf("%c",numsix);
                num=num+numsix;
                i++;
                if(i==1) {
                    num=num-10;
                }
            } while(numsix != 10 );

            printf("%d",num);                               //User is able to use chracters from A to F in 16 base.We're able to control the input with that way.

            if (num <= 7 && num >= 0) {                                 //We're controlling whether the input is suitable to evaluate or not!
                while (num>0) {                                         //Heximal to Binary!
                    rm = num % 2;
                    bin = (rm*location) + bin;
                    num /= 2;
                    location *= 10;
                }
                a = bin / 100;                                                  //for abc we're finding here a ,b and c's own value!
                b = bin / 10 - a * 10;
                c = bin - (a * 100 + b * 10);
                if (a == b) {                                                   //We're finding Sum below
                    sum = 0;
                    if (sum == c) {
                        sum = 0;
                    }
                    else {
                        sum = 1;
                    }
                }
                else {
                    sum = 1;
                    if (sum == c) {
                        sum = 0;
                    }
                    else {
                        sum = 1;
                    }
                }
                if (a == b) {                                           //We're finding cout below
                    part3 = 0;
                }
                else {
                    part3 = 1;
                }
                if (c == part3) {
                    if (c == 1) {
                        part2 = 1;
                    }
                    else {
                        part2 = 0;
                    }                       //Part1 is (A AND B) ,Part2 is (C_IN AND (A XOR B)) ,Part3 is  (A XOR B)
                }
                if (a == b)         {
                    if (a == 1) {
                        part1 = 1;
                    }
                    else {
                        part1 = 0;
                    }
                }
                if (part1 == part2) {
                    if (part1 == 0) {
                        cout = 0;
                    }
                    else {
                        cout = 1;
                    }
                }
                else {
                    cout = 1;
                }
                printf("Sum:%d", sum);
                printf("C-out:%d", cout);
                printf("\n Welcome to Full-Adder! \n");                                     //to be able to start the program again
                printf("(1)Compute and display the outputs");
                printf("\n(2)Quit");
                printf("\nYou choose:");
                scanf("%d", &choice);
            }
            else if (num>=17 && num<=22 || num==8 || num==9 ) {
                printf("cannot be convered to 3 digits at binary",numsix);      //We're controlling whether the input is suitable to evaluate or not!
                printf("\n Try Again.");
                printf("\n Welcome to Full-Adder! \n");                                 //to be able to start the program again
                printf("(1)Compute and display the outputs");
                printf("\n(2)Quit");
                printf("\nYou choose:");
                scanf("%d", &choice);
            }
            else {
                printf("is not valid a number in base 16", numsix);                //We're controlling whether the input is suitable to evaluate or not!
                printf("\nTry Again? \n");
                printf("\n Welcome to Full-Adder! \n");                             //to be able to start the program again
                printf("(1)Compute and display the outputs");
                printf("\n(2)Quit");
                printf("\nYou choose:");
                scanf("%d", &choice);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

此代码中存在许多问题,我看到的主要问题是您在初始化之前使用var simg = new Simg(svg); simg.download();

i

int i,length;