#include
#include<stdio.h>
#include<stdlib.h >
#include<math.h>
#include
using namespace std;
#define DATA_Input 10
#define New_Data 4
struct Stack {
char *base;
char *top;
int stacksize;
};
//构建一个空栈
void InitStack(Stack *D) {
D->base = (char *) malloc(DATA_Input * sizeof(char));
if(!D->base)
cout << “栈空间开辟错误!”<< endl;
D->top = D->base;
D->stacksize = DATA_Input;
}
//入栈
void Push(Stack *D) {
int n;
cout << "输入数据的个数:" ;
cin >> n;
if(D->top - D->base >= D->stacksize) {
cout << "栈已满!" << endl;
}
while(n >= D->stacksize) {
D->base = (int *)realloc (D->base,(D->stacksize + New_Data)*sizeof(int));
if (!D->base)
cout << "空间开辟失败!" << endl;
D->top = D->base;
D->stacksize = D->stacksize + New_Data;
}
cout << "请输入数据:" << endl;
for(int i=n; i <= D->stacksize&&D->top - D->base < D->stacksize&&i>0; i--) {
cin >> *D->top;
D->top++;
}
}
//插入函数
void InsertData(Stack *D) {
int e;
cout << endl << “请输入需要插入的元素:”;
cin >> e;
cout << endl ;
if(D->top - D->base >= D->stacksize) {
D->base = (int *)realloc (D->base,(D->stacksize + New_Data)*sizeof(int));
if (!D->base)
cout << “空间开辟失败!” << endl;
D->top = D->base + D->stacksize;
D->stacksize = D->stacksize + New_Data;
}
*D->top++ = e;
}
//取得栈顶元素,用e返回
int GotTop(Stack D) {
int e ;
if(D.top - D.base >= D.stacksize) {
cout << “此栈为空栈!” << endl;
} else {
D.top–;
e = *(D.top);
}
D.top++;
return (e);
}
//出栈函数
int Pop(Stack *D) {
D->top–;
return *(D->top);
}
//输出栈元素
void OutputStack(Stack D) {
cout << endl << “输出栈元素:” << endl;
int *p;
p = D.top;
if(p != D.base) {
while(p != D.base) {
p–;
cout << *§ << “\t”;
}
} else cout << “此栈为空!” << endl;
}
//删除元素
void DeleteStack(Stack *D) {
if(D->top != D->base) {
D->top–;
cout << endl <<“删除后:”;
OutputStack(*D);
} else cout << “此栈为空!” << endl;
}
// 实现十进制数向其他进制转换的程序
void transitionStack(int Num,int Tnum,Stack *DD) {
int e;
while(Num!=0) {
e = Num%Tnum;
Num/=Tnum;
if(DD->top - DD->base >= DD->stacksize) {
cout << “栈已满!” << endl;
}
*(DD->top) = e;
DD->top++;
}
cout << endl << Tnum <<“进制数为:”;
if(DD->top!=DD->base) {
while(DD->top!=DD->base) {
DD->top–;
cout << *(DD->top);
}
} else
cout << "此栈为空!" << endl;
}
//判断是否为回文
void AnagramStack(Stack *D) {
D->top=D->base;
int i=0;
string str;
cin >> str[i];
while(str[i]!=’\0’) {
cin >> str[i];
i++;
}
for(int n=0; n<i; n++) {
(D->top) = str[n];
D->top++;
}
while(D->top!=D->base) {
D->top–;
int n=0;
while(n<i) {
if((D->top)==str[n]) {
n++;
D->top–;
} else {
cout << “不是回文!” << endl;
}
}
}
cout << "是回文!" << endl;
int Num,e;
cout << "输入需要判断的数:" << endl;
cin >> Num;
int number=Num;
if(D->top - D->base >= D->stacksize) {
cout << "栈已满!" << endl;
}
D->top=D->base;
while(Num!=0) {
e = Num%10;
Num/=10;
*(D->top) = e;
D->top++;
}
int i=0,sum=0;
while(D->top!=D->base) {
e = Pop(D);
sum += e*pow(10,i);
i++;
}
if(sum == number) {
cout << sum ;
}
}
void Menu() {
system("pause");
system("cls");
cout << "1.初始化栈" << endl << "2.进栈"<< endl <<"3.输出栈元素" << endl << "4.删除元素" << endl << "5.获得栈顶元素" << endl << "6.实现十进制数向其他进制转换" << endl << "7.判断是否为回文" << endl << "8 .出栈" << endl;
cout << "请选择你要执行的功能:";
}
int main () {
Stack D,DD;
InitStack(&D);
int e,Num,Tnum;
AnagramStack(&D);
while(true) {
Menu();
int choice;
cin >> choice;
cout << endl;
switch(choice) {
case 1 :
InitStack(&D);
cout << "栈的初始化完成!" << endl;
break;
case 2 :
Push(&D);
break;
case 3:
OutputStack(D);
break;
case 4:
DeleteStack(&D);
break;
case 5:
e = GotTop(D);
cout << endl << "栈顶元素为:" << e << endl;
OutputStack(D);
break;
case 6:
InitStack(&DD);
cout << "十进制数:";
cin >> Num;
cout << endl << "转换为:" ;
cin >> Tnum ;
cout << "进制数!";
transitionStack(Num,Tnum,&DD);
break;
case 7:
AnagramStack(&D);
break;
case 8:
int e;
while(D.top!=D.base) {
e = Pop(&D);
}
cout << "元素已全部出栈!" << endl;
break;
}
}
return 0;
}