class6_Struct

개굴·2023년 5월 14일
0

solidity

목록 보기
6/11
// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.18;

contract Struct {
    struct Student {
        string name;
        string gender;
        uint number;
        uint birthday;
    } 
    /* 구조체 선언 
    struct 구조체명 {
        .../
        .../
        ...
    }
    */
    // Struct 서로 다른 자료형 들어감 -> 디비 구조 
    // Struct 변수를 새로 만든것 
    

    uint a = 5;
    struct Email {
         string name;
         string id; 
         uint createdDay;
         bytes T;
    }
    Email C; 
    Email[] email;

    struct Party{
        string title;
        uint date;
        string name;
    }

    Party P;
    Party[] parties;



    function setP(string memory _title, uint _date, string memory _name) public {
        P = Party(_title, _date, _name);
    }

    function pushParty(string memory _title, uint _date, string memory _name) public {
        parties.push(Party(_title, _date, _name));
    }

    function getP() public view returns (Party memory){
        return P;
    }

    function getArrayParties() public view returns (Party[] memory){
        return parties;
    }



    function setEmailC(string memory _name, string memory _id, uint _createdDay,bytes memory _T ) public {
        C = Email(_name, _id, _createdDay,_T);
    }
    function getEmailC() public view returns(Email memory) { //Email형으로 리턴해야됨
        return C;
    }

    function pushEmail(string memory _name, string memory _id, uint _createdDay,bytes memory _T ) public {
        email.push(Email(_name, _id, _createdDay,_T)); 
    }

    Student A; // Student형 변수 s
    Student[] students; //Student형 변수들의 array students

    Student W;

    function setStudentA(string memory _name, string memory _gender, uint _number, uint _birth) public {
        A = Student(_name, _gender, _number, _birth);  //Student A;에 담아주는 것 
    }

    function setStudentW(string memory _name, string memory _gender, uint _number, uint _birth) public {
        W = Student(_name, _gender, _number, _birth);
    }

    function pushStudents(string memory _name, string memory _gander, uint _number, uint _birth) public {
        students.push(Student(_name, _gander,_number,_birth));
    }

    function setAStudent(string memory _name, string memory _gender, uint _number, uint _birth) public {
        A = Student(_name, _gender, _number, _birth);
    }


    function getStudentA() public view returns(Student memory) {
        return A;
    }

    function pushStudent(string memory _name, string memory _gender, uint _number, uint _birth) public {
        students.push(Student(_name, _gender, _number, _birth)); // 배열명.push(구조체명(구조체 정보들))
    }

    function popStudent() public {
        students.pop();
    }

    function getLength() public view returns(uint) {
        return students.length;
    }

    function getStudent(uint _n) public view returns(Student memory) {
        return students[_n-1];
    }

    function getStudents() public view returns(Student[] memory) {
        return students;
    }
}

contract Errors {
    uint a;

    function add(uint _a, uint _b) public pure returns(uint) {
        return _a+_b;
    }

    /*
    function add(uint _a, uint _b) public returns(uint) {
        a = a + _a + _b;
        return a;
    }

    function add(uint _a, uint _b) public view returns(uint) {
        return a+_a+_b;
    }

    function add(uint _a, uint _c) public pure returns(uint) {
        return _a+_c;
    }

    function add(uint _a, uint _b) public pure returns(uint, uint) {
        return (_a+_a, _a+_b);
    }
    */

    function add(uint _a, uint _b, uint _c) public pure returns(uint) {
        return _a+_b+_c;
    }

    function add2() public pure returns(uint) {
        uint c = 5;
        uint d = 7;
        return c+d;
    }

}

// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.18;

