C++题题题题题题题题题踢踢踢
后缀表达式求值
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
string a[100];
string b[100];
stack<string> op;
int la=0,lb=0;
int main(){while(true){cin>>a[la];if(a[la]==".") break;la++;}for(int i=0;i<la;i++){if(a[i]=="*"||a[i]=="/"){if(op.empty()==false){while(op.top()=="*"||op.top()=="/"){b[lb++]=op.top();op.pop();}op.push(a[i]);}op.push(a[i]);}else if(a[i]=="+"||a[i]=="-"){if(op.empty()==false){while(op.top()=="*"||op.top()=="/"||a[i]=="+"||a[i]=="-"){b[lb++]=op.top();op.pop();}}op.push(a[i]);}else if(a[i]=="("||a[i]==")"){if(a[i]=="(") op.push(a[i]);else if(a[i]==")"){while(op.top()!="("){b[lb++]=op.top();op.pop();}}op.pop();}else{b[lb++]=a[i];}}while(op.empty()!=true){b[lb++]=op.top();op.pop();}for(int i=0;i<lb;i++){cout<<b[i]<<" ";}for(int i=0;i<lb;i++){if(b[i]=="+"){string ta=op.top();op.pop();string tb=op.top();op.pop();string res=to_string(stoi(tb)+stoi(ta));op.push(res);}else if(b[i]=="*"){string ta=op.top();op.pop();string tb=op.top();op.pop();string res=to_string(stoi(tb)*stoi(ta));op.push(res);}else if(b[i]=="/"){string ta=op.top();op.pop();string tb=op.top();op.pop();string res=to_string(stoi(tb)/stoi(ta));op.push(res);}else{op.push(b[i]);}}cout<<op.top()<<endl;return 0;
}
封装最大最小辅助栈
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
class my{
private:stack<int> mains;stack<int> mins;stack<int> maxs;
public:void push(int);int top();int maxv();int minv();int pop();bool empty();};
int main(){my a;a.push(1);a.push(2);a.push(3);a.push(4);a.push(5);a.push(6);a.push(7);cout<<a.maxv()<<endl;cout<<a.minv()<<endl;return 0;
}
void my::push(int k){mains.push(k);if(maxs.empty()==true&&maxs.top()<=k){maxs.push(k);}if(mins.empty()==true&&mins.top()>=k){mins.push(k);}
}
int my::top(){int k=mains.top();mins.pop();if(k==mins.top()) mins.pop();if(k==maxs.top()) maxs.pop();
}
int my::maxv(){return maxs.top();
}
int my::minv(){return mins.top();
}
int my::pop(){return mains.top();}
bool my::empty(){return mains.empty();
}