Advertisement
SeriousVenom

lab4

Jun 7th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.28 KB | None | 0 0
  1. // LabProgramming4.0.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //
  3.  
  4. #define _CRT_SECURE_NO_WARNINGS
  5. #include <iostream>
  6. #include <fstream>
  7. #include <cstring>
  8. #include <ctime>
  9. #include <cctype>
  10. #include <limits>
  11. #include <stdio.h>
  12.  
  13. using namespace std;
  14.  
  15. void cleaning(); //очищение
  16. void Stringlength(char* str); //длина строки
  17. void CopyStr(char* str1, char* str2); //копирование строк
  18. void BondStr(char* str1, char* str2); //склеивание строк
  19. void DelSpace(char* str1, char* str2); //удаление пробелов из строки
  20.  
  21.  
  22. int main()
  23. {
  24.     int choice;
  25.     cout << "Enter task number (from 1 to 10), or enter 0 to exit: "; cin >> choice;
  26.    
  27.     if (choice == 1) //Task 1
  28.     {
  29.         char a[100][100], b[100][100], c[100][100], d[100][100];
  30.         char str[100];
  31.         srand(time(NULL));
  32.        
  33.         fstream f("file.txt", ios::in);
  34.         int i = 0;
  35.  
  36.         while (f >> str)
  37.         {
  38.             strcpy_s(a[i], str); i++;
  39.         }
  40.         int na = i;
  41.         for (i = 0; i < na; i++)
  42.         {
  43.             cout << a[i] << endl;
  44.         }
  45.         cout << endl;
  46.                
  47.         fstream g("file1.txt", ios::in);
  48.         i = 0;
  49.  
  50.         while (g >> str)
  51.         {
  52.             strcpy_s(b[i], str); i++;
  53.         }
  54.         int nb = i;
  55.         for (i = 0; i < nb; i++)
  56.         {
  57.             cout << b[i] << endl;
  58.         }
  59.         cout << endl;
  60.                
  61.         fstream h("file2.txt", ios::in);
  62.         i = 0;
  63.  
  64.         while (h >> str)
  65.         {
  66.             strcpy_s(c[i], str); i++;
  67.         }
  68.         int nc = i;
  69.         for (i = 0; i < nc; i++)
  70.         {
  71.             cout << c[i] << endl;
  72.         }
  73.         cout << endl;
  74.                
  75.         fstream p("file3.txt", ios::in);
  76.         i = 0;
  77.  
  78.         while (p >> str)
  79.         {
  80.             strcpy_s(d[i], str); i++;
  81.         }
  82.         int nd = i;
  83.         for (i = 0; i < nd; i++)
  84.         {
  85.             cout << d[i] << endl;
  86.         }
  87.         cout << endl;
  88.        
  89.  
  90.         int ka = rand() % na; //заголовок
  91.         cout << a[ka] << " ";
  92.  
  93.         int kb = rand() % nb; //существительные
  94.         cout << b[kb] << " ";
  95.  
  96.         int kc = rand() % nc; //глаголы
  97.         cout << c[kc] << " ";
  98.  
  99.         int kd = rand() % nd; //предлоги
  100.         cout << d[kd] << " ";
  101.  
  102.         ka = rand() % na; //заголовок
  103.         cout << a[ka] << " ";
  104.  
  105.         kb = rand() % nb; //существительные
  106.         cout << b[kb] << " ";
  107.  
  108.         cout << endl;
  109.     }
  110.        
  111.     if (choice == 2) // Task 2
  112.     {
  113.         int n;
  114.         cout << "Enter the size of the sequence: "; cin >> n;
  115.         n += 1;
  116.         cleaning();
  117.         char* arr1 = new char[n];
  118.         char* arr2 = new char[n];
  119.         char* arr3 = new char[n];
  120.         char* arr4 = new char[n];
  121.         char* arr5 = new char[n];
  122.  
  123.         cout << "Enter more than 4 digits: "; cin.getline(arr1, 100);
  124.         int z = strlen(arr1);
  125.         if (z >= 4) {
  126.             int string = z / 4, string1 = string, string2 = string, cons = 0, a, b, c, d;
  127.             for (int i = 0; i < string; i++) { arr2[i] = arr1[i]; }
  128.             string += string1;
  129.             for (int i = string1; i < string; i++) { arr3[cons] = arr1[i]; cons++; }
  130.             string += string1;
  131.             string2 += string1;
  132.             cons = 0;
  133.             for (int i = string2; i < string; i++) { arr4[cons] = arr1[i]; cons++; }
  134.             cons = 0;
  135.             string2 += string1;
  136.             for (int i = string2; i < z; i++) { arr5[cons] = arr1[i]; cons++; }
  137.             a = atoi(arr2); b = atoi(arr3); c = atoi(arr4); d = atoi(arr5);
  138.             cout << a << " " << b << " " << c << " " << d << endl;
  139.             if (a == b) cout << "Your answer: 1\n"; if (c == d) cout << "Your answer: 1\n";
  140.             cout << "Your answers: \n";
  141.             if (a < b) cout << a << "/" << b << endl;
  142.             if (a > b) cout << b << "/" << a << endl;
  143.             if (c < d) cout << c << "/" << d << endl;
  144.             if (c > d) cout << d << "/" << c << endl;
  145.  
  146.         }
  147.         else { cout << "Error, less than 4 numbers were entered (x_x)"; return 0; }
  148.             delete[]arr1;
  149.             delete[]arr2;
  150.             delete[]arr3;
  151.             delete[]arr4;
  152.             delete[]arr5;
  153.     }
  154.     if (choice == 3) // Task 3
  155.     {
  156.         int n;
  157.         cout << "Enter the length of the string: "; cin >> n; n += 1;
  158.         char* str = new char[n];
  159.         cleaning();
  160.         cout << "Enter your string: "; cin.getline(str, n); cout << "Your string: " << str << endl;
  161.         Stringlength(str);
  162.             delete[]str;
  163.     }
  164.     if (choice == 4) // Task 4
  165.     {
  166.         int n;
  167.         cout << "Enter the length of the string: "; cin >> n; n += 1;
  168.         char* str1 = new char[n];
  169.         char* str2 = new char[n];
  170.         cleaning();
  171.         cout << "Enter your first string: "; cin.getline(str1, n);
  172.         cout << endl;
  173.         CopyStr(str1, str2);
  174.         cout << "First string: " << str1 << endl;
  175.         cout << "Second string: " << str2 << endl;
  176.             delete[]str1;
  177.             delete[]str2;
  178.     }
  179.     if (choice == 5) // Task 5
  180.     {
  181.         int n;
  182.         cout << "Enter the length of the string: "; cin >> n; n += 1;
  183.         char* str1 = new char[n];
  184.         char* str2 = new char[n];
  185.         cleaning();
  186.         cout << "Enter first string: "; cin.getline(str1, n);
  187.         cout << "Enter second string: "; cin.getline(str2, n);
  188.         BondStr(str1, str2);
  189.             delete[]str1;
  190.             delete[]str2;
  191.     }
  192.     if (choice == 6) // Task 6
  193.     {
  194.         int n, cons = 0;
  195.         cout << "Enter the length of the string: "; cin >> n; n += 1;
  196.         char* str1 = new char[n];
  197.         char* str2 = new char[n];
  198.         cleaning();
  199.         cout << "Enter string: "; cin.getline(str1, n);
  200.         cout << endl;
  201.         cout << "Original string: " << str1 << endl;
  202.         DelSpace(str1, str2);
  203.             delete[]str1;
  204.             delete[]str2;
  205.     }
  206.     if (choice == 7) // Task 7
  207.     {
  208.    
  209.     }
  210.     if (choice == 8) // Task 8 - Не доделано
  211.     {
  212.        
  213.  
  214.     }
  215.     if (choice == 9) // Task 9
  216.     {
  217.  
  218.     }
  219.     if (choice == 10) // Task 10
  220.     {
  221.  
  222.     }
  223. }
  224.  
  225. void cleaning()
  226. {
  227.     cin.clear();
  228.     while (cin.get() != '\n');
  229. }
  230.  
  231. void Stringlength(char* str)
  232. {
  233.     int cons = 0;
  234.     while (str[cons] != '\0') cons++;
  235.     cout << "String length: " << cons << endl;
  236. }
  237.  
  238. void CopyStr(char* str1, char* str2)
  239. {
  240.     int cons = 0;
  241.     while (str1[cons] != '\0') { str2[cons] = str1[cons]; cons++; }
  242.     str2[cons] = '\0';
  243. }
  244.  
  245. void BondStr(char* str1, char* str2)
  246. {
  247.     int cons = 0, cons1 = 0;
  248.     while (str1[cons] != '\0') cons++;
  249.     cout << "First string length: " << cons << endl;
  250.     str1[cons++] = ' ';
  251.     while (str2[cons1] != '\0') { str1[cons + cons1] = str2[cons1]; cons1++; }
  252.     str1[cons + cons1] = '\0';
  253.     cout << "Second string length: " << cons1 << endl;
  254.     cout << "Result: " << str1 << endl;
  255. }
  256.  
  257. void DelSpace(char* str1, char* str2)
  258. {
  259.     int cons = 0;
  260.     str2 = strtok(str1, " "); //поиск лексем
  261.     cout << "Result: ";
  262.     while (str2 != NULL)
  263.     {
  264.         cout << str2;
  265.         str2 = strtok(NULL, " ");
  266.         cons++;
  267.     }
  268.     cout << endl;
  269.     cout << "Numbers of spaces: " << cons - 1;
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement