8.29.2017

08.29 C++

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include
<string>

using namespace std;

class Test {
        char* name;
public:
        Test() {

        }
        Test(char* p) {
                 name = p;
        }
        Test(const Test& p) {
                name = new char[strlen(p.name) + 1];
                strcpy(name, p.name);
        }
        void print() {
                printf("%s \n", name);
        }
};

void main() {
        Test t1("Test1");
        t1.print();
        Test t2 = t1;
        t2.print();
}

========================================================================
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include
<string>

using namespace std;


class Student {
        char* name, *studentNum;
        int kor, math, eng;
public:
        Student(char* n, char* stN, int k, int m, int e) {
                name = n;
                studentNum = stN;
                kor = k;
                math = m;
                eng = e;
        }
        Student(const Student& s) {
                name = new char[strlen(s.name) + 1];
                strcpy(name, s.name);
                studentNum = new char[strlen(s.studentNum) + 1];
                strcpy(studentNum, s.studentNum);
                kor = s.kor;
                math = s.math;
                eng = s.eng;
        }
        void show() {
                cout << "이름은 : " << name << ", 학번은 : " << studentNum << ", 국어는 : " << kor <<
                   ", 수학은 : " << math << ", 영어는 : " << eng << endl;
        }
        /*~Student() {
                delete name;
                delete studentNum;
        }*/

};

void main() {
        Student s1("이름", "2017", 8,9,10);
        Student s2 = s1;
        Student s3 = s1;
        s1.show();
        s2.show();
        s3.show();
        // delete , new : 동적할당 된 곳에서만 사용 가능
        // delete s1;

}

8.26.2017

선택정렬

#include <stdio.h>

void main() {
        /*
        정렬 : 선택정렬, 버블정렬, 퀵정렬

        선택정렬 : 최고 낮은 값을 찾아서 앞에서부터 차례대로 쌓아주는 기법
        */
        int arr[] = { 5,72,3,1,92,7,25,19,13 };
        int min; //낮은 값을 갖는 변수
        int min_index; //낮은 값의 위치(인덱스)

        for (int i = 0; i < sizeof(arr) / sizeof(int); i++) {
                printf("%d ", arr[i]);
        }printf("\n");
        for (int i = 0; i < sizeof(arr) / sizeof(int) - 1; i++) {
                //기준값을 잡을 반복문
                min = arr[i];
                min_index = i;

                for (int k = i + 1; k < sizeof(arr) / sizeof(int); k++) {
                        //가장 낮은 값을 찾기 위한 반복문
                        if (arr[k] < min) {
                                min = arr[k];
                                min_index = k;
                        }
                }//end for(k)
                arr[min_index] = arr[i];
                arr[i] = min;

        }
        for (int i = 0; i < sizeof(arr) / sizeof(int); i++) {
                printf("%d ", arr[i]);
        }printf("\n");
}

doublyLinkedList 자료구조

#include <stdio.h>
#include <stdlib.h>

typedef struct Test{
        int data;
        struct Test* next; //다음노드의 주소
        struct Test* prev; //이전 노드의 주소
}test;

void main() {
        test* head = (test*)malloc(sizeof(test));
        test* tail = (test*)malloc(sizeof(test));
     
        head->next = tail;
        tail->prev = head;

        test* newnode1 = (test*)malloc(sizeof(test));
        newnode1->next = tail;
        tail->prev = newnode1;
        head->next = newnode1;
        newnode1->prev = head;

        test* newnode2 = (test*)malloc(sizeof(test));
        newnode2->next = tail;
        tail->prev = newnode2;
        newnode1->next = newnode2;
        newnode2->prev = newnode1;

        test* newnode3 = (test*)malloc(sizeof(test));
        newnode3->next = tail;
        tail->prev = newnode3;
        newnode2->next = newnode3;
        newnode3->prev = newnode2;

        newnode1->data = 10;
        newnode2->data = 20;
        newnode3->data = 30;

        test* move = head->next;
        test* move2 = tail->prev;
        while (move != tail) {
                printf("%d", move->data);
                move = move->next;
        }printf("\n"); // 출력
        while (move2 != head) {
                printf("%d", move2->data);
                move2 = move2->prev;
        } printf("\n");
}

========================================================================
//함수 이용하기

#include <stdio.h>
#include <stdlib.h>

typedef struct Test{
        int data;
        struct Test* next; //다음노드의 주소
        struct Test* prev; //이전 노드의 주소
}test;

