nikitaxe132

lab3.1

Nov 20th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.72 KB | None | 0 0
  1. #include <iostream>
  2. #include<fstream>
  3. #include<string>
  4. #include <set>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. bool Choic() {
  10.     bool IsCorrect = true;
  11.     string Choice;
  12.     bool Console = true;
  13.     while(IsCorrect) {
  14.         cout << "If you want to enter data from the console, enter Y or y, and if from the file, enter N or n\n";
  15.         cin >> Choice;
  16.         if ((Choice == "Y") || (Choice == "y") || (Choice == "N") || (Choice == "n")) {
  17.             IsCorrect = false;
  18.             if ((Choice == "Y") || (Choice == "y")) {
  19.                 Console = true;
  20.             }
  21.             else {
  22.                 Console = false;
  23.             }
  24.         }
  25.         else {
  26.             IsCorrect = true;
  27.             cout << "This is mistake\n";
  28.         }
  29.     }
  30.     return Console;
  31. }
  32.  
  33. string EnterText() {
  34.     cout << "Enter the text. All special characters and numbers such as 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, /, -, _, =, +, @, #, $, %, ^, &, *, (, ) will be deleted.\n";
  35.     string Text;
  36.     while (getline(cin, Text))
  37.         return  Text;
  38. }
  39.  
  40. string EnterFileText() {
  41.     ifstream fin("D:\\input.txt");
  42.     string Text;
  43.     while (getline(fin, Text))
  44.         return  Text;
  45. }
  46.  
  47. string EditText(bool Choise) {
  48.     set<char> InvalidValues {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '"', ']', '}', '}', '[', '/', '-', '_', '=', '+', '@', '#', '$', '%', '^', '&', '*', '(', ')'};
  49.     set<char> Values {'!', ';', ':', ',', '.', '?'};
  50.     string Text;
  51.     if (Choise) {
  52.         Text = EnterText();
  53.     }
  54.     else {
  55.         Text = EnterFileText();
  56.     }
  57.     int Lengt;
  58.     int I = 0;
  59.     Lengt = size(Text);
  60.     while (I <= Lengt) {
  61.         if (InvalidValues.count(Text[I])) {
  62.             Text.erase(I,1);
  63.             I--;
  64.             Lengt--;
  65.         }
  66.         if ((Text[I] == ' ') && (Text[I] + 1 == ' ')){
  67.             Text.erase(I,1);
  68.             I--;
  69.             Lengt--;
  70.         }
  71.         if ((Values.count(Text[I])) && (Values.count(Text[I + 1]))) {
  72.             Text.erase(I,1);
  73.             I--;
  74.             Lengt--;
  75.         }
  76.         I++;
  77.     }
  78.     return Text;
  79. }
  80.  
  81. void  Encryption(bool Choice) {
  82.     ofstream fout("D:\\output.txt");
  83.     set<char> Values {'!', ';', ':', ',', '.', '?'};
  84.     string Text = EditText(Choice);
  85.     int Lengt = size(Text);
  86.     int Count = 0;
  87.     int I = 0;
  88.     string Temp;
  89.     int Parity = 0;
  90.     while (I < Lengt) {
  91.         Count++;
  92.         if (Text[I] == ' ') {
  93.             Parity++;
  94.             if (Parity % 2 == 0) {
  95.                 Temp = Text.substr(I - (Count - 1), Count - 1);
  96.                 for (int J = 0; J < Count; J++) {
  97.                     Temp[J] = toupper(Temp[J]);
  98.                 }
  99.                 Text.erase(I - (Count - 1), Count - 1);
  100.                 Text.insert(I - (Count - 1), Temp);
  101.                 Count = 0;
  102.             }
  103.             else {
  104.                 Text.insert(I - (Count - 1), 1, '(');
  105.                 Text.insert(I + 1, 1, ')');
  106.                 Lengt = Lengt + 2;
  107.                 I = I + 2;
  108.                 Count = 0;
  109.             }
  110.             if (Values.count(Text[I + 1])) {
  111.                 I++;
  112.             }
  113.         }
  114.         if ((Values.count(Text[I])) && (I < Lengt) && (Text[I - 1] != ' ')) {
  115.             Parity++;
  116.             if (Parity % 2 == 0) {
  117.                 Temp = Text.substr(I - (Count - 1), Count - 1);
  118.                 for (int J = 0; J < Count; J++) {
  119.                     Temp[J] = toupper(Temp[J]);
  120.                 }
  121.                 Text.erase(I - (Count - 1), Count - 1);
  122.                 Text.insert(I - (Count - 1), Temp);
  123.                 Count = 0;
  124.             }
  125.             else {
  126.                 Text.insert(I - (Count - 1), 1, '(');
  127.                 Text.insert(I + 1, 1, ')');
  128.                 Lengt = Lengt + 2;
  129.                 I = I + 2;
  130.                 Count = 0;
  131.             }
  132.             if (Values.count(Text[I + 1])) {
  133.                 I++;
  134.             }
  135.         }
  136.         if ((I == Lengt) && (Values.count(Text[I]))) {
  137.             Parity++;
  138.             if (Parity % 2 == 0) {
  139.                 Temp = Text.substr(I - (Count - 1), Count - 1);
  140.                 for (int J = 0; J < Count; J++) {
  141.                     Temp[J] = toupper(Temp[J]);
  142.                 }
  143.                 Text.erase(I - (Count - 1), Count - 1);
  144.                 Text.insert(I - (Count - 1), Temp);
  145.                 Count = 0;
  146.             }
  147.             else {
  148.                 Lengt = Lengt + 2;
  149.                 Text.insert(I - (Count - 1), 1, '(');
  150.                 Text.insert(I + 1, 1, ')');
  151.                 I = I + 2;
  152.                 Count = 0;
  153.             }
  154.         }
  155.         else {
  156.             if (I == Lengt - 1) {
  157.                 Parity++;
  158.                 if (Parity % 2 == 0) {
  159.                     Temp = Text.substr(I - Count, Count + 1);
  160.                     for (int J = 0; J < Count; J++) {
  161.                         Temp[J] = toupper(Temp[J]);
  162.                     }
  163.                     Text.erase(I - Count, Count + 1);
  164.                     Text.insert(I - Count, Temp);
  165.                     Count = 0;
  166.                 }
  167.                 else {
  168.                     Lengt = Lengt + 2;
  169.                     Text.insert(I - (Count - 1), 1, '(');
  170.                     Text.insert(I + 2, 1, ')');
  171.                     I = I + 2;
  172.                     Count = 0;
  173.                 }
  174.             }
  175.         }
  176.         I++;
  177.     }
  178.     if (Choice) {
  179.         cout << Text;
  180.     } else {
  181.         cout << Text;
  182.         fout << Text;
  183.     }
  184. }
  185.  
  186. void body() {
  187.     bool Choice = Choic();
  188.     Encryption(Choice);
  189. }
  190.  
  191. int main() {
  192.     cout << "This program in the text in every even word replaces all lowercase alphabetic characters with uppercase, and each odd word is enclosed in parentheses";
  193.     body();
  194.     return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment