嚴蔚敏3.17

//判斷一個以@爲結束符,形如‘序列1&序列2’的字符串
//是否爲轉置字符串
#include <iostream>
using namespace std;
const int Max=100;

class MyStack
{
private:
	int top;
	char Value[Max];
public:
	void InitStack();
	int IsEmpty();
	void Push(char c);
	void Pop(char &c);
};

void MyStack::InitStack()
{
	top=0;
}
int MyStack::IsEmpty()
{
	return(top==0);
}
void MyStack::Push(char c)
{
	top+=1;
	Value[top]=c;
}
void MyStack::Pop(char &c)
{
	if (top<1)
	{
		cout<<"棧爲空,不可出棧!"<<endl;
		top=0;
	}
	else
	{
		c=Value[top];
		top-=1;
	}
}

int main()
{
	MyStack by;
	char c,e;
	by.InitStack();
	while(1)
	{
		cin>>e;
		if (e!='&')
		{
			by.Push(e);
		}
		else
			break;
	}
	while(1)
	{
		cin>>e;
		if (e=='@')
		{
			break;
		}
		else
		{
			if (by.IsEmpty())
			{
				cout<<"棧爲空!不可操作!"<<endl;
				return 0;
			}
			by.Pop(c);
			if (c!=e)
			{
				cout<<"不是轉置字符串!"<<endl;
				return 0;
			}
		}
	}
	if (by.IsEmpty())
	{
		cout<<"是轉置字符串!"<<endl;
		return 0;
	}
	cout<<"不是轉置字符串!"<<endl;
	return 1;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章