stackプログラム

1つのオブジェクトのすべてのデータメンバがもう1つのオブジェクトに代入されます。

#include < iostream >
using namespace std;

#define SIZE 10

class stack {
char stck[SIZE];
int tos;
public:
stack();
void push(char ch);
char pop();
};

stack::stack()
{
cout << "creat stack\n"; tos = 0; } void stack::push(char ch) { if(tos==SIZE){ cout << "stack is full\n"; return; } stck[tos] = ch; tos++; } char stack::pop() { if(tos==0){ cout << "stack is empty\n"; return 0; } tos--; return stck[tos]; } int main() { stack s1, s2; int i; s1.push('a'); s1.push('b'); s1.push('c'); s2 = s1; for(i=0; i < 3; i++) cout << "pop s1: " << s1.pop() << "\n"; for(i=0; i < 3; i++) cout << "pop s2: " << s2.pop() << "\n"; return 0; }

オブジェクトの代入

型が同じであれば、1つのオブジェクトをもう1つのオブジェクトに代入することができます。

#include 
using namespace std;

class myclass {
    int a, b;
public:
    void set(int i, int j){ a = i; b = j; }
    void show() { cout << a << ' ' << b << '\n'; }
};

int main()
{
    myclass o1, o2;
    
    o1.set(10, 4);
    
    o2 = o1;
    
    o1.show();
    o2.show();
    
    return 0;
}

オーバーロード関数のインライン化

各関数はinlineを使用して宣言。

#include < iostream >
using namespace std;

inline int min(int a, int b)
{
    return a < b ? a: b;
}

inline long min(long a, long b)
{
    return a < b ? a : b;
}

inline double min(double a, double b)
{
    return a < b ? a : b;
}

int main()
{
    cout << min(-10, 10) << "\n";
    cout << min(-10.01, 100.002) << "\n";
    cout << min(-10L, 12L) << "\n";
    
    return 0;
}

インライン関数

関数はどのようなものでもインライン化することができます。

#include 
using namespace std;

class samp {
    int i, j;
public:
    samp(int a, int b);
    int divisible();
};

samp::samp(int a, int b)
{
    i = a;
    j = b;
}

inline int samp::divisible()
{
    return !(i%j);
}

int main()
{
    samp ob1(10, 2), ob2(10, 3);
    
    if(ob1.divisible()) cout << "10 can be divided 2.\n";
    
    if(ob2.divisible()) cout << "10 can be divided 3.\n";
    
    return 0;
}

無名共用体

#include 
using namespace std;

int main()
{
    union {
        unsigned char byte[8];
        double value;
    };
    int i;
    value = 859345.324;
    for(i=0; i<8; i++)
        cout << (int) bytes[i] << " ";
    
    return 0;
}

コンストラクタとデストラクタ

#include 
#include 
#include 
using namespace std;

struct strtype {
    strtype(char *ptr);
    ~strtype();
    void show();
private:
    char *p;
    int len;
};

strtype::strtype(char *ptr)
{
    len = strlen(ptr);
    p = (char *) malloc(len+1);
    if(!p){
        cout << "error memory\n";
        exit(1);
    }
    strcpy(p, ptr);
}

strtype::~strtype()
{
    cout << "release p\n";
    free(p);
}

void strtype::show()
{
    cout << p << " - long: " << len;
    cout << "\n";
}

int main()
{
    strtype s1("this is a test."), s2("I like c++.");
    
    s1.show();
    s2.show();
    
    return 0;
}

継承

#include 
#include 
using namespace std;

enum yn {no, yes};
enum color {red, yellow, green, orange};
void out(enum yn x);
char *c[] = {
    "red","yellow","green","orange"
};

class fruit {
public:
    enum yn annual;
    enum yn perennial;
    enum yn tree;
    enum yn tropical;
    enum color clr;
    char name[40];
};

class Apple : public fruit {
    enum yn cooking;
    enum yn crunchy;
    enum yn eating;
public:
    void seta(char *n, enum color c, enum yn ck, enum yn crchy, enum yn e);
    void show();
};

class Orange : public fruit {
    enum yn juice;
    enum yn sour;
    enum yn eating;
public:
    void seto(char *n, enum color c, enum yn j, enum yn sr, enum yn e);
    void show();
};

void Apple::seta(char *n, enum color c, enum yn ck, enum yn crchy, enum yn e)
{
    strcpy(name, n);
    annual = no;
    perennial = yes;
    tree = yes;
    tropical = no;
    clr = c;
    cooking = ck;
    crunchy = crchy;
    eating = e;
}

void Orange::seto(char *n, enum color c, enum yn j,
                  enum yn sr, enum yn e)
{
    strcpy(name, n);
    annual = no;
    perennial = yes;
    tree = yes;
    tropical = yes;
    clr = c;
    juice = j;
    sour = sr;
    eating = e;
}

