1、每张选票可以选举多个候选人。当每张选票进行选举生效时,需要对选票中的信息进行合法性验证,判断是否存在对应的候选人。根据系统设置,对每一个候选人产生唯一的数字编号ID作为标识,为了避免候选人的姓名相同。经过分析,得出,选票应该设计成一个实体类,该类用于保存选票过程中,有哪些候选人被选入其中。候选人同样需要设计成一个实体类,包括候选人的ID,姓名和其他相应的信息。选票的选举过程和候选人票数的统计则需要在选票管理类中进行完成。1.3 流程图否是1.3 实现方案本程序采用c#的编程语言,面向对象的设计思想进行功能实现。开发环境:win7 开发工具;visual studio 2010编程语言:c#2
2、静态模型设计2.1 类设计(1)类“票数”Ticket属性: private int id;/投递的人(2) 类“候选人”Candidate 属性: private int age;/年龄 private string phone;/电话 private string position;/职位 private int count = 0;/票数 private string name;/姓名 private int id;/编号 ,唯一标识一个候选人 (3)类“投票控制类”TicketControl private List canList;/统计候选人Ticket ticList;/统计票数
3、 方法: public TicketControl() /实例化初始数据 public void vote()/ 进行投票结果的统计 public 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 = valu
4、e; public Ticket(int id) this.id = id; public Ticket() using System; class Candidate : IComparable private string name; public string Name get return name; set name = value; private int id;/编号 public int Id private int age; public int Count get return count; set count = value; public void AddTicket(
5、) this.count+; public Candidate(int id,string name,int age ,string phone,string position) this.name = name; this.age = age; this.phone = phone; this.position = position; public Candidate(int id) public override string ToString() string str = 候选人姓名:+name+n年龄:+age+n电话:+phone +n职位是:+position+n最后总票数为:+c
6、ount; return str; public override bool Equals(object obj) Candidate c = (Candidate)obj; if(c.id=this.id) return true; return false; 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 =
7、this.count) return 0; if (c.count this.count) return -1;using System.Collections; class TicketControl private int index=1; public TicketControl() canList = new List(); /初始化6个候选人 Candidate c = new Candidate(index, 张三1, 12, 123456, 职位 + index); canList.Add(c);index+; c = new Candidate(index, 张三2+index
8、); index+;张三3张三4张三5张三6 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); t = new Ticket(new int 1, 2); t = new Ticket(new int 1, 2, 6, 7); t = new Ticket(new int 2, 3, 4 ); t = new Ticket(new int 2,4 ,8); t = new Ticket(new int 6,5,
9、 4 ); t = new Ticket(new int 5,6); t = new Ticket(new int 2,3 ); t = new Ticket(new int 4,7 ); /进行结果的显示 public void showResult() /对数据进行排序 canList.Sort(); Candidate c1 = canList0; Console.WriteLine(); Console.WriteLine(当选人是: + canList0.Name + ,票数为: + canList0.Count); foreach (Candidate c in canList)
10、Console.WriteLine(c.ToString(); /进行投票选举过程 public void vote() foreach( Ticket t in ticList) int tArr = t.Id; foreach(int id in tArr) foreach(Candidate c in canList) if(c.Id=id) c.AddTicket(); class Program static void Main(string args) TicketControl t = new TicketControl(); t.vote(); t.showResult(); Console.ReadLine();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1