第六次课程作业
核心部分
將中綴表達式轉換成后綴表達式
string InfixToPostfix(string infix) {char current = 0;string postfix;//后綴表達式stack<char> mark;//符號棧map<char,int> priority;//符號優先級priority['+'] = 0;priority['-'] = 0;priority['*'] = 1;priority['/'] = 1;for(int i = 0;i < infix.size(); ++i){current = infix[i];switch(current){case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':case '9':case '.':postfix.push_back(current);//數字直接寫入break;case '+':case '-':case '*':case '/'://如果運算符的前一項不是右括號即說明前一個數字輸入完畢,用#標識if(infix[i-1] != ')')postfix.push_back('#');//如果符號棧非空,即比較目前符號與棧頂符號優先級,低于等于出棧(并寫入輸出字符串),//直至符號全部出棧或者遇到了'('或者大于棧頂符號的優先級if(!mark.empty()){char tempTop = mark.top();while(tempTop != '(' && priority[current] <= priority[tempTop]){postfix.push_back(tempTop);mark.pop();if(mark.empty())break;tempTop = mark.top();}}mark.push(current);//新符號入棧break;case '(':if(infix[i-1] >= '0' && infix[i-1] <= '9')// for expression 2-5*2(6/2){postfix.push_back('#');mark.push('*');}mark.push(current);break;case ')':postfix.push_back('#');//右括號說明前方數字輸入完成,標識一下while(mark.top() != '('){postfix.push_back(mark.top());mark.pop();}mark.pop();//左括號出棧break;default:break;//忽略其他字符}}if(infix[infix.size()-1] != ')')postfix.push_back('#');//中綴表達式最后一個是數字需要加上#。while(!mark.empty())//如果棧非空,全部出棧并寫入輸出字符串{postfix.push_back(mark.top());mark.pop();}return postfix; }計算后綴表達式
float posfixCompute(string s) {stack<float> tempResult;string strNum;float currNum = 0;float tempNum = 0;for(string::const_iterator i = s.begin(); i != s.end(); ++i){switch(*i){case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':case '9':case '.':strNum.push_back(*i);break;case '+':tempNum = tempResult.top();tempResult.pop();tempNum += tempResult.top();tempResult.pop();tempResult.push(tempNum);break;case '-':tempNum = tempResult.top();tempResult.pop();tempNum = tempResult.top() - tempNum;tempResult.pop();tempResult.push(tempNum);break;case '*':tempNum = tempResult.top();tempResult.pop();tempNum *= tempResult.top();tempResult.pop();tempResult.push(tempNum);break;case '/':tempNum = tempResult.top();tempResult.pop();tempNum = tempResult.top() / tempNum;tempResult.pop();tempResult.push(tempNum);break;case '#':currNum = atof(strNum.c_str());//in c++11, use currNum = std::stof(strNUm);strNum.clear();tempResult.push(currNum);break;}}return tempResult.top(); }// 表達式計算 float expressionCalculate(string s) {return posfixCompute(InfixToPostfix(s)); }C++界面編程
通過網絡上學習簡單的了解了部分
Hello World!的顯示
點擊運行,不知道發生了什么,沒有彈出窗口。
將上面第 22 行代碼改為:
label->setText(tr("Hello World!"));
可以將Hello World!更改為紅色,微軟雅黑字體。但是同樣運行彈出窗口。
不知道軟件哪里出現了問題。。
轉載于:https://www.cnblogs.com/Zzwena/p/6925889.html
總結
- 上一篇: Python内置函数(49)——isin
- 下一篇: 1602液晶显示