聽聽聽 渚嬪錛?123*-"杞崲鎴愭嘗鍏板紡涓?-1*23"
聽聽聽 閫嗘嘗鍏板紡"123*-"鐨勮〃杈懼紡鏍戝涓嬶細
聽聽聽
鎵浠ヨ繖涓漿鎹㈣繃紼嬪氨鏄細宸茬煡涓涓簩鍙夋爲鐨勫悗鏍歸亶鍘嗗簭鍒楋紝姹傚厛鏍歸亶鍘嗗簭鍒椼?br />聽聽聽 鎴戠殑綆楁硶鏄牴鎹悗鏍歸亶鍘嗙殑搴忓垪鏋勯犱竴涓〃杈懼紡鏍戯紝榪涜屽厛鏍歸亶鍘嗘鏍戣幏寰楁嘗鍏板紡琛ㄨ揪寮忋?br />聽聽聽 瀹氫箟浜嗕袱涓粨鏋勪綋錛?br />struct聽Exp{
聽聽聽聽char聽聽op;
聽聽聽聽Item聽聽lhs;
聽聽聽聽Item聽聽rhs;
聽聽聽聽Exp(){};
聽聽聽聽Exp(char聽_op,聽Item聽_lhs,聽Item聽_rhs):op(_op),聽lhs(_lhs),聽rhs(_rhs){聽}
聽聽聽聽Exp(const聽Exp&聽e):op(e.op),聽lhs(e.lhs),聽rhs(e.rhs)聽{聽}
};
琛ㄧず涓涓〃杈懼紡錛屼篃鏄〃杈懼紡鏍戜笂鐨勪竴涓瓙鏍戙?br />struct聽Item{
聽聽聽聽char聽聽number;
聽聽聽聽shared_ptr<Exp>聽pExp;
聽聽聽聽bool聽isNumber;
聽聽聽聽explicit聽Item():isNumber(true),聽number('0'),聽pExp(){聽聽聽聽}
聽聽聽聽Item(const聽Item&聽i):number(i.number),聽pExp(i.pExp),聽isNumber(i.isNumber){聽}
};
琛ㄧず涓涓妭鐐癸紝瀹冨彲浠ユ槸涓涓暟瀛楋紝鎴栬呬竴涓〃杈懼紡錛坧Exp榪欓噷鎴戜嬌鐢ㄧ殑鏄?a >boost搴?/a>鐨勬櫤鑳芥寚閽坰hared_ptr錛屾墍浠ョ紪璇戠殑璇濓紝闇瑕佸厛瀹夎boost搴擄級銆?br />榪愯鐨勭粨鏋滃鍥撅細

*杈撳叆鏃訛紝浠?e'琛ㄧず杈撳叆緇撴潫銆?br />瀹屾暣鐨勪唬鐮佸拰鍙墽琛屾枃浠剁偣鍑?a class="" title="" href="/Files/nj-blog/porland.rar" target="">榪欓噷涓嬭澆銆傛潈褰撴姏鐮栧紩鐜変簡錛屽笇鏈涙湁鏇村ソ綆楁硶鐨勫悓瀛﹁祼鏁欍?br />
瀹屾暣鐨勪唬鐮侊細

#include聽<stack>
#include聽<algorithm>
#include聽<string>
#include聽<iostream>
#include聽<boost/shared_ptr.hpp>
using聽namespace聽std;
using聽boost::shared_ptr;

struct聽Exp;

struct聽Item
{
聽聽聽聽char聽聽number;
聽聽聽聽shared_ptr<Exp>聽pExp;
聽聽聽聽bool聽isNumber;

聽聽聽聽explicit聽Item():isNumber(true),聽number('0'),聽pExp()
{聽聽聽聽}

聽聽聽聽Item(const聽Item&聽i):number(i.number),聽pExp(i.pExp),聽isNumber(i.isNumber)
{聽}
};


struct聽Exp
{
聽聽聽聽char聽聽op;
聽聽聽聽Item聽聽lhs;
聽聽聽聽Item聽聽rhs;

聽聽聽聽Exp()
{};

聽聽聽聽Exp(char聽_op,聽Item聽_lhs,聽Item聽_rhs):op(_op),聽lhs(_lhs),聽rhs(_rhs)
{聽}

聽聽聽聽Exp(const聽Exp&聽e):op(e.op),聽lhs(e.lhs),聽rhs(e.rhs)聽
{聽}
};