contract Struct {

// 이름, 번호, 생년월일을 담은 student라는 구조체와 제목, 번호, 날짜를 담은 class라는 구조체를 선언하시오.
struct Student {
    string name;
    uint num;
    uint birth;
}

struct Class{
    string title;
    uint num;
    uint date;
}

// student형 변수 s 와 class형 변수 c를 선언하시오.
Student S;
Class C; 

// student형 변수들이 들어가는 array students와 class형 변수들이 들어가는 array classes를 선언하시오.
Student[] students;
Class[] classes;
// s에 값을 부여하는 함수 setS와 c에 값을 부여하는 함수 setC를 각각 구현하시오
function setS(string memory _name, uint _num, uint _birth) public {
    S = Student(_name, _num, _birth);
} 

function setC(string memory _title, uint _num , uint _date)public {
    C = Class(_title, _num, _date);
}
// 변수 s의 값을 반환받는 함수 getS와 c의 값을 반환받는 함수 getC를 각각 구현하시오.
function getS() public view returns (Student memory){
 return S;
}

function getC() public view returns (Class memory){
    return C;
}

// students에 student 구조체를 넣는 pushStudent 함수를 구현하세요.
function pushStudent(string memory _name, uint _num, uint _birth) public {
    students.push(Student(_name,_num, _birth ));
}

// classes에 class 구조체를 넣는 pushClass 함수를 구현하세요.

function pushClass(string memory _title, uint _num, uint _date) public {
    classes.push(Class(_title, _num, _date ));
}

//숫자형 변수 a, 문자형 변수 b, bytes2형 변수 c를 담은 구조체 D를 선언하세요.
struct D{
    uint a;
    string b; 
    bytes2 c;
}

// D형 변수 dd를 선언하세요.
D dd;

// dd에 값을 부여하는 setDD함수를 구현하세요.
function setDD(uint _a, string memory _b, bytes2 _c) public {
    dd = D(_a, _b, _c);
}

// dd의 값을 반환하는 getDD 함수를 구현하세요
function getDD() public view returns (D memory){
    return dd;
}

// D형 변수들이 들어가는 array Ds를 선언하세요.
D[] Ds; 

// Ds에 새로운 D형 변수를 넣는 pushD 함수를 구현하세요.
function pushD(uint _a, string memory _b, bytes2 _c) public {
    Ds.push(D(_a, _b, _c)); //dd = D(_a, _b, _c); 와 같음 
}

function pushD2() public {
    Ds.push(dd);
}

// Ds array의 n번째 요소를 반환받는 getN이라는 함수를 구현하세요.
function getN(uint n) public view returns (D memory){
    return Ds[n-1]; //동적에서 정적으로 가는 것은 괜찮음 
}


// 숫자형 변수 number, 문자형 변수 name, bytes2형 변수 password 그리고 memeber라는 구조체를 선언하세요.
struct member{
    uint number;
    string name;
    bytes2 password;
}

// member형 변수 Michael을 선언하세요.
member Michael;


// Michael에 값을 부여하는 setM 함수를 구현하세요.
function setM(string memory _name, uint _number, bytes2 _password) public {
    Michael = member(_number,_name, _password);
}


// member형 변수들이 들어가는 members를 선언하세요.
member[] members;


// members에 새로운 member 변수를 넣는 pushMember 함수를 구현하세요
function pushMember(string memory _name, uint _number, bytes2 _password) public {
    members.push(member(_number,_name, _password));
}

}

// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.18;

contract homwWork {
//숫자형 변수 a, 문자형 변수 b, bytes2형 변수 c를 담은 구조체 D를 선언하세요.
  
struct D{
    uint a;
   string b;
   bytes2 c;
}  
   
//D형 변수 dd를 선언하세요.

D dd;

//dd에 값을 부여하는 setDD함수를 구현하세요.

function setDD(uint _a, string memory _b, bytes2 _c) public {
    dd = D(_a, _b, _c);
}

//D형 변수들이 들어가는 array Ds를 선언하세요.

D[] Ds;

//Ds에 새로운 D형 변수를 넣는 pushD 함수를 구현하세요.
function pushD(uint _a, string memory _b, bytes2 _c) public {
    Ds.push(D(_a, _b, _c));
}

function popD() public {
    Ds.pop();
}

function lengthD() public view returns (uint) {
    return Ds.length;
}

//배열전체 
function getDs() public view returns (D[] memory){
    return Ds;
} 



//숫자형 변수 number, 문자형 변수 name, bytes2형 변수 password 그리고 member라는 구조체를 선언하세요.
struct member{
    uint number;
    string name;
    bytes2 password;
}
//member형 변수 Michael을 선언하세요.

member Michael;

//Michael에 값을 부여하는 setM 함수를 구현하세요.

function setM(uint _number, string memory _name, bytes2 _password) public {
    Michael = member(_number, _name, _password);
}

function getM() public view returns (member memory){
    return Michael;
}

//member형 array 변수들이 들어가는 members를 선언하세요.

member[] members;

//members에 새로운 member 변수를 넣는 pushMember 함수를 구현하세요
function pushMember(uint _number, string memory _name, bytes2 _password) public {
    members.push(member(_number, _name, _password));
}
  //원하는 배열 순번의 데이터
function getMember(uint _n) public view returns (member memory){
    return members[_n-1];
}
    //배열 Pop
function popMember() public{
    members.pop();
}

    //길이 
function getLengthMember() public view returns(uint){
    return members.length;
}

    // 배열전체 
function getMembers() public view returns(member[] memory){
    return members;
}

    //원하는 배열삭제 
   function deleteMember(uint _n) public {
    delete members[_n -1];
} 


// 배열 특정 값 바꾸기
function setNMembers(uint _n ,uint _number, string memory _name, bytes2 _password ) public {
    members[_n-1] = member(_number, _name, _password);
}

}
// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.18;

contract test {
    // ABC라는 구조체 안에는 숫자형 a, 문자형 b, 문자형 array c가 들어있다.
    struct ABC{
        uint a; 
        string b;
        string[] c;
    }

    // ABC가 들어가는 array ABCs를 구현하고 
    ABC[] ABCs; 

    //ABCs에 ABC를 넣는 함수, 
    function pushABCs(uint _a, string memory _b , string[] memory _c) public {
        ABCs.push(ABC(_a, _b, _c));
    }
    //특정 ABC를 반환하는 함수, 
    function getNABCs(uint _n) public view returns(ABC memory){
       return  ABCs[_n-1];
    } 
    //ABCs 전체를 반환하는 함수, 
    function getABCs() public view returns (ABC[] memory){
        return ABCs;
    }
    //특정 ABC의 c array를 반환받는 함수를 각각 구현하시오.
    function getCArray(uint _n) public view returns (string[] memory){
        return ABCs[_n-1].c;
    }
 


}

0개의 댓글