Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- double hipotenuse(double a, double b) {
- return sqrt(a * a + b * b);
- }
- void test_pointers() {
- int val = 12;
- cout << val << " " << &val << endl;
- int* pval = &val;
- cout << pval << " " << *pval << endl;
- const int val2 = 13;
- //val2++; // константы менять нельзя
- cout << val2 << " " << &val2 << endl;
- //int* const pval2 = &val2;
- //cout << "before: " << pval2 << " " << *pval2 << endl;
- //pval2 = &val;
- val += 100; // через эту переменную можно менять
- //*pval2 += 100; // через эту нельзя - она const
- //cout << "arfter: " << pval2 << " " << *pval2 << endl;
- }
- void my_swap0(int a, int b) {
- cout << "in my swap0 before" << endl;
- cout << "a = " << a << " " << "b =" << b << endl;
- cout << "a = " << &a << " " << "b =" << &b << endl;
- int tmp = a;
- a = b;
- b = tmp;
- cout << "in my swap0 after" << endl;
- cout << "a = " << a << " " << "b =" << b << endl;
- cout << "a = " << &a << " " << "b =" << &b << endl;
- }
- void my_swap1(int *a, int *b) {
- cout << "in my swap0 before" << endl;
- cout << "a = " << *a << " " << "b =" << *b << endl;
- cout << "a = " << a << " " << "b =" << b << endl;
- int tmp = *a;
- *a = *b;
- *b = tmp;
- cout << "in my swap0 after" << endl;
- cout << "a = " << *a << " " << "b =" << *b << endl;
- cout << "a = " << a << " " << "b =" << b << endl;
- }
- void my_swap2(int &a, int &b) {
- cout << "in my swap0 before" << endl;
- cout << "a = " << a << " " << "b =" << b << endl;
- cout << "a = " << &a << " " << "b =" << &b << endl;
- int tmp = a;
- a = b;
- b = tmp;
- cout << "in my swap0 after" << endl;
- cout << "a = " << a << " " << "b =" << b << endl;
- cout << "a = " << &a << " " << "b =" << &b << endl;
- }
- int* create_arr(int n) {
- return new int[n] {};
- }
- int my_arr[]{ 1,2,3,4,5 };
- int& find(int val){
- for (int k = 0; k < 5; k++) {
- if (my_arr[k] == val) {
- return my_arr[k];
- }
- }
- }
- void test_swap() {
- //cout << hipotenuse(3, 4) << endl;
- //test_pointers();
- /*
- int a = 3;
- int b = 4;
- cout << "in main before" << endl;
- cout << "a = " << a << " " << "b =" << b << endl;
- cout << "a = " << &a << " " << "b =" << &b << endl;
- //my_swap1(&a, &b);
- my_swap2(a, b);
- cout << "in main after" << endl;
- cout << "a = " << a << " " << "b =" << b << endl;
- cout << "a = " << &a << " " << "b =" << &b << endl;
- int* arr = create_arr(5);
- for(int k =0; k < 5; ++k)
- cout << arr[k] << endl;
- */
- }
- void test_fun_pointers() {
- for (int k = 0; k < 5; ++k)
- cout << my_arr[k] << endl;
- find(2) = 15;
- for (int k = 0; k < 5; ++k)
- cout << my_arr[k] << endl;
- cout << endl;
- cout << *(my_arr + 1) << my_arr[1] << endl;
- cout << *create_arr << endl;
- }
- int sum(int a, int b) {
- cout << "Sum: ";
- return a + b;
- }
- int mul(int a, int b) {
- cout << "Mult: ";
- return a * b;
- }
- void function_pointers() {
- cout << sum << endl;
- cout << mul << endl;
- int (*pfun)(int, int) = sum; // это указатель на функцию
- cout << pfun(3, 4) << endl;
- pfun = mul;
- cout << pfun(3, 4) << endl;
- int((*pfun2[2])(int, int)) { mul, sum };
- cout << "Enter 0 - for mult calc" << endl;
- cout << "Enter 1 - for sum calc" << endl;
- int c;
- cin >> c;
- cout << pfun2[c](13, 15) << endl;
- }
- struct Point {
- int x = 0;
- int y = 0;
- };
- struct Line {
- Point pt1;
- Point pt2;
- };
- struct Man {
- const char* name;
- int age;
- const char* workplace;
- int salary;
- void show() {
- cout << name << " "
- << age << " "
- << workplace << " "
- << salary << endl;
- }
- };
- void show(Man& man) {
- cout << man.name << " "
- << man.age << " "
- << man.workplace << " "
- << man.salary << endl;
- }
- void show(Point &pt) {
- cout << "(" << pt.x << ", " << pt.y << ")" << endl;
- }
- double distance(Point& pt1, Point& pt2) {
- int dx = (pt1.x - pt2.x);
- int dy = (pt1.y - pt2.y);
- return sqrt(dx * dx + dy * dy);
- }
- void main() {
- Point pt1{0, 0};
- Point pt2{3, 4};
- //pt.x = 10;
- //pt.y = 11;
- Point arr_points[]{ pt1, pt2 };
- show(pt1);
- show(pt2);
- cout << distance(pt1, pt2) << endl;
- cout << distance(arr_points[0], arr_points[1]) << endl;
- Man man{ "Egor", 42, "Agent", 95000 };
- show(man);
- man.salary = 87000;
- show(man);
- man.show();
- struct Point2 {
- double d1;
- int d2;
- };
- Point2 pt3;
- cout << sizeof(pt1) << " " << sizeof(pt1.x) << endl;
- cout << sizeof(pt3) << " " << sizeof(pt3.d1) + sizeof(pt3.d2) << endl;
- pt1 > pt2;
- }
Advertisement
Add Comment
Please, Sign In to add comment