#include<iostream>
using namespace std;
#include<string>
typedef int DATA;
#define MAXLEN 50
class Cstack {
private:
int top;
DATA data[MAXLEN];
public:
Cstack(); //构造函数
bool STIsEmpty() const;//测试栈是否空
bool STIsFull() const;//测试栈是否满
void PushST(DATA x);//进栈
int PopST(DATA &x);//出栈
int PeekST();//读取栈顶结点
void print();//打印栈
int len();//获取栈元素个数
void DecimalTor(int num, int r); //进制转换
};
/********初始化栈结构*********/
Cstack::Cstack(): top(-1) {}
/*********判断空栈**********/
bool Cstack::STIsEmpty() const
{
if (this->top == -1)
{
return 1;
}
else
{
return 0;
}
}
/***********判断满栈********/
bool Cstack::STIsFull() const
{
if (this->top == MAXLEN - 1)
{
return 1;
}
else
{
return 0;
}
}
/*********打印栈**********/
void Cstack::print()
{
cout << "此时栈中的元素为:";
for (int i = 0; i < this->len(); i++)
{
cout << this->data[i] << '\t';
}
cout << endl;
}
/*********获取栈元素个数**********/
int Cstack::len()
{
int count = 0;
while (count != this->top + 1)
{
count++;
}
return count;
}
//入栈
void Cstack::PushST(DATA x)
{
if (top == MAXLEN - 1) throw("上溢!");
else
{
this->data[++top] = x;
}
}
//出栈
int Cstack::PopST(DATA &x)
{
if (top == -1)
{
throw("下溢!");
}
else
{
x = this->data[top--];
}
return x;
}
//读取栈顶元素
int Cstack::PeekST()
{
if (top <= -1)
{
cout << "此时栈中还未有元素,请让元素入栈!" << endl;
}
else if (top > MAXLEN - 1)
{
cout <<"此时栈顶上溢!" << endl;
}
return this->data[this->top];
}
//十进制转换2~9进制
void Cstack::DecimalTor(int num,int r)
{
int k = 0; //入栈的进制数
int n = num; //保存num的数值
//将转换后的进制数入栈
while (num != 0)
{
k = num % r;
data[++this->top] = k;
num = num / r;
}
cout << "十进数" << n << "转换成" << r << "进制后的值为:";
while (this->top != -1)
{
cout << data[this->top--];
}
cout << endl;
}
int main()
{
Cstack cs;
int data[] = { 23,45,3,7,6,945 };
//显示菜单
cout << "操作菜单如下:" << endl;
cout << "======================" << endl
<< "1:入栈 " << endl
<< "2:出栈 " << endl
<< "3:打印栈 " << endl
<< "4:获取栈中元素个数 " << endl
<< "5:读取栈顶元素 " << endl
<< "6:判断栈空 " << endl
<< "7:判断栈满 " << endl
<< "8:十进制进制转换 " << endl
<< "0:退出系统 " << endl
<< "======================" << endl
int i = 1, op = 0, m = 0, q = 0, num = 0, r = 0;
DATA x;
while (i == 1)
{
cout << endl;
cout << "请选择操作代码:";
cin >> op;
switch (op)
{
case 1:
try
{
cout << "*******入栈操作*******" << endl;
cout << "请输入需要入栈的元素:";
cin >> x;
cs.PushST(x);
}
catch (char *err)
{
cerr << err << endl;
}
break;
case 2:
try
{
cout << "*******出栈操作*******" << endl;
cout << "出栈元素为:" << cs.PopST(x) << endl;
}
catch (char *err)
{
cerr << err << endl;
}
break;
case 3:
cout << "*******打印栈*******" << endl;
cs.print();
break;
case 4:
cout << "*******获取栈中元素个数*******" << endl;
cout << "栈中元素个数为:";
m = cs.len();
cout << m << endl;
break;
case 5:
cout << "*******读取栈顶元素*******" << endl;
cout << "栈顶元素为:";
q = cs.PeekST();
cout << q << endl;
break;
case 6:
cout << "*******判断栈空*******" << endl;
if (cs.STIsEmpty() == 1)
{
cout << "此时栈空!" << endl;
}
else
{
cout << "此时栈不为空~" << endl;
}
break;
case 7:
cout << "*******判断栈满*******" << endl;
if (cs.STIsFull() == 1)
{
cout << "此时栈满!" << endl;
}
else
{
cout << "此时栈还未满~" << endl;
}
break;
case 8:
cout << "请输入需要转换的十进制数:";
cin >> num;
cout << "请输入需要转换的进制数(2~9进制):";
cin >> r;
cs.DecimalTor(num, r);
break;
default:
exit(0);
break;
}
}
return 0;
}
c++实现顺序栈
最新推荐文章于 2023-07-06 03:07:47 发布