Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. /*
  8. * File: Listenbearbeitung.cpp
  9. * Author: Thorsten
  10. *
  11. * Created on 16. März 2018, 12:19
  12. */
  13.  
  14. #include <cstdlib>
  15. #include <algorithm>
  16. #include <list>
  17. #include <iostream>
  18. #include <limits>
  19. #include <vector>
  20. #include <numeric>
  21.  
  22. using namespace std;
  23.  
  24. /*
  25. *
  26. */
  27.  
  28. int validInputInt() {
  29. int eingabe;
  30.  
  31. cin >> eingabe;
  32. while (cin.fail()) //Solange die Eingabe nicht eindeutig von Typ int ist (Sonderzeichen, etc.)
  33. {
  34. cin.clear(); //Fehlerstatus wird auf goodbit gesetzt, um weiterhin mit der Eingabe zu arbeiten
  35. cin.ignore(numeric_limits<streamsize>::max(), '\n'); //Eingabe wird bis zum newline character gelöscht
  36. cout << "Fehlerhafte Eingabe. Bitte eine gueltige Zahl eingeben: ";
  37. cin >> eingabe;
  38. }
  39. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  40. return eingabe;
  41. }
  42.  
  43. list<int> gemeinsameElemente (list<int>& liste_1, list<int>& liste_2)
  44. {
  45. list<int> liste_output;
  46.  
  47. liste_1.sort();
  48. liste_2.sort();
  49.  
  50. set_intersection(liste_1.begin(), liste_1.end(), liste_2.begin(), liste_2.end(), back_inserter(liste_output));
  51.  
  52. liste_output.unique();
  53.  
  54. return liste_output;
  55. }
  56.  
  57. bool keineGemeinsamenWerte (list<int>& liste_1, list<int>& liste_2)
  58. {
  59. list<int> liste_output = gemeinsameElemente (liste_1, liste_2);
  60.  
  61. return liste_output.size() == 0;
  62. }
  63.  
  64. bool hatGleicheAnzahl (list<int>& liste_1, list<int>& liste_2)
  65. {
  66. list<int> liste_1_unique = liste_1;
  67. liste_1_unique.unique();
  68. list<int> liste_2_unique = liste_2;
  69. liste_2_unique.unique();
  70. //Annahme: Es sei wahr. Widerlegung folgt ggf.
  71. bool hatGleicheAnzahl = true;
  72.  
  73. list<int>::iterator it;
  74. for (it=liste_1_unique.begin(); it != liste_1_unique.end(); ++it)
  75. {
  76. //cout << "*it_1: " << *it << endl;
  77. //If count (die Zahl bei liste 1) != count (bei liste 2)
  78. if( count(liste_1.begin(), liste_1.end(), *it) != count(liste_2.begin(), liste_2.end(), *it) )
  79. {
  80. hatGleicheAnzahl = false;
  81. }
  82. }
  83. for (it=liste_2_unique.begin(); it != liste_2_unique.end(); ++it)
  84. {
  85. //cout << "*it_2: " << *it << endl;
  86. //If count (die Zahl bei liste 2) != count (bei liste 1)
  87. if( count(liste_2.begin(), liste_2.end(), *it) != count(liste_1.begin(), liste_1.end(), *it) )
  88. {
  89. hatGleicheAnzahl = false;
  90. }
  91. }
  92.  
  93. return hatGleicheAnzahl;
  94. }
  95.  
  96. void fillWithUniques (list<int>& liste_1, list<int> liste_2)
  97. {
  98. liste_2.unique();
  99.  
  100. list<int>::iterator it;
  101. for (it=liste_2.begin(); it != liste_2.end(); ++it)
  102. {
  103. if (*find(liste_1.begin(), liste_1.end(), *it) != *it)
  104. {
  105. cout << "Zahl " << *it << " in Liste 1 nicht gefunden." << endl;
  106. liste_1.push_back(*it);
  107. }
  108. }
  109. liste_1.sort();
  110. }
  111.  
  112. int n_kleinstes_Element (list<int>& liste)
  113. {
  114. //In Vektor konvertieren wegen Random Access Iterator
  115. vector<int> zahlen{liste.begin(), liste.end()};
  116. cout << "Bitte n eingeben, um das n-kleinste Element zu erhalten: " << endl;
  117. int n = validInputInt()-1;
  118.  
  119. while (n >= zahlen.size())
  120. {
  121. cout << "n ist out of bounds. Bitte ein n zwischen 1 und " << zahlen.size() << " eingeben: " << endl;
  122. n = validInputInt()-1;
  123. }
  124.  
  125. nth_element(zahlen.begin(), zahlen.begin()+n, zahlen.end());
  126.  
  127. return zahlen[n];
  128. }
  129.  
  130. int rekursiv_vektor_fuellen (vector<int>& zahlen, int n)
  131. {
  132. if (n <= 0)
  133. {
  134. return(n);
  135. }
  136.  
  137. zahlen[n] = accumulate(zahlen.begin(), zahlen.begin()+n+1, 0);
  138.  
  139. return rekursiv_vektor_fuellen(zahlen, n-1);
  140. }
  141.  
  142. list<int> n_tes_Element_Summe (list<int>& liste)
  143. {
  144. //In Vektor konvertieren wegen Random Access Iterator
  145. vector<int> zahlen{liste.begin(), liste.end()};
  146. cout << "Bitte n eingeben, um die Summe der ersten n Elemente zu erhalten: " << endl;
  147. int n = validInputInt()-1;
  148.  
  149. while (n >= zahlen.size())
  150. {
  151. cout << "n ist out of bounds. Bitte ein n zwischen 1 und " << zahlen.size() << " eingeben: " << endl;
  152. n = validInputInt()-1;
  153. }
  154.  
  155. rekursiv_vektor_fuellen(zahlen, n);
  156.  
  157. list<int> output;
  158. copy(zahlen.begin(), zahlen.begin()+n+1, back_inserter(output));
  159.  
  160. return output;
  161. }
  162.  
  163. void outputList (list<int>& liste)
  164. {
  165. //Liste ausgeben
  166. list<int>::iterator it;
  167. for (it=liste.begin(); it != liste.end(); ++it)
  168. {
  169. cout << *it << '\t';
  170. }
  171. cout << endl << endl;
  172. }
  173.  
  174. int main() {
  175.  
  176. list<int> liste_1 = {2, 5, 5, 3, 8, 8, 10};
  177. list<int> liste_2 = {5, 5, 10, 8, 8, 3, 2, 12};
  178.  
  179. list<int> liste_3 = {10, 5, 7, 2, 8, 4};
  180. list<int> liste_output = gemeinsameElemente (liste_1, liste_2);
  181.  
  182. cout << "Gemeinsame Elemente von Liste 1 und Liste 2: " << endl << endl;
  183. outputList(liste_output);
  184.  
  185. cout << boolalpha << "Liste 1 & Liste 2 haben keine gemeinsamen Werte -> " << keineGemeinsamenWerte (liste_1, liste_2) << endl << endl;
  186.  
  187. cout << boolalpha << "Gleiche Anzahl aller Werte der ersten Liste in der zweiten Liste und umgekehrt? " << hatGleicheAnzahl(liste_1, liste_2) << endl << endl;
  188.  
  189. fillWithUniques (liste_1, liste_2);
  190. cout << "Neue Liste 1: " << endl << endl;
  191. outputList(liste_1);
  192.  
  193. cout << "n-kleinstes Element: " << n_kleinstes_Element(liste_3) << endl;
  194.  
  195. list<int> liste_summe = n_tes_Element_Summe (liste_3);
  196.  
  197. outputList(liste_summe);
  198.  
  199. return 0;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement