| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 296 人关注过本帖
标题:关于表达式求值 注释部分不知道哪错啦
只看楼主 加入收藏
lishenming
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2011-9-14
结帖率:100%
收藏
 问题点数:0 回复次数:0 
关于表达式求值 注释部分不知道哪错啦
#include <stdio.h>
#include <stdlib.h>

#define MaxSize 100

void trans(char str[], char postexp[]);
float compvalue(char postexp[]);

int main(void)
{
    char exp[] = "(22+6)/4+2";
    char postexp[MaxSize];
    trans(exp, postexp);
    printf("%s", postexp);
    return 0;
}


void trans(char str[ ], char postexp[ ])
{
    struct Stack
    {
        char data[MaxSize];
        int top;
    }op;
    char ch;
    int i = 0, t = 0;
    op.top = -1;
    ch = str[i];
    i++;
    while (ch != '\0')
    {
        switch (ch)   
        {
            case '(':
                ++op.top;
                op.data[op.top] = ch;
                break;
            case  ')':
                while (op.data[op.top] != '(')
                {
                    postexp[t] = op.data[op.top];
                    ++t;
                    --op.top;
                }
                --op.top;
                break;
            case '+':
            case '-':
                while (op.data[op.top] != '(' && op.top != -1)
                {
                    postexp[t] = op.data[op.top];
                    ++t;
                    --op.top;
                }
                ++op.top;
                op.data[op.top] = ch;
                break;
            case '*':
            case '/':
//                while (op.data[op.top] == '*' || op.data[op.top] == '/')
//                {
//                    postexp[t] = op.data[op.top];
//                    ++t;
//                    --op.top;
//                }
                op.top++;
                op.data[op.top] = ch;   
                break;
            case ' ':
                break;
            default:
                while(ch>='0' && ch<='9')
                {
                    postexp[t] = ch;
                    ++t;
                    ch = str[i];
                    ++i;
                }
                --i;
                postexp[t] = '#';
                ++t;
        }
        ch = str[i];
        ++i;
    }
    while (op.top != -1)
    {
        postexp[t] = op.data[op.top];
        ++t;
        --op.top;
    }
    postexp[t] = '\0';
}
搜索更多相关主题的帖子: include return 表达式 
2011-11-27 19:30
快速回复:关于表达式求值 注释部分不知道哪错啦
数据加载中...
 
   



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

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