Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <unistd.h>
- #include <cmath>
- #include <string>
- #include <limits>
- using namespace std;
- int FGiveNumber()
- { int NumberGiven = 0;
- while (NumberGiven == 0)
- {
- cout << "Podaj liczbe: ";
- while (!(cin >> NumberGiven)) {
- cin.clear();
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- cout << endl << "Nie podano liczby. Sprobuj ponownie: "; //Musi byc endl bo inaczej tekst jest fuckd
- }
- if (NumberGiven == 0)
- {
- cout <<"Podana liczba musi byc rozna od 0. "<<endl;
- }
- }
- return NumberGiven;
- }
- bool FGiveYesOrNo()
- {
- char GivenChar = 0;
- do{
- cout <<"Podaj y lub n: ";
- cin >> GivenChar;
- if (GivenChar != 'y' && GivenChar != 'n')
- {
- cout << "Nie podano poprawnej opcji! ";
- }
- }while( GivenChar != 'y' && GivenChar != 'n');
- if (GivenChar == 'y' || GivenChar =='Y')
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- void FSubMethod()
- {
- int HowLongItTook=0;
- int SleepTime;
- cout << "Podaj Pierwsza liczbe. ";
- int FirstNumber = FGiveNumber();
- cout << "Podaj Druga liczbe. ";
- int SecondNumber = FGiveNumber();
- cout << "Czy chcesz aby program poczekal z wynikami?."<<endl;
- if (FGiveYesOrNo())
- {
- cout << "Podaj czas czekania. ";
- SleepTime = FGiveNumber();
- }
- else SleepTime = 0;
- int BackUpFirst = FirstNumber;
- int BackUpSecond = SecondNumber;
- cout << endl;
- while (SecondNumber != FirstNumber) {
- if (SecondNumber > FirstNumber) {
- cout << "NWD(" << BackUpFirst <<","<< BackUpSecond <<") "<< SecondNumber << ">" << FirstNumber <<" -> "<<SecondNumber<<"-"<< FirstNumber<<"=";
- SecondNumber -= FirstNumber;
- cout << SecondNumber << endl;
- HowLongItTook++;
- sleep(SleepTime);
- } else {
- cout << "NWD(" << BackUpFirst <<","<< BackUpSecond <<") "<< SecondNumber << "<" << FirstNumber <<" -> "<<FirstNumber<<"-"<< SecondNumber<<"=";
- FirstNumber -= SecondNumber;
- cout << FirstNumber << endl;
- HowLongItTook++;
- sleep(SleepTime);
- }
- }
- cout <<"\nDla liczb: " << BackUpFirst << " i " << BackUpSecond << endl;
- cout <<" Najwiekszym wspolnym dzielnikiem jest: " << FirstNumber << "," << SecondNumber << endl;
- cout <<" Najmniejsza wspolna wielokrotnoscia dla wzoru 'NWW = a*b / NWD(a,b)' jest: "<<(BackUpFirst*BackUpSecond/FirstNumber)<< endl;
- cout <<" Zajelo to : " << HowLongItTook<<endl<<"Wcisnij enter by kontynuowac.... ";
- cin.ignore();
- cin.ignore();
- }
- void FModMethod()
- {
- int HowLongItTook=0;
- int SleepTime;
- cout << "Podaj Pierwsza liczbe. ";
- int FirstNumber = FGiveNumber();
- cout << "Podaj Druga liczbe. ";
- int SecondNumber = FGiveNumber();
- if (FGiveYesOrNo())
- {
- cout << "Podaj czas czekania. ";
- SleepTime = FGiveNumber();
- }
- else SleepTime = 0;
- int modAB;
- int BackUpFirst = FirstNumber;
- int BackUpSecond = SecondNumber;
- cout << endl;
- while (SecondNumber != 0)
- {
- modAB = FirstNumber % SecondNumber; //reszta z dzielenia ab
- cout << modAB <<"r = "<<FirstNumber <<" / "<< SecondNumber<<endl;
- cout << "A = B"<<FirstNumber << "<="<< SecondNumber <<endl;
- FirstNumber = SecondNumber;
- cout << "B = modAB"<<SecondNumber << "<="<< modAB<<endl;
- SecondNumber = modAB;
- HowLongItTook = HowLongItTook + 3;
- sleep(SleepTime);
- }
- cout <<"\nDla liczb: " << BackUpFirst << " i " << BackUpSecond << endl;
- cout <<" Najwiekszym wspolnym dzielnikiem jest: " << FirstNumber << endl;
- cout <<" Najmniejsza wspolna wielokrotnoscia dla wzoru 'NWW = a*b / NWD(a,b)' jest: "<<(BackUpFirst*BackUpSecond/FirstNumber)<< endl;
- cout <<" Zajelo to : " << HowLongItTook<<endl<<"Wcisnij enter by kontynuowac.... ";
- cin.ignore();
- cin.ignore();
- }
- void FAdvenceMethod()
- {
- //-----------------
- int x = 1;
- int y = 0;
- int lastx = 0; //r
- int lasty = 1; //s
- int templastx;
- int templasty;
- int modAB; //c
- int divAB; //q
- //-----------
- int HowLongItTook=0;
- cout << "Podaj Pierwsza liczbe. ";
- int FirstNumber = FGiveNumber(); // a
- cout << "Podaj Druga liczbe. ";
- int SecondNumber = FGiveNumber(); // b
- //--------------
- int BackUpFirst = FirstNumber;
- int BackUpSecond = SecondNumber;
- //-------------
- //Jesli wielkosci sa odwrotnie
- if (SecondNumber > FirstNumber)
- {
- swap(FirstNumber,SecondNumber);
- }
- //ax + by = NWD (a,b)
- while (SecondNumber != 0)
- {
- cout<<endl;
- //Wyliczenie NWD--------------------------
- modAB= FirstNumber % SecondNumber ; // c = a%b
- divAB= floor(FirstNumber/SecondNumber); //q = floor(a/b)
- cout << FirstNumber <<"="<<SecondNumber<<"("<<divAB<<")"<<"+"<<modAB<<endl;
- cout <<"A=B => "<<"A="<<SecondNumber<<" B=modAB => "<<"B="<<modAB<<endl;
- FirstNumber = SecondNumber; //a = b
- SecondNumber = modAB;// a = c
- //-----------------------------------------
- templastx = lastx; //r' = r
- templasty = lasty; //s' = s
- lastx = x - divAB*lastx; //r = x - q * r
- lasty = y - divAB*lasty; //s = y - q * s
- cout <<"LastX=x-divAB*lastX => "<<lastx<<"="<<x<<"-"<<divAB<<"*"<<templastx<<endl;
- cout <<"LastY=y-divAB*lastY => "<<lasty<<"="<<y<<"-"<<divAB<<"*"<<templasty<<endl;
- x = templastx; // x = r'
- y = templasty; // y = s'
- HowLongItTook = HowLongItTook + 10;
- }
- cout <<"\nDla liczb: " << BackUpFirst << " i " << BackUpSecond << endl;
- cout <<" Najwiekszym wspolnym dzielnikiem jest: " << FirstNumber << ". Ze wzoru 'a*x + b*y = NWD(a,b)' x wynosi: " << x << " zas y: " << y << endl;
- cout <<" Najmniejsza wspolna wielokrotnoscia dla wzoru 'NWW = a*b / NWD(a,b)' jest: "<<(BackUpFirst*BackUpSecond/FirstNumber)<< endl;
- cout <<" Zajelo to : " << HowLongItTook<<endl<<"Wcisnij enter by kontynuowac.... ";
- cin.ignore();
- cin.ignore();
- }
- void FAdvenceMineMethod()
- {
- //-----------------
- int x = 1;
- int y = 0;
- int lastx = 0; //r
- int lasty = 1; //s
- int templastx;
- int templasty;
- int modAB; //c
- int divAB; //q
- //-----------
- int HowLongItTook=0;
- cout << "Podaj Pierwsza liczbe. ";
- int FirstNumber = FGiveNumber(); // a
- cout << "Podaj Druga liczbe. ";
- int SecondNumber = FGiveNumber(); // b
- //--------------
- int BackUpFirst = FirstNumber;
- int BackUpSecond = SecondNumber;
- //-------------
- //Jesli wielkosci sa odwrotnie
- if (SecondNumber > FirstNumber)
- {
- swap(FirstNumber,SecondNumber);
- }
- //ax + by = NWD (a,b)
- cout << "A = B(divAB) + modAB)"<<endl;
- while (SecondNumber != 0)
- {
- cout<<endl;
- //Wyliczenie NWD--------------------------
- modAB= FirstNumber % SecondNumber ; // c = a%b
- divAB= floor(FirstNumber/SecondNumber); //q = floor(a/b)
- cout << FirstNumber <<"="<<SecondNumber<<"("<<divAB<<")"<<"+"<<modAB<<endl;
- cout << FirstNumber <<"-"<<SecondNumber<<"("<<divAB<<")"<<"="<<modAB<<endl;
- cout <<"A=B => "<<"A="<<SecondNumber<<" || B=modAB => "<<"B="<<modAB<<endl;
- FirstNumber = SecondNumber; //a = b
- SecondNumber = modAB;// a = c
- //-----------------------------------------
- templastx = lastx; //r' = r
- templasty = lasty; //s' = s
- lastx = x - divAB*lastx; //r = x - q * r
- lasty = y - divAB*lasty; //s = y - q * s
- x = templastx; // x = r'
- y = templasty; // y = s'
- HowLongItTook = HowLongItTook + 10;
- }
- cout <<"\nNWD:(" << BackUpFirst << "," << BackUpSecond <<") = "<< FirstNumber<<endl;
- cout <<"A*X + B*Y = NWD(A,B) || X= " << x << " , Y= " << y << endl;
- cout <<"NWW = a*b / NWD(a,b) = "<<(BackUpFirst*BackUpSecond/FirstNumber)<< endl<<"Wcisnij enter by kontynuowac.... ";
- cin.ignore();
- cin.ignore();
- }
- void FHowManyLoops()
- {
- cout << "Podaj Pierwsza liczbe. ";
- int FirstNumber = FGiveNumber(); // a
- cout << "Podaj Druga liczbe. ";
- int SecondNumber = FGiveNumber(); // b
- int HowLongItTook = (2*log2(FirstNumber+SecondNumber));
- cout <<"\nDla liczb: " << FirstNumber << " i " << SecondNumber << endl;
- cout <<" Wynik zostanie uzyskany po maksymalnie: "<<HowLongItTook<<" przebiegach petli."<<endl<<"Wcisnij enter by kontynuowac.... ";
- cin.ignore();
- cin.ignore();
- }
- void FMenu()
- {
- int MenuOption;
- do {
- do {
- cout << "\n\nAlgorytm Euklidesa" << endl;
- cout << "Menu od 1-6" << endl;
- cout << "1.Metoda Odejmowania" << endl;
- cout << "2.Metoda Reszty z dzielenia" << endl;
- cout << "3.Rozszerzony algorytm" << endl;
- cout << "4.Rozszerzony algorytm - dane do obliczen" << endl;
- cout << "5.Zlozonosc czasowa" << endl;
- cout << "6.Wyjscie z programu" << endl;
- MenuOption = FGiveNumber();
- } while (MenuOption > 6);
- switch (MenuOption) {
- case 1:
- cout << "\nPrzejscie do metody Odejmowania" << endl;
- FSubMethod();
- break;
- case 2:
- cout << "\nPrzejscie do metody reszty z dzielenia" << endl;
- FModMethod();
- break;
- case 3:
- cout << "\nPrzejscie do rozszerzonego alorytmu" << endl;
- FAdvenceMethod();
- break;
- case 4:
- cout << "\nPrzejscie do rozszerzonego alorytmu dane do obliczen" << endl;
- FAdvenceMineMethod();
- break;
- case 5:
- cout << "\nPrzejscie do zlozonosci czasowej" << endl;
- FHowManyLoops();
- break;
- case 6:
- cout << "\nZakonczenie programu" << endl;
- break;
- }
- }while(MenuOption != 6);
- }
- int main() {
- FMenu();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement