2016北航机试枚举类型转换

>

题目描述 给一个C语言的enum定义语句,输出enum中规定的各项值。 输入 输入一个c语言的enum定义语句。 输出
输出enum中规定的各项值。 样例输入 enum BOOL{true,false}; 样例输出 true 0 false 1

/*
函数说明:
1.主函数,对枚举部分提取,控制整个循环
2.输出函数,输出当前子符串的数值
3.对于数字字符串的转换
*/
#include<stdio.h>
#include<string.h>
int move_toint(char *p,int n)
{
    int res = 0;
    if (p == NULL)
    {
        return res;
    }
    else
    {
        for (int i = 0; i < n; i++)
        {
            res = res * 10 + (int)(p[i] - '0');
        }
    }
    return res;
}
int match(char p[], int n, int num)
{
    int newnum = num;
    for (int i = 0; i <n; i++)
    {
        if (p[i] == '=')
        {
            char *temp = new char[n - i-1];
            int m = 0;
            for (int j = i+1; j < n; j++)
            {
                temp[m++] = p[j];
            }
            newnum = move_toint(temp,m);
            printf(" %d", newnum);
            printf("\n");
            return newnum;
        }
        else
        {
            printf("%c", p[i]);
        }
    }
    printf(" %d", newnum);
    printf("\n");
    return newnum;
}
int  main()
{
    char str[100];
    fgets(str, sizeof(str), stdin);//stdin为系统默认缓存区
    int length = strlen(str);
    int i = 0;
    while (str[i] != '{')
    {
        i++;
    }
    i = i + 1;
    int enumnum = 0;
    char * temp = new char[length];
    for (; i < length - 2;)
    {
        int t = 0;
        while (str[i] != ',')
        {
            if (str[i] == '}')
            {
                break;
            }
            temp[t++] = str[i];
            i++;
        }
        enumnum = match(temp, t, enumnum);
        i++;
        enumnum++;
    }
    getchar();
    getchar();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/luncy_yuan/article/details/74316111