summaryrefslogtreecommitdiffstats
path: root/Bachelor/Prog1/Prakt2/prg1p2_6/prg1p2_6.cpp
blob: 70abc04e6044a756d69da7fba8527283a052e7eb (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
// Programmieren 1, Praktikum 2, Aufgabe 6
// Sven Eisenhauer
// 06.11.2004
//
// file: prg1p2_6.cpp
//
// purpose: generate a table of bin, oct and hex presentation of the numbers from 1 to 256
//          
//

#include <iostream>
#include <iomanip>
#include <cmath>

using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::hex;
using std::dec;

const int iBit=8; // 256 is 8 bit

int iDec,iOct,iHex,iBin,iTemp;

int main()
{
	cout << setw(5) << "Dec" << setw (11) << "Binary" << setw(7) << "Octal" << setw(5) << "Hex" << endl;

	for (int iCounter=1;iCounter<=256;iCounter++)
	{
		iDec=iCounter;
		iTemp=0;
		iBin=0;
		iOct=0;
		// generate binary
		for (int iBinC=iBit;iBinC>=0;iBinC--)
		{
			iTemp = iDec % 2;
			iDec = iDec / 2;
			iBin += iTemp * static_cast <int> (pow(10,iBit-iBinC));

		}
		// generate octal
		iDec=iCounter;
		for ( int iOctC=3;iOctC>=0;iOctC--)
		{
			iTemp = iDec % 8;
			iDec = iDec / 8;
			iOct += iTemp * static_cast <int> (pow(10,3-iOctC));
		}
		cout << setw(5) << dec << iCounter << setw (11) << iBin << setw(7) << iOct << setw(5) << hex << iCounter << endl;
		if (!(iCounter%23))
            cout << endl << setw(5) << "Dec" << setw (11) << "Binary" << setw(7) << "Octal" << setw(5) << "Hex" << endl;
	}


	return 0;
}// end main