완숙의 블로그

[C++] Lab #11 본문

Programing Language/C++

[C++] Lab #11

완숙 2019. 6. 16. 19:48

#1

스크린샷 2019-06-14 오후 8 18 16 스크린샷 2019-06-14 오후 8 18 30

 

//
//  main.cpp
//  Lab #11
//
//  Created by 최완식 on 28/05/2019.
//  Copyright © 2019 최완식. All rights reserved.
//

#include <iostream>
#include <vector>

using namespace std;

template<class T>
void sort(vector<T>& list){
    
    
    int count = 0;
    
    while(count != 4){
        for(int i = 0; i < 4; i++){
            T temp;
            if(list[i] > list[i+1]){
                temp = list[i+1];
                list[i+1] = list[i];
                list[i] = temp;
            }else{
                count++;
            }
        }
    }
}

template<class T>
void print(const vector<T>& list){
    for(auto elem:list){
        cout << elem << "\t";
    }
    cout << endl;
}


int main(){
    vector<int> int_list(5);
    int_list = {10, 5, 8, 1, 3};
    
    vector<double> double_list(5);
    double_list = {10.1, 5.1, 8.1, 1.1, 3.1};
    
    vector<string> string_list(5);
    string_list = {"하나", "둘", "셋", "넷", "다섯"};
    
    
    sort(int_list);
    sort(double_list);
    sort(string_list);
    
    print(int_list);
    print(double_list);
    print(string_list);
    
    return 0;
}

 

 

 

 

 

#2

스크린샷 2019-06-14 오후 8 18 47

 

//
//  main.cpp
//  #2
//
//  Created by 최완식 on 29/05/2019.
//  Copyright © 2019 최완식. All rights reserved.
//

#include <iostream>
#include <vector>
#include <string>
#include <functional>
#include <algorithm>

using namespace std;

int evaluate(const string &input, const string &rev_input){
    int count = 0;
    for(int i = 0; i < input.length(); i++){
        if(input[i] != rev_input[i]){
            cout << "이 문자는 회문이 아닙니다." << endl;
            return 0;
        }
        count++;
    }
    if(count == input.length()){
        cout << "이 문자는 회문입니다." << endl;
        return 1;
    }
    cout << "에러 발생" << endl;
    return -1;
}

string make_reverse(function<string(string)>func, string input){
    return func(input);
}

int main(){
    string input;
    string target;
    
    while(1){
        cout << "문자열 하나를 입력해주세요 : ";
        cin >> input;
        
        if(input == "exit"){
            return 0;
        }
    
        target = make_reverse([](string input)->string{
            string rev_input = input;
            
            for(int i = (int)input.length()-1; i >=0; i--){
                rev_input[i] = input[(int)input.length()-i-1];
            }
            
            return rev_input;
            
        }, input);
    
    
        evaluate(input, target);
        
    }
    
    return 0;
}

 

 

 

 

 

 

#3

스크린샷 2019-06-14 오후 8 18 58 스크린샷 2019-06-14 오후 8 19 17 스크린샷 2019-06-14 오후 8 19 29

 

//
//  main.cpp
//  #3
//
//  Created by 최완식 on 29/05/2019.
//  Copyright © 2019 최완식. All rights reserved.
//

#include <iostream>
#include <vector>

using namespace std;

int main(){
    int n = 0;
    
    cout << "홀수 숫자를 하나 입력해 주세요 : ";
    cin >> n;
    int ar[n][n];
    
    for(int i = 0, j = n/2, num = 1; num <= n*n; num++){
        ar[i][j] = num;
        if(num%n == 0){
            i++;
        }else{
            i--;
            j++;
            if(i<0){
                i = n - 1;
            }
            if(j>n-1){
                j = 0;
            }
        }
    }
    
    cout << n << "차 마방진" << endl;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cout << ar[i][j] << "\t";
        }
        cout << endl;
    }
    
    return 0;
}


 

 

 

 

 

#4

스크린샷 2019-06-14 오후 8 19 44 스크린샷 2019-06-14 오후 8 20 15 스크린샷 2019-06-14 오후 8 20 27

//
//  main.cpp
//  #4
//
//  Created by 최완식 on 29/05/2019.
//  Copyright © 2019 최완식. All rights reserved.
//

#include <iostream>

using namespace std;

template<class T>
class Queue{
private:
    T* p_list;
    int size;
    int MAX_SIZE;
    int front;
    
public:
    Queue(int _MAX_SIZE = 1000): MAX_SIZE(_MAX_SIZE){
        p_list = new T [MAX_SIZE];
        size = 0;
        front = 0;
//        front = 0;
    }
    
    ~Queue(){
        delete[] p_list;
    }
    
    int find_index(T _item){
        for(int i = front; i < size; i++){
            if(p_list[i] == _item){
                return i;
            }
        }
        return -1;
    }
    
    void Enqueue(T _item){
        if(size >= MAX_SIZE){
            cout << "큐가 꽉차있습니다. Out of memory" << endl;
        }else{
            if (find_index(_item) == -1){
                
                p_list[size] = _item;
                size++;
            }else{
                cout << "해당 아이템은 Queue 안에 있습니다. index : "<< find_index(_item) << endl;
            }
        }
    }
    
    T Dequeue(){
        if(size == front){
            cout << "아이템이 없습니다." << endl;
            return 0;
        }else{
            T result;
            result = p_list[front];
            front++;
            return result;
        }
    }
    
    void print() const {
        for(int i = front; i < size; i++){
            cout << p_list[i] << "\t";
        }
        cout << endl;
    }
    
    int get_size(){
        return size;
    }
    
    T get_item(int _index){
        if(_index + front <= size){
            return p_list[front + _index];
        }else{
            cout << "크기를 초과했습니다." << endl;
            return 0;
        }
        
    }
    
};



int main(){
    Queue<int> int_queue;
    Queue<float> float_queue;
    Queue<char> char_queue;
    
    int_queue.Enqueue(1);
    int_queue.Enqueue(2);
    int_queue.Enqueue(2);
    int_queue.Enqueue(5);
    
    float_queue.Enqueue(4.3);
    float_queue.Enqueue(2.5);
    float_queue.Enqueue(3.7);
    float_queue.Enqueue(3.7);
    
    char_queue.Enqueue('b');
    char_queue.Enqueue('b');
    char_queue.Enqueue('c');
    char_queue.Enqueue('a');
    
    cout << endl << "<Before Dequeue>" << endl;
    
    int_queue.print();
    float_queue.print();
    char_queue.print();
    
    int_queue.Dequeue();
    
    float_queue.Dequeue();
    float_queue.Dequeue();
    
    char_queue.Dequeue();
    char_queue.Dequeue();
    char_queue.Dequeue();
    char_queue.Dequeue();
    
    cout << endl << "<After Dequeue>" << endl;
    
    int_queue.print();
    float_queue.print();
    char_queue.print();
    
    return 0;
}

 

Comments