Skip to content

Commit

Permalink
Exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashish Lal committed Nov 1, 2012
1 parent e6f2821 commit 8a1edb0
Show file tree
Hide file tree
Showing 7 changed files with 481 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 11_3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>

using namespace std;
class INT {
public:
INT() {};
INT(int j) {i=j;};
INT(const INT &j) {i=j.i;};
int operator+ (int j) { return i + j; };
int operator+ (INT k) { return i + k.i; };
int operator++ () { return ++i; };
int operator+= (int j) { return i+=j; };
INT operator= (int j) { i=j; return *this; };
INT operator= (INT j) { i=j.i; return *this; };
operator int() { return i; };
private:
int i;
};
int main()
{
INT a = 10;
cout << a << endl;
cout << a+1 << endl;
a+=1;
cout << a << endl;
}
16 changes: 16 additions & 0 deletions 11_5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>

using namespace std;

class LINT {
public:
LINT(int i) {
}
private:
char i[64];
};

int main()
{

}
102 changes: 102 additions & 0 deletions 11_7.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <iostream>
#include <cstdlib>
#include <cstring>

class String {
struct Srep {
char *s;
int sz; // number of characters
int n; // reference count
Srep(int nsz, const char *p) {
n = 1;
sz = nsz;
s = new char[sz + 1];
strcpy(s, p);
}
~Srep() { delete [] s; }
Srep *get_own_copy()
{
if(n == 1) return this;
n--;
return new Srep(sz, s);
}
void assign(int nsz, const char *p)
{
if(sz != nsz) {
delete [] s;
sz = nsz;
s = new char[sz + 1];
}
strcpy(s,p);
}
};
Srep *rep;
public:
String() { rep = new Srep(0, ""); };
String(const String &x) { x.rep->n++; rep = x.rep; };
~String() { if(--rep->n == 0) delete rep; };
String & operator=(const String &x)
{
x.rep->n++;
if(--rep->n == 0) delete rep;
rep = x.rep;
return *this;
};
String(const char *s) { rep = new Srep(strlen(s), s); }
String & operator=(const char *s)
{
if(rep->n == 1)
rep->assign(strlen(s),s);
else {
rep->n--;
rep = new Srep(strlen(s), s);
}
return *this;
}
void check(int i) const {
if (i < 0 || rep->sz <= i) std::cout << "bad" << std::endl;
}
char read(int i) const { return rep->s[i]; }
char operator[](int i) const { return rep->s[i]; }
int size() const { return rep->sz; }
};

class String_iter {
// refer to string and string element
public:
String_iter(String &s); // iterator for s
char &get() { c = str[elemptr]; return c; }; // reference to curr element
char &next(); // reference to next element
bool hasNext(); // reference to next element
private:
unsigned int elemptr; // ptr to current element
String str;
char c;
};

String_iter::String_iter(String &s)
{
elemptr = 0;
str = s;
}
char & String_iter::next()
{
elemptr++;
c = str[elemptr];
return c;
}

bool String_iter::hasNext()
{
bool ret = (elemptr < str.size())? true: false;
return ret;
}

int main()
{
String s("hello world");
String_iter itr(s);
for(char c = itr.get(); itr.hasNext(); c = itr.next()) {
std::cout << c << std::endl;
}
}
120 changes: 120 additions & 0 deletions 11_8.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include <iostream>
#include <cstdlib>
#include <cstring>

class String {
struct Srep {
char *s;
int sz; // number of characters
int n; // reference count
Srep(int nsz, const char *p) {
n = 1;
sz = nsz;
s = new char[sz + 1];
strcpy(s, p);
}
~Srep() { delete [] s; }
Srep *get_own_copy()
{
if(n == 1) return this;
n--;
return new Srep(sz, s);
}
void assign(int nsz, const char *p)
{
if(sz != nsz) {
delete [] s;
sz = nsz;
s = new char[sz + 1];
}
strcpy(s,p);
}
};
Srep *rep;
public:
String() { rep = new Srep(0, ""); };
String(int a, int b) { rep = new Srep(b-a, ""); };
String(const String &x) { x.rep->n++; rep = x.rep; };
~String() { if(--rep->n == 0) delete rep; };
String & operator=(const String &x)
{
x.rep->n++;
if(--rep->n == 0) delete rep;
rep = x.rep;
return *this;
};
String(const char *s) { rep = new Srep(strlen(s), s); }
String & operator=(const char *s)
{
if(rep->n == 1)
rep->assign(strlen(s),s);
else {
rep->n--;
rep = new Srep(strlen(s), s);
}
return *this;
}
void check(int i) const {
if (i < 0 || rep->sz <= i) std::cout << "bad" << std::endl;
}
char read(int i) const { return rep->s[i]; }
void write(int i, char c) const { rep->s[i]=c; }
char operator[](int i) const { return rep->s[i]; }
int size() const { return rep->sz; }
String & operator() (int a, int b)
{
if(a >= b) return *this;
if(b >= size()) return *this;
String *s = new String(a, b);
for(int i=0; i < (b-a); i++) {
s->write(i, rep->s[a+i]);
}
return *s;
}
};

