第三章 栈和队列更新完毕

This commit is contained in:
Ruvikm
2021-01-24 21:11:07 +08:00
parent cbf85c29e0
commit df562a90ed
4 changed files with 259 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
#include <iostream>
#include <stack>
#include <string>
using namespace std;
#define _for(i,a,b) for(int i=(a);i<(b);i++)
void Bracket_Pairing() {
// test: "a+{[b*(c-d)]*e+f/(g-h)}*I";
// test: "a+{[b*(c-d)]]*e+f/(g-h)}*I)";
string s;
getline(cin, s);
stack<char> bracket;
_for(i, 0, s.size()) {
if (s[i] == '(' || s[i] == '{' || (s[i] == '[')) {
bracket.push(s[i]);
}
if (s[i] == ')' || s[i] == '}' || (s[i] == ']')) {
char Parentheses = bracket.top();
switch (Parentheses)
{
case '(': {
if (s[i] == ')') {
bracket.pop();
break;
}
}
case '[': {
if (s[i] == ']') {
bracket.pop();
break;
}
}
case '{': {
if (s[i] == '}') {
bracket.pop();
break;
}
}
default: {
cout << "匹配失败!" << endl;
return;
}
}
}
}
if (bracket.empty()) {
cout << "匹配成功!" << endl;
}
return;
}
int main()
{
Bracket_Pairing();
return 0;
}

View File

@@ -0,0 +1,41 @@
#include <iostream>
#include <stack>
#include <string>
using namespace std;
#define _for(i,a,b) for(int i=(a);i<(b);i++)
//P96.2
//按下图所示铁道进行车厢调度(注意, 两侧铁道均为单向行驶道, 火车调度站有一个用
//于调度的“栈道”), 火车调度站的入口处有n节硬座和软座车厢(分别用H和S表示)
//等待调度, 试编写算法, 输出对这n节车厢迸行调度的操作(即入栈或出栈操作)序列,
//以使所有的软座车厢都被调整到硬座车厢之前。
string Arrange(string sats) {
stack<char> arr;
string s;
int j = 0;
_for(i, 0, sats.size()) {
if (sats[i] == 'S') {
s += sats[i];
}
else {
arr.push(sats[i]);
}
}
while (!arr.empty()) {
s += arr.top();
arr.pop();
}
return s;
}
int main()
{
string sats= "HSSSHHHSSHHHHHSSHHSSHSHSHSHS";
cout << Arrange(sats) << endl;
return 0;
}

View File

@@ -0,0 +1,44 @@
#include <iostream>
#include <string>
using namespace std;
#define _for(i,a,b) for(int i=(a);i<(b);i++)
#define MaxSize 30
//P96.3
//利用一个栈实现以下递归函数的非递归计算:
// 1 n = 0
// Pn(x) = 2x n = 1
// 2x * Pn-1(x) - 2(n - 1) * Pn-2(x) n > 1
//直接用数组实现不是更方便 -> ->
double Calculate(int n,double x) {
struct stack {
int number;
double value;
}Sqstack[MaxSize];
int top = -1, i;
double f1 = 1, f2 = 2 * x;
for (i = n; i >= 2; i--) {
top++;
Sqstack[top].number = i;
}
while (top >= 0) {
Sqstack[top].value = 2 * x * f2 - 2 * (Sqstack[top].number - 1) * f1;
f1 = f2;
f2 = Sqstack[top].value;
top--;
}
if (n == 0) {
return f1;
}
return f2;
}
int main()
{
cout << Calculate(6,3.14) << endl;
return 0;
}

View File

@@ -0,0 +1,113 @@
#include <iostream>
#include <stack>
#include <string>
#include <queue>
using namespace std;
#define _for(i,a,b) for(int i=(a);i<(b);i++)
#define MaxSize 10
#define WaitCars 23
#define BusNum 7
#define TrackNum 2
struct Cars {
int id;
string type; //货车:Track 客车:Bus
};
//P96.4
//某汽车轮渡口, 过江渡船毎次能载10辆车过江。过江车辆分为客车类和货车类, 上渡船
//有如下规定 : 同类车先到先上船; 客车先于货车上船, 且每上4辆客车, 才允许放上1
//辆货车; 若等待客车不足4辆, 则以货车代替; 若无货车等待, 允许客车都上船。试设
//计一个算法模拟渡口管理。
#pragma region P96.4
queue<Cars> Bus, Trucks, CarList;
void Load_PassCar() {
Cars c;
c.id = -999;
if (!Bus.empty()) {
c = Bus.front();
Bus.pop();
CarList.push(c);
}
else {
if (!Trucks.empty()) {
c = Trucks.front();
Trucks.pop();
CarList.push(c);
}
}
if (c.id != -999)
cout << "第" << c.id << "号" << c.type << "上船" << endl;
}
void Load_Track() {
Cars c;
c.id = -999;
if (!Trucks.empty()) {
c = Trucks.front();
Trucks.pop();
CarList.push(c);
}
else {
if (!Trucks.empty()) {
c = Bus.front();
Bus.pop();
CarList.push(c);
}
}
if (c.id != -999)
cout << "第" << c.id << "号" << c.type << "上船" << endl;
}
void Ferry_Manage() {
int Bus_Count = 0;
int MaxNum = 0;
while (MaxNum < 10)
{
if (Bus_Count != 4) {
Bus_Count++;
MaxNum++;
Load_PassCar();
}
if (Bus_Count == 4) {
Bus_Count = 0;
MaxNum++;
Load_Track();
}
}
}
#pragma endregion
//test
void Init() {
//Bus
_for(i, 0, BusNum) {
Cars t;
t.id = i, t.type = "Bus";
Bus.push(t);
}
//Track
_for(i, 0, TrackNum) {
Cars t;
t.id = i, t.type = "Track";
Trucks.push(t);
}
}
int main()
{
Init();
Ferry_Manage();
return 0;
}