Upgrade to Pro — share decks privately, control downloads, hide ads and more …

№ 2018/2. Численные типы данных, массивы, указа...

№ 2018/2. Численные типы данных, массивы, указатели

Лекция о целочисленных и вещественных типах данных, одномерных массивах, указателях.

More Decks by Основы Программирования

Other Decks in Programming

Transcript

  1. ࡏ ࡏ кӌҸӈӍӋӐӑҺӍ џ ࡴ бӊҸҾӄӋӄӏ бӊҸҾӄӋӄӏӍҺӄӖ хҸӏӔӄӌҿӌӈӍࡈ ࡄ ҹҸӈҸӊҸҺӏ

    ӔӄӃӄӈӄ ࡫ыы࡬ࡈ ӋҸһӄӐӑӏ ӋҸӑҿӋҸӑӄӈӄ ࡫тты࡬ࡈ ࡄ ӎӏӍӔҿӐӐӄӍӌҸӊӛӌӝӅ ӎӏӍһӏҸӋӋӄӐӑ ࡫CʾżƚȀɯǞȩɟ࡬ࡈ ࡄ ӏҿһӒӊӨӏӌӍ ӖҿӋӒࡷӑӍ ӒӖӒ ࡫ыыࡈ Яышлࡈ ся ытя࡬ࡏ пӍӌӑҸӈӑࡇ ʲȀŏƇǞȒǞɟࡏɔŏɟ˙ȘƚȘǺȩॅǃȒŏǞȀࡏżȩȒ ߞ
  2. ࡏ ࡏ еҺӍӄӖӌҸӨ ӐӄӐӑҿӋҸ ӐӖӄӐӊҿӌӄӨ чҸӋӝӅ ӎӏӍӐӑӍӅ ӋҿӑӍҾ ӃҸӎӄӐӄ ӖӄӐҿӊࡈ

    ӄӐӎӍӊӛӃӒӧәӄӅ ӑӍӊӛӈӍ ҾҺҿ ӗӄӔӏӝࡇ ߜ ӄ ߝࡏ ч ӎӍӋӍәӛӧ ȗ ӔӓӉӊӝӊӋ ӋӍӂӌӍ ӃҸӎӄӐҸӑӛ ߞȗ ӖӄӐҿӊࡏ 000ߞ = ߜ, 100ߞ = ߠ, 001ߞ = ߝ, 101ߞ = ߡ, 010ߞ = ߞ, 110ߞ = ߢ, 011ߞ = ߟ, 111ߞ = ߣ. ߠ
  3. ࡏ ࡏ эӄӐӊҸ Һ ӎҸӋӨӑӄ ӈӍӋӎӛӧӑҿӏҸ ߝ ҿҾӋӗ ӐӍӐӑӍӄӑ ӄӃ

    ߤ ҿӊӗ ӄ ӋӍӂҿӑ ӈӍҾӄӏӍҺҸӑӛ ߞߤ = ߞߡߢ ӏҸӃӊӄӖӌӝӕ ӖӄӐҿӊࡏ уҸӎӏӄӋҿӏࡈ ӖӄӐӊӍ ߟߟߣ ӈӍҾӄӏӒҿӑӐӨ ӋӄӌӄӋӒӋ ߞ ҹҸӅӑҸӋӄࡇ 00000001 ҹӄӑӝ ߝߡ...ߤ 01010001 ҹӄӑӝ ߣ...ߜ ࡄ ߝ ҹҸӅӑ ࡫ߤ ҹӄӑ࡬ ӈӍҾӄӏӒҿӑ ߞߡߢ ӖӄӐҿӊࡈ ࡄ ߞ ҹҸӅӑҸ ࡫ߝߢ ҹӄӑ࡬ ӈӍҾӄӏӒӧӑ ߢߡ ߡߟߢ ӖӄӐҿӊࡈ ࡄ ߠ ҹҸӅӑҸ ࡫ߟߞ ҹӄӑҸ࡬ ӈӍҾӄӏӒӧӑ ߠ ߞߥߠ ߥߢߣ ߞߥߢ ӖӄӐҿӊࡈ ࡄ ߤ ҹҸӅӑ ࡫ߢߠ ҹӄӑҸ࡬ ӈӍҾӄӏӒӧӑ ߝߤ ߠߠߢ ߣߠߠ ߜߣߟ ߣߜߥ ߡߡߝ ߢߝߢ ӖӄӐҿӊࡏ ߡ
  4. ࡏ ࡏ юҿӊӍӖӄӐӊҿӌӌӝҿ ӑӄӎӝ Һ + шӄӎ цҸӃӋҿӏࡈ ҹӄӑ тӄӌӄӋӒӋ

    тҸӈӐӄӋӒӋ unsigned char ߸ ߰ ߞߡߡ unsigned short ߱߶ ߰ ߢߡ ߡߟߡ unsigned int ߲߳ ߰ ߠ ߞߥߠ ߥߢߣ ߞߥߡ unsigned long long ߶ߴ ߰ ߝߤ,ߠ · ߝߜߝߤ signed char ߸ −ߝߞߤ ߝߞߣ short ߱߶ −ߟߞ ߣߢߤ ߟߞ ߣߢߣ int ߲߳ −ߞ ߝߠߣ ߠߤߟ ߢߠߤ ߞ ߝߠߣ ߠߤߟ ߢߠߣ long long ߶ߴ −ߥ,ߞ · ߝߜߝߤ ߥ,ߞ · ߝߜߝߤ ߢ
  5. ࡏ ࡏ чӊӍӂҿӌӄҿ ӗҿӊӝӕ ӖӄӐҿӊ Ӑ ӎҿӏҿӎӍӊӌҿӌӄҿӋ цҸӐӐӋӍӑӏӄӋ ӐӊӍӂҿӌӄҿ ҾҺӒӕ

    ߤࡷҹӄӑӌӝӕ ҹҿӃӃӌҸӈӍҺӝӕ ӖӄӐҿӊ ߞߡߜ + ߥ = 1111 1010ߞ + 0000 1001ߞ = . . . + 1111 1010 0000 1001 1 0000 0011 ߤ ҹӄӑ ч ӑӍӖӈӄ ӃӏҿӌӄӨ ӈӍӋӎӛӧӑҿӏҸࡇ ߞߡߜ + ߥ = 0000 0011ߞ = ߟࡏ ߣ
  6. ࡏ ࡏ бӄҾӝ ӎҿӏҿӎӍӊӌҿӌӄӨ ӗҿӊӝӕ ӖӄӐҿӊ аҿӃӃӌҸӈӍҺӝҿ ߤࡷҹӄӑӌӝҿࡇ ࡄ ߞߡߡ

    + ߝ = ߜࡈ ࡄ ߜ − ߝ = ߞߡߡࡏ кӌҸӈӍҺӝҿ ߤࡷҹӄӑӌӝҿࡇ ࡄ ߝߞߣ + ߝ = −ߝߞߤࡈ ࡄ −ߝߞߤ − ߝ = ߝߞߣࡏ ߤ
  7. ࡏ ࡏ CɔǞż _ŏǞȀɯ ࡄ ߝߟ ӄӧӌӨ ߞߜߜߥ һࡏ ࡴ

    ÷ʶǞɾɾƚɟࡇ ӎӍӏӨҾӈӍҺӝӅ ӌӍӋҿӏ ӑҺӄӑӍҺ ӎҿӏҿӎӍӊӌӄӊ ߟߞࡷҹӄӑӌӍҿ ӃӌҸӈӍҺӍҿ ӗҿӊӍҿࡏ ࡄ ߞߞ ӐҿӌӑӨҹӏӨ ߞߜߜߥ һࡏ ࡴ ÷ʶǞɾɾƚɟࡇ ӎӍӏӨҾӈӍҺӝӅ ӌӍӋҿӏ ӑҺӄӑӍҺ ӎҿӏҿӎӍӊӌӄӊ ߟߞࡷҹӄӑӌӍҿ ҹҿӃӃӌҸӈӍҺӍҿ ӗҿӊӍҿࡏ ࡄ ߥ ӔҿҺӏҸӊӨ ߞߜߝߟ һࡏ ࡴ °ɔƚȘëɾɟƚƚɾ›ŏɔࡇ ӎӍӏӨҾӈӍҺӝӅ ӌӍӋҿӏ ӑӍӖҿӈ ӌҸ ӈҸӏӑҿ ӎҿӏҿӎӍӊӌӄӊ ߟߞࡷҹӄӑӌӍҿ ӃӌҸӈӍҺӍҿ ӗҿӊӍҿࡏ ࡄ ߝ ҾҿӈҸҹӏӨ ߞߜߝߠ һࡏ ࡴ Įȩʕ÷ʕųƚࡇ ӈӍӊӄӖҿӐӑҺӍ ӎӏӍӐӋӍӑӏӍҺ ӍҾӌӍһӍ ҺӄҾҿӍ ӎҿӏҿӎӍӊӌӄӊӍ ߟߞࡷҹӄӑӌӍҿ ӃӌҸӈӍҺӍҿ ӗҿӊӍҿࡏ ࡄ ߞߜ ӨӌҺҸӏӨ ߞߜߝߣ һࡏ ࡴ +ȩƇƚࡏȩɟǃࡇ ӄҾҿӌӑӄӔӄӈҸӑӍӏ ӐӑӒҾҿӌӖҿӐӈӄӕ ӏҸҹӍӑ ӎҿӏҿӎӍӊӌӄӊ ߟߞࡷҹӄӑӌӍҿ ҹҿӃӃӌҸӈӍҺӍҿ ӗҿӊӍҿࡏ ߥ
  8. ࡏ ࡏ эӄӐӊҸ Ӑ ӎӊҸҺҸӧәҿӅ ӑӍӖӈӍӅ ʺ = ȑ ·

    ųƙ, һҾҿࡇ ࡄ ȑ ࡴ ӑҾӒӗӊӖӖҾ ࡫ӃӌҸӖҸәҸӨ ӖҸӐӑӛ࡬ࡈ ࡄ ų ࡴ ӓӖӒӓӀҾӒӊӅ ӖӗӅӔӅӒӊ ࡫ӍҹӝӖӌӍ ߞ ӄӊӄ ߝߜ࡬ࡈ ࡄ ƙ ࡴ ӨӎӖӔӓӒӅӒӗҾ ࡫ӎӍӏӨҾӍӈ࡬ࡏ ߝߝ
  9. ࡏ ࡏ эӄӐӊҸ Ӑ ӎӊҸҺҸӧәҿӅ ӑӍӖӈӍӅ Һ + шӄӎ тӄӌࡏ

    ҸҹӐࡏ ӃӌҸӖҿӌӄҿ тҸӈӐࡏ ҸҹӐࡏ ӃӌҸӖҿӌӄҿ шӍӖӌӍӐӑӛࡈ ҾҿӐࡏ ӃӌҸӈӍҺ float ߝ,ߝߤ · ߝߜ−ߟߤ ߟ,ߠߜ · ߝߜߟߤ ≈ ߣ double ߞ,ߞߟ · ߝߜ−ߟߜߤ ߝ,ߤߜ · ߝߜߟߜߤ ≈ ߝߢ ߝߞ
  10. ࡏ ࡏ фӐӍҹҿӌӌӝҿ ӖӄӐӊҸ эӄӐӊҸ ±ߜࡈ ±∞ Ȁȩǃ(ߜ) = −∞,

    ߝ/(+ߜ) = +∞, ߝ/(−ߜ) = −∞. уҿࡷӖӄӐӊҸ ࡫ŸŏŸࡈ Ÿȩɾ ŏ ŸʕȒųƚɟ࡬ࡇ œŏœ = √ −ߝ, ߜ/ߜ, ߜ · ∞, ∞/∞, ∞ − ∞, ߝߟ
  11. ࡏ ࡏ хӏӍҹӊҿӋӝ Ӑ ӑӍӖӌӍӐӑӛӧ ࡄ фӈӏӒһӊҿӌӄҿࡇ ɾǃ(π) ̸= ߜ,

    ɾǃ(π/ߞ) ̸= ∞ࡏ ࡄ уҸӈӍӎӊҿӌӄҿ ӍӘӄҹӍӈࡇ ŏ + (ų + ż) ̸= (ŏ + ų) + żࡏ ࡄ хӍӑҿӏӨ ӑӍӖӌӍӐӑӄࡇ ŏ − ųࡈ ҿӐӊӄ ŏ ≈ ųࡏ ࡄ хӍӑҿӏӨ ӑӍӖӌӍӐӑӄࡇ ŏ + ųࡈ ҿӐӊӄ ŏ ≫ ų ӄӊӄ ŏ ≪ ųࡏ ࡄ чӏҸҺӌҿӌӄҿ ӖӄӐҿӊࡏ ߝߠ
  12. ࡏ ࡏ фҹӜӨҺӊҿӌӄҿ ӋҸӐӐӄҺҸ тҸӐӐӄҺ ࡴ ӒӎӍӏӨҾӍӖҿӌӌӝӅ ӌҸҹӍӏ ӢӊҿӋҿӌӑӍҺ ӍҾӌӍһӍ

    ӑӄӎҸࡏ фҹӜӨҺӊҿӌӄҿ ӋҸӐӐӄҺҸ arrࡈ ӐӍӐӑӍӨәҿһӍ ӄӃ N ӢӊҿӋҿӌӑӍҺ ӎӏӍӄӃҺӍӊӛӌӍһӍ ӑӄӎҸ Tࡇ T arr[N]; һҾҿ N ࡴ ӈӍӌӐӑҸӌӑҸ ҺӏҿӋҿӌӄ ӈӍӋӎӄӊӨӗӄӄࡏ хӏӄӋҿӏ ӍҹӜӨҺӊҿӌӄӨ ӄ ӄӌӄӗӄҸӊӄӃҸӗӄӄ ӋҸӐӐӄҺҸࡇ int xs[4]; // {?, ?, ?, ?} int ys[] = {20, 10}; // {20, 10} int zs[4] = {20, 10}; // {20, 10, 0, 0} ߝߢ
  13. ࡏ ࡏ еӍӐӑӒӎ ӈ ӢӊҿӋҿӌӑҸӋ ӋҸӐӐӄҺҸ T arr[N]ࡇ ࡏ ࡏ

    ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ 0 ࡏ ࡏ 1 ࡏ ࡏ ࡏ ࡏ ࡊ ࡏ ࡏ ࡏ ࡏ N-1 эӑҿӌӄҿ ӢӊҿӋҿӌӑҸ ӄӃ ӋҸӐӐӄҺҸ ӎӍ ӄӌҾҿӈӐӒ iࡇ T value = arr[i]; кҸӎӄӐӛ ӢӊҿӋҿӌӑҸ Һ ӋҸӐӐӄҺ ӎӍ ӄӌҾҿӈӐӒ iࡇ arr[i] = new_value; ߝߣ
  14. ࡏ ࡏ щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ ࡏ ࡏ ࡏ ߡߝߤ ࡏ

    ࡏ ߟߞߜ ࡏ 0x20 ࡏ 0x23 ࡏ 0x40 ࡏ 0x43 int x = 518; int y = 320; int* ptr; // & - взятие адреса // * - разыменование указателя ptr = &x; printf("%p %d\n", ptr, *ptr); // 0x20 518 ptr = &y; printf("%p %d\n", ptr, *ptr); // 0x40 320 ߝߥ
  15. ࡏ ࡏ щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ ࡏ ࡏ ࡏ ߡߝߤ ࡏ

    ࡏ ߥߥߥ ࡏ 0x20 ࡏ 0x23 ࡏ 0x40 ࡏ 0x43 int x = 518; int y = 320; int* ptr; // & - взятие адреса // * - разыменование указателя ptr = &x; printf("%p %d\n", ptr, *ptr); // 0x20 518 ptr = &y; printf("%p %d\n", ptr, *ptr); // 0x40 320 *ptr = 999; printf("%d %d\n", x, y); // 518 999 ߝߥ
  16. ࡏ ࡏ тҸӐӐӄҺӝ ӄ ӒӈҸӃҸӑҿӊӄ ࡏ ࡏ ࡏ 0x20 ࡏ

    a[0] ࡏ ࡏ 0x22 ࡏ a[1] ࡏ ࡏ 0x24 ࡏ a[2] ࡏ ߝߜ ࡏ ߞߜ ࡏ ߟߜ short a[3] = {10, 20, 30}; short* p = &(a[0]); // 0x20 short* p1 = p + 1; // 0x22 short* p2 = p + 2; // 0x24 ߞߜ
  17. ࡏ ࡏ тҸӐӐӄҺӝ ӄ ӒӈҸӃҸӑҿӊӄ ࡏ ࡏ ࡏ 0x20 ࡏ

    a[0] ࡏ ࡏ 0x22 ࡏ a[1] ࡏ ࡏ 0x24 ࡏ a[2] ࡏ ߝߜ ࡏ ߞߜߜ ࡏ ߟߜ short a[3] = {10, 20, 30}; short* p = &(a[0]); // 0x20 short* p1 = p + 1; // 0x22 short* p2 = p + 2; // 0x24 *p1 = 200; ߞߜ
  18. ࡏ ࡏ тҸӐӐӄҺӝ ӄ ӒӈҸӃҸӑҿӊӄ ࡏ ࡏ ࡏ 0x20 ࡏ

    a[0] ࡏ ࡏ 0x22 ࡏ a[1] ࡏ ࡏ 0x24 ࡏ a[2] ࡏ ߝߜ ࡏ ߞߜߜ ࡏ ߞߟߜ short a[3] = {10, 20, 30}; short* p = &(a[0]); // 0x20 short* p1 = p + 1; // 0x22 short* p2 = p + 2; // 0x24 *p1 = 200; *p2 = *p1 + *p2; ߞߜ
  19. ࡏ ࡏ тҸӐӐӄҺӝ ӄ ӒӈҸӃҸӑҿӊӄ ࡏ ࡏ ࡏ 0x20 ࡏ

    a[0] ࡏ ࡏ 0x22 ࡏ a[1] ࡏ ࡏ 0x24 ࡏ a[2] ࡏ ߝߜ ࡏ ߞߜߜ ࡏ ߞߟߜ short a[3] = {10, 20, 30}; short* p = &(a[0]); // 0x20 short* p1 = p + 1; // 0x22 short* p2 = p + 2; // 0x24 *(p+1) = 200; *(p+2) = *(p+1) + *(p+2); ߞߜ
  20. ࡏ ࡏ тҸӐӐӄҺӝ ӄ ӒӈҸӃҸӑҿӊӄ ࡏ ࡏ ࡏ 0x20 ࡏ

    a[0] ࡏ ࡏ 0x22 ࡏ a[1] ࡏ ࡏ 0x24 ࡏ a[2] ࡏ ߝߜ ࡏ ߞߜߜ ࡏ ߞߟߜ short a[3] = {10, 20, 30}; short* p = &(a[0]); // 0x20 short* p1 = p + 1; // 0x22 short* p2 = p + 2; // 0x24 p[1] = 200; p[2] = p[1] + p[2]; // *(x + y) == x[y] ߞߜ
  21. ࡏ ࡏ тҸӐӐӄҺӝ ӄ ӒӈҸӃҸӑҿӊӄ ࡏ ࡏ ࡏ 0x20 ࡏ

    a[0] ࡏ ࡏ 0x22 ࡏ a[1] ࡏ ࡏ 0x24 ࡏ a[2] ࡏ ߝߜ ࡏ ߞߜߜ ࡏ ߞߟߜ short a[3] = {10, 20, 30}; short* p = &(*(a)); // 0x20 short* p1 = p + 1; // 0x22 short* p2 = p + 2; // 0x24 p[1] = 200; p[2] = p[1] + p[2]; // *(x + y) == x[y] ߞߜ
  22. ࡏ ࡏ тҸӐӐӄҺӝ ӄ ӒӈҸӃҸӑҿӊӄ ࡏ ࡏ ࡏ 0x20 ࡏ

    a[0] ࡏ ࡏ 0x22 ࡏ a[1] ࡏ ࡏ 0x24 ࡏ a[2] ࡏ ߝߜ ࡏ ߞߜߜ ࡏ ߞߟߜ short a[3] = {10, 20, 30}; short* p = a; // 0x20 short* p1 = p + 1; // 0x22 short* p2 = p + 2; // 0x24 p[1] = 200; p[2] = p[1] + p[2]; // *(x + y) == x[y] ߞߜ