#include <iostream>using namespace std;char arr[10]="12345678";
void show(int i)
{if(i>=10){throw int(2);}cout << arr[i] <<endl;
}
int main()
{int i;cin >> i;try {show(i);} catch (int) {cout << "越界" <<endl;}return 0;
}
使用模板类,实现顺序栈:
#include <iostream>using namespace std;template <typename T>
class stack{T top;T data[5]={};
public:stack():top(4){}void ru(T a);void chu();void show(){for(int i=0;i<5;i++){cout << data[i] <<endl;}}
};template <typename T>
void stack<T>::ru(T a)
{data[top]=a;top--;
}template <typename T>
void stack<T>::chu()
{top++;data[top]=0;
}int main()
{stack <int>s1;s1.show();cout << "---------"<<endl;s1.ru(99);s1.ru(88);s1.show();cout << "---------"<<endl;s1.chu();s1.show();cout << "---------"<<endl;s1.ru(66);s1.show();return 0;
}