Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream> #include <math.h> #include <sstream> #ifdef _WIN32
- void cls(){
- HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
- COORD coord = {0, 0};
- DWORD count;
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- GetConsoleScreenBufferInfo(hStdOut, &csbi);
- FillConsoleOutputCharacter(hStdOut, ' ',
- csbi.dwSize.X * csbi.dwSize.Y,
- coord, &count);
- SetConsoleCursorPosition(hStdOut, coord);
- }
- #else
- void cls(){
- cout << "\f";
- }
- #endif
- using namespace std;
- template <class T> inline string toString (const T& t){
- stringstream ss;
- ss << t;
- return ss.str();
- }
- void printBin(int dec){
- int *bin;
- bin = (int *)malloc(sizeof(int));
- int d=0;
- while(dec!=0){
- bin[d] = dec%2;
- dec = (int)dec/2;
- d++;
- };
- for(int i=d-1;i>=0;i--){
- cout<<bin[i];
- }
- bin = NULL;
- cin.ignore(2);
- }
- void printDec(int bina){
- char bin[32];
- string temp = toString(bina);
- char *tempc;
- tempc = new char[temp.length() + 1];
- int d=(temp.length());
- for(int i = 0; i<d; i++){
- strcpy(tempc, temp.substr(i,1).c_str());
- bin[i] = *tempc;
- }
- int binToDec = 0;
- int cnt = 0;
- for(int i=d-1; i>=0; i--){
- int b = (int)bin[i]-'0';
- binToDec += (int)b*pow(2.0,(double)cnt);
- cnt++;
- }
- cout<<binToDec<<endl;
- cin.ignore(2);
- }
- int main(){
- bool stop = false;
- while(!stop){
- cls();
- int option = 5;
- cout<<"1:\tDec to Bin"<<endl;
- cout<<"2:\tBin to Dec"<<endl;
- cout<<"----------------"<<endl;
- cout<<"0:\texit\n"<<endl;
- cout<<"Option: ";
- cin>>option;
- switch(option){
- case 1:
- int dec;
- cout<<"\nDec to Bin: ";
- cin>>dec;
- printBin(dec);
- break;
- case 2:
- int bin;
- cout<<"\nBin to Dec: ";
- cin>>bin;
- printDec(bin);
- break;
- case 0:
- stop = true;
- break;
- default:
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement