fork download
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <fstream>
  4.  
  5. inline void require(bool requirement,
  6. const char* msg = "Requirement failed") {
  7. using namespace std;
  8. if (!requirement) {
  9. fputs(msg, stderr);
  10. fputs("\n", stderr);
  11. exit(1);
  12. }
  13. }
  14.  
  15. inline void requireArgs(int argc, int args,
  16. const char* msg = "Must use %d arguments") {
  17. using namespace std;
  18. if (argc != args + 1) {
  19. fprintf(stderr, msg, args);
  20. fputs("\n", stderr);
  21. exit(1);
  22. }
  23. }
  24.  
  25. inline void requireMinArgs(int argc, int minArgs,
  26. const char* msg =
  27. "Must use at least %d arguments") {
  28. using namespace std;
  29. if(argc < minArgs + 1) {
  30. fprintf(stderr, msg, minArgs);
  31. fputs("\n", stderr);
  32. exit(1);
  33. }
  34. }
  35.  
  36. inline void assure(std::ifstream& in,
  37. const char* filename = "") {
  38. using namespace std;
  39. if(!in) {
  40. fprintf(stderr,
  41. "Could not open file %s\n", filename);
  42. exit(1);
  43. }
  44. }
  45.  
  46. inline void assure(std::ofstream& in,
  47. const char* filename = "") {
  48. using namespace std;
  49. if(!in) {
  50. fprintf(stderr,
  51. "Could not open file %s\n", filename);
  52. exit(1);
  53. }
  54. }
  55.  
  56. class Stack{
  57. struct Link{
  58. void* data;
  59. Link* next;
  60. Link(void* dat, Link*nxt);
  61. ~Link();
  62. }*head;
  63. public:
  64. Stack();
  65. Stack(void** mass,int size);
  66. ~Stack();
  67. void push(void *dat);
  68. void * peek();
  69. void * pop();
  70. };
  71.  
  72. using namespace std;
  73.  
  74.  
  75. Stack::Link::Link(void* dat, Link*nxt){
  76. data=dat;
  77. next=nxt;
  78. }
  79. Stack::Link::~Link(){}
  80.  
  81.  
  82. Stack::Stack(){head=0;}
  83.  
  84. Stack::Stack(void** mass,int size){
  85. head = 0;
  86. for(int i=0;i<size;++i)
  87. push(mass[i]);
  88. }
  89.  
  90. Stack::~Stack(){require(head==0,"Stack not empty");}
  91.  
  92. void Stack::push(void *dat){
  93. head=new Link(dat,head);
  94. }
  95.  
  96. void* Stack::peek(){
  97. require(head!=0,"Stack empty");
  98. return head->data;
  99. }
  100. void* Stack::pop(){
  101. if(head==0) return 0;
  102. void*result=head->data;
  103. Link*OL=head;
  104. head=head->next;
  105. delete OL;
  106. return result;
  107. }
  108.  
  109. #include <iostream>
  110. #include <string>
  111.  
  112. int main(){
  113.  
  114. const int size = 25;
  115. std::string* Film[size];
  116. //std::string** Film = new std::string* [size];
  117. //void** Film = reinterpret_cast<void**>(new std::string* [size]);
  118. for(int i=0;i<size;i++)
  119. {
  120. Film[i] = new std::string( i+1, '*');
  121. }
  122.  
  123. Stack FilmsStack( reinterpret_cast<void**>(Film),size);
  124.  
  125. std::string* s;
  126. while (s = static_cast<std::string*>(FilmsStack.pop()) )
  127. {
  128. std::cout << *s << std::endl;
  129. delete s;
  130. }
  131.  
  132.  
  133. return 0;
  134.  
  135. }
  136.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
*************************
************************
***********************
**********************
*********************
********************
*******************
******************
*****************
****************
***************
**************
*************
************
***********
**********
*********
********
*******
******
*****
****
***
**
*