Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LabProgramming4.0.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
- //
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <fstream>
- #include <cstring>
- #include <ctime>
- #include <cctype>
- #include <limits>
- #include <stdio.h>
- using namespace std;
- void cleaning(); //очищение
- void Stringlength(char* str); //длина строки
- void CopyStr(char* str1, char* str2); //копирование строк
- void BondStr(char* str1, char* str2); //склеивание строк
- void DelSpace(char* str1, char* str2); //удаление пробелов из строки
- int main()
- {
- int choice;
- cout << "Enter task number (from 1 to 10), or enter 0 to exit: "; cin >> choice;
- if (choice == 1) //Task 1
- {
- char a[100][100], b[100][100], c[100][100], d[100][100];
- char str[100];
- srand(time(NULL));
- fstream f("file.txt", ios::in);
- int i = 0;
- while (f >> str)
- {
- strcpy_s(a[i], str); i++;
- }
- int na = i;
- for (i = 0; i < na; i++)
- {
- cout << a[i] << endl;
- }
- cout << endl;
- fstream g("file1.txt", ios::in);
- i = 0;
- while (g >> str)
- {
- strcpy_s(b[i], str); i++;
- }
- int nb = i;
- for (i = 0; i < nb; i++)
- {
- cout << b[i] << endl;
- }
- cout << endl;
- fstream h("file2.txt", ios::in);
- i = 0;
- while (h >> str)
- {
- strcpy_s(c[i], str); i++;
- }
- int nc = i;
- for (i = 0; i < nc; i++)
- {
- cout << c[i] << endl;
- }
- cout << endl;
- fstream p("file3.txt", ios::in);
- i = 0;
- while (p >> str)
- {
- strcpy_s(d[i], str); i++;
- }
- int nd = i;
- for (i = 0; i < nd; i++)
- {
- cout << d[i] << endl;
- }
- cout << endl;
- int ka = rand() % na; //заголовок
- cout << a[ka] << " ";
- int kb = rand() % nb; //существительные
- cout << b[kb] << " ";
- int kc = rand() % nc; //глаголы
- cout << c[kc] << " ";
- int kd = rand() % nd; //предлоги
- cout << d[kd] << " ";
- ka = rand() % na; //заголовок
- cout << a[ka] << " ";
- kb = rand() % nb; //существительные
- cout << b[kb] << " ";
- cout << endl;
- }
- if (choice == 2) // Task 2
- {
- int n;
- cout << "Enter the size of the sequence: "; cin >> n;
- n += 1;
- cleaning();
- char* arr1 = new char[n];
- char* arr2 = new char[n];
- char* arr3 = new char[n];
- char* arr4 = new char[n];
- char* arr5 = new char[n];
- cout << "Enter more than 4 digits: "; cin.getline(arr1, 100);
- int z = strlen(arr1);
- if (z >= 4) {
- int string = z / 4, string1 = string, string2 = string, cons = 0, a, b, c, d;
- for (int i = 0; i < string; i++) { arr2[i] = arr1[i]; }
- string += string1;
- for (int i = string1; i < string; i++) { arr3[cons] = arr1[i]; cons++; }
- string += string1;
- string2 += string1;
- cons = 0;
- for (int i = string2; i < string; i++) { arr4[cons] = arr1[i]; cons++; }
- cons = 0;
- string2 += string1;
- for (int i = string2; i < z; i++) { arr5[cons] = arr1[i]; cons++; }
- a = atoi(arr2); b = atoi(arr3); c = atoi(arr4); d = atoi(arr5);
- cout << a << " " << b << " " << c << " " << d << endl;
- if (a == b) cout << "Your answer: 1\n"; if (c == d) cout << "Your answer: 1\n";
- cout << "Your answers: \n";
- if (a < b) cout << a << "/" << b << endl;
- if (a > b) cout << b << "/" << a << endl;
- if (c < d) cout << c << "/" << d << endl;
- if (c > d) cout << d << "/" << c << endl;
- }
- else { cout << "Error, less than 4 numbers were entered (x_x)"; return 0; }
- delete[]arr1;
- delete[]arr2;
- delete[]arr3;
- delete[]arr4;
- delete[]arr5;
- }
- if (choice == 3) // Task 3
- {
- int n;
- cout << "Enter the length of the string: "; cin >> n; n += 1;
- char* str = new char[n];
- cleaning();
- cout << "Enter your string: "; cin.getline(str, n); cout << "Your string: " << str << endl;
- Stringlength(str);
- delete[]str;
- }
- if (choice == 4) // Task 4
- {
- int n;
- cout << "Enter the length of the string: "; cin >> n; n += 1;
- char* str1 = new char[n];
- char* str2 = new char[n];
- cleaning();
- cout << "Enter your first string: "; cin.getline(str1, n);
- cout << endl;
- CopyStr(str1, str2);
- cout << "First string: " << str1 << endl;
- cout << "Second string: " << str2 << endl;
- delete[]str1;
- delete[]str2;
- }
- if (choice == 5) // Task 5
- {
- int n;
- cout << "Enter the length of the string: "; cin >> n; n += 1;
- char* str1 = new char[n];
- char* str2 = new char[n];
- cleaning();
- cout << "Enter first string: "; cin.getline(str1, n);
- cout << "Enter second string: "; cin.getline(str2, n);
- BondStr(str1, str2);
- delete[]str1;
- delete[]str2;
- }
- if (choice == 6) // Task 6
- {
- int n, cons = 0;
- cout << "Enter the length of the string: "; cin >> n; n += 1;
- char* str1 = new char[n];
- char* str2 = new char[n];
- cleaning();
- cout << "Enter string: "; cin.getline(str1, n);
- cout << endl;
- cout << "Original string: " << str1 << endl;
- DelSpace(str1, str2);
- delete[]str1;
- delete[]str2;
- }
- if (choice == 7) // Task 7
- {
- }
- if (choice == 8) // Task 8 - Не доделано
- {
- }
- if (choice == 9) // Task 9
- {
- }
- if (choice == 10) // Task 10
- {
- }
- }
- void cleaning()
- {
- cin.clear();
- while (cin.get() != '\n');
- }
- void Stringlength(char* str)
- {
- int cons = 0;
- while (str[cons] != '\0') cons++;
- cout << "String length: " << cons << endl;
- }
- void CopyStr(char* str1, char* str2)
- {
- int cons = 0;
- while (str1[cons] != '\0') { str2[cons] = str1[cons]; cons++; }
- str2[cons] = '\0';
- }
- void BondStr(char* str1, char* str2)
- {
- int cons = 0, cons1 = 0;
- while (str1[cons] != '\0') cons++;
- cout << "First string length: " << cons << endl;
- str1[cons++] = ' ';
- while (str2[cons1] != '\0') { str1[cons + cons1] = str2[cons1]; cons1++; }
- str1[cons + cons1] = '\0';
- cout << "Second string length: " << cons1 << endl;
- cout << "Result: " << str1 << endl;
- }
- void DelSpace(char* str1, char* str2)
- {
- int cons = 0;
- str2 = strtok(str1, " "); //поиск лексем
- cout << "Result: ";
- while (str2 != NULL)
- {
- cout << str2;
- str2 = strtok(NULL, " ");
- cons++;
- }
- cout << endl;
- cout << "Numbers of spaces: " << cons - 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement