===== Prevod ciselnych soustav ======
#include
using namespace std;
/*
void conv1 (int n)
{
while (n > 0)
{
cout << n % 2;
n = n / 2;
}
}
void conv2 (int n)
{
if (n > 0)
{
cout << n % 2;
conv2 (n / 2);
}
}
void conv3(int n)
{
if (n > 0)
{
conv3(n / 2);
cout << n % 2;
}
}
*/
string conv (int n, int z = 2)
{
string s = "";
bool neg = false;
if (n < 0) { n = -n; neg = true; }
while (n > 0)
{
int t = n % z; // cislice jako int
char d; // cislice jako char
if (t <= 9)
d = '0' + t;
else
d = 'A' + t - 10 ;
s = d + s;
n = n / z;
}
if (s == "") { s = "0"; }
if (neg) { s = '-' + s; }
return s;
}
int main ()
{
int n;
// cout << "vlozte cislo : ";
// cin >> n;
n = 27;
cout << "n = " << n << endl;
cout << "ve dvojkove soustave " << conv(n) << endl;
cout << "v osmickove soustave " << conv(n, 8) << endl;
cout << "v desitkove soustave " << conv(n, 10) << endl;
cout << "v sestnactkove soustave " << conv(n, 16) << endl;
/*
const int Max = 10;
int c [Max];
char znam = ' ';
if (n < 0)
{
n = -n;
znam = '-';
}
int i = 0;
while (n > 0 && i < Max)
{
c [i] = n % 2;
n = n / 2;
i = i + 1;
}
// c[0] ... c[i-1]
cout << "ve dvojkove soustave ";
if (n != 0) cout << "overflow ";
cout << znam;
if (i == 0)
cout << "0";
while (i >= 1)
{
i = i - 1;
cout << c[i];
}
cout << endl;
*/
/*
int v = 1;
while (n > 0)
{
cout << "cislice " << n % 2 << " ... " << v << endl;
// n = n / 2;
n /= 2;
// v = 2 * v;
v *= 2;
}
*/
}
====== Jednorozměrná pole ======
#include
using namespace std;
void add (double* z, double* x, double* y , int n) // z := x + y, n ... length
{
for (int i = 0; i < n; i++)
z[i] = x[i] + y[i];
}
void print(double* p, int n) // z := x + y
{
for (int i = 0; i < n; i++)
cout << i << " ... " << p[i] << endl;
}
void print2 (double* p, int n) // z := x + y
{
cout << "[ ";
for (int i = 0; i < n; i++)
{
cout << p[i];
if (i < n - 1) cout << ", ";
}
cout << " ]" << endl;
}
int main()
{
int n = 10;
double* a, * b, * c;
a = new double [n];
b = new double [n];
c = new double [n];
for (int i = 0; i < n; i++)
{
a[i] = i + 1;
b[i] = 10.1 * (i + 1);
}
add (c, a, b, n); // c = a + b
print2 (c, n);
delete a;
delete b;
delete c;
cout << "O.K.\n";
}