| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4206 人关注过本帖
标题:一个枚举变量赋值给一个字符变量?
只看楼主 加入收藏
ysa555
Rank: 2
等 级:论坛游民
帖 子:14
专家分:21
注 册:2013-4-14
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:6 
一个枚举变量赋值给一个字符变量?
struct token stack[MAXTOKENS];
struct token the;
enum type_tag
{
    IDENTIFIER,QUALIFIER,TYPE
};

struct token
{
    char type;
    char string[MAXTOKENLEN];
};
enum type_tag classify_string(void)
{
    char * s = the.string;
    if(!strcmp(s,"const"))
    {
        strcpy(s,"read-only");
        return QUALIFIER;
    }
    if(!strcmp(s,"volatile")) return QUALIFIER;
    if(!strcmp(s,"void")) return TYPE;
    if(!strcmp(s,"char")) return TYPE;
    if(!strcmp(s,"singed")) return TYPE;
    if(!strcmp(s,"unsigned")) return TYPE;
    if(!strcmp(s,"short")) return TYPE;
    if(!strcmp(s,"int")) return TYPE;
    if(!strcmp(s,"long")) return TYPE;
    if(!strcmp(s,"float")) return TYPE;
    if(!strcmp(s,"double")) return TYPE;
    if(!strcmp(s,"union")) return TYPE;
    if(!strcmp(s,"enum")) return TYPE;
    return IDENTIFIER;
}
void gettoken(void)
{
    .....
    the.type = classify_string();
        .....


这个函数是可以运行的,那为什么这个枚举变量可以赋值给一个字符变量呢?(程序来自《C专家编程》)
搜索更多相关主题的帖子: void return 
2013-04-26 23:43
ysa555
Rank: 2
等 级:论坛游民
帖 子:14
专家分:21
注 册:2013-4-14
收藏
得分:0 
这是源程序代码,有兴趣的童鞋可以看看~有关于栈的使用的。
函数的功能是,对一个变量和函数的声明的解读:如判断 char * const *(* next)();
这个是在讲什么?

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define MAXTOKENS 100
#define MAXTOKENLEN 64

enum type_tag
{
    IDENTIFIER,QUALIFIER,TYPE
};

struct token
{
    char type;
    char string[MAXTOKENLEN];
};

int top = -1;
struct token stack[MAXTOKENS];
struct token the;

#define pop stack[top--]
#define push(s) stack[++top] = s

enum type_tag classify_string(void)
{
    char * s = the.string;
    if(!strcmp(s,"const"))
    {
        strcpy(s,"read-only");
        return QUALIFIER;
    }
    if(!strcmp(s,"volatile")) return QUALIFIER;
    if(!strcmp(s,"void")) return TYPE;
    if(!strcmp(s,"char")) return TYPE;
    if(!strcmp(s,"singed")) return TYPE;
    if(!strcmp(s,"unsigned")) return TYPE;
    if(!strcmp(s,"short")) return TYPE;
    if(!strcmp(s,"int")) return TYPE;
    if(!strcmp(s,"long")) return TYPE;
    if(!strcmp(s,"float")) return TYPE;
    if(!strcmp(s,"double")) return TYPE;
    if(!strcmp(s,"union")) return TYPE;
    if(!strcmp(s,"enum")) return TYPE;
    return IDENTIFIER;
}
void gettoken(void)
{
    char *p = the.string;
    while((*p = getchar()) == ' ');
      //如果是字母+数字,那么就把它的类型存入the.type,内同存入the.string
    if(isalnum(*p))
    {
        while(isalnum(*++p = getchar()));
        ungetc(*p,stdin);
        *p = '\0';
        the.type = classify_string();
        return ;

    }
      //如果是*,那么就把它的类型存入the.type,内同存入the.string
    if(*p == '*')
    {
        strcpy(the.string,"pointer to");
        the.type = '*';
        return ;
    }
    the.string[1] = '\0';  //字符串后面手动加上‘\0’

    the.type = *p;  //如果不是前面两者,那么可能是(或[,先将它存入the.type中

    return ;

}
void read_to_first_identifier()
{
    gettoken();
    while(the.type != IDENTIFIER) //如果没得到标识符,就将内容压入栈中,继续读下一个内容
    {
        push(the);
        gettoken();
    }        
    printf("%s is ",the.string); //跑出循环,说明得到标识符,输出标识符。
    gettoken(); //再读下一个内容。
}

void deal_with_arrays()
{
    while(the.type == '[')
    {
        printf("array ");
        gettoken();
        if(isdigit(the.string[0]))
        {
            printf("0..%d ",atoi(the.string)-1);
            gettoken();
        }
        gettoken();
        printf("of");
    }
}
void deal_with_function_args()
{
    while(the.type != ')')
    {
        gettoken();
    }
    gettoken();
    printf("function returning ");

}
void deal_with_pointers()
{
    while(stack[top].type == '*')
    {
        printf("%s ",pop.string);
    }
}
void deal_with_declarator()
{
    switch(the.type)
    {
    case '[':deal_with_arrays();break;
    case '(':deal_with_function_args();
    }

    deal_with_pointers();

    while(top >= 0)
    {
        if(stack[top].type == '(')
        {
            pop;
            gettoken();
            deal_with_declarator();

        }
        else
        {
            printf("%s ",pop.string);
        }
    }
}
int main()
{
    read_to_first_identifier();
    deal_with_declarator();
    printf("\n");
    return 0;
}

[ 本帖最后由 ysa555 于 2013-4-27 00:47 编辑 ]
2013-04-27 00:46
fanpengpeng
Rank: 8Rank: 8
来 自:南极洲
等 级:蝙蝠侠
威 望:7
帖 子:299
专家分:849
注 册:2013-2-1
收藏
得分:10 
枚举变量在内部也是用整数来表示的 比如枚举可以有如下定义
enum A{first = 0, second = 1};
那么first的值就是0 second的值就是1 如果不指定那么默认从前一个已定义的值开始加1 第一个不指定 就是0
像你上面的那个枚举 其实就是 0 1 2 3……
所以可以赋值给一个字符 因为字符也是用整数表示的

人生是一场错过 愿你别蹉跎
2013-04-27 10:17
ysa555
Rank: 2
等 级:论坛游民
帖 子:14
专家分:21
注 册:2013-4-14
收藏
得分:0 
回复 3楼 fanpengpeng
不需要加上强制类型转换吗?
2013-04-27 15:14
fanpengpeng
Rank: 8Rank: 8
来 自:南极洲
等 级:蝙蝠侠
威 望:7
帖 子:299
专家分:849
注 册:2013-2-1
收藏
得分:0 
哦? 有一种神奇的东西叫做隐式类型转换 但是没事最好不要去玩他

人生是一场错过 愿你别蹉跎
2013-04-27 16:52
gina620
Rank: 2
等 级:论坛游民
帖 子:3
专家分:10
注 册:2013-4-27
收藏
得分:10 
枚举其实就是整型的,所以可以赋值

]IT专业面试网jobexam。net
2013-04-27 17:22
ysa555
Rank: 2
等 级:论坛游民
帖 子:14
专家分:21
注 册:2013-4-14
收藏
得分:0 
好吧!
2013-04-27 23:46
快速回复:一个枚举变量赋值给一个字符变量?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015551 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved