[TIL] Part2 - DAY37 Solidity 및 블록체인 기초 이론

박동우·2023년 5월 10일

블록체인 스쿨 3기

목록 보기
34/72

[TIL] 2023-05-10 DAY37


Solidity 및 블록체인 기초 이론

GIT 주소 : https://github.com/dowoo303/SOLIDITY


오늘 배운것들

  1. 저번시간 복습
    1) mapping+array
  2. IF 기초 및 응용
  3. ENUM 기초 및 응용

각종 팁

회고

  1. 책 추천: 비탈릭 부테린 지분증명의 서 -> 비트코인 백서 -> 블록사이즈 워(blocksize war)



💻 Solidity 코딩

✅ 복습

  1. mapping (string => Student[]) a;처럼 value를 Student[]로 받을 경우 a는 배열처럼
  2. students[_n-1].name 받으려는 순서->배열성분 순으로 적어야함

✅ IF

형태: if(조건){만족시 실행}else{아닐경우 실행}
if문 - struct와 응용, 점수 넣으면 학점 자동 부여

🟥 코드

contract IF {
    struct student {
        uint number;
        string name;
        uint score;
        string credit;
    }

    student a;
    student b;
    student c;

    student[] Students;

    // 학생 정보 중 번호, 이름, 점수를 입력하면 학점이 자동 계산해주는 함수
    // 점수가 90점 이상이면 A, 80점 이상이면 B, 70점 이상이면 C, 나머지는 F
    function setAlice(uint _number, uint _score) public {
        string memory _credit;
        if(_score>= 90) {
            _credit = "A";
        } else if(_score >= 80) {
            _credit = "B";
        } else if(_score >= 70) {
            _credit = 'C';
        } else {
            _credit = 'F';
        }

        a = student(_number, "Alice", _score, _credit);
    }


    function setBob(uint _number, string memory _name, uint _score) public {
        string memory _credit;
        if(_score>= 90) {
            _credit = "A";
        } else if(_score >= 80) {
            _credit = "B";
        } else if(_score >= 70) {
            _credit = 'C';
        } else {
            _credit = 'F';
        }

        b = student(_number, _name, _score, _credit);
    }


    function setCharlie(uint _number, string memory _name, uint _score) public {
        string memory _credit;
        if(_score>= 90) {
            _credit = "A";
        } else if(_score >= 80) {
            _credit = "B";
        } else if(_score >= 70) {
            _credit = 'C';
        } else {
            _credit = 'F';
        }

        c = student(_number, _name, _score, _credit);
    }

    function getStudent() public view returns(student memory, student memory, student memory) {
        return (a, b, c);
    }


    function pushStudent(uint _number,string memory _name, uint _score) public {
        string memory _credit;
        if(_score>= 90) {
            _credit = "A";
        } else if(_score >= 80) {
            _credit = "B";
        } else if(_score >= 70) {
            _credit = 'C';
        } else {
            _credit = 'F';
        }

        Students.push(student(_number, _name, _score, _credit));
    }

    // setGrade 사용해서 간단히 구성해보기
    function pushStudents2(uint _number, string memory _name, uint _score) public {
        Students.push(student(_number, _name, _score, setGrade(_score)));
    }


    function getStudents() public view returns(student[] memory) {
        return Students;
    }

    function setGrade(uint _score) public pure returns(string memory _credit) {
        if(_score>= 90) {
            return 'A';
        } else if(_score >= 80) {
            return 'B';
        } else if(_score >= 70) {
            return 'C';
        } else {
            return "F";
        }
    }

}

✅ ENUM(열거형)

상태 조절을 도와줌 - 자동차 운행 생각해보기
구조는 struct와 같다.
ENUM 변수는 모두 uint8 이다.
숫자에 의미부여(관리효율증가): string으로 입력 -> ENUM -> 숫자로 관리
쓰는이유: 용량적 측면+가독성

🟥 코드

contract ENUM {
    enum Food { // enum 변수명 {변수1, 변수2, 변수3, 변수4}
        Chicken,    // - 결과값: 0, 디폴트 값
        Suish,      // - 결과값: 1
        Bread,      // - 결과값: 2
        Coconut     // - 결과값: 3
    }

    Food a;     // Food형 변수 선언
    Food b;
    Food c;

    function setA() public {
        a = Food.Chicken;
    }

    function setB() public {
        b = Food.Suish;
    }
    
    function setC() public {
        c = Food.Bread;
    }

    // 번호로도 부여가 가능하다
    function setC2(uint _n) public {
        c = Food(_n);
    }

    function getABC() public view returns(Food, Food, Food) {
        return(a,b,c);
    }

}

// ENUM 응용
contract ENUM2 {
    enum Status {
        neutral,
        high,
        low
    }
    Status st;

    uint a=5;

    function higher() public {
        a++;
        setA();
    }

    function lower() public {
        a--;
        setA();
    }

    function setA() public {
        if(a >= 7) {
            st = Status.high;
        } else if(a<= 3) {
            st = Status.low;
        } else {
            st = Status.neutral;
        }
    }


    function getA() public view returns(uint) {
        return a;
    }

    function getSt() public view returns(Status) {
        return st;
    }

}

✅ BOOL

🟥 코드

contract BOOL {
    bool isFun;

    function getVar() public view returns(bool) {
        return isFun;
    }

    function Fun() public {
        isFun = true;
    }

    function notFun() public {
        isFun = false;
    }

    function notFun2() public {
        isFun = !isFun;
    }

    // true 넣으면 true로 false 넣으면 false로, 기타 아무 값을 넣으면 true로 (1, "a" 넣어보기)
    function Fun(bool _a) public {
        isFun = _a;
    }
}
profile
HELLO!

0개의 댓글