Program do przeliczania na bin oct dec hex

0

Ktoś mógł by mi wyjaśnić ten kod najlepiej co się dzieje w każdej linijce kodu

#include <iostream>
#include <sstream>
#include <cmath>
#include <string>
#include <cstdlib>

using namespace std;


string toBinary(int n)
{
    string r;
    while(n!=0) 
	{
		r=(n%2==0 ?"0":"1")+r; n/=2; 
}	
    return r;
}


int convertBinaryToDecimal(int n)
{
    int decimalNumber = 0, i = 0, remainder;
    while (n!=0)
    {
        remainder = n%10;
        n /= 10;
        decimalNumber += remainder*pow(2,i);
        ++i;
    }
    return decimalNumber;
}

int main(int argc, char** argv) 
{

	cout << "Konwersja" << endl << "hex dec oct bin" << endl;

	string type = "", source;
	int res;

	bool done;

	while (!done)
	{
		cin >> source >> type; 
		done = true;
		
		
		if(type == "hex")
			istringstream(source) >> hex >> res;
			
		else if(type == "dec")	
			istringstream(source) >> dec >> res;	
					
		else if(type == "oct")	
			istringstream(source) >> oct >> res;
		
		else if (type == "bin")
		{
			res = convertBinaryToDecimal(atoi(source.c_str()));
		}
		
		else 
		{		
			cout << "Nierozpoznano symbolu\n"; 
			done = false;
		}
	}
	
	
	cout << endl << "dziesietnie: " << dec << res << endl;
	cout << "heksadecymalnie: " << hex << res << endl;
	cout << "oktalnie: " << oct << res << endl;
	cout << "binarnie: " << toBinary(res);
	return 0;
}
0

Całego? Znajdź sobie korepetytora i mu zapłać.

1 użytkowników online, w tym zalogowanych: 0, gości: 1