速求一个c++四则混合表达式运算的程序有+,-,*,/,(,),比如7*(5+6*9)+3/5,都是整数运算,除为整除.

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/01 06:56:12
速求一个c++四则混合表达式运算的程序有+,-,*,/,(,),比如7*(5+6*9)+3/5,都是整数运算,除为整除.
xZOVW6MEœj!|)TU"QʷmLbĸ~Li)$G@%@ 1M$Ů}@0c`r}m{{^=\ِsYpm Fb&#֮\(Ө\[iwbb N@XZ;ot?t>X2~&ؑK@yuz<3\oOS'I)1--㈹T^+ ί* IlS:Hǩ?oTJpLOi. x$fZ "B`N<] '.c%.K;Bq73:|eX p>a ~8]nZoxSp~aUH^Gks SsxDA襇ϰ1 (r¯Av3%W~v|waUkޭwVɊ>QѷTaX_exΈʇbPˎ``JF$/ԙ /#|+H{cf@6ͫkKrDi_~pZ-JfP3*#WסyՍ7&"0<);jlZH;H4dasaY+Aw3l=W(Y{uk"R W캪ԎE]jbV+F܌6雋5 ]Af8۟lOXC:vG"|k5KxQ~ 7EP^^hˬVfD:Ѧp'"Rp6^ʬ^ jm5h vibmZo*!7WA?M=6B,5#o}ʹjNud7D+Os*%B 1LOLۑW?oK&{Yҿ"FꍡǔR

速求一个c++四则混合表达式运算的程序有+,-,*,/,(,),比如7*(5+6*9)+3/5,都是整数运算,除为整除.
速求一个c++四则混合表达式运算的程序
有+,-,*,/,(,),比如7*(5+6*9)+3/5,都是整数运算,除为整除.

速求一个c++四则混合表达式运算的程序有+,-,*,/,(,),比如7*(5+6*9)+3/5,都是整数运算,除为整除.
//看来这个问题还没有搞定啊,我来帮你搞定吧,稍等~
//好了,现在搞定,发给你吧~
//为了你方便看,就写在一个cpp文件里了,比较长,三百多行.我已经编译通过,也做了简单测试,没详细测,如果有bug,你就自己改一下吧.
//表达式输入完了之后直接回车,就出结果了,跟平时输入字符串一样.

//
int main()
{    
    SqStack OPTR,OPND ;    
    int element=0 ;    
    char OPTR_element ;    
    int leftNum,rightNum ;    
    char input ;
    //获取输入    
    char OP[7]={'+','-','*','/','(',')','\n'};
    
    InitStack(&OPTR);    
    Push(&OPTR,'\n');    
    InitStack(&OPND);
    printf("请输入表达式\n");    
    input=getchar();    
    while('\n'!=input||'\n'!=GetTop(&OPTR,&OPTR_element))    
    {        
        int temp=0 ;        
        if(isdigit(input))        
        {
            //如果是数字            
            ungetc(input,stdin);
            //返回给输入流            
            scanf("%d",&temp);            
            Push(&OPND,temp);
            //数字就进OPND栈            
            input=getchar();            
            continue ;            
        }
        
        if(OK==InOP(input,OP))        
        {            
            GetTop(&OPTR,&OPTR_element);            
            switch(Compare(OPTR_element,input))            
            {                
            case '<' :
                //栈顶元素优先级低                
                Push(&OPTR,input);
                //运算符进OPTR栈                
                input=getchar();                
                break ;                
            case '=' :
                //脱括号                
                PopOPTR(&OPTR,&OPTR_element);                
                input=getchar();                
                break ;                
            case '>' :
                //退栈,并将运算结果入栈                
                PopOPTR(&OPTR,&OPTR_element);                
                Pop(&OPND,&rightNum);                
                Pop(&OPND,&leftNum);                
                Push(&OPND,Calculate(leftNum,OPTR_element,rightNum));                
                break ;                
            default :                
                printf("表达式括号不匹配\n");               
                return ERROR ;                
            }//switch
        }//if
        
        else         
        {            
            printf("表达式内有未知字符,即将退出\n");            
            return ERROR ;            
        }
    }//while
    
    int value ;    
    Pop(&OPND,&value);    
    printf("结果 = %d\n",value);
    
    return OK ;    
}//end