问题描述 :
目的:使用C++模板设计顺序栈的抽象数据类型(ADT)。并在此基础上,使用顺序栈ADT的基本操作,设计并实现简单应用的算法设计。
内容:
(1)请参照顺序表的ADT模板,设计顺序栈的抽象数据类型。(由于该环境目前仅支持单文件的编译,故将所有内容都集中在一个源文件内。在实际的设计中,推荐将抽象类及对应的派生类分别放在单独的头文件中。参考教材、课件,以及网盘中的顺序表ADT原型文件,自行设计顺序栈的ADT。)
(2)ADT的简单应用:使用该ADT设计并实现若干应用顺序栈的算法设计。
应用:正读与反读都相同的字符序列称为“回文”序列。请使用顺序栈,设计并实现一个算法,判断依次读入的一个以@为结束符的字符序列是否为形如'序列1&序列2'模式的字符序列。其中序列1和序列2中都不含有字符‘&’,且序列2是序列1的逆序列。例如,‘a+b&b+a’是属于该模式的字符序列,而‘1+3&3-1’则不是。
参考函数原型:
template<class ElemType>
bool parlindrome_judge( SqStack<ElemType> &S );
输入说明 :
第一行:以@为结束符的字符序列
输出说明 :
第一行:true(回文)
false(不是回文)
输入范例 :
a+b&b+a@
输出范例 :
true
解题代码:
// 栈.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <sstream>
#include <stack>
#include <map>
#include <ctime>
#include <set>
using namespace std;
vector<string> departString_string(string data)
{
vector<int> back_part;//output type
int i, j;
vector<string> part;
string A_part;
stringstream room;
room.str(data);
while (room >> A_part)
part.push_back(A_part);
return part;
}
//————————————————
//版权声明:本文为CSDN博主「systemyff」的原创文章,遵循CC 4.0 BY - SA版权协议,转载请附上原文出处链接及本声明。
//原文链接:https ://blog.csdn.net/u014377763/article/details/113845490
int main()
{
int i, j;
stack<char> sta;
string s;
char c;
while (c = getchar())
{
if (c == '@')
break;
s.push_back(c);
sta.push(c);
}
for (auto i : s)
{
if (i != sta.top())
{
cout << "false" << endl;
return 0;
}
sta.pop();
}
cout << "true" << endl;
return 0;
}