⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pex5_3.cpp

📁 数据结构C++代码,经典代码,受益多多,希望大家多多支持
💻 CPP
字号:
#include <iostream.h>
#pragma hdrstop

typedef char DataType;
#include "astack.h"

void main(void)
{
	Stack S;
	char  str[50];
	int   j;
	
	for (int i = 0; i < 5; i++)
	{
		cin >> str;
		
		// push characters onto the stack until encounter
		// '#' or end of string
		j = 0;
		while(str[j] != '#' && str[j] != NULL)
		{
			S.Push(str[j]);
			j++;
		}
		
		// we must have encountered '#'
		if (str[j] != NULL)
		{
			// advance past the '#'
			j++;

			// cycle through the remainder of the string, popping
			// the stack and comparing the two characters. terminate
			// if the stack becomes empty, we find the end of the
			// string or the current character and the one popped
			// from the stack are not equal
			while (!S.StackEmpty() && str[j] != NULL)
				if (str[j++] != S.Pop())
					break;

			// for the pattern to match, the stack must be empty
			// and we must have reached the end of the string
			if (S.StackEmpty() && str[j] == NULL)
				cout << "Pattern matches";
			else
				cout << "Pattern does not match";
			cout << endl << endl;
		}
		else
			// the was no '#', so the pattern does not match
			cout << "Pattern does not match" << endl << endl;
		S.ClearStack();
	}
}

/*
<Run>

walking#gniklaw
Pattern matches

level
Pattern does not match

level#leve
Pattern does not match

abcd#dcbae
Pattern does not match

amanaplanacanalpanama#amanaplanacanalpanama
Pattern matches
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -