系统粉 > 电脑问答 > 其他问答

已解决数据程序.....请教..

提问者:_blac_k  |  浏览次  |  提问时间:2017-01-18  |  回答数量:2

数据程序.....请教..题目:设一个算术表达式中包括().[],{}三种类型的括号,试编写一个算法判断其中的括号是否匹配.[b]我写了一个程序,老是调不出来,各位高手们帮帮忙 ....

已有2条答案
vae520一生一世

vae520一生一世

回答数:156  |  被采纳数:119

这是我去年写的算数表达式的完整算法代码,你可以借鉴一下:

要用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;}复制代码
2017-01-18 16:52:35
赞 15
aret丶

aret丶

回答数:117  |  被采纳数:74

那个判断是否匹配的的函数太复杂了,你可以比较他们阿斯科马的关系来判断
这个程序我也做过可惜现在找不到了
2017-01-18 20:47:21
赞 11
相关问答
最新其他问答
解决方法