#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
//定义栈
void mume();
int confirm_int();
int confirm_float();
int confirm_double();
int confirm_char();
int confirm_string();
template<typename T>
class Node
{
T *ptr;
int top = -1;
int size ;
public:
//
Node()
{
}
//size 是栈的大小
Node(int a):size(a)
{
ptr = new T[size];
memset(ptr,0,sizeof(T)*size);
}
~Node()
{
delete []ptr;
ptr = nullptr;
}
//入栈
void stacked(T d)
{
if(top == size-1)
{
throw -1;
}
++top;
*(ptr + top) = d;
}
//出栈
void outstack()
{
if(-1 == top)
{
throw -2;
}
top--;
}
//查看栈顶和栈大小
void show()
{
cout<<"size = "<<size<<endl;
cout<<"top = "<<top<<endl;
for(int i = 0; i <= top ;i++)
{
cout<<*(ptr+i)<<endl;
}
}
void change(T d)
{
*(ptr + top) = d;
}
};
int main()
{
mume();
return 0;
}
void mume()
{
int a = 0;
while(1)
{
system("cls");
cout<<"01:int"<<endl;
cout<<"02:float"<<endl;
cout<<"03:char"<<endl;
cout<<"04:double"<<endl;
cout<<"05:string"<<endl;
cout<<"请输入栈的类型"<<endl;
cin>>a;
switch(a)
{
case 1:
{
//在int内使用栈
confirm_int();
break;
}
case 2:
{
confirm_float();
break;
}
case 3:
{
confirm_char();
break;
}
case 4:
{
confirm_double();
break;
}
case 5:
{
confirm_string();
break;
}
default:
{
cout<<"输入错误,请重新输入"<<endl;
continue;
}
}
getchar();
cout<<"请输入enter键";
getchar();
}
}
int confirm_int()
{
int s = 0,a = 0;
cout<<"请输入栈的大小"<<endl;
cin>>s;
int num = 0;
//申请栈空间
Node<int>str(s);
while(1)
{
system("cls");
cout<<"int类型栈"<<endl;
cout<<"01:入栈"<<endl;
cout<<"02:出栈"<<endl;
cout<<"03:遍历栈"<<endl;
cout<<"04:修改栈顶元素"<<endl;
cout<<"05:销毁栈"<<endl;
cout<<"请输入进行的操作"<<endl;
cin>>a;
switch(a)
{
case 1:
{
cout<<"请输入入栈的值"<<endl;
cin>>num;
try
{
str.stacked(num);
}
catch(int b)
{
cout<<"栈满"<<endl;
}
break;
}
case 2:
{
try
{
str.outstack();
}
catch(int b)
{
cout<<"栈已空"<<endl;
}
break;
}
case 3:
{
str.show();
break;
}
case 4:
{
cout<<"请输入修改后栈顶的值"<<endl;
cin>>num;
try
{
str.stacked(num);
}
catch(int b)
{
cout<<"栈满"<<endl;
}
break;
}
case 5:
{
return -1;
}
default:
{
continue;
}
}
getchar();
cout<<"请输入enter键";
getchar();
}
return 0;
}
int confirm_float()
{
int s = 0,a = 0;
cout<<"请输入栈的大小"<<endl;
cin>>s;
float num = 0;
//申请栈空间
Node<float>str(s);
while(1)
{
system("cls");
cout<<"float类型栈"<<endl;
cout<<"01:入栈"<<endl;
cout<<"02:出栈"<<endl;
cout<<"03:遍历栈"<<endl;
cout<<"04:修改栈顶元素"<<endl;
cout<<"05:销毁栈"<<endl;
cout<<"请输入进行的操作"<<endl;
cin>>a;
switch(a)
{
case 1:
{
cout<<"请输入入栈的值"<<endl;
cin>>num;
try
{
str.stacked(num);
}
catch(int b)
{
cout<<"栈满"<<endl;
}
break;
}
case 2:
{
try
{
str.outstack();
}
catch(int b)
{
cout<<"栈已空"<<endl;
}
break;
}
case 3:
{
str.show();
break;
}
case 4:
{
cout<<"请输入修改后栈顶的值"<<endl;
cin>>num;
str.change(num);
break;
}
case 5:
{
return -1;
}
default:
{
continue;
}
}
getchar();
cout<<"请输入enter键";
getchar();
}
return 0;
}
int confirm_char()
{
int s = 0,a = 0;
cout<<"请输入栈的大小"<<endl;
cin>>s;
char num = 0;
//申请栈空间
Node<char>str(s);
while(1)
{
system("cls");
cout<<"char类型栈"<<endl;
cout<<"01:入栈"<<endl;
cout<<"02:出栈"<<endl;
cout<<"03:遍历栈"<<endl;
cout<<"04:修改栈顶元素"<<endl;
cout<<"05:销毁栈"<<endl;
cout<<"请输入进行的操作"<<endl;
cin>>a;
switch(a)
{
case 1:
{
cout<<"请输入入栈的值"<<endl;
cin>>num;
try
{
str.stacked(num);
}
catch(int b)
{
cout<<"栈满"<<endl;
}
break;
}
case 2:
{
try
{
str.outstack();
}
catch(int b)
{
cout<<"栈已空"<<endl;
}
break;
}
case 3:
{
str.show();
break;
}
case 4:
{
cout<<"请输入修改后栈顶的值"<<endl;
cin>>num;
str.change(num);
break;
}
case 5:
{
return -1;
}
default:
{
continue;
}
}
getchar();
cout<<"请输入enter键";
getchar();
}
return 0;
}
int confirm_double()
{
int s = 0;
int a = 0;
cout<<"请输入栈的大小"<<endl;
cin>>s;
double num = 0;
//申请栈空间
Node<double>str(s);
while(1)
{
system("cls");
cout<<"double类型栈"<<endl;
cout<<"01:入栈"<<endl;
cout<<"02:出栈"<<endl;
cout<<"03:遍历栈"<<endl;
cout<<"04:修改栈顶元素"<<endl;
cout<<"05:销毁栈"<<endl;
cout<<"请输入进行的操作"<<endl;
cin>>a;
switch(a)
{
case 1:
{
cout<<"请输入入栈的值"<<endl;
cin>>num;
try
{
str.stacked(num);
}
catch(int b)
{
cout<<"栈满"<<endl;
}
break;
}
case 2:
{
try
{
str.outstack();
}
catch(int b)
{
cout<<"栈已空"<<endl;
}
break;
}
case 3:
{
str.show();
break;
}
case 4:
{
cout<<"请输入修改后栈顶的值"<<endl;
cin>>num;
str.change(num);
break;
}
case 5:
{
return -1;
}
default:
{
continue;
}
}
getchar();
cout<<"请输入enter键";
getchar();
}
return 0;
}
int confirm_string()
{
int s = 0,a = 0;
cout<<"请输入栈的大小"<<endl;
cin>>s;
string num = "";
//申请栈空间
Node<string>str(s);
while(1)
{
system("cls");
cout<<"string类型栈"<<endl;
cout<<"01:入栈"<<endl;
cout<<"02:出栈"<<endl;
cout<<"03:遍历栈"<<endl;
cout<<"04:修改栈顶元素"<<endl;
cout<<"05:销毁栈"<<endl;
cout<<"请输入进行的操作"<<endl;
cin>>a;
switch(a)
{
case 1:
{
num = "";
cout<<"请输入入栈的值"<<endl;
cin>>num;
cout<<num<<endl;
try
{
str.stacked(num);
}
catch(int b)
{
cout<<"栈满"<<endl;
}
break;
}
case 2:
{
try
{
str.outstack();
}
catch(int b)
{
cout<<"栈已空"<<endl;
}
break;
}
case 3:
{
str.show();
break;
}
case 4:
{
cout<<"请输入修改后栈顶的值"<<endl;
cin>>num;
try
{
str.stacked(num);
}
catch(int b)
{
cout<<"栈满"<<endl;
}
break;
}
case 5:
{
return -1;
}
default:
{
continue;
}
}
getchar();
cout<<"请输入enter键";
getchar();
}
return 0;
}
c++ day6
于 2023-04-24 20:53:53 首次发布