AmateurKid

Untitled

Dec 25th, 2021
1,766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     char answ;
  10.     string temp;
  11.     string name1, name2;
  12.     do {
  13.         int count_fin = 0;
  14.  
  15.         int file1_arr[256] = { 0 };
  16.         int file2_arr[256] = { 0 };
  17.  
  18.         cout << "Input a filename of 1st file>";
  19.         ifstream file1;
  20.         getline(cin, name1);
  21.         file1.open(name1+".txt");
  22.         while (!file1.is_open()) {
  23.             cout << "Can't open your file\n";
  24.             cout << "Input a filename of 1st file>";
  25.             getline(cin, name1);
  26.             file1.open(name1+".txt");
  27.         }
  28.  
  29.         while (!file1.eof()) {
  30.                 getline(file1,temp);
  31.                 for(int g = 0; g < temp.length(); g ++){
  32.                     file1_arr[(int)temp[g]] += 1;
  33.                  }
  34.                 temp.clear();
  35.          }
  36.         file1.close();
  37.  
  38.         cout << "Input a filename of 2nd file>";
  39.         ifstream file2;
  40.         getline(cin, name2);
  41.         file2.open(name2 + ".txt");
  42.  
  43.         while (!file2.is_open()) {
  44.             cout << "Can't open your file\n";
  45.             cout << "Input a filename of 2st file>";
  46.             getline(cin, name2);
  47.             file2.open(name2 + ".txt");
  48.         }
  49.  
  50.         while (!file2.eof()) {
  51.             getline(file2, temp);
  52.             for (int g = 0; g < temp.length(); g++) {
  53.                     file2_arr[(int)temp[g]] += 1;
  54.                     }
  55.  
  56.             temp.clear();
  57.             }
  58.         file2.close();
  59.  
  60.         name1.clear();
  61.         name2.clear();
  62.  
  63.  
  64.  
  65.         for (int i = 0; i < 255; i++) {
  66.             if (file1_arr[i] == file2_arr[i]) {
  67.                 count_fin += 1;
  68.             }
  69.  
  70.         }
  71.  
  72.        
  73.         /*cout << count_fin << endl;
  74.  
  75.         for (int i = 0; i < 255; i++) {
  76.             cout << file1_arr[i];
  77.         }
  78.         cout << endl;
  79.  
  80.         for (int i = 0; i < 255; i++) {
  81.             cout << file2_arr[i];
  82.         }*/
  83.  
  84.  
  85.         if (count_fin == 255) {
  86.             cout << "\nIt's possible to rearrange characters in the first file to get the second file";
  87.         }
  88.         else {
  89.             cout << "\nIt's impossible to rearrange characters in the first file to get the second file";
  90.         }
  91.  
  92.         cout << "\nContinue? (Y/N)>";
  93.         cin >> answ;
  94.         while (cin.fail() || cin.peek() != '\n' || (answ!='y' && answ != 'Y' && answ != 'n' && answ != 'N')) {
  95.             cin.clear();
  96.             cin.ignore(10000, '\n');
  97.             cout << "\nContinue? (Y/N)>";
  98.             cin >> answ;
  99.         }
  100.         cin.ignore(10000, '\n');
  101.     } while (answ == 'y' || answ == 'Y');
  102.  
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment