๐ŸŒˆTest ์ค€๋น„-(2)_ ๊ตฌ์กฐ์ฒด(+array) ๋ณต์Šต

RoRAยท2023๋…„ 5์›” 8์ผ
0

Solidity (use_ Remix)

๋ชฉ๋ก ๋ณด๊ธฐ
5/10

๐Ÿช‚ 2023. 05. 08. Monday_ ์—Ž๋“œ๋ ค๋ป—์ณ ๐Ÿช‚


โœ… Intro

โ€‹

์•ˆ๋…•ํ•˜์„ธ์š”!๐Ÿ˜

๋ชจ๋‘ ํ•จ๊ป˜ 4๊ฐœ์›” ๋™์•ˆ ํ•˜๋‚˜์˜ ๋ฉ‹์ง„ ํ”„๋กœ์ ํŠธ๋ฅผ ์™„์„ฑํ•˜๋Š” ๊ทธ๋‚ ๊นŒ์ง€ ํ™”์ดํŒ…ํ•ฉ์‹œ๋‹ค !๐Ÿ‘๐Ÿ‘๐Ÿ‘
โ€‹
* ์ด ๊ณณ์—์„œ๋Š” ๋‹น์ผ์— ๋ฐฐ์šด ๋‚ด์šฉ์„ ๋ณต์Šตํ•˜๋Š” ์šฉ๋„๋กœ ๊ธ€์„ ์ž‘์„ฑํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
โ€‹
* ์˜ค๋Š˜์€ '~~~๊ตฌ์กฐ์ฒด ๋ณต์Šต ' ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


โ€‹
โ€‹

โœ… RE - struct + array + push/pop

โ€‹

๐Ÿ‘‰ ex_1-(1) ๊ตฌ์กฐ์ฒด


// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.18;

//์‹œ์ž‘ ~!~
contract struct {
      //์ด๋ฆ„, ๋ฒˆํ˜ธ, ์ƒ๋…„์›”์ผ์„ ๋‹ด์€ student ๋ผ๋Š” ๊ตฌ์กฐ์ฒด๋ฅผ ์„ ์–ธ
      struct student {
          string name;
          uint num;
          uint birth;
      }



      // ์ œ๋ชฉ, ๋ฒˆํ˜ธ ๋‚ ์งœ๋ฅผ ๋‹ด์€ class๋ผ๋Š” ๊ตฌ์กฐ์ฒด ์„ ์–ธ
      struct class {
          string title;
          uint num;
          uint date;
      }



      //studentํ˜• ๋ณ€์ˆ˜ s์™€ classํ˜• ๋ณ€์ˆ˜ c ์„ ์–ธ
      student s;
      class c;
      ```

      ```
      //s์— ๊ฐ’์„ ๋ถ€์—ฌํ•˜๋Š” ํ•จ์ˆ˜ setS
      function setS(string memory _name, uint _num, uint _birth) public {
          s = student(_name, _num, _birth);
      }



      //c์— ๊ฐ’์„ ๋ถ€์—ฌํ•˜๋Š” ํ•จ์ˆ˜ setC
      function setC(string memory _title, uint _num, uint _date) public {
          c = class(_title, _num, _date);
      }
      ```

      ```
      //๋ณ€์ˆ˜ s์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ getS
      function getS() public view returns(student memory) {
          return s;
      }


      // ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ getC
      function getC() public view returns(class memory) {
          return c;
      }

โ€‹

๐Ÿ‘‰ ex_1-(2) array

      //์œ„์˜ ์ฝ”๋“œ์™€ ์ด์–ด์„œ ๋ณต์Šต ~

      //studentํ˜• ๋ณ€์ˆ˜๋“ค์ด ๋“ค์–ด๊ฐ€๋Š” array students ์„ ์–ธ
      student[] students;



      //classํ˜• ๋ณ€์ˆ˜๋“ค์ด ๋“ค์–ด๊ฐ€๋Š” array classes ์„ ์–ธ
      class[] classes;
      //students์— student ๊ตฌ์กฐ์ฒด๋ฅผ ๋„ฃ๋Š” pushStudent ํ•จ์ˆ˜
      function pushStudent(string memory _name, uint _num, uint _birth) public{
          students.push(student(_name, _number, _birth));
      }



      //classes์— class ๊ตฌ์กฐ์ฒด๋ฅผ ๋„ฃ๋Š” pushClass ํ•จ์ˆ˜
      function pushClass(string memory _title, uint _num, uint _date) {
          classes.push(class(_title,_num,_date));
      }
      
}

โ€‹

๐Ÿ‘‰ ex_2) ๊ตฌ์กฐ์ฒด + array


// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.18;

//์‹œ์ž‘
contract struct {
      //์ˆซ์žํ˜• ๋ณ€์ˆ˜ a, ๋ฌธ์žํ˜• ๋ณ€์ˆ˜ b, bytes2ํ˜• ๋ณ€์ˆ˜ c๋ฅผ ๋‹ด์€ ๊ตฌ์กฐ์ฒด D ์„ ์–ธ
      struct D {
          uint a;
          string b;
          bytes2 c;		//์ •์ ์„ ์–ธ
      }
      //Dํ˜• ๋ณ€์ˆ˜ dd ์„ ์–ธ
      D dd;


      //dd์— ๊ฐ’์„ ๋ถ€์—ฌํ•˜๋Š” setDDํ•จ์ˆ˜
      dd = DD(_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));
      }

  }	
        
		//Ds array์˜ n๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜๋ฐ›๋Š” getNํ•จ์ˆ˜
        //Dํ˜• ๋ฆฌํ„ด
        
        function getN(uint _n) public view returns(D memory) {
        	return Ds[_n-1];
        }

โ€‹

๐Ÿ‘‰ ex_3) ๊ตฌ์กฐ์ฒด + array

// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.18;

//์‹œ์ž‘
contract struct {

///๊ตฌ์กฐ์ฒด ์„ ์–ธ///


		//memeber๋ผ๋Š” ๊ตฌ์กฐ์ฒด๋ฅผ ์„ ์–ธ
		//์ˆซ์žํ˜• ๋ณ€์ˆ˜ number, ๋ฌธ์žํ˜• ๋ณ€์ˆ˜ name, bytes2ํ˜• ๋ณ€์ˆ˜ password
		struct member {
			uint number;
			string memory name;
			bytes2 password;
		}     
	
    
    
    
        //memberํ˜• ๋ณ€์ˆ˜ Michael์„ ์„ ์–ธํ•˜์„ธ์š”.
        member Michael;
        
        
		//Michael์— ๊ฐ’์„ ๋ถ€์—ฌํ•˜๋Š” setM ํ•จ์ˆ˜
        function setM(uint _number, string memory _name, bytes2 _password) public {
        	Michael = member(_number, _name, _password);
        }
        



///๋ฐฐ์—ด ์„ ์–ธ///


        //memberํ˜• ๋ณ€์ˆ˜๋“ค์ด ๋“ค์–ด๊ฐ€๋Š” members ์„ ์–ธ.
        member[] members;
        
        
        //members์— ์ƒˆ๋กœ์šด member ๋ณ€์ˆ˜๋ฅผ ๋„ฃ๋Š” pushMember ํ•จ์ˆ˜
        function pushMember(uint _number, string memory _name, bytes2 _password) {
        	members.pop(member(_number, _name, _password));
        }
}
โ€‹

โ€‹

โ €โ €โ €

๐Ÿ“Œ23. 05. 08

์ฆ ๊ฒ ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€