Advertisement
Ilya_and

не моё

Dec 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <conio.h>
  5. #include <stdlib.h>
  6. #include <stdarg.h>
  7. #define clr system("cls")
  8. #define pause system("pause")
  9.  
  10. using namespace std;
  11.  
  12. int strlen1(char s[]) {
  13.     int numb = 0;
  14.     while (s[numb] != 0)
  15.         numb++;
  16.     return numb;
  17. }
  18.  
  19. int strlen2(char s[]) {
  20.     int numb = 0;
  21.     for (int i = 0; i < 256; i++) {
  22.         if (s[i] != 0)
  23.             numb++;
  24.         else break;
  25.     }
  26.     return numb;
  27. }
  28.  
  29. int strlen3(char s[]) {
  30.     int numb = 0, i = 0;
  31. begin:if (s[i] != 0) {
  32.     numb++;
  33.     i++;
  34.     goto begin;
  35. }
  36.       return numb;
  37. }
  38.  
  39. int strlen4(char s[]) {
  40.     int numb = 0, i = 0;
  41.     char *s1 = s;
  42. begin:if (*s1 != 0) {
  43.     numb++;
  44.     *s1++;
  45.     goto begin;
  46. }
  47.       return numb;
  48. }
  49.  
  50.  
  51. char* strcopy1(char *dest, char *s, int p, int k) {
  52.     int lens = strlen1(s);
  53.     if (p < 1 || k < 1)
  54.         return dest;
  55.     if (p + k > lens)
  56.         k = lens - p;
  57.     for (int i = 0; i < k; i++, p++) {
  58.         dest[i] = s[p];
  59.     }
  60.     return dest;
  61. }
  62.  
  63. char* strcopy2(char *dest, char *s, int p, int k) {
  64.     int lens = strlen1(s);
  65.     if (p < 1 || k < 1 || p > strlen1(s))
  66.         return dest;
  67.     if (p + k > lens)
  68.         k = lens - p;
  69.     int i = 0;
  70.     p++;
  71.     while (k != 0 && dest != '\0') {
  72.         *(dest + i) = *(s + p - 1);
  73.         i++;
  74.         p++;
  75.         k--;
  76.     }
  77.     return dest;
  78. }
  79.  
  80. char* strcopy3(char *dest, char *s, int p, int k) {
  81.     int lens = strlen1(s);
  82.     if (p < 1 || k < 1 || p > strlen1(s))
  83.         return dest;
  84.     if (p + k > lens)
  85.         k = lens - p;
  86.     for (int i = 0; s[i]; p++, i++) {
  87.         if (s[p] == 0 || i == k)
  88.             break;
  89.         dest[i] = s[p];
  90.     }
  91.     return dest;
  92. }
  93.  
  94. char* strcopy4(char *dest, char *s, int p, int k) {
  95.     int lens = 0;
  96.     lens = strlen1(s);
  97.     if (p < 1 || k < 1 || p > lens) {
  98.         return dest;
  99.     }
  100.     if (p + k > lens)
  101.         k = lens - p;
  102.     int i = 0;
  103. begin:if (k > 0) {
  104.     dest[i] = s[p];
  105.     p++;
  106.     i++;
  107.     k--;
  108.     goto begin;
  109. }
  110.       return dest;
  111. }
  112.  
  113. void strins1(char *sub, char *s, int p) {
  114.     int lens = strlen1(s);
  115.     if (p > lens)
  116.         p = lens;
  117.     if (p < 1)
  118.         p = 0;
  119.     char tmp[256]{ 0 };
  120.     for (int i = 0, k = p; k < strlen1(s); i++, k++)
  121.         tmp[i] = s[k];
  122.     for (int i = 0, k = p; k < strlen1(sub) + p; i++, k++)
  123.         s[k] = sub[i];
  124.     for (int i = 0, k = (strlen1(sub) + p); k < (strlen1(s) + strlen1(sub)); i++, k++)
  125.         s[k] = tmp[i];
  126. }
  127.  
  128. void strins2(char *sub, char *s, int p) {
  129.     int lens = strlen1(s);
  130.     if (p > lens)
  131.         p = lens;
  132.     if (p < 1)
  133.         p = 0;
  134.     for (int i = p, k = 0; i < strlen1(sub) + p; i++, k++) {
  135.         for (int m = lens; m >= i; m--) {
  136.             *(s + m + 1) = *(s + m);
  137.             printf("s = '%s'\n", s);
  138.         }
  139.         *(s + i) = *(sub + k);
  140.     }
  141. }
  142.  
  143. void strins3(char *sub, char *s, int p) {
  144.     int lensub = strlen1(sub);
  145.     int lens = strlen1(s), N = lens;
  146.     if (p > lens)
  147.         p = lens;
  148.     if (p < 1)
  149.         p = 0;
  150.     for (int i = p; i < lensub + p; i++) {
  151.         while (N + 1 != i) {
  152.             s[N + 1] = s[N];
  153.             N--;
  154.         }
  155.         N = lens;
  156.         s[i] = sub[i - p];
  157.     }
  158. }
  159.  
  160. void strins4(char *sub, char *s, int p) {
  161.     int lens = strlen1(s);
  162.     int lensub = strlen1(sub);
  163.     if (p > lens)
  164.         p = lens;
  165.     if (p < 1)
  166.         p = 0;
  167.     int i = p, k = 0;
  168. begin1:int m = lens;
  169. begin2:s[m + 1] = s[m];
  170.     m--;
  171.     if (m >= i)
  172.         goto begin2;
  173.     s[i] = sub[k];
  174.     i++, k++;
  175.     if (i < lensub + p)
  176.         goto begin1;
  177. }
  178.  
  179. int strcmp1(char *s1, char *s2) {
  180.     int lens1 = strlen1(s1), lens2 = strlen1(s2), i = 0, s = 0;
  181.     if (lens1 == 0 || lens2 == 0)
  182.         return s;
  183.     while (s1[i] != 0 && s2[i] != 0) {
  184.         if (s1[i] > s2[i]) {
  185.             s = 1;
  186.             break;
  187.         }
  188.         else {
  189.             if (s1[i] < s2[i]) {
  190.                 s = -1;
  191.                 break;
  192.             }
  193.         }
  194.         i++;
  195.     }
  196.     return s;
  197. }
  198.  
  199. int strcmp2(char *s1, char *s2) {
  200.     while (*s1 && (*s1 == *s2))
  201.     {
  202.         s1++; s2++;
  203.     }
  204.     return *s1 == *s2 ? 0 : *s1 > *s2 ? 1 : -1;
  205. }
  206.  
  207. int strcmp3(char *s1, char *s2) {
  208.     return *s1 > *s2 ? 1 : *s1 < *s2 ? -1 : *s1 ? strcmp3(s1 + 1, s2 + 1) : 0;
  209. }
  210.  
  211. int strcmp4(char *s1, char *s2) {
  212.     int i, lens1(strlen(s1));
  213.     for (i = 0; i < lens1; i++)
  214.     {
  215.         if (s1[i] < s2[i])
  216.             return -1;
  217.         if (s1[i] > s2[i])
  218.             return 1;
  219.     }
  220.     return s2[i] ? -1 : 0;
  221. }
  222.  
  223. void strdel1(char *s, int p, int k) {
  224.     p--;
  225.     int lens = strlen1(s);
  226.     if (p + k > lens)
  227.         k = lens - p;
  228.     int k1 = k;
  229.     for (int j = 0; j < k; j++) {
  230.         for (int i = p + k1; i < strlen1(s); i++) {
  231.             printf("s = '%s'\n", s);
  232.             s[i] = s[i + 1];
  233.         }
  234.         k1--;
  235.     }
  236. }
  237.  
  238. void strdel2(char *s, int p, int k) {
  239.     int lens = strlen1(s);
  240.     if (p + k > lens)
  241.         k = lens - p;
  242.     p--;
  243.     if (p <= lens) {
  244.         while (strlen1(s) != lens - k) {
  245.             for (int i = p + 1; i < lens; i++)
  246.                 *(s + i) = *(s + i + 1);
  247.         }
  248.     }
  249. }
  250.  
  251. void strdel3(char *s, int p, int k) {
  252.     int lens = strlen1(s);
  253.     if (p + k > lens)
  254.         k = lens - p;
  255.     int i = p + k - 1;
  256. begin:if (k > 0) {
  257.     for (int j = i; j < strlen1(s); j++)
  258.         s[j] = s[j + 1];
  259.     printf("s = '%s'\n", s);
  260.     k--;
  261.     i--;
  262.     goto begin;
  263. }
  264. }
  265.  
  266. void strdel4(char *s, int p, int k) {
  267.     int lens = strlen1(s);
  268.     if (p + k > lens)
  269.         k = lens - p;
  270.     if (k > 0) {
  271.         int N = lens - k;
  272.         char *st = new char[N];
  273.         int pt = p;
  274.         for (int i = 0, j = 0; i < lens; i++) {
  275.             if (i != pt) {
  276.                 st[j] = s[i];
  277.                 j++;
  278.             }
  279.             else if ((pt - p) < k - 1)
  280.                 pt++;
  281.         }
  282.         for (int i = lens - 1; i >= 0; i--)
  283.             s[i] = 0;
  284.         for (int i = 0; i < N; i++) {
  285.             s[i] = st[i];
  286.         }
  287.     }
  288. }
  289.  
  290. int strpos1(char *sub, char *s) {
  291.     int pos = -1, lensub = strlen1(sub), lens = strlen1(s), j = 0;
  292.     if (lensub > lens)
  293.         return -1;
  294.     for (int i = 0; *(s + i) != 0; i++) {
  295.         j = 0;
  296.         if (*(sub + j) == *(s + i)) {
  297.             pos = i + 1;
  298.             for (j = 0; j < lensub; j++, i++) {
  299.                 if (*(sub + j) != *(s + i)) {
  300.                     pos = -1;
  301.                     break;
  302.                 }
  303.                 if (j == lensub - 1)
  304.                     return pos;
  305.             }
  306.             i--;
  307.         }
  308.     }
  309.     return pos;
  310. }
  311.  
  312. int strpos2(char *sub, char *s) {
  313.     int pos = -1, i = 0, j = 0, lens = strlen1(s), lensub = strlen1(sub);
  314.     if (lensub > lens)
  315.         return -1;
  316.     while (s[i] != 0) {
  317.         j = 0;
  318.         if (s[i] == sub[j]) {
  319.             pos = i + 1;
  320.             while (j != lensub) {
  321.                 if (s[i] != sub[j]) {
  322.                     pos = -1;
  323.                     break;
  324.                 }
  325.                 if (j == lensub - 1)
  326.                     return pos;
  327.                 j++;
  328.                 i++;
  329.             }
  330.         }
  331.         else
  332.             i++;
  333.     }
  334.     return pos;
  335. }
  336.  
  337. int strpos3(char *sub, char *s) {
  338.     int pos = -1;
  339.     int i = 0, j = 0, lensub = strlen1(sub), lens = strlen1(s);
  340.     if (lensub > lens)
  341.         return -1;
  342.     i--;
  343. begin:
  344.     if (i == lens - 1)
  345.         return -1;
  346.     i++;
  347. body:if (s[i] == sub[j]) {
  348.     if (pos == -1)
  349.         pos = i + 1;
  350.     if (j == lensub - 1)
  351.         return pos;
  352.     if (s[i + 1] != sub[j + 1]) {
  353.         pos = -1;
  354.         j = 0;
  355.         goto begin;
  356.     }
  357.     j++; i++;
  358.     goto body;
  359. }
  360.      else
  361.     goto begin;
  362. }
  363.  
  364. int strpos4(char *sub, char *s) {
  365.     int pos = -1, lens = strlen1(s) - 1, len = strlen1(s) - 1, lensub = strlen1(sub);
  366.     int i = 0, j = 0, b = lensub;
  367.     while (len > 0) {
  368.     startiter:if (b == 0)
  369.         return pos;
  370.               if (s[0] == sub[j]) {
  371.                   if (pos == -1)
  372.                       pos = lens - len + 1;
  373.                   j++;
  374.                   b--;
  375.               }
  376.               else {
  377.                   pos = -1;
  378.                   j = 0;
  379.                   if (b != lensub) {
  380.                       b = lensub;
  381.                       goto startiter;
  382.                   }
  383.               }
  384.               for (int i = 0; i < len; i++)
  385.                   s[i] = s[i + 1];
  386.               len--;
  387.     }
  388.     return pos;
  389. }
  390.  
  391. void testlen(char *s1) {
  392.     cout << "strlen1(\"" << s1 << "\")=: " << strlen1(s1) << endl;
  393.     cout << "strlen2(\"" << s1 << "\")=: " << strlen2(s1) << endl;
  394.     cout << "strlen3(\"" << s1 << "\")=: " << strlen3(s1) << endl;
  395.     cout << "strlen4(\"" << s1 << "\")=: " << strlen4(s1) << endl;
  396.     cout << endl;
  397. }
  398.  
  399. void testcpy(char *dest, char *s, int p, int k) {
  400.     char dest1[256]{ 0 }; int i = 0;
  401.     while (dest[i] != 0) {
  402.         dest1[i] = dest[i];
  403.         i++;
  404.     }
  405.     cout << "strcopy1(\"" << dest1 << "\",\"" << s << "\"," << p << "," << k << ")=: ";
  406.     cout << "\"" << strcopy1(dest1, s, p, k) << "\"" << endl;
  407.     for (int i = strlen1(dest1) - 1; i >= 0; i--)
  408.         dest1[i] = 0;
  409.     i = 0;
  410.     while (dest[i] != 0) {
  411.         dest1[i] = dest[i];
  412.         i++;
  413.     }
  414.     cout << "strcopy2(\"" << dest1 << "\",\"" << s << "\"," << p << "," << k << ")=: ";
  415.     cout << "\"" << strcopy2(dest1, s, p, k) << "\"" << endl;
  416.     for (int i = strlen1(dest1) - 1; i >= 0; i--)
  417.         dest1[i] = 0;
  418.     i = 0;
  419.     while (dest[i] != 0) {
  420.         dest1[i] = dest[i];
  421.         i++;
  422.     }
  423.     cout << "strcopy3(\"" << dest1 << "\",\"" << s << "\"," << p << "," << k << ")=: ";
  424.     cout << "\"" << strcopy3(dest1, s, p, k) << "\"" << endl;
  425.     for (int i = strlen1(dest1) - 1; i >= 0; i--)
  426.         dest1[i] = 0;
  427.     i = 0;
  428.     while (dest[i] != 0) {
  429.         dest1[i] = dest[i];
  430.         i++;
  431.     }
  432.     cout << "strcopy4(\"" << dest1 << "\",\"" << s << "\"," << p << "," << k << ")=: ";
  433.     cout << "\"" << strcopy4(dest1, s, p, k) << "\"" << endl;
  434.     cout << endl;
  435. }
  436.  
  437. void testins(char *sub, char *s, int p) {
  438.     char s1[256]{ 0 }; int i = 0;
  439.     while (s[i] != 0) {
  440.         s1[i] = s[i];
  441.         i++;
  442.     }
  443.     printf("s1 = '%s'\n", s1);
  444.     i = 0;
  445.     cout << "strins1(\"" << sub << "\",\"" << s1 << "," << p << ")=: ";
  446.     strins1(sub, s1, p);
  447.     cout << "\"" << s1 << "\"" << endl;
  448.     for (int i = strlen1(s1) - 1; i >= 0; i--)
  449.         s1[i] = 0;
  450.     while (s[i] != 0) {
  451.         s1[i] = s[i];
  452.         i++;
  453.     }
  454.     i = 0;
  455.     cout << "strins2(\"" << sub << "\",\"" << s1 << "," << p << ")=: ";
  456.     strins2(sub, s1, p);
  457.     cout << "\"" << s1 << "\"" << endl;
  458.     for (int i = strlen1(s1) - 1; i >= 0; i--)
  459.         s1[i] = 0;
  460.     while (s[i] != 0) {
  461.         s1[i] = s[i];
  462.         i++;
  463.     }
  464.     i = 0;
  465.     cout << "strins3(\"" << sub << "\",\"" << s1 << "," << p << ")=: ";
  466.     strins3(sub, s1, p);
  467.     cout << "\"" << s1 << "\"" << endl;
  468.     for (int i = strlen1(s1) - 1; i >= 0; i--)
  469.         s1[i] = 0;
  470.     while (s[i] != 0) {
  471.         s1[i] = s[i];
  472.         i++;
  473.     }
  474.     i = 0;
  475.     cout << "strins4(\"" << sub << "\",\"" << s1 << "," << p << ")=: ";
  476.     strins4(sub, s1, p);
  477.     cout << "\"" << s1 << "\"" << endl;
  478.     cout << endl;
  479. }
  480.  
  481. void testcmp(char *s1, char *s2) {
  482.     cout << "strcmp1(\"" << s1 << "\",\"" << s2 << "\")=: ";
  483.     cout << strcmp1(s1, s2) << endl;
  484.  
  485.     cout << "strcmp2(\"" << s1 << "\",\"" << s2 << "\")=: ";
  486.     cout << strcmp2(s1, s2) << endl;
  487.  
  488.     cout << "strcmp3(\"" << s1 << "\",\"" << s2 << "\")=: ";
  489.     cout << strcmp3(s1, s2) << endl;
  490.  
  491.     cout << "strcmp4(\"" << s1 << "\",\"" << s2 << "\")=: ";
  492.     cout << strcmp4(s1, s2) << endl;
  493.     cout << endl;
  494. }
  495.  
  496. void testdel(char *s, int p, int k) {
  497.     char s1[256]{ 0 }; int i = 0;
  498.     while (s[i] != 0) {
  499.         s1[i] = s[i];
  500.         i++;
  501.     }
  502.     i = 0;
  503.     cout << "strdel1(\"" << s1 << "\"," << p << "," << k << ")=: ";
  504.     strdel1(s1, p, k);
  505.     cout << "\"" << s1 << "\"" << endl;
  506.     while (s[i] != 0) {
  507.         s1[i] = s[i];
  508.         i++;
  509.     }
  510.     i = 0;
  511.     cout << "strdel2(\"" << s1 << "\"," << p << "," << k << ")=: ";
  512.     strdel2(s1, p, k);
  513.     cout << "\"" << s1 << "\"" << endl;
  514.     while (s[i] != 0) {
  515.         s1[i] = s[i];
  516.         i++;
  517.     }
  518.     i = 0;
  519.     cout << "strdel3(\"" << s1 << "\"," << p << "," << k << ")=: ";
  520.     strdel3(s1, p, k);
  521.     cout << "\"" << s1 << "\"" << endl;
  522.     while (s[i] != 0) {
  523.         s1[i] = s[i];
  524.         i++;
  525.     }
  526.     i = 0;
  527.     cout << "strdel4(\"" << s1 << "\"," << p << "," << k << ")=: ";
  528.     strdel4(s1, p, k);
  529.     cout << "\"" << s1 << "\"" << endl;
  530.     cout << endl;
  531. }
  532.  
  533. void teststrpos(char *sub, char *s) {
  534.     cout << "strpos1(\"" << sub << "\",\"" << s << "\")=: ";
  535.     cout << strpos1(sub, s) << endl;
  536.     cout << "strpos2(\"" << sub << "\",\"" << s << "\")=: ";
  537.     cout << strpos2(sub, s) << endl;
  538.     cout << "strpos3(\"" << sub << "\",\"" << s << "\")=: ";
  539.     cout << strpos3(sub, s) << endl;
  540.     cout << "strpos4(\"" << sub << "\",\"" << s << "\")=: ";
  541.     cout << strpos4(sub, s) << endl;
  542.     cout << endl;
  543. }
  544.  
  545. int main()
  546. {
  547.     setlocale(LC_ALL, "rus");
  548. menu:while (true) {
  549.     clr;
  550.     cin.clear();
  551.     cout << "1.strlen\n2.strcopy\n3.strins\n4.strcmp\n5.strdel\n6.strpos\n\n\n0.Выход";
  552.     switch (_getch()) {
  553.     case '1': {
  554.         clr;
  555.         char s1[] = "hello";
  556.         testlen(s1);
  557.         char s2[] = "";
  558.         testlen(s2);
  559.         char s3[] = "012";
  560.         testlen(s3);
  561.         char s4[] = "-12345";
  562.         testlen(s4);
  563.         pause;
  564.         goto menu;
  565.     }
  566.     case '2': {
  567.         clr;
  568.         int p, k;
  569.         char dest1[25] = "rty";
  570.         char s1[] = "012345";
  571.         p = 0;
  572.         k = 3;
  573.         testcpy(dest1, s1, p, k);
  574.         char s2[] = "012345";
  575.         p = 20;
  576.         k = 1;
  577.         char dest2[] = "";
  578.         testcpy(dest2, s2, p, k);
  579.         char s3[] = "012345";
  580.         p = 4;
  581.         k = 10;
  582.         char dest3[] = "";
  583.         testcpy(dest3, s3, p, k);
  584.         char s4[] = "0helloboom343";
  585.         p = 2;
  586.         k = 8;
  587.         char dest4[] = "";
  588.         testcpy(dest4, s4, p, k);
  589.         pause;
  590.         goto menu;
  591.     }
  592.     case '3': {
  593.         clr;
  594.         int p;
  595.         char sub1[] = "ab", s1[] = "012345678";
  596.         p = 0;
  597.         testins(sub1, s1, p);
  598.         char sub2[] = "ab", s2[] = "012345678";
  599.         p = 3;
  600.         testins(sub2, s2, p);
  601.         char sub3[] = "ab", s3[] = "012345678";
  602.         p = 10;
  603.         testins(sub3, s3, p);
  604.         char sub4[] = "ab", s4[] = "012345678";
  605.         p = 0;
  606.         testins(sub4, s4, p);
  607.         pause;
  608.         goto menu;
  609.     }
  610.     case '4': {
  611.         clr;
  612.         char s11[] = "hello", s12[] = "hellor";
  613.         testcmp(s11, s12);
  614.         char s21[] = "hello", s22[] = "boomboom";
  615.         testcmp(s21, s22);
  616.         char s31[] = "bello", s32[] = "booms";
  617.         testcmp(s31, s32);
  618.         char s41[] = "", s42[] = "";
  619.         testcmp(s41, s42);
  620.         pause;
  621.         goto menu;
  622.     }
  623.     case '5': {
  624.         clr;
  625.         int p, k;
  626.         char s1[] = "012345";
  627.         p = 2; k = 30;
  628.         testdel(s1, p, k);
  629.         char s2[] = "012345";
  630.         p = 20; k = 3;
  631.         testdel(s2, p, k);
  632.         char s3[] = "012345abcde";
  633.         p = 0; k = 6;
  634.         testdel(s3, p, k);
  635.         char s4[] = "012345";
  636.         p = 1; k = 3;
  637.         testdel(s4, p, k);
  638.         pause;
  639.         goto menu;
  640.     }
  641.     case '6': {
  642.         clr;
  643.         char sub1[] = "345", s1[] = "0123456";
  644.         teststrpos(sub1, s1);
  645.         char sub2[] = "123", s2[] = "012123";
  646.         teststrpos(sub2, s2);
  647.         char sub3[] = "55", s3[] = "444545544";
  648.         teststrpos(sub3, s3);
  649.         char sub4[] = "ab", s4[] = "acdeabcde";
  650.         teststrpos(sub4, s4);
  651.         pause;
  652.         goto menu;
  653.     }
  654.     case '0': exit(0);
  655.     }
  656. }
  657. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement