Const nx=30;var i,j,p,n:integer;A:array[1..nx,1..nx] of integer;d,Sn,min:real;begin p:=1;write('Введите размер матрицы');Read(n);for i:=1 to n do beginfor j:=1 to n do beginA[i,j]:=random(3)-2;write(A[i,j]:4);end;writeln;end;writeln;writeln;for i:=1 to n do beginfor j:=1 to n do beginif i = j then begin if A[i,j]<> 0 then p:=p* A[i,j];write(A[i,j]:4);endelse write(' ':4);end;writeln;end;write('p =',p);writeln;writeln;for i:=1 to n do beginfor j:=1 to n do beginif (i= n -j +1) then begin if A[i,j]<> 0 then p:=p* A[i,j];write(A[i,j]:4);endelse write(' ':4);end;writeln;end;write('p =',p);end.
#include <cstdlib>#include <string>#include <iostream>#include "field.h"using namespace std;
int main(int argc, char** argv, char** env){ srand(time(0)); vector< vector< string > > S; S.resize(2); S[0].push_back("Vremena_goda"); S[1].push_back("Zima"); S[1].push_back("Vesna"); S[1].push_back("Leto"); S[1].push_back("Osen");
Field A(S, Field().clGreen, Field().clLightblue); cout << A << std::endl; return 0;}
#ifndef FIELD_H_INCLUDED#define FIELD_H_INCLUDED
#include <vector>#include <iterator>#include <algorithm>#include <string>#include <sstream>class Field{public: enum ConsoleColor { clBlack, clRed, clGreen, clYellow, clBlue, clPurple, clLightblue, clWhite };private: size_t field_width, field_height; std::vector< std::vector< std::string > > Data; std::vector< size_t > column_width; ConsoleColor color_border, color_font; std::string get_format_color_string(std::string S, ConsoleColor color) { std::stringstream result; result << "\x1b[1;" << color + 30 << "m" << S << "\x1b[0m"; return result.str(); } std::string str_mul(std::string s, size_t num) { std::string result = ""; for(size_t i = 0; i < num; i++) result += s; return result; }public: Field() {}; Field(std::vector< std::vector< std::string > > D, ConsoleColor color_border, ConsoleColor color_font) : Data(D), color_border(color_border), color_font(color_font) { field_height = Data.size(); field_width = 0; for(size_t i = 0; i < field_height; i++) field_width = std::max(field_width, Data[i].size()); for(size_t i = 0; i < field_height; i++) while(Data[i].size() < field_width) Data[i].push_back(""); column_width.assign(field_width, 0); for(size_t i = 0; i < field_height; i++) for(size_t j = 0; j < field_width; j++) column_width[j] = std::max(column_width[j], Data[i][j].length()); } void logs() { std::cout << "field_height: " << field_height << std::endl; std::cout << "field_widht: " << field_width << std::endl; } friend std::ostream& operator <<(std::ostream& output_stream, Field & field) { /* std::cout << field.field_width << " " << field.field_height << std::endl; for(size_t i = 0; i < field.Data.size(); i++) { for(size_t j = 0; j < field.Data[i].size(); j++) std::cout << field.Data[i][j] << " "; std::cout << std::endl; } */
output_stream << field.get_format_color_string(" ┌", field.color_border); for(size_t i = 0; i < field.field_width - 1; i++) { output_stream << field.get_format_color_string(field.str_mul("─", field.column_width[i] + 2), field.color_border); output_stream << field.get_format_color_string("┬", field.color_border); } output_stream << field.get_format_color_string(field.str_mul("─", field.column_width[field.field_width - 1] + 2), field.color_border); output_stream << field.get_format_color_string("┐\n ", field.color_border);
for(size_t i = 0; i < field.field_height; i++) { output_stream << field.get_format_color_string("│", field.color_border); for(size_t j = 0; j < field.field_width; j++) { std::stringstream ss; ss << field.str_mul(" ", field.column_width[j] - field.Data[i][j].size() + 1) << (field.Data[i][j] != "" ? field.Data[i][j] : ""); output_stream << field.get_format_color_string(ss.str(), field.color_font); output_stream << field.get_format_color_string(" │", field.color_border); } output_stream << "\n "; if(i != field.field_height - 1) { output_stream << field.get_format_color_string("├", field.color_border); for(size_t j = 0; j < field.field_width - 1; j++) { output_stream << field.get_format_color_string(field.str_mul("─", field.column_width[j] + 2), field.color_border); output_stream << field.get_format_color_string("┼", field.color_border); } output_stream << field.get_format_color_string(field.str_mul("─", field.column_width[field.field_width - 1] + 2), field.color_border); output_stream << field.get_format_color_string("┤", field.color_border); } else { output_stream << field.get_format_color_string("└", field.color_border); for(size_t j = 0; j < field.field_width - 1; j++) { output_stream << field.get_format_color_string(field.str_mul("─", field.column_width[j] + 2), field.color_border); output_stream << field.get_format_color_string("┴", field.color_border); } output_stream << field.get_format_color_string(field.str_mul("─", field.column_width[field.field_width - 1] + 2), field.color_border); output_stream << field.get_format_color_string("┘\n", field.color_border); } output_stream << "\n "; } return output_stream;
}};
#endif // FIELD_H_INCLUDED