Разработка простейших классов на примере разработки моделей элементарных объектов и динамических информационных структур (одно и двунаправленных списков). Разработка простых классов. Вызывающая программа (main). Информационные динамические структуры.
Аннотация к работе
Запоріжжя 2007 Цель: Разработка простейших классов на примере разработки моделей элементарных объектов и динамических информационных структур (одно и двунаправленных списков). Програма: #ifndef rectangle__h #define rectangle__h #include class rectangle { private: double a, b; public: rectangle(); rectangle(double, double); ~rectangle(); rectangle & operator=(const rectangle & x); double square(void); double perimeter(void); double diagonal(void); int is_square(void); void print_rectangle(void); }; #endif #ifndef conrectangle__h #define conrectangle__h #include #include rectangle.h rectangle::rectangle() { couta=x.a; this->b=x.b; return *this; }; double rectangle::square(void) { return a*b; }; double rectangle::perimeter() { return 2*a 2*b; }; double rectangle::diagonal() { return sqrt(a*a b*b); }; int rectangle::is_square() { if(a==b) return 1; return 0; }; void rectangle::print_rectangle() { char msg1[]= , msg2[]= not ; cout #include conrectangle.h void main() { rectangle r1; double a,b; char s[1]; cout>a>>b; rectangle r2(a, b); cout>s; } while (s[0]!=q); return; }; Результати: D:\Studies\Labs\Cpp\Individ_6>ind1 The constructor rectangle() worked. Your rectangle: a=456.125 b=789.545 S= 360131 m^2 P=2491.34 m d=911.829 m Rectangle is not square. Your rectangle is our now: a=456.125 b=789.545 S= 360131 m^2 P=2491.34 m d=911.829 m Rectangle is not square. Repeat or quit?(r/q)...r The destructor ~rectangle() worked. Програма: #ifndef list_h #define list_h class list { private: struct element { char* info; element* up; element* down; }; element *first, *last, *current; public: list(); //constructor ~list(); //destructor void del_list(); //ochishchajet spisok void init_list(int,char*); //initsializatsia spiska fonarhym metodom void print_list(); //vyvod na ekran spiska int current_element(int); //zdelat element s nomerom int tekushchim, vernetsa 0; //Esli doidem do kontsa spiska,to vernetsa nomer poslednego elementa,kotoryi i budet tekushchim int next_element(); //sdelat tekushchim sledujushchiy element tekuschuego int previous_element(); //sdelat tekushchim predydushchiy element tekuschuego int num_current(); //nomer v spiske tekushchego elementa char* read_element(int &); //prochitat element s nomerom; Robe jogo tekushchim char* read_element(); //prochitat tekushchiy element void add_element(char*, int &);//dobavit element na nomer int; Robe novyi element tekushchim void add_after(char*); //dobavit element posle tekushchego; Robe novyi element tekushchim void add_before(char*); //dobavit element do tekushchego; Robe novyi element tekushchim void del_element(); //udalit tekushchiy element; Robe sleduushchiy element tekushchim void del_element(int &); //udalit element z nomerom int; Robe sleduushchiy element tekushchim int fput_list(char*); //zapisat spisok v fail s imenem char* int fget_list(char*); //vostanovit spisok iz faila s imenem char* int num_list(); //kol-vo zvenjev u spiska }; #endif #ifndef list__h #define list__h #include list.h #include list::list() { current=last=first=NULL; }; void list::add_after(char* s) { if (current==NULL) { current=new element[1]; current->info=new char[strlen(s) 1]; strcpy(current->info,s); current->up=current->down=NULL; first=last=current; return; }; if (current==last) { last=new element[1]; last->info=new char[strlen(s) 1]; strcpy(last->info,s); last->down=NULL; last->up=current; current->down=last; current=last; return; }; //put in buffer adress down last->down=current->down; current->down=new element[1]; current->down->up=current; current=current->down; current->down=last->down; //clear buffer last->down->up=current; last->down=NULL; current->info=new char[strlen(s) 1]; strcpy(current->info,s); return; }; void list::add_before(char* s) { if (current==NULL) { add_after(s); return; }; if (current==first) { first=new element[1]; first->up=NULL; first->down=current; current=current->up=first; first->info=new char[strlen(s) 1]; strcpy(first->info,s); return; }; first->up=current->up; current->up=new element[1]; current->up->down=current; current=current->up; current->up=first->up; first->up->down=current; first->up=NULL; current->info=new char[strlen(s) 1]; strcpy(current->info,s); return; }; void list::add_element(char* s, int &k) { int i; i=current_element(k); if ((idown; first->up=NULL; }; delete[]current->info; delete[]current; current=first; return; }; if (current==last) { last=current->up; last->down=NULL; delete[]current->info; delete[]current; current=last; return; }; last->down=current->down; current->up->down=current->down; current->down->up=current->up; delete[]current->info; delete[]current; current=last->down; last->down=NULL; return; }; void list::del_element(int &k) { int i=current_element(k); if ((iinfo; last=last->up; delete [] current; }; if (