#include <iostream>
using namespace std;
int Max=10;
class Stack
{
public:
int* base;//栈底;
int* top;//栈顶;
void creat(Stack& s);//初始化顺序栈
void in(Stack& s);//入栈
void out(Stack& s);//出栈
void empty(Stack& s);//判断栈是否为空
void full( Stack& s);//判断栈是否为满
~Stack()
{
delete base;
delete top;
}
};
void Stack::creat(Stack& s)
{
s.base=new int[Max];
s.top=s.base;
}
void Stack::empty(Stack& s)
{
if(s.base-s.top==Max)
{
cout<<"栈已满"<<endl;
}
}
void Stack::full(Stack& s)
{
if(s.base==s.top)
{
cout<<"栈空"<<endl;
}
}
void Stack::in(Stack& s)
{
if(s.base-s.top==Max)
{
cout<<"栈已满"<<endl;
}
int a;
cout<<"请输入入栈元素"<<endl;
cin>>a;
*s.top=a;
s.top++;
cout<<"入栈成功"<<endl;
}
void Stack::out(Stack& s)
{
if(s.base==s.top)
{
cout<<"栈空"<<endl;
}
int a;
s.top--;
cout<<"出栈元素为:"<<*s.top<<endl;
}
int main()
{
Stack s;
s.creat(s);
s.in(s);
s.out(s);
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
int test(char *arr,unsigned int n)
{
if(n > (strlen(arr)-1))
{
throw int(0);
}
cout << arr[n] << endl;
return arr[n];
}
int main()
{
char arr[6] = "hello";
try
{
test(arr,1);
test(arr,6);
}
catch (int)
{
cout << "数组越界" << endl;
}
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/b20f38522174cd6d51b83683f5a79d8e.png)