在C中将2位或更多位数作为char

时间:2017-07-10 21:27:49

标签: c

我必须编写一个程序,它将一个数字作为字符,然后找到它的二进制文件,所以我使用了这段代码:

screenshotofmycode

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

int main(){

    int operation,basetype,k=1,k2=1,binary=0,binary2=0,remainder,remainder2,binaryzero=0000;
    char num1,num2;
    printf("Please enter the number of the operation you would like to perform:");
    printf("\n1. Bitwise OR\n2. Bitwise NOT\n3. Bitwise COMPARE\n4. Exit");
    scanf("%d",&operation);
    if(operation==1){
    printf("You chose Bitwise OR operation.");
    printf("\nPlease enter the first number:");
    scanf(" %c",&num1);//*here i tried using %s also but it doesn't work for 2 
    // or more digit numbers..only for numbers from 0-9*//
    printf("Please specify the base(10/16):");
    scanf(" %d",&basetype);
    if(basetype==10){
        num1=num1-'0';//*changing from char to decimal*//
        if(num1==0){
            printf("\nYour first number is base 2 is %04d",binaryzero);
        }
        else if(num1>0){
                while(num1!=0){
                    remainder=num1%2;
                    num1=num1/2;
                    binary=remainder*k+binary;
                    k=k*10;
        }
    printf("Your first number in base 2 is:%04d",binary);
        }
        else{
            printf("WARNING: Your number is not valid in base 10!");
            printf("\nPlease enter the first number:");
            scanf(" %c",&num1);
        }


     }
       else if(basetype==16){
        if(num1==0){
            printf("\nYour first number in base 2 is %04d",binaryzero);
        }
        else if(num1>0 && num1<10 ){{
                while(num1!=0){
                    remainder=num1%2;
                    num1=num1/2;
                    binary=remainder*k+binary;
                    k=k*10;}
            }
            printf("\nYour first number in base 2 is %04d",binary);}
        else if(num1>='A' && num1<='F'){
                switch(num1){//*i will continue writing cases..*//
                    case 'A':printf("\nYour first number in base 2 is 1010",binary);}
                }
        }
    }

    printf("\nPlease enter the second number:");
    scanf("%d",&num2);
    printf("Please specify the base(10/16):");
    scanf("%d",&basetype);
    if(basetype==10){
    if(num2==0){

            printf("\nYour second number is base 2 is %04d",binaryzero);
        }
        else if(num2>0 && num2<10){
                while(num2!=0){
                    remainder2=num2%2;
                    num2=num2/2;
                    binary2=remainder2*k2+binary2;
                    k2=k2*10;
        }
    printf("Your second number in base 2 is:%04d",binary2);
        }
        else{
            printf("WARNING: Your number is not valid in base 10!");
            printf("\nPlease enter the second number:");
            scanf("%d",&num2);

    }}
    return 0;

}

不幸的是,它仅适用于从0到9的数字,但它应该适用于2位或更多位数字。 我必须将数字作为char,因为用户可能输入基数16中的数字,我必须首先询问数字然后是基本类型。我们不允许使用数组。

2 个答案:

答案 0 :(得分:1)

使用atoi()功能

首先包括string.h
然后这样做

char arr[3];
scanf("%s",arr);
int num=atoi(arr);

答案 1 :(得分:0)

这不适用于您的2位数字:

-N -L 28777:localhost:28778 myapp-db