编写应用合约之前,先弄清它的逻辑,有助于我们更好的部署合约 

pragma solidity ^0.4.21;
pragma experimental ABIEncoderV2; contract voter1{
//voter candidate //["eilinge", "lin" ,"meimei"] //admin 0x4B0897b0513fdC7C541B6d9D7E929C4e5364D2dB //0x14723a09acff6d2a60dcdf7aa4aff308fddc160c 2
//0x583031d1113ad414f02576bd6afabfb302140225 1
//0xdd870fa1b7c4700f2bd7f44238821c26f7392148 0
//投票人
struct Voter{
uint _voterAddr;
uint _weight;
bool _voted;
address delegate;
}
//候选人
struct Candidate{
string candidateName;
uint voterNum;
} address public admin;
//候选人集合
Candidate[] public candidates;
//投票人集合
mapping(address=>Voter) public voters;
//添加候选人
constructor(string[] candidateNames)public{
admin = msg.sender; for(uint i;i<candidateNames.length;i++){
Candidate memory canNum = Candidate(candidateNames[i],0);
candidates.push(canNum);
}
} modifier OnlyAdmin(){
require(admin == msg.sender);
_;
}
//赋予投票权
function giveVoteRight(address addr) OnlyAdmin() public{
if (voters[addr]._weight > 0){
revert();
}
voters[addr]._weight = 1;
} //进行投票
function vote(uint voteNum) public{ Voter storage voter = voters[msg.sender]; if(voter._weight <= 0 || voter._voted ==true){
revert();
} voter._voted = true;
voter._voterAddr = voteNum; candidates[voteNum].voterNum += voter._weight;
} //设置代理人
function delegateFunc(address to) public{
Voter storage voter = voters[msg.sender]; if(voter._weight <= 0 || voter._voted ==true){
revert();
} //设置代理人的地址不为空,且不能是自己
while (voters[to].delegate != address(0) && voters[to].delegate !=msg.sender){
to = voters[to].delegate;//新代理人地址
} //代理人的地址不能是自己
require(msg.sender != to); voter._voted = true;
voter.delegate = to; //代理人
Voter storage finalDelegateVoter = voters[to];
//代理人投过票,则在代理人投票的候选人票数上加上自己的权重
if(finalDelegateVoter._voted){
candidates[finalDelegateVoter._voterAddr].voterNum += voter._weight;
}
//代理人未投过票,则在代理人权重加上自己的权重
else{
finalDelegateVoter._weight += voter._weight;
}
}
//查看谁胜出
function whoWin() public constant returns(string,uint){
string winner;
uint winnerVoteCounts; for (uint i;i>candidates.length;i++){
if (candidates[i].voterNum > winnerVoteCounts){
winnerVoteCounts = candidates[i].voterNum;
winner = candidates[i].candidateName;
}
}
return(winner,winnerVoteCounts);
}
}

ethereum(以太坊)(十二)--应用(二)__投票(基础总和)的更多相关文章

  1. ethereum(以太坊)(十二)--应用(一)__集资(构造函数/映射)

    pragma solidity ^0.4.4; contract funder{ //0xca35b7d915458ef540ade6068dfe2f44e8fa733c //0x14723a09ac ...

  2. ethereum(以太坊)(十一)--字节数组(二)

    pragma solidity ^0.4.0; contract test { uint [5] T =[1,2,3,4,5] ;//固定长度的数组:可修改数组内值大小,不支持push,不可更改长度 ...

  3. ethereum(以太坊)(十四)--Delete

    pragma solidity ^0.4.10; contract Delete{ /* delete可用于任何变量(除mapping),将其设置成默认值 bytes/string:删除所有元素,其长 ...

  4. ethereum(以太坊)(十)--函数修饰符

    pragma solidity ^0.4.0; contract modifierTest{ uint public v1; uint constant v2 =10; //uint constant ...

  5. 创建自己的加密货币MNC——以太坊代币(二)

    创建一个基于以太坊平台的分红币MNC,根据持有的代币数量,进行分红的算法.github地址: https://github.com/lxr1907/MNC 1.使用以太坊根据比例换购token MNC ...

  6. 以太坊开发教程(二) 利用truffle发布宠物商店 DAPP 到 以太坊测试环境Ropsten

    1.环境安装 1) node安装 设置镜像地址: curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -下载安装 ...

  7. ethereum(以太坊)(一)

    从这周开始,开始学习以太坊开发--solidity,开始决定往区块链方向发展,毕竟区块链技术应用广泛.一开始接触solidity开发语言不太习惯,毕竟一直在学习python语法,有很多都不能接受.有难 ...

  8. ethereum(以太坊)(基础)--容易忽略的坑(二)

    pragma solidity ^0.4.0; contract EMath{ string public _a="lin"; function f() public{ modif ...

  9. ethereum(以太坊)(二)--合约中属性和行为的访问权限

    pragma solidity ^0.4.0; contract Test{ /* 属性的访问权限 priveta public internal defualt internal interlnal ...

随机推荐

  1. C语言函数调用简简介

    1.函数的声明: 在编写程序时,首先要对函数进行声明,然后对函数进行定义: 函数的声明是要让编译器知道函数的名称.参数.返回值类型等信息: 函数的定义是要让编译器知道函数的功能: 函数声明的格式由函数 ...

  2. vue监听input标签的value值方法

    <input id="materialSearch" type="text" @keyup.enter="search" @input ...

  3. [UnityShader]点染队列、ZWrite和ZTest

    转载自:http://www.myexception.cn/mobile/1902628.html [UnityShader]渲染队列.ZWrite和ZTest 参考链接:http://blog.cs ...

  4. Javascript基础--数据类型

    一.基本数据类型 1.字符类型:表示字符的类型,例:'aaa',"aaaa",'123456',''(空字符) 2.数字类型:表示数字的类型,例:0,1,3.1415936等 特殊 ...

  5. js&jquery:添加事件的三种方法和常用的一些事件

    一.添加事件的方法 1.EventTarget.addEventListener添加 获取事件目标元素,通过addEventListener函数添加 // Assuming myButton is a ...

  6. webstrom使用

    主题 主题下载:http://color-themes.com/?view=index&page=1&order=popular&search=&layout=HTML ...

  7. Multi-modal Sentence Summarization with Modality Attention and Image Filtering 论文笔记

     文章已同步更新在https://ldzhangyx.github.io/,欢迎访问评论.   五个月没写博客了,不熟悉我的人大概以为我挂了…… 总之呢这段时间还是成长了很多,在加拿大实习的两个多月来 ...

  8. Spring MVC框架下提交Date数据无法在controller直接接收

    主要有两步,controller中添加initBinder方法,再创建一个时间类型数据转换类就OK了. 1.在Controller中创建方法: // 相关包 import java.text.Date ...

  9. April 5 2017 Week 14 Wednesday

    Today is a perfect day to start living your dream. 实现梦想,莫如当下. Miracles may happen every day. If you ...

  10. Hash模板

    ;//一般为靠近总数的素数 struct Hashtable { int x;//hash存的值 Hashtable * next; Hashtable() { next = ; } }; Hasht ...