有哪位大神帮我解释一下这段代码吗? - 爱问答

(爱问答)

有哪位大神帮我解释一下这段代码吗?

#include <iostream>

#include <cstdlib>

 

using namespace std;

 

#define MAX_OP 5

#define operator(c) ((c=='+')||(c=='-')||(c=='*')||(c=='/'))?1:0

#define operands(c) ((c)>='a' && (c)<='z')?1:0

#define MAX_ITEM 100

 

typedef struct tagSTACK{

int item[MAX_ITEM];

int top;

}STACK;

STACK S;

 

char op[MAX_OP]={'(','+','-','*','/'};

char prio[MAX_OP]={0,1,1,2,2};

int op_value[26]={10,30,6,9,8,11,12,13,7,8,22,56,77,76,55,56,43,40,13,19,18,16,46,52,61,63};

 

int preprocessing(char *infix1, char *infix){

    char *pstr, c;

    int *pa, i, j, k, m, n, e10, digit, c_asc=97, ndigit=0;

 

    pstr=infix1;

    pa=op_value;

    ndigit=0;

    i=0; j=0;n=0;

    while(*(pstr+i)!=''){

        if((*(pstr+i)>='0') && (*(pstr+i)<='9'))

            j++;

        else {

            if (j>0){

                digit=*(pstr+i-1)-48;

                k=1;

                while (k<j) {

                    e10=1;

                    for (m=1;m<=k;m++)

                        e10*=10;

                    digit+=(*(pstr+i-1-k)-48)*e10;

                    k++;

                }//while

                *(infix+n)=c_asc++;

                n++;

                *pa=digit;

                ndigit++;

                pa++;

                j=0;

            }//if

            *(infix+n)=*(pstr+i);

            n++;

        }//else

        i++;

    }//while

    if (j>0) {

        digit=*(pstr+i-1)-48;

        k=1;

        while (k<j){

            e10=1;

            for (m=1;m<=k;m++)

                e10*=10;

            digit+=(*(pstr+i-1-k)-48)*e10;

            k++;

        }//while

        *pa=digit;

        ndigit++;

        j=0;

        *(infix+n)=c_asc++;

        n++;

    }//

    *(infix+n)='';

    return ndigit;

}//preprocessing

 

void Push(int x){

    if (S.top < MAX_ITEM-1) {

S.item[++S.top]=x;

    }//if

}//Push

 

void Pop(int *x){

    if (S.top >= 0) {

*x=S.item[S.top--];

    }//if

}//Pop

 

int priority(int c){

    int i;

for(i=0;i<MAX_OP;i++)

if(op[i] == c) return(prio[i]);

    return(-1);

}//priority

 

int evaluate(char optr,int op1,int op2){

    int result;

    switch(optr){

case '+':result=op1+op2;

break;

case '-':result=op1-op2;

break;

case '*':result=op1*op2;

break;

case '/':result=op1/op2;

break;

    }//switch

    return(result);

}//evaluate

 

int post_evaluate(char *postfix){

    char token;

    int op1,op2,result,i=0;

 

    while((token=postfix[i]) != '') {

        if(operands(token))Push(op_value[token-'a']);

        else if(operator(token)){

            Pop(&op2);

            Pop(&op1);

            result=evaluate(token,op1,op2);

            Push(result);

        }//else if(operator(token))

        i=i+1;

    }//while

    Pop (&result);

    return(result);

}//post_evaluate

 

void in_to_post(char *infix,char *postfix){

    int i,j,element;

    char token;

 

    i=j=0;

    while((token=infix[i]) != '') {

        if(operands(token))

            postfix[j++]=token;

        else if(token == '(')

            Push(token);

        else if(token == ')')

            while (S.top >= 0){

                Pop(&element);

                if(element == '(') break;

                postfix[j++]=element;

            }//while

        else if(operator(token)){

            while(S.top >= 0){

                element=S.item[S.top];

                if(priority(token) <= priority(element)){

                    Pop(&element);

                    postfix[j++]=element;

                }//if

                else break;

            }//while

            Push(token);

        }//else if(operator(token))

        i=i+1;

    }//while ((token=infix[i]) != '')

    while(S.top >= 0){

Pop(& element);

postfix[j++]=element;

    }//while(S.top >= 0)

    postfix[j]='';

}//in_to_post

 

int main(){

    int i, count=0;

    char infix[MAX_ITEM],infix_c[MAX_ITEM];

    char postfix[MAX_ITEM];

 

    S.top=-1;

 

    cout << "Please enter infix expression, e.g. (a+b)*c-d/e : ";

    cin >> infix_c;

 

    count = preprocessing(infix_c, infix);

 

    in_to_post(infix,postfix);

    cout << "Infix expression => " << infix << " Postfix expression=> " << postfix;

 

    cout << " And the values of  a ~ z are => ";

    for(i=0;i <count;i++)

cout << op_value[i] << "  " ;

    cout << " The evaluation of infix expression = " << post_evaluate(postfix) << " ";

 

    system("pause");

    return 0;

}//main


这个程序是完成表达式运算的,实现的大概方法是用栈(很多数据结构教程上有这类算法的)

而个人一向不建议是初学者研究分析别人的程序 ,因为这么做对你学习语言基本没有帮助,首先它不会增加你的语法的理解 ,其次,即使看懂它每行程序,也不一定能理解它的具体算法过程,所有程序员都会认为,看懂别人的程序要比自己写一个难很多的。所以,学习语言初期的最好方法是先掌握所有语法,然后是自己试着写程序。到能看懂别人程序时,再去进一步研究

相关标签:大神

下一篇:8000元的电脑配置单

上一篇:双J,Jordan&Judy年度爆品热水袋是否值得入手?

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?