| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 671 人关注过本帖
标题:c语言四则运算
取消只看楼主 加入收藏
暂不存在
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2013-5-30
结帖率:75%
收藏
 问题点数:0 回复次数:1 
c语言四则运算
求大神帮忙注释下下面四则运算的,那些栈的基本操作就不必了,谢谢大家了,时间紧迫,谢谢了

# include <stdio.h>
# include <stdlib.h>

# define STACK_INIT_SIZE   20
# define INCREASE_STACK_SIZE   10
# define ERROR   0
# define OK  1
typedef int SElemType;

struct SqStack
{
    SElemType *base;
    SElemType *top;
    int size;
};

int initStack(SqStack &s)
{
    s.base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType));
    if(! s.base)
        return ERROR;
    s.top = s.base;
    s.size = STACK_INIT_SIZE;
    return OK;
}

int push(SqStack &s, SElemType e)
{
    if(s.top - s.base >= s.size)
    {
        s.base = (SElemType*)realloc(s.base, (s.size + INCREASE_STACK_SIZE) * sizeof(SElemType));
        if(! s.base)
            return ERROR;
        s.top = s.base + s.size;
        s.size += INCREASE_STACK_SIZE;
    }
    * s.top ++ = e;
    return OK;
}

int pop(SqStack &s, SElemType &e)
{
    if(s.top == s.base)
        return ERROR;
    e = * -- s.top;
    return OK;
}

int getTop(SqStack s, SElemType &e)
{
    if(s.top == s.base)
        return ERROR;
    e = * (s.top - 1);
    return OK;
}

bool isOperator(SElemType e)
{
    switch(e)
    {
    case '+':
    case '-':
    case '*':
    case '/':
    case '(':
    case ')':
    case '=':    return true;
    default :    return false;
    }
}

SElemType operate(SElemType a, SElemType op, SElemType b)
{
    SElemType c;
    switch(op)
    {
    case '+':    c = a + b;    break;
    case '-':    c = a - b;    break;
    case '*':    c = a * b;    break;
    case '/':    c = a / b;    break;
    }
    return c;
}

SElemType precede(SElemType a, SElemType b)
{
    SElemType f = '=';
    switch(b)
    {
    case '+':
    case '-':
        if(a == '(' || a == '=')
            f = '<';
        else
            f = '>';
        break;
    case '*':
    case '/':
        if(a == '*' || a == '/' || a == ')')
            f = '>';
        else
            f = '<';
        break;
    case '(':
        if(a == ')')
        {
            printf("表达式有错误\n");
            exit(0);
        }
        else
            f = '<';
        break;
    case ')':
        if(a == '(')
            f = '=';
        else    if(a == '=')
        {
            printf("表达式有错误\n");
            exit(0);
        }
        else
            f = '>';
        break;
    case '=':
        if(a == '(')
        {
            printf("表达式有错误\n");
            exit(0);
        }
        else    if(a == '=')
            f = '=';
        else
            f = '>';
        break;
    }
    return f;
}

SElemType expressionValue()
{
    char c;
    SElemType s1, s2, op, x;
    SqStack opera, num;
    initStack(opera);
    initStack(num);
    push(opera, '=');
    c = getchar();
    getTop(opera, x);
    while((c != '=') || x != '=')
    {
        if(isOperator(c))
        {
            switch(precede(x, c))
            {
            case '<':
                push(opera, c);
                c = getchar();
                break;
            case '=':
                pop(opera, x);
                c = getchar();
                break;
            case '>':
                pop(num, s2);
                pop(num, s1);
                pop(opera, op);
                push(num, operate(s1, op, s2));
                break;
            }
        }
        else    if(c >= '0' && c <= '9')
        {
            s1 = c - '0';
            c = getchar();
            while(c <= '9' && c >= '0')
            {
                s1 = s1 * 10 + c - '0';
                c = getchar();
            }
            push(num, s1);
        }
        else
        {
            printf("表达式有错误\n");
            exit(0);
        }
        getTop(opera, x);
    }
    pop(num, x);
    return x;
}

int main()
{
    printf("%d\n", expressionValue());
    return 0;
}
搜索更多相关主题的帖子: include return c语言 
2014-02-18 21:07
暂不存在
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2013-5-30
收藏
得分:0 
求大神搭救啊啊啊啊
2014-02-18 21:58
快速回复:c语言四则运算
数据加载中...
 
   



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

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