코틀린에는 타입 확인할 수 있는 함수가 없어서 검색하다보니, 이런게 나오네?

검색해서 밑에 부분만 읽고 열심히 코딩했다가, 계속 에러가 나서 보니 위에서 함수를 만들어야 했던 것이었다. 그래서 파바박 코딩해서 해보니 생각보다 잘 나오는구만.

 예전에는 SAS 만 써서 다른 것은 R밖에 몰랐는데, 파이썬을 2년 정도 열심히 사용하다보니 왠만한 것은 그냥 파이썬 찾아서 라이브러리 불러오면 다 있던데. 파이썬으로 앱이나 웹을 만드는 것은 아무래도 무리겠지.

 

https://www.tutorialspoint.com/equivalent-of-getclass-for-kclass-in-kotlin

 

Equivalent of getClass() for KClass in Kotlin

Equivalent of getClass() for KClass in Kotlin - In this article, we will take an example and demonstrate how we can obtain the class reference in Kotlin. Kotlin does not support fetching the class reference directly, but you can obtain the same reference v

www.tutorialspoint.com

import kotlin.reflect.KClass

fun main() {
    fun<T: Any> T.getClass(): KClass<T> {
        return javaClass.kotlin
    }
   
    val aToz = 'a'..'z'

    val isTrue = 'c' in aToz
    println(aToz)
    println("isTrue = 'c' in aToz : $isTrue")
    for (t in 'a'..'z') {
       print("$t, ")
    }
    println()

    val oneToTen = 1..10

    for (k in oneToTen) {
        for (j in 1..5) {
            println("k * j = ${k * j}")
        }
    }
    print("type of aToz: ${oneToTen.getClass().simpleName}")
    //for (t in aToz) {
    //    print("$t, ")
    // }
    // println()
}
반응형

'C, C++, Java' 카테고리의 다른 글

Java의 정석 2장  (0) 2016.12.11
Score cut-off C++ program  (0) 2014.09.05
Overriding1.cpp  (0) 2014.05.02
CReference2.cpp  (0) 2014.05.02
P304.cpp  (0) 2014.05.02

 2장에서 나온 예를 정리하 것들이다. 아래 내용들은 예전에 본 것인데, 절대 쉬워 보이지 않는다. 작은 것들이 모여서 기반을 쌓고 나중에 하나 하나 블럭이 되어서 커다란 장벽을 만드는 것이라고 본다. 아, 근데 작은 것을 모으는 것이 절대 쉽지만은 않구나.


class CharToCode

{

    public static void main(String[] args)

    {

        char ch = 'A';      //char ch= '\u0041';으로 바꿔 써도 된다.

        int code = (int)ch;  //ch에 저장된 값을  int형으로 변환하여 저장한다.

        

        System.out.println(ch);

        System.out.println(code);

    }

}



class CodeToChar

{

    public static void main(String[] args)

    {

        int code = 65; // 또는int cod e= 0x0041;

        char ch = (char)code;

        

        System.out.println(code);

        System.out.println(ch);

    }

}



class SpecialChar

{

    public static void main(String[] args)

    {

        char single = '\'';     // signle = ''';와 같이 할 수 없다.

        String dblQuote = "\"Hello\"";  // 큰따옴표를 출력하려면 이렇게 한다.

        String root = "c:\\";

        

        System.out.println(single);

        System.out.println(dblQuote);

        System.out.println(root);

    }

}

반응형

'C, C++, Java' 카테고리의 다른 글

Python type() quivalent in Kotlin  (0) 2023.10.18
Score cut-off C++ program  (0) 2014.09.05
Overriding1.cpp  (0) 2014.05.02
CReference2.cpp  (0) 2014.05.02
P304.cpp  (0) 2014.05.02

회사에서 신용점수 나눠서 등급 조정할 때 하는 시뮬레이션을 보통은 SAS로 짰는데, 이번에 C++ 배우는 김에 한 번 짜봤다. 실제 고객 자료가 없으므로 신용점수는 아래와 같이 랜덤하게 만들어서 나누는 것이다.

 원래는 이런 거를 백만번 정도 돌린 다음에 그 결과를 막대 차트로 그리려고 했는데, 지금 상황으로는 그 근처도 못가고 있는 상황이다.

일 단 이거 가져다가 각 랜덤 결과를 100만번 정도 돌려서 개별값을 배열에 저장하고 텍스트로 빼내는 것을 생각했는데 말이지. 이런 시뮬레이션을 하는 것은 솔직히 속도만 아니라면 엑셀이 더 낫지 않을까 생각이 든다. 이런 거는 SAS/R/C++로 하고 나머지는 엑셀에서 시각화 하는 것을 생각중인데 말이다.

 역시나 시각화 이야기가 나오면 역시나 엑셀로 가야 하는구나. 좀더 동적인 프로그램을 짜려면 아직도 갈길이 먼듯 하고 말이지. 아래 프로그램 짜면서 온갖 잡다한 헤더 다 갖다 붙이고, loop 구문으로 해결이 안되어서 결국에는 if로 떡칠을 해놨구만 에효.

 SAS에서는 매크로를 워낙에 잘 써서 그런지 C++ 오면 이런 삽질이 없다. 가르침을 주세요, 이글루스 프로그래밍 고수님들.

#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <vector>

using namespace std;

int main(void)
{
    srand((unsigned int)time(NULL));

    // input random value to cut-off score array
    int pnt[9];
    for(int i = 0; i < 8; i++)
    {
        pnt[i] = rand()%999+1;
        cout<<pnt[i]<<endl;
    }
    pnt[8] = 1000;


    // Sorting the cut-off score array
    sort(pnt, pnt+9);

    int cnt[9] = {0,};
    // Print Score
    for(int i = 0; i < 9; i++)
    {
        cout<<"pnt["<<i<<"] = "<<pnt[i]<<endl;
    }
    
    int cust_arr[1000000];
    for(int i = 0; i < 1000000; i++)
    {
        cust_arr[i] = rand()%1000+1;
        if(cust_arr[i] <= pnt[0])
            cnt[0] += 1;
        else if(cust_arr[i] <= pnt[1])
            cnt[1] += 1;
        else if(cust_arr[i] <= pnt[2])
            cnt[2] += 1;
        else if(cust_arr[i] <= pnt[3])
            cnt[3] += 1;
        else if(cust_arr[i] <= pnt[4])
            cnt[4] += 1;
        else if(cust_arr[i] <= pnt[5])
            cnt[5] += 1;
        else if(cust_arr[i] <= pnt[6])
            cnt[6] += 1;
        else if(cust_arr[i] <= pnt[7])
            cnt[7] += 1;
        else if(cust_arr[i] <= pnt[8])
            cnt[8] += 1;
    }

    for(int i = 0; i < 8; i++)
    {
        cout<<"Tier0"<<i+1<<" : "<<cnt[i]<<endl;
    }

//    cout<<cust_arr[0][1]<<endl;
//    cout<<cust_arr[1999][0]<<endl;

    return 0;
}

반응형

'C, C++, Java' 카테고리의 다른 글

Python type() quivalent in Kotlin  (0) 2023.10.18
Java의 정석 2장  (0) 2016.12.11
Overriding1.cpp  (0) 2014.05.02
CReference2.cpp  (0) 2014.05.02
P304.cpp  (0) 2014.05.02

/*
    Overriding1.cpp
*/

#include <iostream>

using std::endl;
using std::cout;

class AAA
{
public :
    void fct()
    {
        cout<<"AAA"<<endl;
    }
};

class BBB : public AAA
{
    void fct()
    {
        cout<<"BBB"<<endl;
    }
};

int main(void)

반응형

'C, C++, Java' 카테고리의 다른 글

Java의 정석 2장  (0) 2016.12.11
Score cut-off C++ program  (0) 2014.09.05
CReference2.cpp  (0) 2014.05.02
P304.cpp  (0) 2014.05.02
CPointer2.cpp  (0) 2014.05.02

/*
    CRerference2.cpp
*/

#include <iostream>

using std::cout;
using std::endl;

class Person
{
public :
    void Sleep() const
    {
        cout<<"Sleep"<<endl;
    }
};

class Student : public Person
{
public :
    void Study() const
    {
        cout<<"Study"<<endl;
    }
};

class PartTimeStd : public Student
{
public :
    void Work() const
    {
        cout<<"Work"<<endl;
    }
};

int main(void)
{
    PartTimeStd p;
    p.Sleep();
    p.Study();
    p.Work();

    Person& ref = p;
    ref.Sleep();
    //ref.Study();
    //ref.Work();

    return 0;
}

 

반응형

'C, C++, Java' 카테고리의 다른 글

Score cut-off C++ program  (0) 2014.09.05
Overriding1.cpp  (0) 2014.05.02
P304.cpp  (0) 2014.05.02
CPointer2.cpp  (0) 2014.05.02
EmployeeManager3.cpp  (0) 2014.05.02

/*
    CRerference1.cpp
*/

#include <iostream>

using std::cout;
using std::endl;

class Person
{
public :
    void Sleep() const
    {
        cout<<"Sleep"<<endl;
    }
};

class Student : public Person
{
public :
    void Study() const
    {
        cout<<"Study"<<endl;
    }
};

class PartTimeStd : public Student
{
public :
    void Work() const
    {
        cout<<"Work"<<endl;
    }
};

int main(void)
{
    PartTimeStd p;
    Student& ref1 = p;
    Person&  ref2 = p;

    p.Sleep();
    ref1.Sleep();
    ref2.Sleep();

    return 0;
}

 

반응형

'C, C++, Java' 카테고리의 다른 글

Overriding1.cpp  (0) 2014.05.02
CReference2.cpp  (0) 2014.05.02
CPointer2.cpp  (0) 2014.05.02
EmployeeManager3.cpp  (0) 2014.05.02
Person.cpp  (0) 2014.04.30

+ Recent posts