void input(testhead, int a) {
        test* newnode = (test*)malloc(sizeof(test));
        newnode->next = head->next;
        head->next->prev = newnode;
        head->next = newnode;
        newnode->prev = head;
        newnode->data = a;
}

void del_Node(testheadtest* tail, int b) {
        test* move = head->next;
        while (move != tail) {
                if (move->data == b) {
                        move = move->next;
                        move->next->next->prev = move->next->prev;
                        free(move);
                        break;
                }
        }
}

void main() {
        int del;
        test* head = (test*)malloc(sizeof(test));
        test* tail = (test*)malloc(sizeof(test));

        head->next = tail;
        tail->prev = head;

        input(head, 10);
        input(head, 20);
        input(head, 30);
        input(head, 40);
        input(head, 50);

        printf("입력\n");
        scanf("%d", &del);
        del_Node(head, tail, del);

        test* move = head->next;
        while (move != tail) {
                printf("%d ", move->data);
                move = move->next;
        }printf("\n");
}

8.24.2017

08.24 C++

#include <iostream>
#include <string>

using namespace std;

class Human{
public:
        string name;
        int age;
public:
        Human(){
                cout << "이름과 나이를 입력하시오.\n";
                cin >> name;
                cin >> age;
        }
        string returnName() {
                return name;
        }
        void print() {
                cout << "이름은 : " << name << "나이는 : " << age << endl;
        }
};

class Student public Human {
        int korean, math, english;
public:
        void s_input() {
                cout << "학생의 국어, 수학, 영어 점수를 입력하시오." << endl;
                cin >> korean >> math >> english;
        }
        void printStudent(){
                print();
                cout << "국어 점수 : " << korean << ", 수학 점수 : " << math << ", 영어 점수 : " << english << endl;
        }
};

class Teacher : public Human {
        Student* s[3];
        int index;
public:
        Teacher() {
                index = 0;
        }
        void addStudent(Student* p) {
                if (index > 2) {
                        cout << "학생 추가 불가" << endl;
                        return;
                }
                s[index] = p;
                index++;

        void printTeacher(){
                print();
                for (int i = 0; i < index; i++)
                        s[i]->printStudent();
         }
};

void main() {
        Student* s[10];
        Teacher* t[3];

        string name;
        int input = 0, i = 0, j = 0;
        while (input != 6) {
                cout << "[menu]\n1.학생추가 2.선생님추가 3.학생출력 4.선생님정보출력 5. 선생님의 학생 추가 6.종료" << endl;
        cin >> input;
                if (input == 1) {
                        s[i] = new Student();
                        s[i] -> s_input();
                        i++;
                }
                else if (input == 2) {
                        cout << "관리할 학생의 이름을 입력해주세요." << endl;
                        cin >> name;
                        for (int k = 0; k < i; k++) {
                                if (name == s[k]->returnName()) { // (*s[k]).returnName()
                                        t[j] = new Teacher();
                                        t[j]->addStudent(s[k]);
                                        j++;
                                        break;
                                }
                        }
                }
                else if (input == 3) {
                        for (int p = 0; p < i; p++) {
                                s[p]->printStudent();
                        }
                }
                else if (input == 4) {
                        //선생님의 이름, 나이, 학생명단출력
                        for (int q = 0; q < j; q++) {
                                t[q]->printTeacher();
                        }
                }
                else if (input == 5) {
                        cout << "어느 선생님을 바꾸실건가요?" << endl;
                        cin >> name;
                        for (int u = 0; u < j; u++) {
                                if (name == t[u]->returnName()) {
                                        cout << "학생 이름 입력해주세요." << endl;
                                        cin >> name;
                                        for (int k = 0; k < i; k++){
                                                if (name == s[k]->returnName())
                                                t[u]->addStudent(s[k]);
                                        }
                                }
                        }
                }
        }
}

===========================================================================================

#include <iostream>
#include <string>

using namespace std;

class Score {
        int kor, math, eng;
        string name;
public:
        Score(int a, int b, int c, string d) {
                kor = 0;
                math = 0;
                eng = 0;
                name = d;
        }
        void change_value() (int p, int q, int r) {
                kor = p;
                math = q;
                eng = r;
        }
        void change_value(string a) {
                name = a;
        }
        void printOut() {
                cout << "국어 점수는 : " << kor << "수학 점수는 : " << math << "영어 점수는 : " << eng << "\n이름은 : " << name << endl;
        }
};

void main(){
        int input = 0, score1, score2, score3;
        string name;
        Score s(12, 13, 14, "점수");
        while (input != 4) {
                cout << "[menu]\n1.점수 변경 2.이름변경 3.모두 출력 4.종료" << endl;
                cin >> input;
                if (input == 1) {
                        cout << "변경할 점수를 입력해주세요." << endl;
                        cin >> score1 >> score2 >> score3;
                        s.change_value(score1, score2, score3);
                }
                else if (input == 2) {
                        cout << "변경할 이름을 입력해주세요." << endl;
                        cin >> name;
                        s.change_value(name);
                }
                else if (input == 3) {
                        s.printOut();
                }
        }
}

===========================================================================================

#include <iostream>
#include <string>

using namespace std;

class hamburger {
protected :
        string name;
        int price, number;
public:
        /*
        hamburger(string name, int price, int number) {
                this->name = name;
                this->price = price;
                this->number = number;
        }
        */
        void show_info() {
                cout << name << price << number;
        }
};

class Set_menu :public hamburger{

public:
        Set_menu(string name, int price, int number)// : hamburger(name, price, number) {
                this->name = name;
                this->price = price;
                this->number = number;
        }
        // overriding
        void show_info() {
                cout << name << price - 500 << number;
        }
};

