1、面向对象实验报告二投票选举管理程序的实现投票选举管理程序的实现姓名:尹鹏飞学号:S2*11题目描述1.1 系统的功能需求投票选举过程预先指定若干名侯选人(最多12人)预先指定当选标准(给定百分数)提供投票记录功能(每张票可以选 0-12人)支持选举结果的统计输出(当选人和票数)设计一个监控模块作为选举系统的一部分仅负责数据统计和结果计算分析:对每个候选人分配一个id,编号,进行投票的选举.1.2功能描述投票选举系统中有一些候选人和选票。每张选票可以选举多个候选人。当每张选票进行选举生效时,需要对选票中的信息进行合法性验证,判断是否存在对应的候选人。根据系统设置,对每一个候选人产生唯一的数字编号
2、ID作为标识,为了避免候选人的姓名相同。经过分析,得出,选票应该设计成一个实体类,该类用于保存选票过程中,有哪些候选人被选入其中。候选人同样需要设计成一个实体类,包括候选人的ID,姓名和其他相应的信息。选票的选举过程和候选人票数的统计则需要在选票管理类中进行完成。1.3 流程图否是是否1.3 实现方案本程序采用c#的编程语言,面向对象的设计思想进行功能实现。开发环境:win7 开发工具;visual studio 2010编程语言:c#2 静态模型设计2.1 类设计(1)类“票数”Ticket属性: private int id;/投递的人(2) 类“候选人”Candidate 属性: pri
3、vate int age;/年龄 private string phone;/电话 private string position;/职位 private int count = 0;/票数 private string name;/姓名 private int id;/编号 ,唯一标识一个候选人 (3)类“投票控制类”TicketControl属性: private List canList;/统计候选人 private List ticList;/统计票数 方法: public TicketControl() /实例化初始数据 public void vote()/ 进行投票结果的统计 p
4、ublic void showResult()/进行统计结果的显示2.2类图设计3 动态模型设计4 程序截图5 程序代码using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace voteDemo class Ticket private int id;/投递的人 public int Id get return id; set id = value; public Ticket(int id) this.id = id; public Ticket() using Sy
5、stem;using System.Collections.Generic;using System.Linq;using System.Text;namespace voteDemo class Candidate : IComparable private string name;/姓名 public string Name get return name; set name = value; private int id;/编号 public int Id get return id; set id = value; private int age;/年龄 private string
6、phone;/电话 private string position;/职位 private int count = 0;/票数 public int Count get return count; set count = value; public void AddTicket() this.count+; public Candidate(int id,string name,int age ,string phone,string position) this.id = id; this.name = name; this.age = age; this.phone = phone; th
7、is.position = position; public Candidate(int id) this.id = id; public override string ToString() string str = 候选人姓名:+name+n年龄:+age+n电话:+phone +n职位是:+position+n最后总票数为:+count; return str; public override bool Equals(object obj) Candidate c = (Candidate)obj; if(c.id=this.id) return true; return false;
8、public override int GetHashCode() return base.GetHashCode(); public int CompareTo(object obj) Candidate c=(Candidate)obj; if(c.countthis.count) return 1; if (c.count = this.count) return 0; if (c.count this.count) return -1; return 0; using System;using System.Collections.Generic;using System.Linq;u
9、sing System.Text;using System.Collections;namespace voteDemo class TicketControl private List canList;/统计候选人 private List ticList;/统计票数 private int index=1; public TicketControl() canList = new List(); /初始化6个候选人 Candidate c = new Candidate(index, 张三1, 12, 123456, 职位 + index); canList.Add(c);index+;
10、c = new Candidate(index, 张三2, 12, 123456, 职位+index); canList.Add(c); index+; c = new Candidate(index, 张三3, 12, 123456, 职位 + index); canList.Add(c); index+; c = new Candidate(index, 张三4, 12, 123456, 职位 + index); canList.Add(c); index+; c = new Candidate(index, 张三5, 12, 123456, 职位 + index); canList.Ad
11、d(c); index+; c = new Candidate(index, 张三6, 12, 123456, 职位 + index); canList.Add(c); index+; ticList = new List(); Ticket t = new Ticket(new int 1, 2, 3, 4 ); ticList.Add(t); t = new Ticket(new int 1, 2, 3, 4 ,5); ticList.Add(t); t = new Ticket(new int 1, 2); ticList.Add(t); t = new Ticket(new int 1
12、, 2, 6, 7); ticList.Add(t); t = new Ticket(new int 2, 3, 4 ); ticList.Add(t); t = new Ticket(new int 2,4 ,8); ticList.Add(t); t = new Ticket(new int 6,5, 4 ); ticList.Add(t); t = new Ticket(new int 5,6); ticList.Add(t); t = new Ticket(new int 2,3 ); ticList.Add(t); t = new Ticket(new int 4,7 ); ticL
13、ist.Add(t); /进行结果的显示 public void showResult() /对数据进行排序 canList.Sort(); Candidate c1 = canList0; Console.WriteLine(); Console.WriteLine(当选人是: + canList0.Name + ,票数为: + canList0.Count); foreach (Candidate c in canList) Console.WriteLine(); Console.WriteLine(c.ToString(); /进行投票选举过程 public void vote() f
14、oreach( Ticket t in ticList) int tArr = t.Id; foreach(int id in tArr) foreach(Candidate c in canList) if(c.Id=id) c.AddTicket(); using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace voteDemo class Program static void Main(string args) TicketControl t = new TicketControl(); t.vote(); t.showResult(); Console.ReadLine();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1