Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使输入的字母表(通过scanf)按其';在枚举中定义了。请参考下面的程序_C_Enums - Fatal编程技术网

如何使输入的字母表(通过scanf)按其';在枚举中定义了。请参考下面的程序

如何使输入的字母表(通过scanf)按其';在枚举中定义了。请参考下面的程序,c,enums,C,Enums,我正在尝试制作一个C程序,用于将给定的基数x转换为基数y。我选择将其缩小到20进制(即2进制到20进制)。例如,当涉及到扫描十六进制数(也包括ABCDEF,对吗?)时,我就被卡住了。请看下面我的节目: /* NOTE: This program uses two step approach to convert a given number in any base (except base 10, in which case we will use only "toany()") to any

我正在尝试制作一个C程序,用于将给定的基数x转换为基数y。我选择将其缩小到20进制(即2进制到20进制)。例如,当涉及到扫描十六进制数(也包括ABCDEF,对吗?)时,我就被卡住了。请看下面我的节目:

/* NOTE: This program uses two step approach to convert a given number in any base (except base 10, in which case we will use only "toany()") to any other base*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int inum,ibase, obase;
int todec(); //function to convert to decimal from any base
int toany(int); //function to convert from decimal to any base
int exp(int,int); //used in other function 
void main()
{
    int num,choice;
    char strr[100];
    enum{A=10,B,C,D,E,F,G,H,I,J};
    here:
    printf("Enter the base (RADIX) of your number: ");
    scanf("%d",&ibase);
    printf("Enter the number in base %d: ",ibase);
    scanf("%s",strr);
    printf("Enter the base in which you want the output: ");
    scanf("%d",&obase);
    inum=atoi(strr);
    switch(obase)
    {
        case 10:
        num=todec();
        printf("Output in base 10: %d\n",num);
        break;
        default:
        if(ibase==10)
            num=toany(inum);
        else
            num=toany(todec());
        printf("Output in base %d: %d\n",obase,num);
        break;
    }
    printf("WANNA DO IT AGAIN? If yes, Press 1 else press 0:");
    scanf("%d",&choice);
    if(choice==1)
        goto here;
    else
        exit(0);
    getch();
}
int exp(int p, int q)
{
    int i,result=1;
    for(i=1;i<=q;i++)
    {
        result=result*p;
    }
    return(result);
}
int todec()
{
    int inumarr[100],dupnum=inum,i=0,counter,decnum=0;  
    while(dupnum!=0)
    {
        inumarr[i]=dupnum%10;
        dupnum/=10;
        i++;
    }
    for(counter=0;counter<i;counter++)
    {
        decnum=decnum+inumarr[counter]*exp(ibase, counter);
    }
    return(decnum);
}
int toany(int num)
{
    int outnumarr[100],i=0,q,result=0;
    while(num!=0)
    {
        outnumarr[i]=num%obase;
        num=num/obase;
        i++;
    }
    for(q=0;q<i;q++)
    {
        result=result+outnumarr[q]*exp(10,q);
    }
    return(result);
}
/*注意:此程序使用两步方法将任何基数中的给定数字(基数10除外,在这种情况下,我们将仅使用“toany()”)转换为任何其他基数*/
#包括
#包括
#包括
int inum、ibase、obase;
int-todec()//函数从任意基转换为十进制
inttoany(int)//函数将十进制数转换为任意基数
int-exp(int,int)//用于其他功能
void main()
{
int-num,选择;
char-strr[100];
枚举{A=10,B,C,D,E,F,G,H,I,J};
在这里:
printf(“输入数字的基数:”);
scanf(“%d”和&ibase);
printf(“在基数%d中输入数字:”,ibase);
scanf(“%s”,strr);
printf(“输入输出所需的基:”;
scanf(“%d”和&obase);
inum=atoi(strr);
交换机(obase)
{
案例10:
num=todec();
printf(“基10中的输出:%d\n”,num);
打破
违约:
如果(ibase==10)
num=toany(单位:inum);
其他的
num=toany(todec());
printf(“基本%d中的输出:%d\n”,obase,num);
打破
}
printf(“想再做一次吗?如果是,按1,否则按0:”;
scanf(“%d”,选择(&C);
如果(选项==1)
转到这里;
其他的
出口(0);
getch();
}
int exp(int p,int q)
{
int i,结果=1;

对于(i=1;i免责声明:我在这个答案中输入的代码未经测试。我目前使用的是移动设备,因此即使编译它也不如平时方便。我将努力提供足够的详细信息,让您找到超越任何(可能)的方法错误,请指出它们…改天我将通过添加更多检查(在末尾描述)来完善这篇文章,并解释序列化和反序列化。然而,就目前情况而言,您似乎只是在询问反序列化,因此无需进一步说明:

建立一个包含基本字符的某些描述的查找表。对于字符,您可以(通常)不使用字符串操作。例如:

unsigned char hex_digit[] = "00112233445566778899AaBbCcDdEeFf";
如果使用
strchr
和一些指针算法,现在可以找到字符的偏移量,除以2将其减少到
0..15
范围内的值,或者用模2区分小写和大写

您可以设计任何类似这样的基,使用通用循环解析输入,以方便更大的值

size_t to_native_uimax(char *str, unsigned char *base, uintmax_t *value) {
    size_t x, base_size = strlen(str);
    uintmax_t v = 0;
    for (x = 0; str[x]; x++) {
        unsigned char *c = strchr(base, str[x]);
        if (!c) break;

        v *= base_size / 2;
        v += (c - base) / 2;
    }
    *value = v;
    return x;
}
标牌的处理有点棘手,但因为我们只需要在字符串开头处理标牌,所以可以重用上面的代码

size_t to_native_imax(unsigned char *str, unsigned char *base, intmax_t *value) {
    uintmax_t v = 0;
    size_t x = to_native_uimax(str + !!strchr("-+", *str), base, &v);
    *value = *str == '-' ? -(intmax_t)v : v;
    return x;
}

还请注意,此代码不是严格可移植的;如果可能将其部署到具有负零或溢出信号的系统,则应在
(intmax\t)之前进行更多检查
转换。

免责声明:我在这个答案中输入的代码未经测试。我目前使用的是移动设备,因此即使编译它也不如平时方便。我将努力提供足够的详细信息,让您找到通过任何(可能)的方法错误,请指出它们…改天我将通过添加更多检查(在末尾描述)来完善这篇文章,并解释序列化和反序列化。然而,就目前情况而言,您似乎只是在询问反序列化,因此无需进一步说明:

建立一个包含基本字符的某些描述的查找表。对于字符,您可以(通常)不使用字符串操作。例如:

unsigned char hex_digit[] = "00112233445566778899AaBbCcDdEeFf";
如果使用
strchr
和一些指针算法,现在可以找到字符的偏移量,除以2将其减少到
0..15
范围内的值,或者用模2区分小写和大写

您可以设计任何类似这样的基,使用通用循环解析输入,以方便更大的值

size_t to_native_uimax(char *str, unsigned char *base, uintmax_t *value) {
    size_t x, base_size = strlen(str);
    uintmax_t v = 0;
    for (x = 0; str[x]; x++) {
        unsigned char *c = strchr(base, str[x]);
        if (!c) break;

        v *= base_size / 2;
        v += (c - base) / 2;
    }
    *value = v;
    return x;
}
标牌的处理有点棘手,但因为我们只需要在字符串开头处理标牌,所以可以重用上面的代码

size_t to_native_imax(unsigned char *str, unsigned char *base, intmax_t *value) {
    uintmax_t v = 0;
    size_t x = to_native_uimax(str + !!strchr("-+", *str), base, &v);
    *value = *str == '-' ? -(intmax_t)v : v;
    return x;
}
还请注意,此代码不是严格可移植的;如果可能将其部署到具有负零或溢出信号的系统,则应在
(intmax_t)
转换之前进行更多检查。

步骤1:
inum=atoi(strr);
-->
char*endptr;inum=strtol(strr,&endptr,ibase)
可能有帮助步骤1:
inum=atoi(strr);
-->
char*endptr;inum=strtol(strr,&endptr,ibase);
可能有帮助