Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- string DecToBin(int number)
- {
- string result = "";
- do
- {
- if ((number & 1) == 0)
- result += "0";
- else
- result += "1";
- number >>= 1;
- } while (number);
- reverse(result.begin(), result.end());
- return result;
- }
- int BinToDec(string number)
- {
- int result = 0, pow = 1;
- for (int i = number.length() - 1; i >= 0; --i, pow <<= 1)
- result += (number[i] - '0') * pow;
- return result;
- }
- void sort(int tab[], int n)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 1; j < n - i; j++)
- {
- if (tab[j - 1] > tab[j])
- swap(tab[j - 1], tab[j]);
- }
- }
- }
- int main()
- {
- int counter = 0;
- int tab[100];
- string line;
- ifstream myfile("file.txt");
- if (myfile.is_open())
- {
- while (getline(myfile, line))
- {
- tab[counter] = BinToDec(line);
- counter++;
- }
- myfile.close();
- }
- else
- cout << "Unable to open file";
- sort(tab, 100);
- for (int i = 0; i < 100; i++)
- cout << i+1 << ". " << "DEC: " << tab[i] << " BIN: " << DecToBin(tab[i]) << endl;
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement