• Как передать двойной массив в подкласс из класса, сохранив адреса массива?

    rusyska55011
    @rusyska55011 Автор вопроса
    Придумал. Использую обычные указатели, а с помощью адресной арифметики буду выводить значения
    class Figure {
    
    	public:
    		Figure(bool * figure, string name) : figure(figure), name(name) {
    
    			Rotation rotation{ this->figure };
    			rotation.show();
    			this->show_figure();
    
    			cout << this->figure << "\n";
    		};
    
    		class Rotation {
    			public:
    				Rotation(bool * figure) {
    					this->figure = figure;
    					cout << this->figure << "\n";
    				};
    				
    				enum class rotate_sides { Normal = 0, Degree90 = 1, Degree180 = 2, Degree270 = 3 };
    
    				void show() {
    					bool* p = this->figure;
    					for (int i = 0; i < 4; i++) {
    						for (int j = 0; j < 4; j++) {
    							cout << *(p++) << " ";
    						}
    						cout << "\n";
    					}
    				}
    
    			private:
    				bool * figure;
    				
    		};
    					
    		void rotate_figure() {
    			switch (side) {
    				case Rotation::rotate_sides::Normal:
    					this->side = Rotation::rotate_sides::Degree90;
    					break;
    				case Rotation::rotate_sides::Degree90:
    					this->side = Rotation::rotate_sides::Degree180;
    					break;
    				case Rotation::rotate_sides::Degree180:
    					this->side = Rotation::rotate_sides::Degree270;
    					break;
    				case Rotation::rotate_sides::Degree270:
    					this->side = Rotation::rotate_sides::Normal;
    					break;
    			}
    
    			cout << static_cast<int>(side) << "\n";
    		}
    
    		void rotate_figure(Rotation::rotate_sides side) {
    			this->side = side;
    		}
    		
    		void show_figure() {
    			bool* p = this->figure;
    			for (int i = 0; i < 4; i++) {
    				for (int j = 0; j < 4; j++) {
    					cout << *(p++) << " ";
    				}
    				cout << "\n";
    			}
    		}
    
    	protected:
    		bool * figure;
    
    		bool figure_sided[4][4];
    		static Rotation::rotate_sides side;
    
    		string name;
    
    };
    Figure::Rotation::rotate_sides Figure::side = Figure::Rotation::rotate_sides::Normal;
    
    
    
    class Box : public Figure {
    	private:
    		static bool box[4][4];
    	public:
    		Box() : Figure(*box, "Box") {
    			cout << box << "\n";
    		};
    
    };
    bool Box::box[4][4] = {
    	{0, 0, 0, 0},
    	{0, 1, 1, 0},
    	{0, 1, 1, 0},
    	{0, 0, 0, 0},
    };
    Ответ написан
    Комментировать
  • Как разрешить только одну запись в БД Django 3?

    rusyska55011
    @rusyska55011 Автор вопроса
    Нашел способ - надо переписать метод сохранения
    def save(self, *args, **kwargs):
            if not self.pk and КлассМодели.objects.exists():
                raise ValidationError('Можно создать только одну запись а базе')
            return super(КлассМодели, self).save(*args, **kwargs)
    Ответ написан
    Комментировать
  • Как нарисовать треугольник?

    rusyska55011
    @rusyska55011
    height = 10
    for row_level in range(height):
        string = ''
        for left_step in range(height - row_level):
            string += ' '
        string += '*'
    
        for right_step in range(row_level * 2):
            string += ' '
        print(string + '*')
    
    else:
        string = ''
        for i in range(height * 2 + 2):
            string += '*'
        print(string)
    Ответ написан
    Комментировать
  • Как вывести форму Select c условием в Django3?

    rusyska55011
    @rusyska55011 Автор вопроса
    Спасибо за подсказку Django. Как вывести поле ForeignKey в шаблон с определённым условием?
    Надо обратиться к форме и изменить объекты:
    views.py:
    from django.db.models import Q
    from datetime import date, datetime
    
    day_today = date.today()
    time_now = datetime.now().time()
    
    form = ApplicationForm()
    # Фильтруем прошедшие ивенты (Конструкця с Q и палочкой позволяет подставить ИЛИ)
    form.fields['event'].queryset = TimeTable.objects.filter((Q(date = day_today) & Q(time__gt = time_now)) | Q(date__gt = day_today))
    Ответ написан
    Комментировать