solidity数据结构

一、Int类型

int & uint

int代表有符号的整型,也就是可以带负数
uint代表没有符号的整型,也就是从0开始的正整数

uint8 代表为2的8次方
uint256 代表为2的256次方
uint 默认为 uint256

int8 代表为-2的7次方到正2的7次方
int256 代表为-2的255次方,到正2的255次方;
int 默认为int256

整型操作符

comparisons: <=, <, ==, !=, >=, >

bit operator: &, |, ^,~

arithmetic operator: +, -, *, /, %, **, <<, >>

二、数组类型

数组类型基础

  • solidity语言中,int/uint 数组类型的定义方式如下:
    uint[] a;
    int[] b;
    
  • 数组类型的成员有两个length 和 push
    1. push 是给数组类型增加一个元素,同时该数组的长度+1
    2. length 返回当前数组的长度。有一个元素,则返回1,有两个元素则返回2.

从一个最简单的int数组合约开始,实现简单的增删改查
pragma solidity ^0.4.0;
contract aaa{
    uint[]  a;
    function aaa(){
        a.push(111);
    }
    function add(uint n){
        a.push(n);
    }
    
    function lenof() returns(uint len) {
        return a.length;
    }
    function updata(uint index,uint value ){
        if(index > a.length -1) throw;
        a[index] = value;
    }
    
    function query(uint index) returns(uint value){
        if(index >= a.length) throw;
        return a[index];
    } 
    function del(uint index){
        if(index >= a.length) throw;
        for(uint i = index;i < a.length-1;i++){
            a[i] = a[i+1];
        }
        delete a[a.length-1];
        a.length --;
        
    }
    
}


上面代码很简单,定义了一个public的int数组a, 以及一个方法add(uint a)等方法。


 在browser-solidity中我们可以先add(111) & add(222). 然后调用a[0] & a[1],来查看结果。还有其它操作

结果显示如下图所示:


三、string类型

String数组类型定义

string[] strArr

String数组的增删改查

  • Step 1: 首先撰写一个String数组的contract和构造函数
    pragma solidity 0.4.10;
    contract Demo {
      string[] public strArr;
      function Demo() {
          strArr.push("init");
      }
    }
    
  • Step 2: 添加一个Add function
      function Add(string str) {
          strArr.push(str);
      }
    
  • Step 3: 添加一个Update function
      function Update(uint index, string str) {
          if (index > strArr.length-1) throw;
          strArr[index] = str;
      }
    

    大家注意一下,如果index>strArr.length-1, 此时应抛出异常

  • Step 4: 添加一个ValueOf function
      function ValueOf(uint index) returns (string str){
          if (index > strArr.length-1) throw;
          return strArr[index];
      }
    
  • Step 5: 添加一个DeleteAt function
    function DeleteAt(uint index) {
        uint len = strArr.length;
        if (index > len-1) throw;
        for (uint i = index; i<len-1; i++) {
            strArr[i] = strArr[i+1];
        }

        delete strArr[len-1];
        strArr.length--;
    }
四、Enum类型

Enum类型定义

  enum ActionCode {Left,Right, Forward, Rollback}
  ActionCode public b2;

Enum的Demo代码

  • Step 1: 首先定义enum类型
pragma solidity 0.4.7;
contract Demo {
    enum ActionCode {Left,Right, Forward}
    ActionCode public b2;
}
  • Step 2: 设置一个构造函数,并给变量b2赋予一个初始值
      function Demo() {
          b2 = ActionCode.Left;
      }
    
  • Step 3:设置一个判断函数,输出结果
      function f() returns (string str) {
          if (b2 == ActionCode.Left) return "Left";
      }
    
Step1-3的完整代码如下:
pragma solidity 0.4.10;
contract Demo {
    enum ActionCode {Left,Right, Forward}
    ActionCode public b2;

    function Demo() {
        b2 = ActionCode.Left;
    }

    function f() returns (string str) {
        if (b2 == ActionCode.Left) return "Left";
    }
}
Step 1-3,实现了enum(枚举)类型的功能,在构造函数中设置变量b2的初始值,然后根据b2的value来进行判断。
  • Step 4: 在Step1-3的基础上 添加几个function。Left()/Right()/Forward()
      function Left() {
          b2 = ActionCode.Left;
      }
    
      function Right() {
          b2 = ActionCode.Right;
      }
    
      function Forward() {
          b2 = ActionCode.Forward;
      }
    
  • Step 5: 修改f(), 增加两个输出
          if (b2 == ActionCode.Right) return "Right";
          if (b2 == ActionCode.Forward) return "Forward";
    
