Skip to content

Instantly share code, notes, and snippets.

@yogggoy
Last active April 22, 2021 18:16
Show Gist options
  • Save yogggoy/a6bc16d0dde11b87763e305ecc2f5656 to your computer and use it in GitHub Desktop.
Save yogggoy/a6bc16d0dde11b87763e305ecc2f5656 to your computer and use it in GitHub Desktop.
примеры для решения задач stepik
void strcat(char *to, const char *from)
{
while (*to){
to++;
}
while (*from){
*to = *from;
from++;
to++;
}
*to='\0';
}
propo
propropofffa
пропо
пропропоавыфав
#include <iostream>
// int * m;
// m = new int[1000];
// delete [] m;
// m = new int(50);
// delete m;
char *resize(const char *str, unsigned size, unsigned new_size)
{
char * m = new char[new_size];
for (auto i=0; i<new_size; i++){
m[i] = str[i];
}
delete [] str;
return m;
}
char *getline()
{
char ch;
unsigned size = 10;
char * str = new char[size];
unsigned cnt = 0;
while (std::cin.get(ch) && ch != '\n') {
str[cnt] = ch;
if (cnt == size) {
str = resize(str, size, size+10);
size+=10;
}
cnt++;
}
return str;
}
=====================================================================
=====================================================================
#include <iostream>
// int * m;
// m = new int[1000];
// delete [] m;
// m = new int(50);
// delete m;
char *resize(const char *str, unsigned size, unsigned new_size) {
char * m = new char[new_size];
for (auto i=0; i<size; i++){
m[i] = str[i];
}
delete [] str;
str = 0;
return m;
}
char *getline() {
char ch;
unsigned size = 1024*1024;
char * str = new char[size];
unsigned cnt = 0;
while (std::cin.get(ch) && ch != '\n') {
str[cnt] = ch;
if (cnt == size) {
str = resize(str, size, size*2);
size*=2;
}
cnt++;
}
str[cnt] = '\0';
return str;
}
=====================================================================
=====================================================================
int ** m = new int * [5];
for (size_t i=0; i!=5; i++)
m[i] = new int[4];
int ** create_array2d(size_t a, size_t b){
int ** m = new int * [a];
for (size_t i=0; i!=a; i++)
m[i] = new int[b];
}
void free_array2d(int ** m, size_t a, size_t b){
for (size_t i=0; i!=a; i++)
delete [] m[i];
delete [] m;
}
int ** m = new int * [5];
m[0] = new int [5 * 4];
for (size_t i=1; i!=5; i++)
m[i] = m[i-1] + 4;
int ** create_array2d(size_t a, size_t b){
int ** m = new int *[a];
m[0] = new int[a * b];
for (size_t i=1; i!=a; i++)
m[i] = m[i-1] + b;
return m;
}
void free_array2d(int ** m, size_t a, size_t b){
delete [] m[0];
delete [] m;
}
int *temp = m[0];
m[0] = m[minRow];
m[minRow] = temp;
int ** create_array2d(auto a, auto b){
int ** m = new int * [a];
m[0] = new int[a * b];
for (auto i=1; i!=a; i++)
m[i] = m[i-1] + b;
return m;
}
int ** transpose(const int * const * m, unsigned rows, unsigned cols)
{
int ** nm = create_array2d(cols, rows);
for (auto i=0; i!=rows; i++){
for (auto j=0; j!=cols; j++){
nm[j][i] = m[i][j];
}
}
return nm;
}
=====================================================================
=====================================================================
#include <cmath>
doublel length(Segment s){
double dx = s.p1.x - s.p2.x;
double dy = s.p1.y - s.p2.y;
return sqrt(dx*dx + dy*dy);
}
=====================================================================
=====================================================================
struct Point {
Point() {
x = y =0;
}
Point(double x, double y) {
this->x = x;
this->y = y;
}
double x;
double y;
}
Point P1;
Point p2(3,7);
-------------------------------
struct Point {
Point() : x(0), y(0)
{}
Point(double x, double y) x(x), y(y)
{}
double x;
double y;
}
-------------------------------
struct Point {
Point(double x=0, double y=0)
: x(x), y(y)
{}
double x;
double y;
}
Point p1;
Point p2(2);
Point p3(3,4);
-------------------------------
struct Segment {
Segment() {}
Segment(double length)
: p2(length, 0)
{}
Point p1;
Point p2;
}
Segment s1;
Segment s2(10);
Segment s3 = 20;
struct Point {
explicit Point (double x=0, double y=0)
: x(x), y(y)
{}
double x;
double y;
}
Point p1;
Point p2(2);
Point p3(3,4);
Point p4 = 5; //error
-------------------------------
struct Segment {
Segment(Point p1, Point p2)
: p1(p1), p2(p2)
{}
Point p1;
Point p2;
}
Segment s1; // error
Segment s2(Point(), Point(2,1));
-------------------------------
struct Point {
explicit Point (double x=0, double y=0)
: x(x), y(y) {}
double x;
double y;
}
Point p1; // определение переменной
Point p2(); // бьявление функции
double k=5.1;
Point p3(int(k)); // обьявление функции
Point p4((int)k); // определение переменной
-------------------------------
struct IntArray {
explicit IntArray(size_t size)
: size(size)
, data(new int[size])
{}
~IntArray() {
delete [] data;
}
size_t size;
int * data;
}
-------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment