완숙의 블로그

[C++] Lab #12 본문

Programing Language/C++

[C++] Lab #12

완숙 2019. 6. 16. 19:51

#1

스크린샷 2019-06-14 오후 8 29 42

 

 

 

//
//  main.cpp
//  Lab #12
//
//  Created by 최완식 on 04/06/2019.
//  Copyright © 2019 최완식. All rights reserved.
//

#include <iostream>
#include <vector>

using namespace std;

int main(){
    
    vector<int> list{10, 20, 30, 40, 50};
    int num;
    
    while(1){
        cout << "출력할 숫자의 수 : ";
        cin >> num;
        
        for(int i = 0; i < num; i++){
            try{
                cout << list.at(i) << "\t";
            }catch(exception& e){
                cout << endl;
                cout << e.what() << endl;
                cout << "Index is out of range. Please try again." <<" index : " << i << endl;
                break;
            }
        }
        cout << endl;
    }
    
    return 0;
}

 

 

 

 

 

#2

스크린샷 2019-06-14 오후 8 29 57 스크린샷 2019-06-14 오후 8 30 22

 

 

 

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

#include <iostream>
#include <vector>
#include <ctime>

using namespace std;

int main(){
    int size = 0;
    srand((unsigned int)time(0));
    size = rand() % 100 + 1;

    vector<int> list;
    
    for(int i = 0; i < size; i++){
        list.push_back(i);
    }
    
    int cnt = -1;
    while(1){
        cnt++;
        
        try{
            list.at(cnt);
        }catch(exception& e){
            cout << "현재 list는 " << cnt << "의 크기를 가지고 있다." << endl;
            return 0;
        }
    }
    
    return 0;
}

 

 

 

 

 

 

#3

스크린샷 2019-06-14 오후 8 30 47 스크린샷 2019-06-14 오후 8 31 06 스크린샷 2019-06-14 오후 8 31 21 스크린샷 2019-06-14 오후 8 31 36

 

 

 

 

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

#include <iostream>
#include <vector>
#include <string>
#include <fstream>


using namespace std;

class FileNotFoundException : public exception{
    string message;
    
public:
    FileNotFoundException(const string& fname):
    message("File \"" + fname + "\" not found"){}
    virtual const char* what() const throw() {
        return message.c_str();
    }
};


class CStudent{
private:
    string m_Name;
    int m_Number;
    string m_Major;
    
public:
    CStudent(string name = "", int num = 0, string maj = ""):m_Name(name), m_Number(num), m_Major(maj){}
    ~CStudent(){}
    
    void setName(string n){ m_Name = n; }
    string getName() {return m_Name; }
    
    void setNumber(int n){ m_Number = n; }
    int getNumber() {return m_Number; }
    
    void setMajor(string n){ m_Major = n; }
    string getMajor() {return m_Major; }
    
    void setAll(string name, int number, string major){
        m_Name = name;
        m_Number = number;
        m_Major = major;
    }
    
    void Display(){
        cout << "이름 : " << m_Name << endl;
        cout << "학번 : " << m_Number << endl;
        cout << "전공 : " << m_Major << endl << endl;
    }
    
};

vector<CStudent> read_file(string str){
    ifstream fin(str);
    vector<CStudent> result;
    
    string name = "";
    int num = 0;
    string maj = "";
    
    if(!fin){
        throw FileNotFoundException(str);
    }
    
    while(!fin.eof()){
        CStudent temp;
        fin >> name >> num >> maj;
        temp.setAll(name, num, maj);

        result.push_back(temp);

    }
    
    return result;
    
}

int main(){
    string str;
    
    
    
    
    cout << "파일 이름 : ";
    cin >> str;
    
    try{
        vector<CStudent> numbers = read_file(str);
        for (CStudent value : numbers)
            value.Display();
    }catch(exception& e){
        cout << e.what() << endl;

    }
    return 0;
}

 

 

 

 

 

#4

스크린샷 2019-06-14 오후 8 31 58 스크린샷 2019-06-14 오후 8 32 09 스크린샷 2019-06-14 오후 8 32 19

 

 

 

 

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

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <ctime>


using namespace std;

class FileNotFoundException : public exception{
    string message;
    
public:
    FileNotFoundException(const string& fname):
    message("File \"" + fname + "\" not found"){}
    virtual const char* what() const throw() {
        return message.c_str();
    }
};

vector<vector<int>> read_file(string str){
    ifstream fin(str);
    vector<vector<int>> result(10, vector<int>(10));
    
    if(!fin){
        throw FileNotFoundException(str);
    }
    
    while(!fin.eof()){
        for(auto& row : result){
            for(auto& elem : row){
                fin >> elem;
            }
        }
    }
    return result;
    
}

void print_file(vector<vector<int>>& vec, int row, int col){
    for(int i = 0 ; i < row; i++){
        vector<int> temp_vec = vec.at(i);
        for(int j = 0; j < col; j++){
            cout << temp_vec.at(j) << "\t";
        }
        cout << endl;
    }
}


int main(){
    
    srand((unsigned int) time(0));
    vector<vector<int>> set(10, vector<int>(10));
    ofstream fout("temp.txt");
    
    
    for(auto& row : set){
        for(auto& elem : row){
            elem = rand() % 101;
            fout << elem << "\t";
        }
        fout << endl;
    }
    
    
    string str;
    int row = 0, col = 0;

    cout << "파일 이름 : ";
    cin >> str;
    
    cout << "출력 행 크기 : ";
    cin >> row;
    
    cout << "출력 열 크기 : ";
    cin >> col;

    try{
        vector<vector<int>> num = read_file(str);
        print_file(num, row, col);
        
    }catch(exception& e){
        cout << e.what() << endl;
    }
    return 0;
}

 

 

Comments