Guest User

Untitled

a guest
Jan 23rd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. #include <vcl.h>
  2. #pragma hdrstop
  3. #include"stdlib.h"
  4. #include "Unit1.h"
  5. //--------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma resource "*.dfm"
  8. TForm1 *Form1;
  9. //--------------------------------------------------------------------------
  10. __fastcall TForm1::TForm1(TComponent* Owner)
  11. : TForm(Owner)
  12. {
  13. }
  14. //-------------------------------------------------------------------------
  15. struct Stack {
  16. int info;
  17. Stack * next;
  18. } *begin, *t;
  19. //--------------------------------------
  20. Stack* InStack(Stack*, int);
  21. void View(Stack*);
  22. void Del_All(Stack **);
  23. int findAverage(Stack *p);
  24. void __fastcall TForm1::Button1Click(TObject *Sender)
  25. {
  26. int i, in, n = StrToInt(Edit1 ->Text);
  27. if(begin != NULL){
  28. ShowMessage("Освободите память!");
  29. return;
  30. }
  31. for(i = 1; i <= n; i++){
  32. in = random(20)-10;
  33. begin = InStack(begin, in);
  34. }
  35. Memo1 ->Lines ->Add("создали " + IntToStr(n) );
  36. }
  37. //--------------------------------------------------------------------------
  38. void __fastcall TForm1::Button2Click(TObject *Sender)
  39. {
  40. int i, in, n = StrToInt(Edit1 ->Text);
  41. for(i = 1; i <= n; i++){
  42. in = random(20)-10;
  43. begin = InStack(begin, in);
  44. }
  45. Memo1 ->Lines ->Add("Добавили " + IntToStr(n) + " - òü.");
  46. }
  47. //--------------------------------------------------------------------------
  48. void __fastcall TForm1::Button3Click(TObject *Sender)
  49. {
  50. if(!begin){
  51. ShowMessage("Стек пуст");
  52. return;
  53. }
  54. Memo1 ->Lines ->Add("элементы");
  55. View(begin);
  56. }
  57. //--------------------------------------------------------------------------
  58. void __fastcall TForm1::Button4Click(TObject *Sender)
  59. {
  60. if (begin != NULL) Del_All(&begin);
  61. ShowMessage("память очищена");
  62. }
  63. //--------------------------------------------------------------------------
  64. void __fastcall TForm1::Button5Click(TObject *Sender)
  65. {
  66. if(begin != NULL) Del_All(&begin);
  67. Close();
  68. }
  69. //--------------------------------------------------------------------------
  70. Stack* InStack(Stack *p, int in){
  71. Stack *t = new Stack;
  72. t -> info = in;
  73. t -> next = p;
  74. return t;
  75. }
  76.  
  77. void View(Stack *p){
  78. Stack *t = p;
  79. while( t != NULL){
  80. Form1 ->Memo1 ->Lines ->Add(" " + IntToStr( t ->info));
  81. t = t -> next;
  82. }
  83. }
  84. //----------------------------------------------
  85. void Del_All(Stack **p){
  86. while(*p != NULL){
  87. t = *p;
  88. *p = (*p) -> next;
  89. delete t;
  90. }
  91. }
  92. //----------------------------------------------
  93. int findAverage(Stack *p){
  94. int amount = 0, i = 0;
  95. for(Stack *temp = p; temp ; temp = temp->next, i++)
  96. amount += temp->info;
  97. return amount/i;
  98. }
  99. //--------------------------------------------------
  100. Stack* del(Stack *p, int sum) {
  101. Stack *head = p;
  102. if (p == NULL)
  103. return NULL;
  104. Stack *t = new Stack;
  105. t->next = head;
  106. p = t;
  107. while (p->next != NULL) {
  108. if (p->next->info < sum) {
  109. Stack *temp = p->next;
  110. p->next = p->next->next;
  111. delete temp; }
  112. else { p = p->next; } }
  113. return t->next;
  114. }
  115. //-------------------------------------------------------------
  116. void __fastcall TForm1::Button6Click(TObject *Sender)
  117. {
  118. int a,b,n=StrToInt(Edit1->Text),i;
  119. int avg;
  120. avg=findAverage(begin);
  121. del(begin,avg);
  122. Memo1->Lines->Add("Среднее значение "+IntToStr(avg));
Add Comment
Please, Sign In to add comment