完整代码如下:
pragma solidity 0.4.7;
contract Demo {
    enum ActionCode {Left,Right, Forward}
    ActionCode public b2;

    function Demo() {
        b2 = ActionCode.Left;
    }

    function Left() {
        b2 = ActionCode.Left;
    }

    function Right() {
        b2 = ActionCode.Right;
    }

    function Forward() {
        b2 = ActionCode.Forward;
    }

    function f() returns (string str) {
        if (b2 == ActionCode.Left) return "Left";
        if (b2 == ActionCode.Right) return "Right";
        if (b2 == ActionCode.Forward) return "Forward";
    }
}
以上完整的代码,实现了left(),right() & forward() 方法
五、struct类型

Struct类型定义

例如定义一个struct类型的Person

  struct Person {
    string name;
    uint sexy; //0: 男性;1:女性;
    uint age;
    string mobile;
  }

建立一个Struct的Demo合约

  • Step 1: 撰写最基本的合约
pragma solidity 0.4.10;
contract DemoTypes9 {
  struct Person {
    string name;
    uint sexy; //0: 男性;1:女性;
    uint age;
    string mobile;
  }


  Person[] public PersonList;
}

最初的合约只定义了struct person类型,以及一个数组Person[] PersonList

  • Step 2: 给构造函数中添加初始化代码
    function DemoTypes9() {
      /*数组类型添加元素方式1*/
      uint id = PersonList.length++;
      Person p  = PersonList[id];
      p.name = "阿三";
      p.sexy = 0;
      p.age = 20;
      p.mobile = "13918802350";
    }
    
Step 1-2后,完整代码如下:
pragma solidity 0.4.10;
/*演示一下结构,如何和数组类型结合,一起使用;*/
contract DemoTypes9 {
  struct Person {
    string name;
    uint sexy; //0: 男性;1:女性;
    uint age;
    string mobile;
  }


  Person[] public PersonList;
  function DemoTypes9() {
    /*数组类型添加元素方式1*/
    uint id = PersonList.length++;
    Person p  = PersonList[id];
    p.name = "阿三";
    p.sexy = 0;
    p.age = 20;
    p.mobile = "13918802350";
  }
}
以上代码在Browser-solidity中的执行结果如下图所示:

  • Step 3: 添加一个AddPerson function, 使用之前的数组类型使用过的push方法
  function AddPerson (string _name, uint _sexy, uint _age, string _mobile) {
    /*数组类型添加元素方式2*/
    Person memory tmp;
    tmp.name = _name;
    tmp.sexy = _sexy;
    tmp.age = _age;
    tmp.mobile = _mobile;

    PersonList.push(tmp);
  }

Browser-solidity的执行情况如下:

  • Step4: 上面的AddPerson() 有点长,下面有一个更简单的方法 AddPerson2()
  function AddPerson2 (string _name, uint _sexy, uint _age, string _mobile) {
    /*数组类型添加元素方式3*/
    uint id = PersonList.length++;
    PersonList[id] = Person({name: _name, sexy: _sexy, age: _age, mobile: _mobile});
  }

AddPerson2() 的方法简洁了很多
Browser-solidity的执行情况如下

完整代码在下面:

pragma solidity 0.4.10;
/*演示一下结构,如何和数组类型结合,一起使用;*/
contract DemoTypes9 {
  struct Person {
    string name;
    uint sexy; //0: 男性;1:女性;
    uint age;
    string mobile;
  }


  Person[] public PersonList;
  function DemoTypes9() {
    /*数组类型添加元素方式1*/
    uint id = PersonList.length++;
    Person p  = PersonList[id];
    p.name = "阿三";
    p.sexy = 0;
    p.age = 20;
    p.mobile = "13918802350";
  }

  function AddPerson (string _name, uint _sexy, uint _age, string _mobile) {
    /*数组类型添加元素方式2*/
    Person memory tmp;
    tmp.name = _name;
    tmp.sexy = _sexy;
    tmp.age = _age;
    tmp.mobile = _mobile;

    PersonList.push(tmp);
  }

  function AddPerson2 (string _name, uint _sexy, uint _age, string _mobile) {
    /*数组类型添加元素方式3*/
    uint id = PersonList.length++;
    PersonList[id] = Person({name: _name, sexy: _sexy, age: _age, mobile: _mobile});
    }
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值