1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <string>
using namespace std;
 
typedef struct tTreeNode{
    struct tTreeNode *left, *right;
    int data;
}TreeNode;
 
TreeNode* createNode(TreeNode* left, int root, TreeNode* right){
    TreeNode *p = (TreeNode*)malloc(sizeof(TreeNode));
    
    p->left = left;
    p->right = right;
    p->data = root;
 
    return p;
}
 
void destroyTree(TreeNode* p){
    if( p == NULL) return;
 
    destroyTree(p->left);
    destroyTree(p->right);
    cout<<"node remove"<<endl;
    free(p);
}
 
void preorder(TreeNode* root){
    if(root == NULL) return;
 
    cout<<root->data;
    preorder(root->left);
    preorder(root->right);
}
 
void postorder(TreeNode* root){
    if(root == NULL) return;
    
    postorder(root->left);
    postorder(root->right);
    cout<<root->data;
}
 
void inorder(TreeNode* root){
    if(root == NULL ) return;
 
    inorder(root->left);
    cout<<root->data;
    inorder(root->right);
 
}
 
void insertNode(TreeNode* root, int item){
    
}
 
TreeNode* addTree(TreeNode* root, TreeNode* NewNode){
    if( root == NULL) return NewNode;
 
    if( root->data > NewNode->data ){
        root->left = addTree(root->left, NewNode);
    }else{//root->data < newNode->data;
        root->right = addTree(root->right , NewNode);
    }
 
    return root;
}
 
void main(){
    TreeNode* root = createNode(NULL,7,NULL);
    
    TreeNode* NewNode = createNode(NULL, 5, NULL);
    TreeNode* NewNode2 = createNode(NULL,3,NULL);
    TreeNode* NewNode3 = createNode(NULL,8,NULL);
    TreeNode* NewNode4 = createNode(NULL,10,NULL);
    
    
    root = addTree(root ,NewNode);
    root = addTree(root ,NewNode2);
    root = addTree(root ,NewNode3);
    root = addTree(root ,NewNode4);
 
    cout<<"inorder";
    inorder(root);
    cout<<endl;
    cout<<"preorder";
    preorder(root);
    
    
    cout<<endl;
    destroyTree(root);
 
}


설정

트랙백

댓글

tree2

전전컴/C++ 2013. 10. 28. 23:46





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <string>
using namespace std;
 
typedef struct tTreeNode{
    struct tTreeNode *left, *right;
    char data;
}TreeNode;
 
TreeNode* createNode(TreeNode* left, char root, TreeNode* right){
    TreeNode *p = (TreeNode*)malloc(sizeof(TreeNode));
    
    p->left = left;
    p->right = right;
    p->data = root;
 
    return p;
}
 
void destroyTree(TreeNode* p){
    if( p == NULL) return;
 
    destroyTree(p->left);
    destroyTree(p->right);
    cout<<"node remove"<<endl;
    free(p);
}
 
void preorder(TreeNode* root){
    if(root == NULL) return;
 
    cout<<root->data;
    preorder(root->left);
    preorder(root->right);
}
 
void postorder(TreeNode* root){
    if(root == NULL) return;
    
    postorder(root->left);
    postorder(root->right);
    cout<<root->data;
}
 
void inorder(TreeNode* root){
    if(root == NULL ) return;
 
    inorder(root->left);
    cout<<root->data;
    inorder(root->right);
 
}
 
void main(){
    TreeNode* a = createNode(NULL, 'a', NULL);
    TreeNode* b = createNode(NULL, 'b', NULL);
    TreeNode* c = createNode(NULL, 'c', NULL);
    TreeNode* d = createNode(NULL, 'd', NULL);
 
    TreeNode* plus1 = createNode(b, '+', c);
    TreeNode* multi = createNode(a, '*', plus1);
    TreeNode* plus2= createNode(multi, '+', d);
    
    cout<<"Preorder========"<<endl;
    preorder(plus2);
    cout<<endl;
    cout<<"Postorder======="<<endl;
    postorder(plus2);
    cout<<endl;
    cout<<"Inorder========="<<endl;
    inorder(plus2);
    cout<<endl;
 
 
    destroyTree(plus2);
 
}


설정

트랙백

댓글



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
using namespace std;
 
 
class Smart{
    int app;
    string color;
 
public:
 
    
    Smart():app(10),color("white"){
        cout<<"Default 생성자"<<endl;
    }
    Smart( int x, string y):app(x),color(y){
        cout<<"매개변수가 있는 생성자"<<endl;
    }
    ~Smart(){
        cout<<"소멸자"<<endl;
    }
    int getApp(){
        return app;
    }//getApp()
 
    string getColor() {
        return color;
    }//getColor()
    void setApp(int num){
        app = num;
    }
    void setColor(string _color){
        color = _color;
    }
    void display(){
        cout<<getApp()<<' '<<getColor()<<endl; 
    }//display()
    
    Smart(Smart& a):app(a.app),color(a.color){
        cout<<"복사생성자"<<endl;
    }
 
 
};
 
 
 
