MSzopa

C++ 25.02.2022 Anagramy, Jednolite

Feb 25th, 2022
1,012
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. std::string SortowanieBabelkowe(std::string s) {
  5.     for (int i = 0; i < s.size(); i++) {
  6.         for (int j = i; j < s.size(); j++) {
  7.             if (s[j] < s[i])
  8.                 std::swap(s[j], s[i]);
  9.         }
  10.     }
  11.     return s;
  12. }
  13. std::string StringToLower(std::string text) {
  14.     for (int i = 0; i < text.size(); i++) {
  15.         text[i] = tolower(text[i]);
  16.     }
  17.     return text;
  18. }
  19. bool CzyAnagram(std::string a, std::string b) {
  20.     if (a.size() == b.size())
  21.         return SortowanieBabelkowe(StringToLower(a)) == SortowanieBabelkowe(StringToLower(b));
  22.     return false;
  23. }
  24. bool CzyJednolity(std::string text){
  25.     if (text.size() > 0) {
  26.         char litera = text[0];
  27.         for (int i = 1; i < text.size(); i++) {
  28.             if (litera != text[i])
  29.                 return false;
  30.         }
  31.     }
  32.     return true;
  33. }
  34. bool CzyJednolityAnagram(std::string a, std::string b) {
  35.     if (a.size() > 0 && b.size() > 0) {
  36.         if (CzyJednolity(a) && CzyJednolity(b)) {
  37.             return (a.size() == b.size()) && (a[0] == b[0]);
  38.         }
  39.         return false;
  40.     }
  41.     return true;
  42. }
  43. int main()
  44. {
  45.     std::ifstream plik;
  46.     plik.open("dane_napisy.txt");
  47.     int zad1 = 0, zad2 = 0;
  48.     while (plik) {
  49.         std::string a, b;
  50.         plik >> a >> b;
  51.         if (a != "" && b != "") {
  52.             if (CzyJednolityAnagram(a, b))
  53.                 zad1++;
  54.             if (CzyAnagram(a, b))
  55.                 zad2++;
  56.         }
  57.     }
  58.     std::cout << zad1 << " " << zad2 << std::endl;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment