首页 > 代码库 > qt_计算器的简单实现

qt_计算器的简单实现

技术分享技术分享

//阶乘不知道怎么实现不了/(ㄒoㄒ)/~~,以后慢慢调试吧.........

 1  2 //转换为后缀表达式,实现最主要功能 3 void MainWindow::toPostfix () 4 { 5     QString exp = ui->lineEdit->text (); 6     //QString exp = "0.3/(5*2+1)", postfix; 7  8     QString tmp = exp; 9 10     int j;11     //如果只有单个数字时12     for (j = 0; j < tmp.size () && (tmp[j].isDigit () || tmp[j] == .); j++) {13         postFix.push_back (tmp[j]);14     }15     if (j == tmp.size ()) {16         postFix.push_back ( );17         return;18     }19     postFix.clear ();20 21     for (int i = 0; i < exp.size (); i++)22     {23         qDebug() << i << exp[i];24         if (exp[i].isDigit () || exp[i] == .) {//为数字或者小数点25             postFix.push_back (exp[i]);          //直接添加到后缀表达式后面26         }27         else if (exp[i] == () {                //入操作符栈28             opStack.push (exp[i]);29         }30         else if (exp[i] == )) {                //遇到右括号,就转换成空格添加到后缀表达式中31             postFix.push_back ( );32             while (opStack.top () != ()33             {34                 //只要遇到右括号,则在操作符栈中将栈顶下一次出现‘(‘前的操作符全部依次添加到后缀表达式后面35                 //因为进栈的时候,是栈顶操作符优先级低;所以出栈的时候,优先级高->低依次添加到后缀表达式后36                 postFix.push_back (opStack.pop ());37                 qDebug() << postFix;38             }39             opStack.pop ();                     //遇到右括号一次,左括号也相应少一次40         }41         //运算符比栈顶的优先级高时,入栈顶42         else if (getLevel (exp[i]) > getLevel (opStack.top ()))43         {44             //将操作符和操作数用空格分开45             postFix.push_back ( );46             opStack.push (exp[i]);47         }48         else {49             postFix.push_back ( ); qDebug() << postFix;50             //当操作符比栈顶的操作符优先级低时,从栈顶(弹出)pop()运算符,添加到后缀表达式中51             while (getLevel (exp[i]) <= getLevel (opStack.top ()))52                 postFix.push_back (opStack.pop ());53             opStack.push (exp[i]);      //栈顶优先级低则,入栈54         }55     }56     while (opStack.top () != #) {    //结束标志57         QChar c = opStack.pop ();58         postFix.push_back ( );       //操作符之间用空格分开59         postFix.push_back (c);         //将栈中剩余的操作符添加到后缀表达式后面60     }61     qDebug() << postFix;62 }
 1 //计算后缀表达式 2  3 void MainWindow::evaluation () 4 { 5     QString tmp; 6     QStack<double> ans; 7  8     for (int i = 0; i < postFix.size (); i++) 9     {10         qDebug() << postFix[i] << i;11         if (postFix[i].isDigit () || postFix[i] == .)12             tmp.push_back (postFix[i]);13         else if (postFix[i] ==  ) {      //相当于遇到一个操作符或者是括号14             if (!tmp.isEmpty ())15             {16                 ans.push (tmp.toDouble ());17                 tmp.clear ();18             }19             qDebug() << ans.top () << tmp.isEmpty ();20         }21         else {       //遇到操作符,从栈中弹出两个数,进行计算,并将结果入栈22             double a = 0, b = 0;23             switch (postFix[i].cell ()) {24             case !: a = ans.pop ();ans.push (Fac(a)); break;25 26             case +: a = ans.pop (); b = ans.pop ();27                       ans.push (b + a); break;28 29             case -: a = ans.pop (); b = ans.pop ();30                       ans.push (b - a); break; //应该是后弹出栈的-先弹出栈的31 32             case *: a = ans.pop (); b = ans.pop ();33                       ans.push (b * a); break;34             case /:a = ans.pop (); b = ans.pop ();35                      ans.push (b / a); break;36 37             case %: a = ans.pop (); b = ans.pop ();38                       ans.push ((int)b % (int)a); break;39 40             case ^: a = ans.pop (); b = ans.pop ();41                       ans.push (Pow(b, a)); break;42             default:43                 break;44             }45             qDebug() << ans.top () << "top";46         }47     }48     ui->lineEdit->setText (QString::number (ans.top ()));49 }

//全部代码见,百度云

链接: https://pan.baidu.com/s/1qXQKPTM 密码: qwfn

qt_计算器的简单实现