注册 登录
编程论坛 C++教室

有一点看不懂关于四则运算

Autow 发布于 2014-04-12 08:44, 506 次点击
//添加内容    较复杂的四则运算
int oopcalc::mixed()
{
    int rtn=mul();
    while(shizi[pos]=='+'||shizi[pos]=='-')
    {
        int op=shizi[pos++];
        int opr2=mul();
        if(op=='+')
            rtn+=opr2;
        else
            rtn-=opr2;
    }
    return rtn;
}
int oopcalc::mul()
{
    int rtn=number();
    while(shizi[pos]=='*'||shizi[pos]=='/')
    {
        int op=shizi[pos++];
        int opr2=number();
        if(op=='*')
            rtn*=opr2;
        else
            rtn/=opr2;
    }
    return rtn;
}
int oopcalc::number()
{
    int rtn;                                                    //这里开始看不懂
    if(shizi[pos]=='(')
    {
        pos++;
        rtn=mixed();
        pos++;
    return rtn;                                              //到这里   到底什么意思
    }
    rtn=atoi(shizi+pos);
    while(isdigit(shizi[pos]))
        pos++;
    return rtn;
}


void main()
{
    oopcalc a ;
    a.menu();
}


1 回复
#2
李_小_辉2014-04-12 09:46
Number函数是返回第一个或下一个数字,并将pos移至下一个非数字;如果遇到括号就进行下一级递归,即子级递归。。
1