void Apple::show()
{
    cout << name << "apple: " << "\n";
    cout << "plant :"; out(annual);
    cout << "ta plant:"; out(perennial);
    cout << "tree:"; out(tree);
    cout << "nettai: "; out(tropical);
    cout << "color:" << c[clr] << "\n";
    cout << "for cook" ; out(cooking);
    cout << "solid: "; out(crunchy);
    cout << "food: "; out(eating);
    cout << "\n";
}

void Orange::show()
{
    cout << name << "orange: " << "\n";
    cout << "plant :"; out(annual);
    cout << "ta plant:"; out(perennial);
    cout << "tree:"; out(tree);
    cout << "nettai: "; out(tropical);
    cout << "color:" << c[clr] << "\n";
    cout << "juice" ; out(juice);
    cout << "suppi: "; out(sour);
    cout << "food: "; out(eating);
    cout << "\n";
}

void out(enum yn x)
{
    if(x==no) cout << "no\n";
    else cout << "yes\n";
}

int main()
{
    Apple a1, a2;
    Orange o1, o2;
    
    a1.seta("red delicious", red, no, yes, yes);
    a2.seta("jonasson", red, yes, no, yes);
    
    o1.seto("neble", orange, no, no, yes);
    o2.seto("ballensia", orange, yes, yes, no);
    
    a1.show();
    a2.show();
    
    o1.show();
    o2.show();
    
    return 0;
}

コンストラクタとデストラクタ

malloc()とfree()関数を使って、メモリの割り当てと解放をしています。

#include 
#include 
#include 
using namespace std;

#define SIZE 255

class strtype {
    char * p;
    int len;
public:
    strtype();
    ~strtype();
    void set(char *ptr);
    void show();
};

strtype::strtype()
{
    p = (char *) malloc(SIZE);
    if(!p){
        cout << "error memory\n";
        exit(1);
    }
    *p = '\0';
    len = 0;
}

strtype::~strtype()
{
    cout << "release p\n";
    free(p);
}

void strtype::set(char *ptr)
{
    if(strlen(ptr) >= SIZE){
        cout << "character is bigger \n";
        return;
    }
    strcpy(p, ptr);
    len = strlen(p);
}

void strtype::show()
{
    cout << p << " - long: " << len;
    cout << "\n";
}

int main()
{
    strtype s1, s2;
    
    s1.set("this is a test.");
    s2.set("I like C++.");
    s1.show();
    s2.show();
    
    return 0;
}

stack class

stckとtosの非公開変数

#include 
using namespace std;

#define SIZE 10

class stack {
    char stck[SIZE];
    int tos;
    
public:
    void init();
    void push(char ch);
    char pop();
};

void stack::init()
{
    tos = 0;
}

void stack::push(char ch)
{
    if(tos==SIZE){
        cout << "stack is full";
        return;
    }
    stck[tos] = ch;
    tos++;
}

char stack::pop()
{
    if(tos==0){
        cout << "tack is empty";
        return 0;
    }
    tos--;
    return stck[tos];
}

int main()
{
    stack s1, s2;
    int i;
    
    s1.init();
    s2.init();
    s1.push('a');
    s2.push('x');
    s1.push('b');
    s2.push('y');
    s1.push('c');
    s2.push('z');
    for(i=0; i<3; i++) cout << "s1 pop:" << s1.pop() << "\n";
    for(i=0; i<3; i++) cout << "s2 pop:" << s2.pop() << "\n";
    
    return 0;
}

二分探索木の形状

#include 
#include 
#include 

#define DATANUM 1000

struct BinarySearchTree{
    int data;
    struct BinarySearchTree *left;
    struct BinarySearchTree *right;
};

struct BinarySearchTree bst[DATANUM];

int makeBinarySearchTree(){
    struct BinarySearchTree *current;
    int i, depth, maxDepth;
    
    srand((unsigned int)time(NULL));
    bst[0].data = rand();
    bst[0].left = NULL;
    bst[0].right = NULL;
    maxDepth = 0;
    
    for (i = 1; i < DATANUM; i++){
        bst[i].data = rand();
        bst[i].left = NULL;
        bst[i].right = NULL;
        
        current = &bst[0];
        
        depth = 1;
        while (-1){
            if(bst[i].data < current->data){
                if (current->left == NULL){
                    current->left = &bst[i];
                    break;
                }
                else {
                    current = current->left;
                    depth++;
                }
            }
            else {
                if (current->right = NULL){
                    current->right = &bst[i];
                    break;
                }
                else {
                    current = current->right;
                    depth++;
                }
            }
        }
        
        if (depth > maxDepth) maxDepth = depth;
    }
    
    return maxDepth;
}

int main(){
    int i, sum;
    
    sum = 0;
    for (i = 1; i <= 10000; i++){
        sum += makeBinarySearchTree();
    }
    
    printf("DATANUM = %d\n", DATANUM);
    printf("max depth average is = %lf\n", sum / 10000.0);
    
    return 0;
}