void main() {
        Set_menu s1("불고기", 2000, 5);
        s1.show_info();
}

8.22.2017

08.22 C++

#include <iostream>
#include <string>

using namespace std;

class drink {
private:
        string name;
        int number, price;
public:
        drink() {
                name = "이름";
                number = 6;
                price = 10;
        }
        void insert() {
                cout << "음료수 이름을 입력하시오." << endl;
                cin >> name;
                cout << "음료수 개수를 입력하시오." << endl;
                cin >> number;
                cout << "음료수 가격을 입력하시오." << endl;
                cin >> price;
        }
        string returnname() {
                return name;
        }
        void printall() {
                cout << "음료수 명 : " << name << ", 음료수 개수 : " << number << ", 음료수 가격 : " << price << endl;
        }
         ~drink() {
        }
};

void main() {
        drink* d[8];

        string name;
        int input = 0, i = 0;
        while (input != 4) {
                cout << "[menu]\n1.물품추가 2.삭제 3.출력 4.종료\n";
                cin >> input;
                if (input > 4) {
                        cout << "1 ~ 4 사이의 수를 입력해주세요. " << endl;
                }
                else if (input == 1) {
                        d[i] = new drink();
                        d[i]->insert();
                        i++;
                }
                else if (input == 2) {
                        cout << "삭제할 음료수는?" << endl;
                        cin >> name;
                        for (int j = 0; j < i; j++) {
                                if (name == d[j]->returnname()) {
                                        delete d[j];
                                        for (int p = 0; p < i-1; p++) {
                                                d[p] = d[p + 1];
                                        } i--;
                                        break;
                                }
                        }
                }
                else if (input == 3) {
                        for (int k = 0; k < i; k++) {
                                d[k]->printall();
                        }
                }
        }
}

======================================================================================

#include <iostream>
#include <string>

using namespace std;

class Human {
public:
        string name;
        int age, height, weight;
        void show() {
                cout << "이름은 " << name << "이고, 나이는 " << age << endl;
        }
};

class student :public Human {
public:
        int k, m, e;
        void input() {
                cout << "국어점수를 입력하시오." << endl;
                cin >> k;
                cout << "수학점수를 입력하시오." << endl;
                cin >> m;
                cout << "영어점수를 입력하시오." << endl;
                cin >> e;
 }
        void printout() {
                show();
                cout << "국어 점수는 " << k << ", 수학점수는" << m << "영어점수는 " << e << "입니다." << endl;
        }
};

class prof :public Human {
public:
        int num;
        void input() {
                cout << "교실 번호를 입력하시오." << endl;
                cin >> num;
        }
        void printout() {
                show();
                cout << "교실 번호는 " << num << "입니다." << endl;
        }
};

void main() {
        student s1;
        prof p1;
        s1.name = "길동이";
        s1.age = 24;
        s1.input();
        s1.printout();

        p1.name = "김선생";
        p1.age = 77;
        p1.input();
        p1.printout();
}

====================================================================================

#include <iostream>

using namespace std;