void main(){
    Smart* a  = new Smart[3];
    a->setApp(10);
    a->setColor("pink");
    
    Smart* p = a;
    (p+1)->setApp(20);
    (p+1)->setColor("RED");
 
    (p+2)->setApp(30);
    (p+2)->setColor("BLUE");
 
    forint i =0; i<3; i++,p++){
        cout<<p->getApp()<<' '<<a[i].getApp()<<endl;
        cout<<p->getColor()<<' ' <<a[i].getColor()<<endl;
    }
    //Smart a[3]={Smart(20,"red"), //임시 객체
    //            Smart(30,"pink"),
    //            Smart(120,"blue")};
 
    delete[] a;
}


설정

트랙백

댓글

class 배열

전전컴/C++ 2013. 10. 23. 13:12



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>
using namespace std;
 
 
class Smart{
    int app;
    string color;
 
public:
 
    
    Smart():app(10),color("white"){
        cout<<"Default 생성자"<<endl;
    }
    Smart( int x, string y):app(x),color(y){
        cout<<"매개변수가 있는 생성자"<<endl;
    }
    ~Smart(){
        cout<<"소멸자"<<endl;
    }
    int getApp(){
        return app;
    }//getApp()
 
    string getColor() {
        return color;
    }//getColor()
 
    void display(){
        cout<<getApp()<<' '<<getColor()<<endl; 
    }//display()
    
    Smart(Smart& a):app(a.app),color(a.color){
        cout<<"복사생성자"<<endl;
    }
 
 
};
 
 
 
void main(){
    
    Smart a[3]={Smart(20,"red"), //임시 객체
                Smart(30,"pink"),
                Smart(120,"blue")};
    Smart *p = a;
    for(int i = 0; i<3; i++,p++){
        a[i].display();
        (p)->display();
    }
 
}


설정

트랙백

댓글

5주차

전전컴/C++ 2013. 10. 23. 12:29

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
using namespace std;
 
class Battery{
    int volt;
    string stuff;
public:
    int getVolt(){ return volt; }
    string getstuff(){         return stuff;    }
    void display(){    cout<<volt<<" "<<stuff<< endl;}
    
    Battery():volt(10),stuff("nothing"){ cout<<"Battery default 생성자"<<endl;    } //default 생성자
    Battery(int _volt, string _stuff="nothing"):volt(_volt),stuff(_stuff){ cout<<"Battery 매개변수가 있는 생성자"<<endl; }
    //int _app=10 한다면 overload에 대한 모호성 존재
 
    ~Battery(){    cout<<"Battery 소멸자"<<endl;    }
    Battery(Battery& a){
        volt=a.volt;        stuff=a.stuff;
        cout<<"Battery default 복사 생성자"<<endl;
    }
};
 
class Smart{
    int app;
    string color;
    Battery b; //객체멤버
public:
    int getApp(){ return app; }
    string getcolor(){         return color;    }
    void display(){    cout<<app<<" "<<color <<" "<<b.getVolt()<<" "<<b.getstuff()<< endl;}
    
    Smart():app(10),color("white"){ cout<<"default 생성자"<<endl;    } //default 생성자
    Smart(int _app, string _color, int _volt ,string _stuff):app(_app),color(_color),b(_volt,_stuff) { cout<<"매개변수가 있는 생성자"<<endl; }
    //int _app=10 한다면 overload에 대한 모호성 존재
 
    ~Smart(){    cout<<"소멸자"<<endl;    }
    Smart(Smart& a):b(a.b){
        app=a.app;    color=a.color;
        cout<<"default 복사 생성자"<<endl;
    }
 
};
 
void main(){
    Smart a(30,"red",4,"Li");
    Smart b(a);
    a.display();
    b.display();
 
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
using namespace std;
 
class Smart{
    int app;
    string color;
    static int count;
public:
    
 
    int getApp() const{ return app; }
    string getcolor() const{         return color;    }
 
    void display(){    cout<<app<<"=="<<color << endl;} 
 
    void talk(){ cout<<"통화"<<endl;}
 
    static int getCount(){    return count; }  
    //정적함수, 객체가 공유, 정적변수와 지역변수만 사용가능
    Smart():app(10),color("white"){ 
        cout<<"default 생성자"<<endl;
        count++;
    } //default 생성자
    Smart(int _app, string _color):app(_app),color(_color) {
        cout<<"매개변수가 있는 생성자"<<endl; 
        count++;
    }
    //int _app=10 한다면 overload에 대한 모호성 존재
 
    ~Smart(){
        cout<<"소멸자"<<endl;    
        count--;
    }
    
    Smart(Smart& a){
        app=a.app;    color=a.color;
        cout<<"default 복사 생성자"<<endl;
    }
    
 
};
 
int Smart::count = 0; //정적변수의 초기화는 클래스 밖에서 함
 
void main(){
 
    Smart a(20,"red");
    Smart b;
    cout<<b.getCount()<<endl;
    Smart c(30,"blue");
    cout<<c.getCount()<<endl;
    cout<<Smart::getCount()<<endl;
    a.display(); 
    int d= b.getCount();
 
}


설정

트랙백

댓글