// Programmieren 1, Praktikum 2, Aufgabe 2 // Sven Eisenhauer // 29.10.2004 // // file: prg1p2_2.cpp // // purpose: convert bin to dec numbers or dec numbers to bin // 10 digits mean 2exp9 to 2exp0 // #include #include using std::cin; using std::cout; using std::endl; int main() { const iBit=10; //define how large the binary output should be. int iMode; int iBin=0; int iActualFactor=0; int iDec=0; int iCounter=0; int iCounter2=0; //int iBinOut[iBit]; int iBinOut=0; do { system("cls"); cout << "Please select Converter:\n1) Bin -> Dec\n2) Dec -> Bin\n\n0) End\n\n" "Your selection: "; cin >> iMode; switch (iMode) { case 1: cout << "\nPlease enter a binary number (max 10 characters): "; cin >> iBin; for (iCounter = 9;iCounter>=0;iCounter--) { iActualFactor=iBin/pow(10,iCounter); iDec+=(iActualFactor * static_cast (pow(2,iCounter))); iBin=iBin%static_cast (pow(10,iCounter)); } cout << "\nDecimal: " << iDec; cout << endl; system ("pause"); break; // end bin2dec case 2://dec2bin cout << "\nPlease enter a decimal number: "; cin >> iDec; cout << "\n"<=0;iCounter--) { iBinOut[iCounter]=iDec%2; iDec=iDec/2; } // print array in reverse order for (iCounter=0;iCounter<=iBit-1;iCounter++) { cout << iBinOut[iCounter]; if (0==((iCounter+1)%4)) // space separator after 4 bit cout <<" "; } */ iBinOut=0; for (iCounter2=iBit;iCounter2>=0;iCounter2--) { iBin=iDec%2; iDec=iDec/2; iBinOut += iBin * static_cast (pow(10,iBit-iCounter2)); } cout << iBinOut << endl; system ("pause"); break; // end dec2bin case 0: // end selected... we are leaving... break; default: // wrong selection cout << "\n !! No functon selected !!"; } // end switch } // end while while (iMode != 0); return 0; // so far, we are ok }// end main