class Test {
private :
        int a, b;
public:
        Test(int c, int d) {
                a = c;
                b = d;
        }
        void show() {
                cout << a << " , " << b << endl;
        }
};

class child :public Test {
        int k;
public:
        child(int i, int b, int c) : Test (i, b) {
                k = c;
        }
        void show_info() {
                show();
                        cout << k << endl;
        }
};

void main() {
        child c1(10, 20, 30);
        c1.show_info();
}

===================================================================================

#include <iostream>

using namespace std;

class Test {
public:
        Test() {
                cout <<  "parent 소멸자 \n";
        }
        ~Test() {
                cout <<  "parent 소멸자 \n";
        }
};
class Child : public Test {
public:
        Child() {
                cout <<  "child 생성자 \n";
        }
        ~Child() {
                cout << "child 소멸자 \n";
        }
};

void main() {
        Child c1;
}

=================================================================================

#include <iostream>

using namespace std;

class Test {
        int num1, num2, num3;

public:

        void init() {
                num1 = 10;
                num2 = 0;
                num3 = 0;
        }
        void init(int k,int p) {
                num1 = num2 = 20;
                num3 = 0;
        }
        void init(int k, char p) {
                num1 = num2 = num3 = 30;
        }
        void show() {
                cout << num1 << " , " << num2 << " , " << num3 << endl;
        }
};

void main() {
        Test t;
        t.init(1, 'l');
        t.show();
}

=================================================================================

#include <iostream>
#include <string>

using namespace std;

class Computer {
private:
        string name;
        int number, price;
public:
        Computer(string abc) {
                name = abc;
                number = 0;
                price = 0;
        }
        Computer(string a, int b) {
                name = a;
                price = b;
                number = 0;
        }
        Computer(string a, int b, int c) {
                name = a;
                price = b;
                number = c;
        }
        void p() {
                cout << "The name is " << name << ", the price is " << price << ", the number is " << number << endl;
        }
};

void main() {
        Computer c1("abc");
        Computer c2("myblog ", 20);
        Computer c3("akljsflqjwfeoijasf", 24, 90);
        c1.p();
        c2.p();
        c3.p();
}

8.19.2017

LinkedList 자료구조

#include <stdio.h>
#include <stdlib.h>

typedef struct Test {
        int data;
        struct Test* p; //주소를 담을 변수 생성
}test;

void main(){
        test* head = (test*)malloc(sizeof(test)); //시작위치

        test t1;
        test t2;
        test t3;

        printf("%d\n", &t1);
        printf("%d\n", &t2);
        printf("%d\n\n", &t3);


        t1.data = 10;
        t2.data = 20; //(*t1.p).data = 20;
        t3.data = 30;

        t1.p = &t2;
        t2.p = &t3;
        t3.p = NULL;

        printf("%d\n", t1.p);
        printf("%d\n", t2.p);
        printf("%d\n", t3.p);

}

============================================================================

#include <stdio.h>
#include <stdlib.h>

typedef struct TEST {
        int data;
        struct TEST* next;
}test;

void main() {
        //연결리스트 : 주소를 연결해 놓은 집합체
        //                         마지막 노드의 next는 NULL
        //                         head는 시작위치를 알려줄 수 있게끔
        //                         데이터 X

        test* head = (test*)malloc(sizeof(test));
        head->next = NULL;

        test* new1 = (test*)malloc(sizeof(test));
        (*head).next = new1;
        new1->next = NULL;

        test* new2 = (test*)malloc(sizeof(test));
        new1->next = new2;
        new2->next = NULL;

        test* new3 = (test*)malloc(sizeof(test));
        new2->next = new3;
        new3->next = NULL;

        new1->data = 10;
        new2->data = 20;
        new3->data = 30;

        test* move = head->next;
        while (move != NULL) {
                printf("%d", move->data);
                move = move->next;
        }printf("\n"); // 출력
}

=============================================================================

#include <stdio.h>
#include <stdlib.h>

typedef struct Test {
        int data;
        struct Test* next;
}test;

test* head;

void input(int data) {
        test* newnode = (test*)malloc(sizeof(test));
        newnode->data = data;
        newnode->next = head->next;
        head->next = newnode;
}

void del_node1() {
   test* move = head->next;

   printf("삭제할 데이터 입력 : "); int data;
   scanf("%d", &data);

   while (move != NULL) {
      if (move->next->data == data) {
         test* temp = move->next;
         move->next = move->next->next;
         free(temp);
         break;
      }
      move = move->next;
   }
}