class String_iter {
// refer to string and string element
public:
String_iter(String &s); // iterator for s
char &get() { c = str[elemptr]; return c; }; // reference to curr element
char &next(); // reference to next element
bool hasNext(); // reference to next element
private:
unsigned int elemptr; // ptr to current element
String str;
char c;
};

String_iter::String_iter(String &s)
{
elemptr = 0;
str = s;
}
char & String_iter::next()
{
elemptr++;
c = str[elemptr];
return c;
}

bool String_iter::hasNext()
{
bool ret = (elemptr < str.size())? true: false;
return ret;
}

int main()
{
String s("hello world");
String_iter itr(s);
for(char c = itr.get(); itr.hasNext(); c = itr.next()) {
std::cout << c << std::endl;
}
std::cout << "" << std::endl;
String s1(s(0, 5));
String_iter itr1(s1);
for(char c = itr1.get(); itr1.hasNext(); c = itr1.next()) {
std::cout << c << std::endl;
}
}
34 changes: 34 additions & 0 deletions 12_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>

using namespace std;
class Base {
public:
Base() {};
virtual void iam() { cout << "Base\n"; }
void iam2() { cout << "Base-iam2\n"; }
};
class Derived1: public Base {
public:
Derived1() {};
virtual void iam() { cout << "Derived1\n"; }
void iam2() { cout << "Derived1-iam2\n"; }
};

class Derived2: public Base {
public:
Derived2() {};
virtual void iam() { cout << "Derived2\n"; }
};

int main()
{
Derived1 *d1 = new Derived1;
Derived2 *d2;
Base *b = d1;
b->iam() ;
d1->iam() ;
b->iam2() ;
d2 = static_cast <Derived2 *>(b);
if(d2) d2->iam() ;
b->iam() ;
}
78 changes: 78 additions & 0 deletions 13_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <iostream>

using namespace std;

template<class T> class List {
typedef struct Link {
Link *pre;
Link *suc;
T val;
Link(Link *p, Link *s, const T& v): pre(p), suc(s), val(v) {}
} Link;
Link *head;
Link *curr;
public:
List(): head(NULL), curr(NULL) {}
~List() { for(Link *p=head; p; p=p->suc) delete p; }
List(const T & t): head(new Link(NULL, NULL, t)), curr(head) {}
void add(T t) { Link *l = new Link(NULL, NULL, t);
if(curr==NULL) curr = l;
if(head == NULL) head = l;
l->pre = curr; l->suc=NULL;
curr->suc=l;
curr = l; }
Link *begin() { return head; }
Link *end() { return curr; }
void erase(Link *l) {
for(Link *p=head; p; p=p->suc) {
if((unsigned int)p == (unsigned int)l) {
p->suc->pre = p->pre;
p->pre->suc = p->suc;
p->pre = NULL; p->suc = NULL;
delete p;
return ;
}
}
}
void remove(T t) {
for(Link *p=head; p; p=p->suc) {
if(p->val == t) {
print_ptrs();
p->suc->pre = p->pre;
p->pre->suc = p->suc;
if((unsigned int)p == (unsigned int)head) {
head = p->suc;
}
if((unsigned int)p == (unsigned int)curr) {
curr = p->pre;
}
p->pre = NULL; p->suc = NULL;
delete p;
return;
}
}
}
size_t size() {
size_t i=0;
for(Link *p=head; p; p=p->suc) i++;
return i;
}
void print_all() {
for(Link *p=head; p; p=p->suc) std::cout << p->val << std::endl;
}
void print_ptrs() {
for(Link *p=head; p; p=p->suc) std::cout << p << " " << p->pre << " " << p->suc << std::endl;
}
};

int main()
{
List<int> l;
l.add(1);
l.add(1);
l.add(2);
l.add(3);
l.print_all();
l.remove(1);
l.print_all();
}
Loading

0 comments on commit 8a1edb0

Please sign in to comment.