Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vcl.h>
- #pragma hdrstop
- #include"stdlib.h"
- #include "Unit1.h"
- //--------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TForm1 *Form1;
- //--------------------------------------------------------------------------
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- }
- //-------------------------------------------------------------------------
- struct Stack {
- int info;
- Stack * next;
- } *begin, *t;
- //--------------------------------------
- Stack* InStack(Stack*, int);
- void View(Stack*);
- void Del_All(Stack **);
- int findAverage(Stack *p);
- void __fastcall TForm1::Button1Click(TObject *Sender)
- {
- int i, in, n = StrToInt(Edit1 ->Text);
- if(begin != NULL){
- ShowMessage("Освободите память!");
- return;
- }
- for(i = 1; i <= n; i++){
- in = random(20)-10;
- begin = InStack(begin, in);
- }
- Memo1 ->Lines ->Add("создали " + IntToStr(n) );
- }
- //--------------------------------------------------------------------------
- void __fastcall TForm1::Button2Click(TObject *Sender)
- {
- int i, in, n = StrToInt(Edit1 ->Text);
- for(i = 1; i <= n; i++){
- in = random(20)-10;
- begin = InStack(begin, in);
- }
- Memo1 ->Lines ->Add("Добавили " + IntToStr(n) + " - òü.");
- }
- //--------------------------------------------------------------------------
- void __fastcall TForm1::Button3Click(TObject *Sender)
- {
- if(!begin){
- ShowMessage("Стек пуст");
- return;
- }
- Memo1 ->Lines ->Add("элементы");
- View(begin);
- }
- //--------------------------------------------------------------------------
- void __fastcall TForm1::Button4Click(TObject *Sender)
- {
- if (begin != NULL) Del_All(&begin);
- ShowMessage("память очищена");
- }
- //--------------------------------------------------------------------------
- void __fastcall TForm1::Button5Click(TObject *Sender)
- {
- if(begin != NULL) Del_All(&begin);
- Close();
- }
- //--------------------------------------------------------------------------
- Stack* InStack(Stack *p, int in){
- Stack *t = new Stack;
- t -> info = in;
- t -> next = p;
- return t;
- }
- void View(Stack *p){
- Stack *t = p;
- while( t != NULL){
- Form1 ->Memo1 ->Lines ->Add(" " + IntToStr( t ->info));
- t = t -> next;
- }
- }
- //----------------------------------------------
- void Del_All(Stack **p){
- while(*p != NULL){
- t = *p;
- *p = (*p) -> next;
- delete t;
- }
- }
- //----------------------------------------------
- int findAverage(Stack *p){
- int amount = 0, i = 0;
- for(Stack *temp = p; temp ; temp = temp->next, i++)
- amount += temp->info;
- return amount/i;
- }
- //--------------------------------------------------
- Stack* del(Stack *p, int sum) {
- Stack *head = p;
- if (p == NULL)
- return NULL;
- Stack *t = new Stack;
- t->next = head;
- p = t;
- while (p->next != NULL) {
- if (p->next->info < sum) {
- Stack *temp = p->next;
- p->next = p->next->next;
- delete temp; }
- else { p = p->next; } }
- return t->next;
- }
- //-------------------------------------------------------------
- void __fastcall TForm1::Button6Click(TObject *Sender)
- {
- int a,b,n=StrToInt(Edit1->Text),i;
- int avg;
- avg=findAverage(begin);
- del(begin,avg);
- Memo1->Lines->Add("Среднее значение "+IntToStr(avg));
Add Comment
Please, Sign In to add comment