当前位置:网站首页>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();
}
}
}边栏推荐
- 三角形类(构造与析构)
- OpenSea PHP开发包
- Machine learning deep learning -- Vectorization
- 515. find the maximum value / Sword finger offer II 095 in each tree row Longest common subsequence
- CTF_ Web: how to recognize and evaluate a regular expression
- Basic introduction of gbase 8s blocking technology
- Codeforces Round #802 (Div. 2) C D
- 【FLink】access closed classloader classloader.check-leaked-classloader
- GBASE 8s的隔离级别介绍
- GBASE 8S内存管理
猜你喜欢

2.0springmvc uses restful

CTF_ Web: advanced problem WP (5-8) of attack and defense world expert zone

在 .NET 6 中使用 dotnet format 格式化代码

Upgrade PHP to php7 The impact of X (I). The problem of session retention. Keep login

Successfully solved: selenium common. exceptions. TimeoutException: Message: timeout: Timed out receiving message from

Deep learning - several types of learning

我的IC之旅——资深芯片设计验证工程师成长——“胡”说IC工程师完美进阶

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

php开发支付宝支付功能之扫码支付流程图

A detailed summary of TCP connection triple handshake
随机推荐
CTF_ Web: deserialization learning notes (I) classes and objects in PHP
GBASE 8s 索引B+树
GBASE 8s存儲過程語法結構
简单的恶意样本行文分析-入门篇
What is the storage engine and the three common database storage engines for MySQL
CTF_ Web: file contains pseudo protocol with PHP
GBASE 8s的多线程结构
Le chemin de l'apprentissage immutable - - Adieu à la copie traditionnelle
CTF_ Web: Learn flask template injection (SSTI) from 0
Gbase 8s index R tree
js的call()和apply()
Unity Quad culls shaders with back faces and transparent parts
论文笔记: 多标签学习 ESMC (没看懂, 还没写出来, 暂时放这里占个位置)
Deep learning - several types of learning
What is persistence? What are RDB and AOF in redis persistence?
What is data persistence?
Gbase 8s memory management
初识 Flutter 的绘图组件 — CustomPaint
CTF_ Web: how to recognize and evaluate a regular expression
Code scanning payment flow chart of Alipay payment function developed by PHP