Autor: Peto Cerno
Pokuste sa vyriesit nasledujuce problemy. Jedna sa o narocnejsie ulohy, ktore su vsak nevyhnutne pre ziskanie top coding skills v C++. Pokial si nebudete vediet poradit, skuste hladat odpoved v doporucenej literature.
s0cketky doplneni: Muzete mi posilat vase reseni, budu je sem pridavat. U kazdeho reseni budu publikovat i nick jeho autora.
Zoznam problemov nie je ani zdaleka uplny. Vacsinu rieseni na uvedene problemy najdete prave v tejto knihe.
Zacneme pekne zostra:
F = fopen("C:\DaTa\myfile.txt", "r");
switch(index) {
case 5:
F1();
case 10:
F2();
default:
F3();
}
if (x == y)
if (x < MaX)
return 1;
else
return 0;
if (index = 0)
// rob nieco
else
// rob nieco ine
double e = 2.718;
double x;
int n;
x = n = e;
if (a & b)
// rob nieco
if ((n < MaX) && (a[n] < 10)) a[n] = 0; // V poriadku
if ((n < MaX) & (a[n] < 10)) a[n] = 0; // Je to to iste?
if (x & y != 0) Spracuj(x, y);
alebo nie?
char result = up << 4 + down;
Je to to iste ako: up
if (0.0 <= d <= 1.0) DoSomething();
Preco je podmienka splnena pre kazde d?
double sum = 0.0;
for (int i = 1; i < n; ++i)
sum += 1/i;
sum = 1/i*5.2;
F1() + F2() + F3()
a[++i] = b[++i];
int x = 10;
int y = (x++) * (y++)
Co znamena: x --- y ?
Co znamena: x ----- y ?
int *p;
/* ... */
n = m/*p;
Comu sa rovna vyraz: /*/*/0*/**/1
Od coho to zavisi?
int max = a[0];
for (int i = 1; i < N; ++i)
if (a[i] > max);
max = a[i];
struct Point
{
int x, y;
}
inc(int & a)
{
++a;
}
#define MaX(a,b) (a)>(b)?(a):(b)
Alebo vyraz: MaX(a, MaX(b, MaX(c, d))) ?
Nocna mora moze byt: MaX(a++, b)
Kolkokrat sa inkrementuje a?
#define CuBE(x) x*x*x
#define CuBE(x) ((x)*(x)*(x))
Alebo: CuBE(getchar()) ?
#define PDBL double*
PDBL pa, pb;
#define MaX 1000;
int n = MaX + 1;
#define assert(e) if(!(e))\
_assert_error(#e, __FiLE__, __LiNE__)
#define assert(e) {if(!(e))\
_assert_error(#e, __FiLE__, __LiNE__);}
#define assert(e) if(!(e)){\
fprintf(stderr,\
"assertation failed: %s, file %s, line %d",\
#e, __FiLE__, __LiNE__);\
exit(1);\
} else
#define assert(e)\
((p) ? (void)0 : (void)\
_assert_error(#e, __FiLE__, __LiNE__))
#define assert(e) (void)( (e) ||
(_assert_error(#e, __FiLE__, __LiNE__), 0) )
#define F (x) ((x) - 1)
#define MiNuS -
/* ... */
int i = 10;
int j = MiNuS-i;
int Matrix[4][3];
alebo sa jedna o pole zlozene z 3 prvkov typu pole o styroch prvkoch int?
int Matrix[4][3];
int *c = Matrix;
int (*d)[3]; d = Matrix;
typedef int* pint;
int * Matrix = new pint[10];
for (int i = 0; i < 10; ++i)
Matrix[i] = new int[365];
int **Matrix = (int**) new int[10][365];
Matrix[0][0] = 54;
int (*Matrix)[365];
Matrix = new int[10][365];
Matrix[0][0] = 54;
int a[MaX];
for (int *pa = a; pa < &a[MaX]; ++pa)
printf("%d\n", *pa);
char *c;
strcpy(c, "Nejaky text");
char *c = "Nejaky text";
char *c = 0;
char *d = "";
int index;
scanf("%d", index);
char *text = "Hello World!";
int count = strlen(text);
for (int i = 0; i < count; ++i)
text[i] = toupper(text[i]);
Spravne by malo byt: char text[] = "Hello World!";
char * CreateText()
{
char text[100];
/* ... */
return text;
}
ConcreteClass& Read(istream& F)
{
/* ... */
return *new ConcreteClass(param);
}
Ked je premenna c typu char, short alebo enum, akeho typu je +c (resp. -c)?
double power(base, exponent)
double base;
{
double result = 1.0;
/* ... */
return result;
}
int main()
{
/* ... */
double d = power(2, 10); // CHYBa!!!
}
void F(int);
void F(long);
void F(char*);
/* ... */
F(NuLL);
printf("sinus x je %d\n", sin(x));
double d;
scanf("%f", &d);
printf("%f", d);
FiLE *F = fopen("numbers.dat", "rb");
if (!F) error();
int data;
while (!feof(F))
{
fread(&data, sizeof(int), 1, F);
printf("\n%d", data);
}
fclose(F);
while(fread(&data, sizeof(int), 1, F) == 1)
printf("\n%d", data);
FiLE *F = fopen("numbers.txt", "r");
if (!F) error();
int data;
while (fscanf(F, "%d", &data) != EOF)
printf("\n%d", data);
fclose(F);
FiLE *F = fopen("numbers.txt", "r");
int data, result;
while ((result = fscanf(F, "%d", &data)) > 0)
printf("\n%d", data);
if (!result) printf("Chyba vo vstupnom subore!");
fclose(F);
ifstream F("numbers.txt");
if (!F) error();
int data;
while (F >> i)
cout << i << endl;
if (!F.eof()) cerr << "Chyba vo vstupnom subore!";
char c[100];
scanf("%s", c);
printf(c);
char c[100];
gets(c);
printf(c);
char c[100];
gets(c);
printf("%s", c);
char c[100];
cin.getline(c, 99);
cout << c << endl;
string c;
getline(cin, c);
cout << c << endl;
int data, ok;
do
{
printf("\nZadaj cislo: ");
scanf("%d", &data);
printf("\n%d: Je to OK? (a/n)", data);
ok = getchar();
}
while (ok != 'a');
Vyriesi ho: scanf("%c", &ok) ?
Vyriesi ho: scanf(" %c", &ok) ?
char c;
while ((c = getc(F)) != EOF)
DoSomething(c);
co sa stane, ked char je typ bez znamienka (unsigned)?
char c;
while (c = getc(F), !foef(F))
DoSomething(c);
FiLE *F = fopen("numbers.dat", "rb+");
if (!F) error();
int data;
while (fread(&data, sizeof(int), 1, F) == 1)
{
++data;
fseek(F, -(long)sizeof(int), SEEK_CUR);
fwrite(&data, sizeof(int), 1, F);
}
fclose(F);
Co by sa stalo, keby sme namiesto
fseek(F, -(long)sizeof(int), SEEK_CUR);
napisali: fseek(F, -sizeof(int), SEEK_CUR); ?
Aky je rozdiel medzi:
cout << "Hello World!" << "\n";
cout << "Hello World!" << endl; ?
FiLE *F = fopen("data.dat", "ab+");
if (!F) error();
fwrite("qwerty", 6, 1, F);
fseek(F, 0, SEEK_SET);
fwrite("QWERTY", 6, 1, F);
fclose(F);
FiLE *F = fopen("data.dat", "wb");
fprintf(F, "%d", 32767);
fclose(F);
Ake su zakladne funkcie pre neformatovany vstup a vystup?
int a = iNT_MaX;
int b = 1;
int c = -1;
cout << a + (b + c);
cout << (a + b) + c;
double d = 0.0;
while (d != 1.0)
{
cout << d << endl;
d += 1.0;
}
double a = 10.0;
double b = -10.0;
double c = DBL_EPSiLON;
cout << setiosflags(ios::showpoint) <<
setprecision(20);
cout << a + b + c << endl;
cout << a + (b + c) << endl;
cout << a + c + b << endl;
void f(int) { }
class C {
public:
void f(double) { }
void g() { f(0); }
};
/* ... */
C c;
c.g();
struct a { int f() {return 0;} };
struct B: virtual public a
{ int f() {return 1;} };
struct C: public B { };
struct D: C, virtual a { };
/* ... */
D d;
d.f();
Co je to pravidlo o dominancii?
class String {
char *data;
public:
String(const char *p);
char& operator[] (int index)
{ return data[index]; }
char operator[] (int index) const
{ return data[index]; }
};
/* ... */
String str("Hello World!");
const String cstr("May the force be with you!");
char c = cstr[1]; // OK
str[1] = str[2]; // OK
cstr[1] = 'm'; // CHYBa
Keby sme riadok
char operator[] (int index) const
{ return data[index]; }
char& operator[] (int index) const
{ return data[index]; }
K comu sluzi modifikator mutable?
class a { /* ... */ };
class B: public a { /* ... */ };
class C: public a { /* ... */ };
class D: public B, public C { /* ... */ };
/* ... */
D d, *pd = &d;
B *pb = pd;
C *pc = pd;
class a {
int a;
public:
virtual ~a() { }
};
class B: public virtual a {
int b;
};
class C: public virtual a {
int c;
};
class D: public a, public B {
int d;
};
/* ... */
D dd;
D * pd = ⅆ
D * pd1 = dynamic_cast(pd); // 1
a * pa = dynamic_cast(pd);
// to je to iste ako: pa = pd;
D * pd2 = (D*)pa; // 2
D * pd3 = dynamic_cast(pa); // 3
B * pb = ⅆ
C * pc = dynamic_cast(pb); // 4
C cc;
pc = &cc;
D * pd4 = static_cast(pc); // 5
D * pd5 = dynamic_cast(pc); // 6
Ktore z oznacenych riadkov neprejdu kompilatorom?
Ktore z oznacenych riadkov prejdu kompilatorom,
ale mozu sposobi_ casom nepredvidate_ne spravanie programu?
Ktore z dynamic_cast-ov prejdu a ktore nie (TJ vysledok bude 0)?
Plati rovnost (samozrejme za predpokladu pa = &dd):
(void*)pa == static_cast
class String {
unsigned len;
char *s;
public:
String(const char *c = 0): len(0)
{
if (c)
{
len = strlen(c);
s = new char[len + 1];
strcpy(s, c);
}
}
~String() { delete[] s; }
/* ... */
};
int main()
{
String str;
return 0;
}
Staci pridat do zoznamu inicializatorov s(0) ?
class a {
int a, b;
public:
a(int i): a(i), b(i) { }
};
class B: public a {
int c;
public:
B(int);
};
B::B(int i): c(i*i), a(c) { }
/* ... */
B x(11);
class a {
int b, a;
public:
a(int i): a(i), b(a) { }
};
Ako prebieha inicializacia?
Ako prebieha inicializacia nepriameho virtualneho predka?
A aky je dovod tohto pravidla?
class array {
int len;
int *data;
public:
array(int _len): len(_len), data(new int[_len]) { }
array(array&);
~array() { delete[] data; }
array& operator=(array&);
/* ... */
};
Ako jej mozno zabranit?
class a {
public:
virtual void f(int);
};
class B: public a
{
public:
virtual void f(double);
};
/* ... */
a * pa = new B;
pa->f(1);
class a {
public:
virtual void f(int);
};
class B: public a
{
public:
virtual void f(int) const;
};
/* ... */
a * pa = new B;
pa->f(1);
class a {
public:
virtual void f(int n = 5)
{ cout << "a: " << n << endl; }
};
class B: public a
{
public:
virtual void f(int n = 10)
{ cout << "B: " << n << endl; }
};
/* ... */
a * pa = new B;
pa->f();
class a {
public:
virtual void f();
virtual void g();
virtual void h();
};
class B: virtual public a {
public:
virtual void f(); // Nova implementacia
};
class C: virtual public a {
public:
virtual void g(); // Nova implementacia
};
class D: public B, public C {
virtual void h();
};
/* ... */
D dd;
C * pc = ⅆ
pc->f();
class a {
public:
a() { f(); }
virtual ~a();
void f() { g(); }
virtual void g() = 0;
};
class B: public a {
public:
virtual void g();
};
/* ... */
B bb;
Kolko existuje verzii operatoru new?
T *p = (T*)operator new(100 * sizeof(T));
T *p = new T[10];
/* ... */
delete p;
class String {
enum {N = 100};
char data[N];
int len;
public:
String(): len(0) {
for (int i = 0; i < N; ++i)
data[i] = '\0';
}
String operator<<(char ch) {
if (len < N - 1)
data[len++] = ch;
return *this;
};
/* ... */
String str1, str2;
str1 << 'h';
str2 = str1 << 'e' << 'l' << 'l' << 'o';
Ako sa da tento problem vyriesit?
class String {
enum {N = 100};
char * data;
public:
String(char *_data = "")
{
data = new char[N];
if (_data) strcpy(data, _data);
else data[0] = '\0';
}
~String() { delete[] data; }
String& operator=(String&);
};
/* ... */
String X("Hello");
String Y = X;
class String {
int len;
char * data;
public:
String(char *_data = "")
~String() { delete[] data; }
String& operator=(String& str) {
len = str.len;
delete[] data;
for (int i = 0; i < len; ++i)
data[i] = str.data[i];
return *this;
};
};
template
class MyStack {
int len;
int peak;
T * data;
public:
explicit MyStack(int _len = 10);
MyStack(MyStack&);
virtual ~MyStack();
T& operator[](int);
MyStack& operator=(MyStack&);
/* ... */
};
template
MyStack& MyStack ::operator=(MyStack & S) {
if (dynamic_cast(this) !=
dynamic_cast(&S)) {
len = S.len;
peak = S.peak;
delete[] data;
data = new T[len];
for (int i = 0; i < peak; ++i)
data[i] = S.data[i];
}
return *this;
}
Ako sa da riesit?
void* operator new(size_t s) {
static char arena[N];
static int index = 0;
char *p;
if (s > N) return 0;
if ((index + s) >= N)
index = 0;
p = &arena[index];
index += s;
return p;
}
Ako sa da tento problem riesit?
class XYZ {
/* ... */
public:
XYZ();
~XYZ();
void* operator new(size_t, void *p)
{ return p; }
/* ... */
};
Co treba doplnit, aby nedoslo k chybe?
Na co je to dobre?
class XYZ {
/* ... */
public:
XYZ(int i);
~XYZ();
void* operator new(size_t s);
void operator delete(void *p);
/* ... */
};
void* XYZ::operator new(size_t s) {
/* ... */
return ::new XYZ;
}
void XYZ::operator delete(void *p) {
/* ... */
::delete(p);
}