Advertisement
Guest User

aaa

a guest
Jan 21st, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>
  4. #include <fstream>
  5.  
  6.  
  7. using namespace std;
  8.  
  9.  
  10. class NWD
  11. {
  12. public:
  13.  
  14.     int liczba1 = { 0 };
  15.     int liczba2 = { 0 };
  16.  
  17.  
  18.     int operacja(int a, int b);
  19.     NWD(int a, int b);
  20.  
  21.  
  22.  
  23. };
  24.  
  25. /**************************************/
  26. int NWD::operacja(int a, int b)
  27. {
  28.  
  29.     while (a != b)
  30.     {
  31.         if (b > a)
  32.             b -= a;
  33.         else
  34.             a -= b;
  35.     }
  36.  
  37.  
  38.     return a;
  39. }
  40. /**************************************/
  41. NWD::NWD(int a, int b)
  42.  
  43. {
  44.     liczba1 = a;
  45.     liczba2 = b;
  46.  
  47. }
  48. /**************************************/
  49. void zapisz(int a, int b, int wynik_NWD);
  50. /**************************************/
  51.  
  52.  
  53. class wczytaj
  54. {
  55. public:
  56.     int a;
  57.  
  58.     wczytaj(int liczba)
  59.     {
  60.         a = liczba;
  61.     }
  62.  
  63.  
  64.     void odczyt();
  65.  
  66. };
  67.  
  68.     int main()
  69.     {
  70.         int a, b;
  71.         int wynik_NWD = { 0 };
  72.         int ktory;
  73.  
  74.  
  75.    
  76.         do
  77.         {
  78.             cout << "******************************" << endl;
  79.             cout << "Witamy w programie" << endl;
  80.             cout << "1 - Konwertuj z Dziesietnego na Binarne i zapisz do pliku  " << endl;
  81.             cout << "2 - Odczytaj zawartosc pliku  " << endl;
  82.             cout << "0 - Koniec Programu  " << endl;
  83.             cout << "Wybierz opcje Programu  ";
  84.             cin >> ktory;
  85.  
  86.  
  87.             switch (ktory)
  88.             {
  89.             case 1:
  90.             {
  91.                 cout << "Podaj liczbe a: ";
  92.                 cin >> a;
  93.                 cout << "Podaj liczbe b: ";
  94.                 cin >> b;
  95.                 NWD zestaw1(a, b);
  96.                
  97.            
  98.                 wynik_NWD = zestaw1.operacja(a, b);
  99.                 zapisz(a, b, wynik_NWD);
  100.                 break;
  101.             }
  102.             case 2:
  103.  
  104.             {
  105.             wczytaj losowe(1);
  106.             losowe.odczyt();
  107.             break;
  108.  
  109.             }
  110.  
  111.  
  112.  
  113.             default:
  114.             {
  115.                 cout << "Podano bledny nr. produktu";
  116.                 break;
  117.             }
  118.             }
  119.  
  120.  
  121.         } while (ktory != 0);
  122.  
  123.  
  124.  
  125.     }
  126.  
  127.  
  128.  
  129.  
  130.     void zapisz(int a, int b, int wynik_NWD)
  131.     {
  132.  
  133.         fstream raport;
  134.         raport.open("raport.txt", ios::out | ios::app);
  135.  
  136.  
  137.         if (raport.good() == true)
  138.  
  139.  
  140.         {
  141.  
  142.  
  143.             raport << endl << "a = " << a << ", b = " << b << ", wynik_NWD = " << wynik_NWD << ", Wynik binarnie =  ";
  144.  
  145.             int i = 0, tab[31];
  146.            
  147.                 while (wynik_NWD)
  148.                 {
  149.                     tab[i++] = wynik_NWD % 2;
  150.                     wynik_NWD /= 2;
  151.                 }
  152.  
  153.                 for (int j = i - 1; j >= 0; j--)
  154.                     raport << tab[j];
  155.            
  156.  
  157.             cout << "" << endl;
  158.         }
  159.  
  160.         else
  161.         {
  162.             cout << "Error! Nie udalo otworzyc sie pliku! " << endl;
  163.         }
  164.  
  165.     }
  166.    
  167.     void wczytaj::odczyt()
  168.     {
  169.         string linia;
  170.         fstream raport;
  171.         raport.open("raport.txt",ios::in);
  172.         if (raport.good() == true)
  173.         {
  174.             while (!raport.eof())
  175.             {
  176.                 getline(raport, linia);
  177.                 cout << linia << endl; //wyświetlenie linii
  178.             }
  179.             raport.close();
  180.         }
  181.  
  182.         else
  183.  
  184.         {
  185.             cout << "Error! Nie udalo otworzyc sie pliku! " << endl;
  186.         }
  187.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement