括号匹配
void MatchingBrackets(string &str){
stack<string> bra;
int len=str.size();//unsigned int string<T>::size()
int i=0;
while(i<len){
char temp=str.at(i);//char string<T>::at(int i)
if(temp=='{' || temp== '(' || temp== '['){
bra.push(temp);
i++;
}
if(temp=='}' || temp== ')' || temp== ']'){
if(temp==bra.top()){
bra.pop();
i++;
}
else{
cout<<"failed"<<endl;
return;
}
}
}
if(bra.empty()==false){
cout<<"failed"<<endl;
}
else
cout<<"success"<<endl;
return;
}
/*
string:
-constructer
--string(const char *)
--string(int n,char ch)
-size
--length
--size()
-makeEmpty
--clear()
-IsEmpty
--empty()
-iterator
--begin()//the front
--end()//after the rear
--rbegin()//the rear
--rend()//before the front
-getElem
--operator[](int)
--at(int i)
-Insert
--insert(int,const char *)
--insert(int,const string)
--append(const char *)
--append(const string &)
-Search
--find(char)
--find(string &)
--find(char *)
*/
后缀表达式计算
void get2num(stack<double> num,double &d1,double &d2){
d1=num.top();num.pop();
assert(num.empty()==false);
d2=num.top();num.pop();
}
double PostfixExpression(){
stack<double> num;
char ch
double d1,d2,res;
while(cin>>ch && ch!='#'){
switch(ch){
case '+':get2num(num,d1,d2);res=d1+d2;num.push(res);break;
case:'-':get2num(num,d1,d2);res=d1-d2;num.push(res);break;
case:'*':get2num(num,d1,d2);res=d1*d2;num.push(res);break;
case:'/':
get2num(num,d1,d2);
if(d2==0) {
cerr<<"zero divided error"<<endl;
num.push(d1);num.push(d2);
break;
}
res=d1/d2;
num.push(res);
break;
default: //number
//get double
double temp;
cin.putback(ch);
cin>>temp;
num.push(temp);
break;
}
}while(true && ch!='#')
return num.top();
}