一,具体代码
#include<iostream>
#include<stdlib.h>
using namespace std;
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef int ElemType;
typedef int Status;
typedef struct
{
ElemType *elem;
int top;
}Sqstack;
Status InitStack(Sqstack &S);
Status Push(Sqstack &S,ElemType e);
Status Pop(Sqstack &S,ElemType &e);
Status GetPop(Sqstack S,ElemType &e);
Status StackEmpty(Sqstack S);
void Show(Sqstack S);
//初始化
Status InitStack(Sqstack &S)
{
S.elem=new ElemType[MAXSIZE];
if(!S.elem) return ERROR;
S.top =-1;
return OK;
}
//入栈
Status Push(Sqstack &S,ElemType e)
{
if(S.top ==MAXSIZE-1) return ERROR;
S.top++;
S.elem[S.top]=e;
return OK;
}
//出栈
Status Pop(Sqstack &S,ElemType &e)
{
if(S.top==-1) return ERROR;
e=S.elem[S.top];
S.top--;
return OK;
}
//取栈顶元素
Status GetPop(Sqstack S,ElemType &e)
{
if(S.top==-1) return ERROR;
e=S.elem [S.top];
return OK;
}
//判断栈是否为空
Status StackEmpty(Sqstack S)
{
if(S.top==-1)
return OK;
else
return ERROR;
}
//输出元素
void Show(Sqstack S)
{
if(S.top==-1)
{
cout<<"没有数据"<<endl;
return ;
}
while(S.top!=-1)
{
cout<<S.elem[S.top]<<" ";
S.top--;
}
cout<<endl;
}
//数值转换
void Conversion(int N,int r)
{
Sqstack S;
ElemType e;
InitStack(S);
while(N)
{
Push(S,N%r);
N=N/r;
}
while(StackEmpty(S)==ERROR)
{
Pop(S,e);
if(e>9){
switch(e)
{
case 10:printf("A");break;
case 11:printf("B");break;
case 12:printf("C");break;
case 13:printf("D");break;
case 14:printf("E");break;
case 15:printf("F");break;
}
}
else
cout<<e;
}
cout<<endl;
}
//回文判断
Status Huiwen(char str[ ])
{
Sqstack S;
int i;
ElemType e;
InitStack(S);
for(i=0;str[i]!='\0';i++)
{
Push(S,str[i]);
}
for(i=0;str[i]!='\0';i++)
{
Pop(S,e);
if(e!=str[i])
return ERROR;
}
return OK;
}
//括号匹配
Status March(char ch[])
{
Sqstack S;
ElemType e;
int i=0;
int flag=1;
InitStack(S);
while(ch[i]!='\0'&&flag==1)
{
if(ch[i]=='(') Push(S,ch[i]);
if(ch[i]==')')
{
if(GetPop(S,e)==OK&&e=='(')
Pop(S,e);
else flag=0;
}
i++;
}
if(StackEmpty(S)==OK&&flag==1)
return OK;
else
return ERROR;
}
int main()
{
Sqstack S;
ElemType e;
int choice;
char ch[MAXSIZE];
char str[MAXSIZE];
int n,N,r,i;
while(1)
{
cout<<"1、初始化 2、入栈 3、出栈 4、显示"<<endl;
cout<<"5、判断栈空 6、取栈顶元素 0、退出"<<endl;
cout<<"7,、数值转换 8、判断回文 9、括号匹配"<<endl;
cout<<"输入选项=";
cin>>choice;
switch(choice)
{
case 1:
InitStack(S);
cout<<"初始化成功!"<<endl;
break;
case 2:
cout<<"入栈数据=";
cin>>e;
if(Push(S,e)==OK)
cout<<"输入成功!"<<endl;
else
cout<<"输入失败!"<<endl;
break;
case 3:
if(Pop(S,e)==OK)
{
cout<<"出栈成功!"<<endl;
cout<<"出栈数据="<<e<<endl;
}
else
cout<<"出栈失败!"<<endl;
break;
case 4:
Show(S);
break;
case 5:
if(StackEmpty(S)==OK)
cout<<"栈空!"<<endl;
else
cout<<"栈非空!"<<endl;
Show(S);
break;
case 6:
if(GetPop(S,e)==OK)
{
cout<<"栈顶元素="<<e<<endl;
}
else
cout<<"栈空!"<<endl;
break;
case 7:
cout<<"转换的数=";
cin>>N;
cout<<"转换进制=";
cin>>r;
Conversion(N,r);
break;
case 8:
cout<<"字符长度=";
cin>>n;
cout<<"输入字符:";
cin>>str;
if(Huiwen(str)==OK)
cout<<"是回文"<<endl;
else
cout<<"不是回文"<<endl;
break;
case 9:
cout<<"字符长度=";
cin>>n;
cout<<"输入括号:";
cin>>ch;
if(March(ch)==OK)
cout<<"括号匹配"<<endl;
else
cout<<"括号不匹配"<<endl;
break;
case 0:
return 0;
default:
cout<<"选项输入错误!"<<endl;
}
system("pause");
system("cls");
}
return 0;
}