当前位置:网站首页>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();
}
}
}边栏推荐
- CTF_ Web: advanced problem WP (5-8) of attack and defense world expert zone
- Office macro virus bounce shell experiment
- After the newly assigned variable of the applet is modified, the original variable will also be modified
- GBASE 8s的多线程结构
- Efficient NoSQL database service Amazon dynamodb experience sharing
- 重磅直播 | 相移法+多频外差之数学原理推导+实现
- 小白学习MySQL - 统计的'投机取巧'
- 华为鸿蒙开发第四课
- Separation of storage and computing in Dahua cloud native database
- GBASE 8s 总体架构
猜你喜欢

执行SQL响应比较慢,你有哪些排查思路?

leetcode1221. Split balance string

哪个编程语言实现hello world最烦琐?

Separation of storage and computing in Dahua cloud native database

A detailed summary of four handshakes (or four waves) over TCP connections

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

GBASE 8s 索引R树

CTF_ Web: Changan cup-2021 old but a little new & asuka

Part I Verilog quick start

leetcode1221. 分割平衡字符串
随机推荐
Classification of gbase 8s locks
Code scanning payment flow chart of Alipay payment function developed by PHP
[Flink] problems and solutions of the continuous growth of checkpoint size in rocksdb incremental mode
515. find the maximum value / Sword finger offer II 095 in each tree row Longest common subsequence
Anaconda installation +tensorflow installation +keras installation +numpy installation (including image and version information compatibility issues)
绝了!自动点赞,我用 PyAutoGUI!
2021.8.29 notes: register, bit operation, pointer, structure
What is persistence? What are RDB and AOF in redis persistence?
SQL injection details
Machine learning deep learning -- Vectorization
GBase 8s的封锁技术的基本介绍
Upgrade PHP to php7 The impact of X (2), the obsolescence of mcrypt decryption
Use of deferred environment variable in gbase 8s
CTF_ Web: Advanced questions of attack and defense world expert zone WP (15-18)
CTF_ Web: how to recognize and evaluate a regular expression
Upgrade PHP to php7 X (III) failure of wechat payment callback
华为鸿蒙开发第四课
What is data persistence?
JS call() and apply()
GBase 8s 锁的分类