Solidity By Example011~015

011ForWhile

//SPDX-License-Identifier:MIT
pragma solidity ^0.8.13;
// 不要写出死循环,这会导致击穿gas limit,会导致交易失败
contract Loop{
    function loop() public pure returns(uint){
        for(uint i = 0;i < 10; i++){
            if(i == 3){
                continue;
            }
            if (i==5){
                break;
            }
        }

        uint j;
        while (j < 10){
            j++;
        }
        return j;
    }
}

012Mapping

//SPDX-License-Identifier:MIT
pragma solidity ^0.8.13;

contract Mapping {
    // mapping语法是mapping(key => value)
    // key可以是string uint bool bytes h或者已经部署的合约
    // value可以是多种类型的变量 甚至是另一个mapping或者是一个array(动态数组)
    mapping(address => uint) myMap;

    function get(address _addr) public view returns(uint){
        return myMap[_addr];
    }

    function set(address _addr,uint _i) public{
        myMap[_addr] = _i;
    }


    function remove(address _addr) public {
        delete myMap[_addr];
    }

}

contract NestedMapping{
    mapping(address => mapping(uint => bool)) public nested;

    function get(address add,uint _i) public view returns(bool) {
        return nested[add][_i];
    }

    function set(address add,uint _i,bool _b) public{
        nested[add][_i] = _b;}

    function remove(address add,uint _i) public{
        delete nested[add][_i];
    }
}

013Array

//SPDX-License-Identifier:MIT
pragma solidity ^0.8.13;

contract array{
    // 初始化动态数组方式
    uint[] public arr;
    uint[] public arr2 = [0,1,2];

    // 初始化固定长度数组的方式
    uint[10] public myFixedSizeArr;

    function get(uint _i) public view returns(uint){
        return arr2[_i];}

    // solidity也可以返回整个数组
    // 但要避免返回了一个动态数组
    function getArray() public view returns(uint[] memory){
    return arr2;}
    
    function push(uint _i) public {
        arr.push(_i);// arr.size 加一
    }

    function pop() public{
        arr.pop();
    }

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

    function remove(uint _i) public{
        delete arr2[_i];//是将对应位置恢复为初始值 => 0
    }

    // function example() public{
    //     // 在内存中创建一个只能是固定长度的数组
    //     uint[] memory a = new uint[](5);
    // }
}

ArrayRemove_0

//SPDX-License-Identifier:MIT
pragma solidity ^0.8.13;

contract ArrayRemoveByShifting {
    uint[] public arr3;
    
    function remove(uint _index) public {
        require(_index<arr3.length,"index out of bound"); 
        //require中的条件的结果是假时,才会跳出
        for (uint i = _index;i<arr3.length-1;i++){
            arr3[i]=arr3[i+1];
        }
        arr3.pop();
    }

    function test() public{
        arr3 = [0,1,2,3,4];
        remove(1);
        // 理想结果为[0,2,3,4]
        assert(arr3[0] == 0);
        assert(arr3[1] == 2);
        assert(arr3[2] == 3);
        assert(arr3[3] == 4);
        assert(arr3.length == 4);

    }
}

 ArrayRemove_1

//SPDX-License-Identifier:MIT
pragma solidity ^0.8.13;

contract ArrayReplaceFromEnd{
    uint[] public arr;

    function remove(uint _index) public {
        require(_index < arr.length, "index out of bound");
        arr[_index] = arr[arr.length - 1];
        arr.pop();
    }

    function test() public{
        arr = [0,1,2,3,4];
        remove(1);
        // 理想结果为[0,2,3,4]
        assert(arr[0] == 0);
        assert(arr[1] == 4);
        assert(arr[2] == 2);
        assert(arr[3] == 3);
        assert(arr.length == 4);  
    }
}

014Enum

//SPDX-License-Identifier:MIT
pragma solidity ^0.8.13;

// 枚举类型是一种预定义好的数据类型,用于表示一个固定的值范围
// 枚举类可以在合约外进行声明
contract Enum{
    enum Status {
        Pending,
        Shipped,
        Accepted,
        Rejected,
        Canceled
    }

    Status public status;

    function get() public view returns(Status){
        return status;
    }

    function set(Status _status) public {
        status = _status;
    }

    function cancel() public{
        status = Status.Canceled;
    }

    function reset() public{
        delete status;
    }
}

015Struct

//SPDX-License-Identifier:MIT
pragma solidity ^0.8.13;

// 015Struct当中涉及的calldata memory storage会在下一张介绍

contract Todos{
    struct Todo{
        string text;
        bool completed;
    }

    Todo[] public todos;

    function create(string calldata _text) public{
        // 三种方式去初始化struct
        // 1
        todos.push(Todo(_text,false));

        // 2
        todos.push(Todo({text:_text,completed:false}));

        // 3
        Todo memory todo;
        todo.text = _text;
        // bool的默认值就是false
        todos.push(todo);
    }

    // 定义todos时,solidity会默认创建一个get
    // 所以一下function不必自己创建
    function get(uint _i) public view returns(string memory text,bool completed){
        Todo storage todo = todos[_i];
        return(todo.text,todo.completed);
    }

    // 更新text
    function updateText(uint _i,string calldata _text) public {
        Todo storage todo = todos[_i];
        todo.text = _text;
    }

     // 更新Completed
    function updateCompleted(uint _i) public {
        Todo storage todo = todos[_i];
        todo.completed = !todo.completed;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值