fork download
  1. #include <iostream>
  2. using namespace std;
  3. struct stack {
  4. long long s[100];
  5. int cursor=0;
  6. void push(long long n){
  7. s[cursor++]=n;
  8. }
  9. long long pop(){
  10. return s[--cursor];
  11. }
  12. long long back(){
  13. return s[cursor-1];
  14. }
  15. unsigned size(){
  16. return cursor;
  17. }
  18. void clear(){
  19. cursor=0;
  20. s[cursor]=0;
  21. }
  22. };
  23. int main() {
  24. stack x;
  25. string b;
  26. long long n;
  27. while(cin>>b){
  28. if(b=="push"){
  29. cin>>n;
  30. x.push(n);
  31. cout<<"ok"<<endl;
  32. }
  33. else if(b=="pop"){
  34. if(x.size()==0){
  35. cout<<"error"<<endl;
  36. }
  37. else
  38. cout<<x.pop()<<endl;
  39. }
  40. else if(b=="back"){
  41. if(x.size()==0){
  42. cout<<"error"<<endl;
  43. }
  44. else cout<<x.back()<<endl;
  45. }
  46. else if(b=="size"){
  47. cout<<x.size()<<endl;
  48. }
  49. else if(b=="clear"){
  50. x.clear();
  51. cout<<"ok"<<endl;
  52. }
  53. else if(b=="exit"){
  54. cout<<"bye"<<endl;
  55. break;
  56. }
  57. }
  58. return 0;
  59. }
Success #stdin #stdout 0s 3468KB
stdin
size
push 1
size
back
pop
exit
stdout
0
ok
1
1
1
bye