当前位置:网站首页>OOP stack class template (template +ds)
OOP stack class template (template +ds)
2022-06-25 04:43:00 【SZU healing system bug】
Catalog
Title Description
With the help of function template, stack operation is realized .
Stack is an advanced and backward data structure , Its insertion 、 Deletion can only be performed at the top of the stack .Push For stack operation , Insert ,Pop For stack out operation , Delete immediately .
Stack operation is similar to stacking plates , The first plate is at the bottom , Put it on the plate after . When you want to pick up the plate , Just take it from the top .
For example, stack data 1 To 2 Until then 3, that 3 At the top ,1 At the bottom . When you want to stack data , Namely 3 First out , Next is 2, And finally 1 Out of the stack .
You are required to define the stack structure by yourself , And use function template and class template to complete char,int and float Data processing . among , We define :
Input
The first line is the number of test data
For each group of test data , The first row is data type , The second line is the operand
For each operation Push or Pop operation . it is to be noted that , When the stack is empty, you should give Error Tips
Output
For each stack , No output required . For each stack , Output out of stack elements or give Error Tips . When all operations are completed , Output the remaining elements in the stack in reverse order
sample input 1
3
I
6
Push 6
Push 1
Push 1
Pop
Pop
Pop
C
4
Pop
Push a
Push a
Pop
F
8
Push 4.1
Push 4.2
Push 14.1
Push 4.2
Push 1314
Push 1314
Pop
Pop
sample output 1
1 is popped from stack!
1 is popped from stack!
6 is popped from stack!
Empty Stack!
Error!
a is popped from stack!
a
1314 is popped from stack!
1314 is popped from stack!
4.2 14.1 4.2 4.1
AC Code
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<iomanip>
using namespace std;
template<class kind>
class stack {
kind p[10000];
int top = -1;
public:
void push() {
kind num;
cin >> num;
p[++top] = num;
}
void pop() {
if (empty()) {
cout << "Error!" << endl;
} else {
cout << p[top--] << " is popped from stack!" << endl;
}
}
bool empty() {
if (top < 0)
return 1;
return 0;
}
void display() {
if (empty())
cout << "Empty Stack!" << endl;
else {
for (int i = top;i>=0;i--)
cout << p[i] << ' ';
cout << endl;
}
}
};
int main() {
int test, count;
char code;
string index;
cin >> test;
while (test--) {
cin >> code >> count;
if (code == 'I') {
stack<int> temp;
while (count--) {
cin >> index;
if (index == "Push")
temp.push();
else
temp.pop();
}
temp.display();
} else if (code == 'C') {
stack<char> temp;
while (count--) {
cin >> index;
if (index == "Push")
temp.push();
else
temp.pop();
}
temp.display();
} else {
stack<float> temp;
while (count--) {
cin >> index;
if (index == "Push")
temp.push();
else
temp.pop();
}
temp.display();
}
}
}边栏推荐
- 多睡觉,能减肥,芝加哥大学最新研究:每天多睡1小时,等于少吃一根炸鸡腿...
- GBASE 8s存儲過程語法結構
- JS' sort() function
- 【esp32学习之路6——flash加密】
- Mongodb cluster
- STM32的DMA双缓冲模式详解
- CTF_ Web: how to recognize and evaluate a regular expression
- Retrofit source code analysis
- 初识 Flutter 的绘图组件 — CustomPaint
- A detailed summary of four handshakes (or four waves) over TCP connections
猜你喜欢

GBASE 8s 索引R树

cnpm : 无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。

CTF_ Web: deserialization of learning notes (II) CTF classic test questions from shallow to deep

js中的concat()

The solution of wechat applet switchtab unable to take parameters

Xiaobai learns MySQL - Statistical 'opportunism'

Record small knowledge points

Machine learning deep learning -- Vectorization
![[untitled]](/img/68/5e711f7c473dcea54a56f7b7e48604.png)
[untitled]

GBASE 8s 索引B+树
随机推荐
Use text analysis to identify the main gender in a text
Use of deferred environment variable in gbase 8s
《牛客刷verilog》Part I Verilog快速入门
30岁了开始自学编程,家里比较困难还来得及吗?
Record the problem of C # print size once
Xiaobai learns MySQL - Statistical 'opportunism'
Gbase 8s index R tree
CTF_ Web: file contains pseudo protocol with PHP
Cannot import name 'escape' from 'jinja2' [solved successfully]
STM32的DMA双缓冲模式详解
cnpm : 无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。
使用文本分析识别一段文本中的主要性别
三角形类(构造与析构)
GBASE 8s存儲過程語法結構
Mongodb cluster
Data import and export for gbase 8s
绝了!自动点赞,我用 PyAutoGUI!
重磅直播 | 相移法+多频外差之数学原理推导+实现
Multithreading structure of gbase 8s
[untitled]