class聽Error
{
聽聽聽聽string聽info;
public:

聽聽聽聽Error(string聽_info):info(_info)
{聽}

聽聽聽聽Error():info("")
{聽}

聽聽聽聽string聽what()
{return聽info;}聽聽聽
};


void聽printPorland(Exp&聽exp)
{
聽聽聽聽cout聽<<聽exp.op聽;
聽聽聽聽if(exp.lhs.isNumber)聽聽cout聽<<聽exp.lhs.number;
聽聽聽聽else聽printPorland(*exp.lhs.pExp);
聽聽聽聽if(exp.rhs.isNumber)聽聽cout聽<<聽exp.rhs.number;
聽聽聽聽else聽printPorland(*exp.rhs.pExp);
聽聽聽聽return;
}

int聽main()


{
聽聽聽聽stack<Item>聽聽ExpStack;
聽聽聽聽char聽tmpChar;
聽聽聽聽Item聽tmpItem;
聽聽聽聽Item聽tmpLhs;
聽聽聽聽Item聽tmpRhs;
聽聽聽聽string聽聽numbers聽=聽"0123456789";
聽聽聽聽string聽聽operators聽=聽"+-*/";

聽聽聽聽cout<<"Input聽the聽Express(杈撳叆聽'e'鏍囪瘑緇撴潫):";

聽聽聽聽do
{

聽聽聽聽try
{

聽聽聽聽聽聽聽聽while(cin>>tmpChar)
{
聽聽聽聽聽聽聽聽聽聽聽聽if(tmpChar聽==聽'e')聽break;聽聽//e涓虹粨鏉熺
聽聽聽聽聽聽聽聽聽聽聽聽else聽if(find(numbers.begin(),聽numbers.end(),聽聽//鏄竴涓暟瀛?/span>

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽tmpChar)!=numbers.end())
{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽tmpItem.isNumber聽=聽true;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽tmpItem.number聽聽聽=聽tmpChar;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽ExpStack.push(tmpItem);//鏁板瓧鍏ユ爤

聽聽聽聽聽聽聽聽聽聽聽聽}else聽if(find(operators.begin(),聽operators.end(),聽//鏄竴涓搷浣滅

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽tmpChar)!=operators.end())
{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//鎿嶄綔絎︽瘡嬈¤瀵瑰簲涓や釜琚搷浣滄暟錛屽惁鍒欒娉曢敊璇?/span>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽if(ExpStack.size()<2)聽throw聽Error("Syntactic聽Error!");聽

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽//鎿嶄綔絎︿袱杈圭殑鍏冪礌鍑烘爤
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽tmpRhs聽=聽ExpStack.top();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽ExpStack.pop();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽tmpLhs聽=聽ExpStack.top();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽ExpStack.pop();

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽tmpItem.isNumber聽=聽false;聽聽聽//闈炴暟瀛楋紝鏄竴涓〃杈懼紡
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽tmpItem.pExp聽=聽shared_ptr<Exp>(new聽Exp(tmpChar,聽tmpLhs,聽tmpRhs));聽

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽ExpStack.push(tmpItem);聽聽聽聽聽//琛ㄨ揪寮忓叆鏍?/span>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽

聽聽聽聽聽聽聽聽聽聽聽聽}else聽
{聽聽//聽鏈煡瀛楃
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽throw聽聽Error("Unknow聽Character!");
聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽}

聽聽聽聽聽聽聽聽if(ExpStack.size()!=1)聽throw聽Error("Syntactic聽Error!");

聽聽聽聽聽聽聽聽tmpItem聽=聽ExpStack.top();
聽聽聽聽聽聽聽聽ExpStack.pop();

聽聽聽聽聽聽聽聽if(tmpItem.isNumber)聽cout聽<<聽tmpItem.number聽<<endl;
聽聽聽聽聽聽聽聽else聽printPorland(*tmpItem.pExp);
聽聽聽聽聽聽聽聽cout聽<<聽endl;


聽聽聽聽}catch(Error&聽e)
{
聽聽聽聽聽聽聽聽cout聽<<聽e.what()聽<<聽endl;
聽聽聽聽聽聽聽聽getline(cin,聽string());聽聽聽聽聽聽聽聽//璺寵繃閿欒鐨勫綋鍓嶈
聽聽聽聽}

聽聽聽聽聽聽聽聽cout聽<<聽"Try聽again?(y/n)"聽<<聽endl;
聽聽聽聽聽聽聽聽cin聽>>聽tmpChar;
聽聽聽聽}while(tmpChar聽==聽'y'聽||聽tmpChar聽==聽'Y');
聽聽聽聽
聽聽聽聽return聽0;
}


]]>