summaryrefslogtreecommitdiffstats
path: root/Bachelor/Prog1/Prakt2/prg1p2_2/prg1p2_2.cpp
blob: 1ac93e169c2e168c3abf1fe2ed051c38ec233f50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// 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 <iostream>
#include <cmath>
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 <int> (pow(2,iCounter)));
					iBin=iBin%static_cast <int>(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"<<iBit<<"-bit Integer is: ";
				/* // fill the array
				for (iCounter=iBit-1;iCounter>=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 <int> (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