当前位置: 首页 > ds >正文

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();
}

http://www.xdnf.cn/news/5608.html

相关文章:

  • 《Go小技巧易错点100例》第三十二篇
  • Redis 缓存
  • C 语言数据结构基石:揭开数组名的面纱与计算数组大小
  • AQS(AbstractQueuedSynchronizer)解析
  • m1 安装 Elasticsearch、ik、kibana
  • 树莓派5+UPS电源 5v
  • 快速搭建一个vue前端工程
  • 大疆卓驭嵌入式面经及参考答案
  • 理解微积分 | 概念 / 定义 / 性质 / 关系
  • Kafka的基本概念和Dokcer中部署Kafka
  • 从0开始学linux韦东山教程第三章问题小结(3)
  • Python-3.14.0|Win英文|python编译器|安装教程
  • NoSQL数据库技术与应用复习总结【看到最后】
  • 第四节第一部分:继承,使用继承的好处
  • Web开发—Vue工程化
  • Redis设计与实现——数据结构与对象
  • 【iOS】SDWebImage源码学习
  • 深入理解反序列化攻击:原理、示例与利用工具实战
  • 计算机网络——以太网交换机
  • .Net HttpClient 发送Http请求
  • PyTorch:深度学习的 powerful 库
  • Spyglass:在batch/shell模式下运行目标的顶层是什么?
  • 理想闯入智驾“无人区”
  • 湖北理元理律师事务所债务优化体系拆解:科学规划如何实现“还款不降质”
  • Lua再学习
  • 拓扑学在天体物理学的应用:python 示例
  • HTTP 响应状态码总结
  • k8s的节点是否能直接 curl Service 名称
  • I2C通讯
  • 基于.Net Core开发的GraphQL开源项目