Advertisement
SeriousVenom

lab$$$

Jun 8th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.24 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. const int N = 1001;
  16. void cleaning(); //очищение
  17. void Stringlength(char* str); //длина строки
  18. void CopyStr(char* str1, char* str2); //копирование строк
  19. void BondStr(char* str1, char* str2); //склеивание строк
  20. void DelSpace(char* str1, char* str2); //удаление пробелов из строки
  21. void CompStr(char* str1, char* str2); //символьное сравнение строк
  22. int Polindrom(char* str1); //полиндром
  23. void LowReg(char* str); //замена регистра символов
  24. void DelSpace2(char* str1, char* str2); //удаление пробелов для 8 задания
  25. void DelPun(char* str1); //удаление знаков препинания
  26. void Count(char* str1);
  27.  
  28. int main()
  29. {
  30.     int choice;
  31.     cout << "Enter task number (from 1 to 10), or enter 0 to exit: "; cin >> choice;
  32.    
  33.     if (choice == 1) //Task 1
  34.     {
  35.         char a[100][100], b[100][100], c[100][100], d[100][100];
  36.         char str[100];
  37.         srand(time(NULL));
  38.        
  39.         fstream f("file.txt", ios::in);
  40.         int i = 0;
  41.  
  42.         while (f >> str)
  43.         {
  44.             strcpy_s(a[i], str); i++;
  45.         }
  46.         int na = i;
  47.         for (i = 0; i < na; i++)
  48.         {
  49.             cout << a[i] << endl;
  50.         }
  51.         cout << endl;
  52.                
  53.         fstream g("file1.txt", ios::in);
  54.         i = 0;
  55.  
  56.         while (g >> str)
  57.         {
  58.             strcpy_s(b[i], str); i++;
  59.         }
  60.         int nb = i;
  61.         for (i = 0; i < nb; i++)
  62.         {
  63.             cout << b[i] << endl;
  64.         }
  65.         cout << endl;
  66.                
  67.         fstream h("file2.txt", ios::in);
  68.         i = 0;
  69.  
  70.         while (h >> str)
  71.         {
  72.             strcpy_s(c[i], str); i++;
  73.         }
  74.         int nc = i;
  75.         for (i = 0; i < nc; i++)
  76.         {
  77.             cout << c[i] << endl;
  78.         }
  79.         cout << endl;
  80.                
  81.         fstream p("file3.txt", ios::in);
  82.         i = 0;
  83.  
  84.         while (p >> str)
  85.         {
  86.             strcpy_s(d[i], str); i++;
  87.         }
  88.         int nd = i;
  89.         for (i = 0; i < nd; i++)
  90.         {
  91.             cout << d[i] << endl;
  92.         }
  93.         cout << endl;
  94.        
  95.  
  96.         int ka = rand() % na; //заголовок
  97.         cout << a[ka] << " ";
  98.  
  99.         int kb = rand() % nb; //существительные
  100.         cout << b[kb] << " ";
  101.  
  102.         int kc = rand() % nc; //глаголы
  103.         cout << c[kc] << " ";
  104.  
  105.         int kd = rand() % nd; //предлоги
  106.         cout << d[kd] << " ";
  107.  
  108.         ka = rand() % na; //заголовок
  109.         cout << a[ka] << " ";
  110.  
  111.         kb = rand() % nb; //существительные
  112.         cout << b[kb] << " ";
  113.  
  114.         cout << endl;
  115.     }
  116.        
  117.     if (choice == 2) // Task 2
  118.     {
  119.         int n;
  120.         cout << "Enter the size of the sequence: "; cin >> n;
  121.         n += 1;
  122.         cleaning();
  123.         char* arr1 = new char[n];
  124.         char* arr2 = new char[n];
  125.         char* arr3 = new char[n];
  126.         char* arr4 = new char[n];
  127.         char* arr5 = new char[n];
  128.  
  129.         cout << "Enter more than 4 digits: "; cin.getline(arr1, 100);
  130.         int z = strlen(arr1);
  131.         if (z >= 4) {
  132.             int string = z / 4, string1 = string, string2 = string, cons = 0, a, b, c, d;
  133.             for (int i = 0; i < string; i++) { arr2[i] = arr1[i]; }
  134.             string += string1;
  135.             for (int i = string1; i < string; i++) { arr3[cons] = arr1[i]; cons++; }
  136.             string += string1;
  137.             string2 += string1;
  138.             cons = 0;
  139.             for (int i = string2; i < string; i++) { arr4[cons] = arr1[i]; cons++; }
  140.             cons = 0;
  141.             string2 += string1;
  142.             for (int i = string2; i < z; i++) { arr5[cons] = arr1[i]; cons++; }
  143.             a = atoi(arr2); b = atoi(arr3); c = atoi(arr4); d = atoi(arr5);
  144.             cout << a << " " << b << " " << c << " " << d << endl;
  145.             if (a == b) cout << "Your answer: 1\n"; if (c == d) cout << "Your answer: 1\n";
  146.             cout << "Your answers: \n";
  147.             if (a < b) cout << a << "/" << b << endl;
  148.             if (a > b) cout << b << "/" << a << endl;
  149.             if (c < d) cout << c << "/" << d << endl;
  150.             if (c > d) cout << d << "/" << c << endl;
  151.  
  152.         }
  153.         else { cout << "Error, less than 4 numbers were entered (x_x)"; return 0; }
  154.             delete[]arr1;
  155.             delete[]arr2;
  156.             delete[]arr3;
  157.             delete[]arr4;
  158.             delete[]arr5;
  159.     }
  160.     if (choice == 3) // Task 3
  161.     {
  162.         int n;
  163.         cout << "Enter the length of the string: "; cin >> n; n += 1;
  164.         char* str = new char[n];
  165.         cleaning();
  166.         cout << "Enter your string: "; cin.getline(str, n); cout << "Your string: " << str << endl;
  167.         Stringlength(str);
  168.             delete[]str;
  169.     }
  170.     if (choice == 4) // Task 4
  171.     {
  172.         int n;
  173.         cout << "Enter the length of the string: "; cin >> n; n += 1;
  174.         char* str1 = new char[n];
  175.         char* str2 = new char[n];
  176.         cleaning();
  177.         cout << "Enter your first string: "; cin.getline(str1, n);
  178.         cout << endl;
  179.         CopyStr(str1, str2);
  180.         cout << "First string: " << str1 << endl;
  181.         cout << "Second string: " << str2 << endl;
  182.             delete[]str1;
  183.             delete[]str2;
  184.     }
  185.     if (choice == 5) // Task 5
  186.     {
  187.         int n;
  188.         cout << "Enter the length of the string: "; cin >> n; n += 1;
  189.         char* str1 = new char[n];
  190.         char* str2 = new char[n];
  191.         cleaning();
  192.         cout << "Enter first string: "; cin.getline(str1, n);
  193.         cout << "Enter second string: "; cin.getline(str2, n);
  194.         BondStr(str1, str2);
  195.             delete[]str1;
  196.             delete[]str2;
  197.     }
  198.     if (choice == 6) // Task 6
  199.     {
  200.         int n, cons = 0;
  201.         cout << "Enter the length of the string: "; cin >> n; n += 1;
  202.         char* str1 = new char[n];
  203.         char* str2 = new char[n];
  204.         cleaning();
  205.         cout << "Enter string: "; cin.getline(str1, n);
  206.         cout << endl;
  207.     //  cout << "Original string: " << str1 << endl;
  208.         DelSpace(str1, str2);
  209.         cout << endl;
  210.         cout << "Original string: " << str2 << endl;
  211.             delete[]str1;
  212.             delete[]str2;
  213.     }
  214.     if (choice == 7) // Task 7
  215.     {
  216.         int n;
  217.         cout << "Enter the length of the string: "; cin >> n; n += 1;
  218.         char* str1 = new char[n];
  219.         char* str2 = new char[n];
  220.         cleaning();
  221.         cout << "Enter first string: "; cin.getline(str1, n);
  222.         cout << "Enter second string: "; cin.getline(str2, n);
  223.         CompStr(str1, str2);
  224.             delete[]str1;
  225.             delete[]str2;
  226.     }
  227.     if (choice == 8) // Task 8
  228.     {
  229.         int n;
  230.         cout << "Enter the length of the string: "; cin >> n; n += 1;
  231.         char* str1 = new char[n];
  232.         char* str2 = new char[n];
  233.         cleaning();
  234.         cout << "Enter string: "; cin.getline(str1, n);
  235.         LowReg(str1);
  236.         DelPun(str1);
  237.         DelSpace2(str1, str2);
  238.         cout << endl;
  239.         if (Polindrom(str1) == 1) cout << "Answer: 1 --> This is polindrom";
  240.         else cout << "Answer: 0 --> This is not polindrom";
  241.  
  242.         delete[]str1;
  243.         delete[]str2;
  244.  
  245.     }
  246.     if (choice == 9) // Task 9
  247.     {
  248.         FILE* simpletext;
  249.         char* str1 = new char[N];
  250.         int cons, z = 0;
  251.         simpletext = fopen("9_task.txt", "rb");
  252.  
  253.         if (simpletext != NULL)
  254.         {
  255.             cons = fread(str1, 1, N, simpletext);
  256.             cout << "Number symbols in file: " << cons << endl;
  257.             Count(str1);
  258.         }
  259.         else cout << "#Error# File not found" << endl;
  260.  
  261.         delete[]str1;
  262.         return 0;
  263.     }
  264.     if (choice == 10) // Task 10
  265.     {
  266.  
  267.     }
  268. }
  269.  
  270. void cleaning()
  271. {
  272.     cin.clear();
  273.     while (cin.get() != '\n');
  274. }
  275.  
  276. void Stringlength(char* str)
  277. {
  278.     int cons = 0;
  279.     while (str[cons] != '\0') cons++;
  280.     cout << "String length: " << cons << endl;
  281. }
  282.  
  283. void CopyStr(char* str1, char* str2)
  284. {
  285.     int cons = 0;
  286.     while (str1[cons] != '\0') { str2[cons] = str1[cons]; cons++; }
  287.     str2[cons] = '\0';
  288. }
  289.  
  290. void BondStr(char* str1, char* str2)
  291. {
  292.     int cons = 0, cons1 = 0;
  293.     while (str1[cons] != '\0') cons++;
  294.     cout << "First string length: " << cons << endl;
  295.     str1[cons++] = ' ';
  296.     while (str2[cons1] != '\0') { str1[cons + cons1] = str2[cons1]; cons1++; }
  297.     str1[cons + cons1] = '\0';
  298.     cout << "Second string length: " << cons1 << endl;
  299.     cout << "Result: " << str1 << endl;
  300. }
  301.  
  302. void DelSpace(char* str1, char* str2)
  303. {
  304.     int cons = 0;
  305.     str2 = strtok(str1, " "); //поиск лексем
  306.     cout << "Result: ";
  307.     while (str2 != NULL)
  308.     {
  309.         cout << str2;
  310.         str2 = strtok(NULL, " ");
  311.         cons++;
  312.     }
  313.     cout << endl;
  314.     cout << "Numbers of spaces: " << cons - 1;
  315. }
  316.  
  317. void CompStr(char* str1, char* str2)
  318. {
  319.     int cons = 0;
  320.     while (str1[cons] != '\0' && str2[cons] != '\0') cons++;
  321.     if (str1[cons] == '\0' && str2[cons] == '\0') cout << "Answer: 0 --> Strings are the same" << endl;
  322.     if (str1[cons] != '\0' && str2[cons] == '\0') cout << "Answer: 1 --> The first line is larger than the second" << endl;
  323.     if (str1[cons] == '\0' && str2[cons] != '\0') cout << "Answer: -1 --> The first line is less than the second" << endl;
  324. }
  325.  
  326. int Polindrom(char* str1)
  327. {
  328.     int n = strlen(str1);
  329.     bool flag = true;
  330.     for (int i = 0; i < n / 2; i++)
  331.     {
  332.         if (str1[i] != str1[n - 1 - i]) flag = false;
  333.     }
  334.     if (flag == false) return 0;
  335.     return 1;
  336. }
  337.  
  338. void LowReg(char* str)
  339. {
  340.     while (*str)
  341.     {
  342.         *str = towlower(*str);
  343.         str++;
  344.     }
  345. }
  346.  
  347. void DelSpace2(char* str1, char* str2)
  348. {
  349.     str2 = strtok(str1, " ");
  350.     while (str2 != NULL)
  351.     {
  352.         cout << str2;
  353.         str2 = strtok(NULL, " ");
  354.     }
  355. }
  356.  
  357. void DelPun(char* str1)
  358. {
  359.  
  360.  
  361.  
  362.     /*int cons = strlen(str1);
  363.     for (int i = 0; i < cons; i++)
  364.     {
  365.         if (str1[i] == '.' || str1[i] == ',' || str1[i] == '!' || str1[i] == '?' || str1[i] == ':' || str1[i] == ';' || str1[i] == '_')
  366.             str1[i] = ' ';
  367.     }*/
  368.  
  369. }
  370.  
  371. void Count(char* str1)
  372. {
  373.     int cons = 0;
  374.     fstream f("9_task.txt");
  375.     if (!f) { cout << "#Error# File not found" << endl; }
  376.     else
  377.     {
  378.         while (f.getline(str1, N))
  379.         {
  380.             cons++;
  381.         }
  382.     }
  383.     f.close();
  384.     cout << "Numbers of string: " << cons - 1 << endl;
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement