to myself 的分类学习日志

做自己想做的事
posts - 232, comments - 6, trackbacks - 0, articles - 0

C/C++点滴(二)

Posted on 2010-10-13 23:43 kongkongzi 阅读(209) 评论(0)  编辑 收藏 引用 所属分类: c programming
/* create and destroy a two-dimension array. */
#include 
<iostream>
#include 
<cstdlib>
using namespace std;
int** create_td_arr(int row, int col)
{
    
int **arr = NULL;

    arr 
= new int*[row];
    
for (int i = 0; i < row; ++i)
        arr[i] 
= new int[col];

    
int num = 0;
    
for (int i = 0; i < row; ++i)
        
for (int j = 0; j < col; ++j)
            arr[i][j] 
= num++;

    
return arr;
}
int destroy_td_arr(int **arr, int row)
{
    
if (arr = NULL)
        
return 0;

    
for (int i = 0; i < row; ++i)
        
if (arr[i]) {
            delete[] arr[i];
            arr[i] 
= NULL;
        }
    
if (arr) {
        delete[] arr;
        arr 
= NULL;
    }
    
return 0;
}

/* find these bugs from under codes.*/
#include 
<iostream>
using namespace std;

class A {
public:
    A() { cout 
<< "ctor is called" << endl; str = new char[10]; }
    A(
const A& a) { cout << "cpy ctor is called" << endl; }
    
~A() { cout << "dtor is called" << endl; delete[] str; }
private:
    
char *str;
};

void foo(A a) {}
// 1.void foo(A &a) {}

int main()
{
    A b;
    foo(b);
    
return 0;
}

/* creat a template class -- Stack, using template class Node. */
#include 
"stack.h"
int main() {
    
char *str = "I am a student";
    
    Stack
<char *> sc;
    sc.push(str);
    cout 
<< sc.top() << endl;
    
while (sc.count())
        cout 
<< sc.pop() << endl;
/*
    char *p;
    Stack<char> sc1, sc2;

    for (p = str; *p; ++p)
        sc1.push(*p);
    while (sc1.count()) {
        while (sc1.count() && sc1.top() != ' ')
            sc2.push(sc1.pop());
        while (sc2.count())
            cout << sc2.pop();
        if (sc1.top() == ' ')
            cout << sc1.pop();
    } 
*/
    
return 0;
}

#include 
<iostream>
using namespace std;
/*
 * template class Node.
 
*/
template
<class T> class Node {
public:
    Node(T invalue): m_Value(invalue), m_Next(
0) {}
    
~Node();

    T getValue() 
const { return m_Value; }
    
void setValue(T value) { m_Value = value; }
    Node
<T>* getNext() const { return m_Next; }
    
void setNext(Node<T>* next) { m_Next = next; }
private:
    T m_Value;
    Node
<T>* m_Next;
};

template
<class T> Node<T>::~Node() {
//    cout << m_Value << "deleted" << endl;
    if (m_Next) {
        delete m_Next;
    }
}

/*
 * template class Stack.
 
*/
template
<class T> class Stack {
public:
    Stack(): m_Head(
0), m_Count(0) {}
    
~Stack<T>() { delete m_Head; }
    
void push(const T& t);
    T pop();
    T top() 
const;
    
int count() const;
private:
    Node
<T> *m_Head;
    
int m_Count;
};

template
<class T> void Stack<T>::push(const T& value) {
    Node
<T> *newNode = new Node<T>(value);
    newNode
->setNext(m_Head);
    m_Head 
= newNode;
    
++m_Count;
}

template
<class T> T Stack<T>::pop() {
    Node
<T> *popped = m_Head;
    
if (m_Head != 0) {
        m_Head 
= m_Head->getNext();
        T retval 
= popped->getValue();
        popped
->setNext(0);
        delete popped;
        
--m_Count;
        
return retval;
    }
    
return 0;
}

template
<class T> inline T Stack<T>::top() const {
    
return m_Head->getValue();
}

template
<class T> inline int Stack<T>::count() const {
    
return m_Count;
}

/* create a class Matrix. */
class Matrix {
public:
    friend Matrix 
operator+(const Matrix &a, const Matrix &b);
    Matrix(
int r, int c): row(r), col(c) {
        arr 
= new int*[row];
        
for (int i = 0; i < row; ++i)
            arr[i] 
= new int[col];
    }
    Matrix(
const Matrix &mtx) {
        row 
= mtx.row;
        col 
= mtx.col;
        arr 
= new int*[row];
        
for (int i = 0; i < row; ++i)
            arr[i] 
= new int[col];
        
for (int i = 0; i < row; ++i)
            
for (int j = 0; j < col; ++j)
                arr[i][j] 
= mtx.getValue(i, j);
    }
    
~Matrix() {
        
for (int i = 0; i < row; ++i) {
            delete[] arr[i];
            arr[i] 
= NULL;
        }
        delete[] arr;
        arr 
= NULL;
    }
    
void setValue(int i, int j, int value) {
        arr[i][j] 
= value;
    }
    
int getValue(int i, int j) const {
        
return arr[i][j];
    }
    
void display() const {
        
for (int i = 0; i < row; i++) {
            
for (int j = 0; j < col; j++)
                cout 
<< arr[i][j] << '\t';
            cout 
<< endl;
        }
    }
    
int getRow() const {
        
return row;
    }
    
int getCol() const {
        
return col;
    }
    Matrix 
&operator=(const Matrix &mtx) {
        
if (this == &mtx)
            
return *this;
        
if (mtx.row != row || mtx.col != col) {
            cout 
<< "row or col not equal, can not '='" << endl;
            
return *this;
        }
            
        
for (int i = 0; i < row; ++i)
            
for (int j = 0; j < col; ++j)
                arr[i][j] 
= mtx.arr[i][j];
        
return *this;
    }
    Matrix transpose() 
const{
        
if (row != col)
            
return *this;
        Matrix temp( 
*this );
        
for (int i = 0; i < row; ++i)
            
for (int j = i+1; j < col; ++j) {
                    
int tmp = temp.arr[i][j];
                    temp.arr[i][j] 
= temp.arr[j][i];
                    temp.arr[j][i] 
= tmp;
            }
        
return temp;
    }

private:
    
int row;
    
int col;
    
int **arr;
};

Matrix 
operator+(const Matrix &a, const Matrix &b) {
    Matrix s( a );
    
if (a.getRow() != b.getRow() || a.getCol() != b.getCol()) {
        cout 
<< "row or col not equal, can not '+'" << endl;
        
return s;
    }
    
for (int i = 0; i < s.row; ++i) 
        
for (int j = 0; j < s.col; ++j) {
            s.setValue( i, j, a.getValue(i, j) 
+ b.getValue(i, j) );
        }
    
return s;
}

ostream 
&operator<< (ostream &outconst Matrix &mtx)
{
    mtx.display();
    
return out;
}

/* student.h */
#ifndef STUDENT_H
#define STUDENT_H

class Person
{
public:
    Person(
const char *name, int age);
    
~Person();
    
void display() const;
protected:
    
char *name;
    
int age;
};

class Student:public Person
{
public:
    Student(
const char *name, int age, int score);
    
~Student();
    
void display() const;
private:
    
int score;
};

#endif //STUDENT_H

/* student.cpp */
#include 
<iostream>
#include 
<cstring>
#include 
"student.h"
using namespace std;

Person::Person(
const char *name, int age)
    :name(NULL), age(age)
{
    cout 
<< "create person" << endl;
    
this->name = new char[strlen(name)+1];
    strcpy(
this->name, name);
}

void Person::display() const
{
    cout 
<< "<<<< display person information >>>>" << endl
        
<< "Name:" << name << endl << "Age:" << age << endl;
}

Person::
~Person()
{
    cout 
<< "delete person" << endl;
    delete[] name;
}

Student::Student(
const char *name, int age, int score)
    :Person(name, age), score(score)
{
    cout 
<< "create student" << endl;
}

void Student::display() const
{
    cout 
<< "<<<< display student information >>>>" << endl
        
<< "Name:" << name << endl << "Age:" << age << endl
        
<< "Score" << score << endl;
}

Student::
~Student()
{
    cout 
<< "delete"
}

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理