Advertisement
DearOohDeer

Algorytm Eucalidesa

Mar 7th, 2022
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <unistd.h>
  3. #include <cmath>
  4. #include <string>
  5. #include <limits>
  6. using namespace std;
  7.  
  8.  
  9. int FGiveNumber()
  10. { int NumberGiven = 0;
  11. while (NumberGiven == 0)
  12. {
  13. cout << "Podaj liczbe: ";
  14. while (!(cin >> NumberGiven)) {
  15. cin.clear();
  16. std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  17. cout << endl << "Nie podano liczby. Sprobuj ponownie: "; //Musi byc endl bo inaczej tekst jest fuckd
  18. }
  19. if (NumberGiven == 0)
  20. {
  21. cout <<"Podana liczba musi byc rozna od 0. "<<endl;
  22. }
  23. }
  24. return NumberGiven;
  25. }
  26.  
  27. bool FGiveYesOrNo()
  28. {
  29. char GivenChar = 0;
  30. do{
  31. cout <<"Podaj y lub n: ";
  32. cin >> GivenChar;
  33. if (GivenChar != 'y' && GivenChar != 'n')
  34. {
  35. cout << "Nie podano poprawnej opcji! ";
  36. }
  37. }while( GivenChar != 'y' && GivenChar != 'n');
  38. if (GivenChar == 'y' || GivenChar =='Y')
  39. {
  40. return true;
  41. }
  42. else
  43. {
  44. return false;
  45. }
  46. }
  47.  
  48. void FSubMethod()
  49. {
  50. int HowLongItTook=0;
  51. int SleepTime;
  52. cout << "Podaj Pierwsza liczbe. ";
  53. int FirstNumber = FGiveNumber();
  54. cout << "Podaj Druga liczbe. ";
  55. int SecondNumber = FGiveNumber();
  56. cout << "Czy chcesz aby program poczekal z wynikami?."<<endl;
  57. if (FGiveYesOrNo())
  58. {
  59. cout << "Podaj czas czekania. ";
  60. SleepTime = FGiveNumber();
  61. }
  62. else SleepTime = 0;
  63.  
  64. int BackUpFirst = FirstNumber;
  65. int BackUpSecond = SecondNumber;
  66.  
  67. cout << endl;
  68. while (SecondNumber != FirstNumber) {
  69. if (SecondNumber > FirstNumber) {
  70. cout << "NWD(" << BackUpFirst <<","<< BackUpSecond <<") "<< SecondNumber << ">" << FirstNumber <<" -> "<<SecondNumber<<"-"<< FirstNumber<<"=";
  71. SecondNumber -= FirstNumber;
  72. cout << SecondNumber << endl;
  73. HowLongItTook++;
  74. sleep(SleepTime);
  75. } else {
  76. cout << "NWD(" << BackUpFirst <<","<< BackUpSecond <<") "<< SecondNumber << "<" << FirstNumber <<" -> "<<FirstNumber<<"-"<< SecondNumber<<"=";
  77. FirstNumber -= SecondNumber;
  78. cout << FirstNumber << endl;
  79. HowLongItTook++;
  80. sleep(SleepTime);
  81. }
  82. }
  83. cout <<"\nDla liczb: " << BackUpFirst << " i " << BackUpSecond << endl;
  84. cout <<" Najwiekszym wspolnym dzielnikiem jest: " << FirstNumber << "," << SecondNumber << endl;
  85. cout <<" Najmniejsza wspolna wielokrotnoscia dla wzoru 'NWW = a*b / NWD(a,b)' jest: "<<(BackUpFirst*BackUpSecond/FirstNumber)<< endl;
  86. cout <<" Zajelo to : " << HowLongItTook<<endl<<"Wcisnij enter by kontynuowac.... ";
  87. cin.ignore();
  88. cin.ignore();
  89. }
  90.  
  91. void FModMethod()
  92. {
  93. int HowLongItTook=0;
  94. int SleepTime;
  95. cout << "Podaj Pierwsza liczbe. ";
  96. int FirstNumber = FGiveNumber();
  97. cout << "Podaj Druga liczbe. ";
  98. int SecondNumber = FGiveNumber();
  99. if (FGiveYesOrNo())
  100. {
  101. cout << "Podaj czas czekania. ";
  102. SleepTime = FGiveNumber();
  103. }
  104. else SleepTime = 0;
  105. int modAB;
  106.  
  107. int BackUpFirst = FirstNumber;
  108. int BackUpSecond = SecondNumber;
  109. cout << endl;
  110. while (SecondNumber != 0)
  111. {
  112. modAB = FirstNumber % SecondNumber; //reszta z dzielenia ab
  113. cout << modAB <<"r = "<<FirstNumber <<" / "<< SecondNumber<<endl;
  114. cout << "A = B"<<FirstNumber << "<="<< SecondNumber <<endl;
  115. FirstNumber = SecondNumber;
  116. cout << "B = modAB"<<SecondNumber << "<="<< modAB<<endl;
  117. SecondNumber = modAB;
  118. HowLongItTook = HowLongItTook + 3;
  119. sleep(SleepTime);
  120. }
  121.  
  122.  
  123. cout <<"\nDla liczb: " << BackUpFirst << " i " << BackUpSecond << endl;
  124. cout <<" Najwiekszym wspolnym dzielnikiem jest: " << FirstNumber << endl;
  125. cout <<" Najmniejsza wspolna wielokrotnoscia dla wzoru 'NWW = a*b / NWD(a,b)' jest: "<<(BackUpFirst*BackUpSecond/FirstNumber)<< endl;
  126. cout <<" Zajelo to : " << HowLongItTook<<endl<<"Wcisnij enter by kontynuowac.... ";
  127. cin.ignore();
  128. cin.ignore();
  129. }
  130.  
  131.  
  132.  
  133. void FAdvenceMethod()
  134. {
  135. //-----------------
  136. int x = 1;
  137. int y = 0;
  138. int lastx = 0; //r
  139. int lasty = 1; //s
  140. int templastx;
  141. int templasty;
  142. int modAB; //c
  143. int divAB; //q
  144. //-----------
  145. int HowLongItTook=0;
  146. cout << "Podaj Pierwsza liczbe. ";
  147. int FirstNumber = FGiveNumber(); // a
  148. cout << "Podaj Druga liczbe. ";
  149. int SecondNumber = FGiveNumber(); // b
  150. //--------------
  151. int BackUpFirst = FirstNumber;
  152. int BackUpSecond = SecondNumber;
  153. //-------------
  154. //Jesli wielkosci sa odwrotnie
  155. if (SecondNumber > FirstNumber)
  156. {
  157. swap(FirstNumber,SecondNumber);
  158. }
  159. //ax + by = NWD (a,b)
  160.  
  161. while (SecondNumber != 0)
  162. {
  163. cout<<endl;
  164. //Wyliczenie NWD--------------------------
  165. modAB= FirstNumber % SecondNumber ; // c = a%b
  166. divAB= floor(FirstNumber/SecondNumber); //q = floor(a/b)
  167. cout << FirstNumber <<"="<<SecondNumber<<"("<<divAB<<")"<<"+"<<modAB<<endl;
  168. cout <<"A=B => "<<"A="<<SecondNumber<<" B=modAB => "<<"B="<<modAB<<endl;
  169. FirstNumber = SecondNumber; //a = b
  170. SecondNumber = modAB;// a = c
  171. //-----------------------------------------
  172. templastx = lastx; //r' = r
  173. templasty = lasty; //s' = s
  174. lastx = x - divAB*lastx; //r = x - q * r
  175. lasty = y - divAB*lasty; //s = y - q * s
  176. cout <<"LastX=x-divAB*lastX => "<<lastx<<"="<<x<<"-"<<divAB<<"*"<<templastx<<endl;
  177. cout <<"LastY=y-divAB*lastY => "<<lasty<<"="<<y<<"-"<<divAB<<"*"<<templasty<<endl;
  178. x = templastx; // x = r'
  179. y = templasty; // y = s'
  180. HowLongItTook = HowLongItTook + 10;
  181. }
  182. cout <<"\nDla liczb: " << BackUpFirst << " i " << BackUpSecond << endl;
  183. cout <<" Najwiekszym wspolnym dzielnikiem jest: " << FirstNumber << ". Ze wzoru 'a*x + b*y = NWD(a,b)' x wynosi: " << x << " zas y: " << y << endl;
  184. cout <<" Najmniejsza wspolna wielokrotnoscia dla wzoru 'NWW = a*b / NWD(a,b)' jest: "<<(BackUpFirst*BackUpSecond/FirstNumber)<< endl;
  185. cout <<" Zajelo to : " << HowLongItTook<<endl<<"Wcisnij enter by kontynuowac.... ";
  186. cin.ignore();
  187. cin.ignore();
  188. }
  189.  
  190. void FAdvenceMineMethod()
  191. {
  192. //-----------------
  193. int x = 1;
  194. int y = 0;
  195. int lastx = 0; //r
  196. int lasty = 1; //s
  197. int templastx;
  198. int templasty;
  199. int modAB; //c
  200. int divAB; //q
  201. //-----------
  202. int HowLongItTook=0;
  203. cout << "Podaj Pierwsza liczbe. ";
  204. int FirstNumber = FGiveNumber(); // a
  205. cout << "Podaj Druga liczbe. ";
  206. int SecondNumber = FGiveNumber(); // b
  207. //--------------
  208. int BackUpFirst = FirstNumber;
  209. int BackUpSecond = SecondNumber;
  210. //-------------
  211. //Jesli wielkosci sa odwrotnie
  212. if (SecondNumber > FirstNumber)
  213. {
  214. swap(FirstNumber,SecondNumber);
  215. }
  216. //ax + by = NWD (a,b)
  217. cout << "A = B(divAB) + modAB)"<<endl;
  218. while (SecondNumber != 0)
  219. {
  220. cout<<endl;
  221. //Wyliczenie NWD--------------------------
  222. modAB= FirstNumber % SecondNumber ; // c = a%b
  223. divAB= floor(FirstNumber/SecondNumber); //q = floor(a/b)
  224. cout << FirstNumber <<"="<<SecondNumber<<"("<<divAB<<")"<<"+"<<modAB<<endl;
  225. cout << FirstNumber <<"-"<<SecondNumber<<"("<<divAB<<")"<<"="<<modAB<<endl;
  226. cout <<"A=B => "<<"A="<<SecondNumber<<" || B=modAB => "<<"B="<<modAB<<endl;
  227. FirstNumber = SecondNumber; //a = b
  228. SecondNumber = modAB;// a = c
  229. //-----------------------------------------
  230. templastx = lastx; //r' = r
  231. templasty = lasty; //s' = s
  232. lastx = x - divAB*lastx; //r = x - q * r
  233. lasty = y - divAB*lasty; //s = y - q * s
  234. x = templastx; // x = r'
  235. y = templasty; // y = s'
  236. HowLongItTook = HowLongItTook + 10;
  237. }
  238. cout <<"\nNWD:(" << BackUpFirst << "," << BackUpSecond <<") = "<< FirstNumber<<endl;
  239. cout <<"A*X + B*Y = NWD(A,B) || X= " << x << " , Y= " << y << endl;
  240. cout <<"NWW = a*b / NWD(a,b) = "<<(BackUpFirst*BackUpSecond/FirstNumber)<< endl<<"Wcisnij enter by kontynuowac.... ";
  241. cin.ignore();
  242. cin.ignore();
  243. }
  244.  
  245. void FHowManyLoops()
  246. {
  247. cout << "Podaj Pierwsza liczbe. ";
  248. int FirstNumber = FGiveNumber(); // a
  249. cout << "Podaj Druga liczbe. ";
  250. int SecondNumber = FGiveNumber(); // b
  251. int HowLongItTook = (2*log2(FirstNumber+SecondNumber));
  252. cout <<"\nDla liczb: " << FirstNumber << " i " << SecondNumber << endl;
  253. cout <<" Wynik zostanie uzyskany po maksymalnie: "<<HowLongItTook<<" przebiegach petli."<<endl<<"Wcisnij enter by kontynuowac.... ";
  254. cin.ignore();
  255. cin.ignore();
  256. }
  257.  
  258.  
  259. void FMenu()
  260. {
  261. int MenuOption;
  262. do {
  263. do {
  264. cout << "\n\nAlgorytm Euklidesa" << endl;
  265. cout << "Menu od 1-6" << endl;
  266. cout << "1.Metoda Odejmowania" << endl;
  267. cout << "2.Metoda Reszty z dzielenia" << endl;
  268. cout << "3.Rozszerzony algorytm" << endl;
  269. cout << "4.Rozszerzony algorytm - dane do obliczen" << endl;
  270. cout << "5.Zlozonosc czasowa" << endl;
  271. cout << "6.Wyjscie z programu" << endl;
  272. MenuOption = FGiveNumber();
  273. } while (MenuOption > 6);
  274. switch (MenuOption) {
  275. case 1:
  276. cout << "\nPrzejscie do metody Odejmowania" << endl;
  277. FSubMethod();
  278. break;
  279. case 2:
  280. cout << "\nPrzejscie do metody reszty z dzielenia" << endl;
  281. FModMethod();
  282. break;
  283. case 3:
  284. cout << "\nPrzejscie do rozszerzonego alorytmu" << endl;
  285. FAdvenceMethod();
  286. break;
  287. case 4:
  288. cout << "\nPrzejscie do rozszerzonego alorytmu dane do obliczen" << endl;
  289. FAdvenceMineMethod();
  290. break;
  291. case 5:
  292. cout << "\nPrzejscie do zlozonosci czasowej" << endl;
  293. FHowManyLoops();
  294. break;
  295. case 6:
  296. cout << "\nZakonczenie programu" << endl;
  297. break;
  298. }
  299. }while(MenuOption != 6);
  300. }
  301.  
  302.  
  303. int main() {
  304. FMenu();
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement