當前位置:係統粉 > 電腦問答 > 其他問答 > 數據程序.....請教..

數據程序.....請教..

提問者:_blac_k  |  瀏覽 次  |  提問時間:2017-01-18  |  回答數量:2

數據程序.....請教..題目:設一個算術表達式中包括().[],{}三種類型的括號,試編寫一個算法判斷其中的括號是否匹配.[b]我寫了一個程序,老是調不出來,各位高手們幫幫忙 ....

已有2條答案
vae520一生一世

vae520一生一世

回答數:156  |  被采納數:119

2017-01-18 16:52:35
這是我去年寫的算數表達式的完整算法代碼,你可以借鑒一下:

要用DEV等運行,VC6.0支持不是很好
#include <iostream>#include <vector>#include <stack>#include <string>#include <sstream>using namespace std;string Postfix_Expression;vector <double> StoreData;vector <char>StoreOper;vector <double> StoreQueue;stack < double, vector<double> > Data_Stack(StoreData);stack < char,vector<char>> Oper_Stack(StoreOper);stack < double, vector<double> > Queue_Stack(StoreQueue);inline intPriority( char );void Make_The_Postfix_Stack();void Calculate_The_Postfix_Stack();int main(int argc, char * argv[]){Make_The_Postfix_Stack();Calculate_The_Postfix_Stack();system("pause");return 0;}void Make_The_Postfix_Stack(){size_t Index = 0;string Scrstr;string Buffer;string Check(".0123456789(+-*/)#");char Space;char Char_Temp;double Double_Temp;Oper_Stack.push(&#39;#&#39;);cout<<"Please enter an expression:"<<endl;check1:getline(cin, Scrstr);if(Scrstr.find_first_not_of(Check,0) != string::npos){ cerr<<"Error input, try again!"<<endl; goto check1;}Scrstr += "#";stringstream PushStream;stringstream TempStream;PushStream.str(Scrstr);while( (PushStream.str().size() - Index) > 0 ){ size_t StringSize; if(isdigit(PushStream.str()[Index])) { PushStream >> Double_Temp; TempStream << Double_Temp; TempStream >> Buffer; StringSize = Buffer.size(); Index += StringSize; Postfix_Expression = Postfix_Expression + " " + Buffer; TempStream.clear(); } else { PushStream >> Char_Temp; switch(Char_Temp) { case &#39;(&#39;: Oper_Stack.push(Char_Temp); break; case &#39;)&#39;: case &#39;#&#39;: do{ Char_Temp = Oper_Stack.top(); Oper_Stack.pop(); if(Char_Temp != &#39;(&#39; && Char_Temp != &#39;#&#39;) Postfix_Expression = Postfix_Expression + " " + Char_Temp; }while(Char_Temp!=&#39;(&#39; && !Oper_Stack.empty()); break; case &#39;+&#39;: case &#39;-&#39;: case &#39;*&#39;: case &#39;/&#39;: while(Priority(Char_Temp) <= Priority(Oper_Stack.top())) { Postfix_Expression = Postfix_Expression + " " + Oper_Stack.top(); Oper_Stack.pop(); } Oper_Stack.push(Char_Temp); break; } Index ++; } }}int Priority( char ch ){int priority;switch( ch ){ case &#39;+&#39; : priority = 1; break; case &#39;-&#39; : priority = 1; break; case &#39;*&#39; : priority = 2; break; case &#39;/&#39; : priority = 2; break; default : priority = 0; break;}return priority;}void Calculate_The_Postfix_Stack(){size_t size = 0;string Buffer;stringstream sstr;stringstream temp;double Value_Buffer;double x,y;char Oper_Buffer;sstr.str(Postfix_Expression);while( (sstr.str().size() - size) > 0 ){ if(isdigit(sstr.str()[size])||isdigit(sstr.str()[size+1])) { sstr >> Value_Buffer; temp << Value_Buffer; temp >> Buffer; size += (Buffer.size() + 1); temp.clear(); Queue_Stack.push(Value_Buffer); } else { sstr >> Oper_Buffer; y = Queue_Stack.top(); Queue_Stack.pop(); x = Queue_Stack.top(); Queue_Stack.pop(); switch(Oper_Buffer) { case &#39;+&#39;: Queue_Stack.push( x+y ); break; case &#39;-&#39;: Queue_Stack.push( x-y ); break; case &#39;*&#39;: Queue_Stack.push( x*y ); break; case &#39;/&#39;: Queue_Stack.push( x/y ); break; } size += 2; }}cout<<"Result = "<<Queue_Stack.top()<<endl;}複製代碼
讚 15
aret丶

aret丶

回答數:117  |  被采納數:74

2017-01-18 20:47:21
那個判斷是否匹配的的函數太複雜了,你可以比較他們阿斯科馬的關係來判斷
這個程序我也做過可惜現在找不到了
讚 11
解決方法
版權信息

Copyright @ 2011 係統粉 版權聲明 最新發布內容 網站導航