| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 400 人关注过本帖
标题:c专家编程中 有一段代码 不是很理解
只看楼主 加入收藏
唯佳
Rank: 2
等 级:论坛游民
帖 子:42
专家分:44
注 册:2012-1-11
结帖率:100%
收藏
已结贴  问题点数:30 回复次数:2 
c专家编程中 有一段代码 不是很理解
程序代码:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define MAXTOKENS 100
#define MAXTOKENLEN 64

enum type_tag{IDENTIFIER,QUALIER,TYPE};

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

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

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

enum type_tag classify_string(void)
{
    char *s = this.string;
    if(!strcmp(s,"const"))
    {
        strcpy(s,"read_only");
        return QUALIER;
    }
    if(!strcmp(s,"volatile")) return QUALIER;
    if(!strcmp(s,"void")) return TYPE;
    if(!strcmp(s,"char")) return TYPE;
    if(!strcmp(s,"signed")) 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,"struct")) return TYPE;
    if(!strcmp(s,"union")) return TYPE;
    if(!strcmp(s,"enum")) return TYPE;
    return IDENTIFIER;
}
//读取下一个标记
void gettoken(void)
{
    char *p=this.string;

    while((*p=getchar())==' ')
        ;
    if(isalnum(*p))//当*p为数字0-9或字母a-z及A-Z时,返回非零值,否则返回零
    {
        while(isalnum(*++p=getchar()))
            ;
        ungetc(*p,stdin);
        *p='\0';
        this.type=classify_string();
        return;
    }
    if(*p=='*')
    {
        strcpy(this.string,"pointer to");
        this.type='*';
        return;
    }
    this.string[1]='\0';
    this.type=*p;
    return;
}

void read_to_first_identifier()
{
    gettoken();
    while(this.type!=IDENTIFIER)//判断数据是否为整形
    {
        push(this);
        gettoken();
    }
    printf("%s is ",this.string);
    gettoken();
}
void deal_with_arrays()
{
    while(this.type=='[')
    {
        printf("array ");
        gettoken();
        if(isdigit(this.string[0]))
        {
            printf("0..%d",atoi(this.string)-1);
            gettoken();
        }
        gettoken();
        printf(" of ");
    }
}
void deal_with_function_args()
{
    while(this.type!=')')
    {
        gettoken();
    }
    gettoken();
    printf("function returning");
}

void deal_with_pointer()
{
    while(stack[top].type=='*')
    {
        printf("%s",pop.string);
    }
}
//处理在读入到标示符之前压入到cai中的符号
void deal_with_declarator()
{
    switch(this.type)
    {
        case'[':deal_with_arrays(); break;
        case'(':deal_with_function_args(); break;
        case'*':deal_with_pointer(); break;
    }

    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;
}


输入 char *p; 结果是 p is
输入 int a[3] 结果是 a is array [0..2]
当输入 int a(); 结果是 a is a function returning
为什么 不能够 连续 在一起 判断
只显示 第一个的 格式 第二个 却不识别

还要哪些改进 才能 让它们 全部显示出来
搜索更多相关主题的帖子: 编程 100 
2012-03-22 23:16
C_戴忠意
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:575
专家分:1349
注 册:2011-10-21
收藏
得分:21 

编程之路定要走完……
2012-03-22 23:47
唯佳
Rank: 2
等 级:论坛游民
帖 子:42
专家分:44
注 册:2012-1-11
收藏
得分:0 
木有人 看看说说么

宁静致远
2012-03-23 23:20
快速回复:c专家编程中 有一段代码 不是很理解
数据加载中...
 
   



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

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