Skip to content

Instantly share code, notes, and snippets.

@sammy-SC
Last active December 17, 2015 00:00
Show Gist options
  • Save sammy-SC/5518128 to your computer and use it in GitHub Desktop.
Save sammy-SC/5518128 to your computer and use it in GitHub Desktop.
ukazky na test.
#include <iostream>
#include <string>
using namespace std;
class CException{
const string message;
public:
CException(const string & message): message(message) {} // string message sa vytvara pri vytvarani CException
friend ostream & operator << ( ostream & os, const CException & x )
{
os << x.message << endl;
return os;
}
};
void testujeme ()
{
throw CException("DOJEBAL SI TO");
}
int main()
{
try {
testujeme();
}
catch ( const CException & c ){
cout << c;
}
}
#include <iostream>
#include <string>
using namespace std;
class Mem
{
public:
struct Thought
{
string idea;
Thought * next;
Thought(const string & idea): idea(idea), next(NULL){}
};
struct Sequence{
Thought * begin;
Thought * end;
Sequence(): begin(NULL), end(NULL){}
int length () const {
int counter = 1;
Thought * tmp;
tmp = begin;
while ( tmp != end )
{
++counter;
tmp = tmp->next;
}
return counter;
}
friend ostream & operator << ( ostream & os, const Sequence & x )
{
Thought * tmp;
tmp = x.begin;
while ( tmp != x.end )
{
os << tmp->idea << endl;
tmp = tmp->next;
}
os << tmp->idea << endl;
return os;
}
};
Thought * root;
Sequence seq;
Mem(): root(NULL) {}
~Mem()
{
Thought * tmp = root, * previous;
while ( tmp )
{
previous = tmp;
tmp = tmp->next;
delete previous;
}
}
Mem & add (const string & mem )
{
Thought * newOne = new Thought (mem);
if ( root == NULL )
root = newOne;
else
{
Thought * tmp;
tmp = root;
while ( tmp->next )
tmp = tmp->next;
tmp->next = newOne;
}
return *this;
}
friend ostream & operator << ( ostream & os, const Mem & x )
{
Thought * tmp;
tmp = x.root;
while ( tmp )
{
os << tmp->idea << endl;
tmp = tmp->next;
}
return os;
}
Sequence & sequence ( const string & begin, const string & end )
{
Thought * tmp;
tmp = root;
while ( tmp )
{
if ( !tmp->idea.compare( begin ) )
seq.begin = tmp;
if ( !tmp->idea.compare( end ) )
seq.end = tmp;
tmp = tmp->next;
}
return seq;
}
};
int main (){
Mem test;
test.add("prva myslienka").add("druha myslienka");
test.add("tretia myslienka").add("stvrta myslienka");
test.add("piata myslienka").add("siesta myslienka");
int x = test.sequence("tretia myslienka", "piata myslienka").length();
cout << x << endl;
cout << test.sequence("tretia myslienka", "piata myslienka") << endl;
Mem test2;
test2.add("blabla").add("blabla");
test2 = test;
}
#include <iostream>
using namespace std;
class Stack
{
struct Item
{
int value;
Item * previous;
Item(int value): value(value), previous(NULL){}
};
Item * top;
public:
Stack(): top(NULL){}
void push ( int value )
{
Item * newOne = new Item (value);
newOne -> previous = top;
top = newOne;
}
int pop ()
{
if ( ! top )
throw "stack is empty";
int result = top -> value;
Item * tmp;
if ( top -> previous ){
tmp = top;
top = top -> previous;
delete tmp;
}
else{
delete top;
top = NULL;
}
return result;
}
};
int main()
{
Stack test;
test.push(1);
test.push(2);
test.push(3);
cout << test.pop() << endl;
cout << test.pop() << endl;
cout << test.pop() << endl;
try
{
cout << test.pop() << endl;
}
catch ( const char * exc )
{
cout << exc << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment