c++实现、求循环队列的长度。 括号匹配的检验。 表达式求值。并运行结果

时间: 2023-09-10 13:07:47 浏览: 32
1. 循环队列长度的实现代码如下: ```c++ #include<iostream> using namespace std; class CircularQueue{ private: int front; // 队头指针 int rear; // 队尾指针 int size; // 队列长度 int *data; // 队列数组 public: CircularQueue(int k){ front = 0; rear = 0; size = k+1; // 为了区分队空和队满,数组长度要比队列长度多1 data = new int[size]; } ~CircularQueue(){ delete[] data; } bool isEmpty(){ return front == rear; } bool isFull(){ return (rear+1)%size == front; } void enqueue(int value){ if(isFull()){ cout<<"Queue is Full"<<endl; return; } data[rear] = value; rear = (rear+1)%size; } int dequeue(){ if(isEmpty()){ cout<<"Queue is Empty"<<endl; return -1; } int res = data[front]; front = (front+1)%size; return res; } int getLength(){ return (rear-front+size)%size; } }; int main(){ CircularQueue cq(3); cq.enqueue(1); cq.enqueue(2); cq.enqueue(3); cout<<"Length: "<<cq.getLength()<<endl; // 输出队列长度 cq.dequeue(); cout<<"Length: "<<cq.getLength()<<endl; cq.dequeue(); cout<<"Length: "<<cq.getLength()<<endl; cq.dequeue(); cout<<"Length: "<<cq.getLength()<<endl; cq.dequeue(); cout<<"Length: "<<cq.getLength()<<endl; return 0; } ``` 2. 括号匹配的检验代码如下: ```c++ #include<iostream> #include<stack> using namespace std; bool isValid(string s) { stack<char> st; for(int i=0;i<s.size();i++){ if(s[i] == '(' || s[i] == '[' || s[i] == '{'){ st.push(s[i]); }else{ if(st.empty()) return false; if(s[i] == ')' && st.top() != '(') return false; if(s[i] == ']' && st.top() != '[') return false; if(s[i] == '}' && st.top() != '{') return false; st.pop(); } } return st.empty(); } int main(){ string s1 = "()[]{}"; string s2 = "([)]"; string s3 = "{[]}"; cout<<"s1: "<<isValid(s1)<<endl; cout<<"s2: "<<isValid(s2)<<endl; cout<<"s3: "<<isValid(s3)<<endl; return 0; } ``` 3. 表达式求值的代码如下: ```c++ #include<iostream> #include<stack> #include<string> using namespace std; int calculate(string s) { stack<int> st; int num = 0; char sign = '+'; for(int i=0;i<s.size();i++){ if(isdigit(s[i])){ num = num*10 + (s[i]-'0'); } if(!isdigit(s[i]) && s[i] != ' ' || i == s.size()-1){ // 判断是否到达符号位或字符串末尾 if(sign == '+'){ st.push(num); }else if(sign == '-'){ st.push(-num); }else if(sign == '*'){ int temp = st.top()*num; st.pop(); st.push(temp); }else if(sign == '/'){ int temp = st.top()/num; st.pop(); st.push(temp); } sign = s[i]; // 更新符号 num = 0; // 清空num } } int res = 0; while(!st.empty()){ res += st.top(); st.pop(); } return res; } int main(){ string s1 = "3+2*2"; string s2 = " 3/2 "; string s3 = " 3+5 / 2 "; cout<<"s1: "<<calculate(s1)<<endl; cout<<"s2: "<<calculate(s2)<<endl; cout<<"s3: "<<calculate(s3)<<endl; return 0; } ``` 运行结果如下: ``` s1: 7 s2: 1 s3: 5 ```

相关推荐

最新推荐

recommend-type

C++中用栈来判断括号字符串匹配问题的实现方法

主要介绍了C++中用栈来判断括号字符串匹配问题的实现方法,是一个比较实用的算法技巧,包含了关于栈的基本操作,需要的朋友可以参考下
recommend-type

C++不使用变量求字符串长度strlen函数的实现方法

主要介绍了C++不使用变量求字符串长度strlen函数的实现方法,实例分析了strlen函数的实现原理与不使用变量求字符串长度的实现技巧,需要的朋友可以参考下
recommend-type

c++使用正则表达式提取关键字的方法

给大家介绍了c++使用正则表达式提取关键字的方法,相对来说比较简单,同时给大家提到了c++通过正则表达式提取匹配到的字符串的方法,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧
recommend-type

C++数据结构与算法之双缓存队列实现方法详解

主要介绍了C++数据结构与算法之双缓存队列实现方法,结合实例形式分析了双缓存队列的原理、实现方法与相关注意事项,需要的朋友可以参考下
recommend-type

C++ 中lambda表达式的编译器实现原理

C++ 11加入了一个非常重要的特性——Lambda表达式。这篇文章主要介绍了C++ 中lambda表达式的编译器实现原理,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

HSV转为RGB的计算公式

HSV (Hue, Saturation, Value) 和 RGB (Red, Green, Blue) 是两种表示颜色的方式。下面是将 HSV 转换为 RGB 的计算公式: 1. 将 HSV 中的 S 和 V 值除以 100,得到范围在 0~1 之间的值。 2. 计算色相 H 在 RGB 中的值。如果 H 的范围在 0~60 或者 300~360 之间,则 R = V,G = (H/60)×V,B = 0。如果 H 的范围在 60~120 之间,则 R = ((120-H)/60)×V,G = V,B = 0。如果 H 的范围在 120~180 之间,则 R = 0,G = V,B =
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。