void del_node2() {
        test* move = head->next; //삭제할 노드를 찾을 변수
        test* move2 = head; //삭제할 노드의 이전 노드

        printf("삭제할 데이터 입력 : "); int data;
        scanf("%d", &data);

        while (move != NULL) {
                if (move->data == data) {
                        move2->next = move->next;
                        free(move);
                        break;
                }
                move = move->next;
                move2 = move2->next;
        }
}

void main() {
        head = (test*)malloc(sizeof(test));
        head->next = NULL;

        input(10);
        input(20);
        input(30);
        input(40);
        input(50);
        del_node2();

        test* move = head->next;
        while (move != NULL) {
                printf("%d", move->data);
                move = move->next;
        }printf("\n"); // 출력
}

8.17.2017

08.17 C++

#include <iostream>
#include
<string>
using namespace std;

class Song {
private :
        string name, singer;
        int number;
public :
        Song(){
                cout << "노래추가 \n";
                name = "가나다";
                singer = "fx";
                number = 0;
        }
        ~Song() {
                cout << "노래 삭제됨 " << endl;
        }
        void insert() {
                cin >> name;
                cin >> singer;
                cin >> number;
        }
        string get_name() {
                return name;
        }
        void printout() {
                cout << "노래 제목은 : " << name << "\n부른 가수는 : " << singer << "\n조회수는 : " << number << endl;
        }
};

void main() {
        Song* s[20];

        int input = 0, i = 0;
        string name;
        while (input != 5) {
                cout << "[menu]\n1.노래추가 2.노래출력 3.노래삭제 4.노래수정 5.종료\n";
                cin >> input;

                if (input == 1) {
                        s[i] = new Song();
                        (*s[i]).insert();
                        i++;
                }
                else if (input == 2) {
                        for (int j = 0; j < i; j++) {
                                s[j]->printout();
                        }
                }
                else if (input == 3) {
                        cout << "어떤 노래를 삭제하시겠습니까? " << endl;
                        cin >> name;
                        for (int p = 0; p < i; p++) {
                               if (name == s[p]->get_name()) {
                                        delete s[p];
                                        for (int p = 0; p < i; p++) {
                                                s[k] = s[k+1];
                                        }
                                        i--;
                                        break;
                                }
                        }
                }
                else if (input == 4) {
                        cout << "어떤 노래를 수정하시겠습니까? " << endl;
                        cin >> name;
                        for (int k = 0; k < i; k++) {
                               if (name == s[k]->get_name()) {
                                        s[k]->insert();
                                        break;
                                }
                        }
                }
                else if (input > 5) {
                        cout << "1~5 사이의 값을 입력해주세요" << endl;
                }
        }

}

8.10.2017

08.10

#include <iostream>
#include <string>

using namespace std;

class product {
public:
        string name;
        int price, productNum;
        product() {
                cout << " 물품이름, 가격, 수량\n";
                cin >> name >> price >> productNum;
        }
        void show_info() {
                cout << name << " , 가격 : " << price << " , 물품 수량 : " << productNum << endl;
                //cout << (*p[k]).name << (*p[k]).price << (*p[k]).productNum;
        }
};

void main() {
        product* p[10];
        int input = 0;
        int i = 0;
        while (input != 3) {
                cout << "[menu]\n1.물품등록 2.물품출력 3.종료\n";
                cin >> input;
                if (input == 1) {
                        if (i > 9) {
                                cout << "더 이상 저장이 불가능합니다." << endl;
                                continue;
                        }
                        p[i] = new product();
                        i++;
                }
                else if (input == 2) {
                for (int k = 0; k < i; k++) {
                        (*p[k]).show_info();
                        // p[k]->show_info();
                        }
                }
        }
}

08.10 C++ 공부

#include <iostream>

using namespace std;

class AAA {
public:
        int num1;
        char ch;
        AAA() {
        }
        AAA(int a, char b) {
                num1 = a;
                ch = b;
        }
        void show() {
                cout << num1 << ", " << ch << endl;
        }
};

class Test {
public:
        void setting(AAA *p) {
                p->num1 = 50;
                p->ch = 'b';
        }
};
void main() {
        AAA a1 = AAA(10000, 'a');

        a1.show();
        Test t;
        t.setting(&a1);

        a1.show();
}