面向对象实验报告二投票选举管理程序的实现.docx

上传人:b****5 文档编号:7909560 上传时间:2023-01-27 格式:DOCX 页数:11 大小:108.79KB
下载 相关 举报
面向对象实验报告二投票选举管理程序的实现.docx_第1页
第1页 / 共11页
面向对象实验报告二投票选举管理程序的实现.docx_第2页
第2页 / 共11页
面向对象实验报告二投票选举管理程序的实现.docx_第3页
第3页 / 共11页
面向对象实验报告二投票选举管理程序的实现.docx_第4页
第4页 / 共11页
面向对象实验报告二投票选举管理程序的实现.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

面向对象实验报告二投票选举管理程序的实现.docx

《面向对象实验报告二投票选举管理程序的实现.docx》由会员分享,可在线阅读,更多相关《面向对象实验报告二投票选举管理程序的实现.docx(11页珍藏版)》请在冰豆网上搜索。

面向对象实验报告二投票选举管理程序的实现.docx

面向对象实验报告二投票选举管理程序的实现

投票选举管理程序的实现

姓名:

尹鹏飞

学号:

S2*******1

1题目描述

1.1系统的功能需求

投票选举过程

–预先指定若干名侯选人(最多12人)

–预先指定当选标准(给定百分数)

–提供投票记录功能(每张票可以选0-12人)

–支持选举结果的统计输出(当选人和票数)

设计一个监控模块

–作为选举系统的一部分

–仅负责数据统计和结果计算

分析:

对每个候选人分配一个id,编号,进行投票的选举.

1.2功能描述

投票选举系统中有一些候选人和选票。

每张选票可以选举多个候选人。

当每张选票进行选举生效时,需要对选票中的信息进行合法性验证,判断是否存在对应的候选人。

根据系统设置,对每一个候选人产生唯一的数字编号ID作为标识,为了避免候选人的姓名相同。

经过分析,得出,选票应该设计成一个实体类,该类用于保存选票过程中,有哪些候选人被选入其中。

候选人同样需要设计成一个实体类,包括候选人的ID,姓名和其他相应的信息。

选票的选举过程和候选人票数的统计则需要在选票管理类中进行完成。

1.3流程图

 

 

 

1.3实现方案

本程序采用c#的编程语言,面向对象的设计思想进行功能实现。

开发环境:

win7

开发工具;visualstudio2010

编程语言:

c#

2静态模型设计

2.1类设计

(1)类“票数”Ticket

属性:

privateint[]id;//投递的人

(2)类“候选人”Candidate

属性:

privateintage;//年龄

privatestringphone;//电话

privatestringposition;//职位

privateintcount=0;//票数

privatestringname;//姓名

privateintid;//编号,唯一标识一个候选人

(3)类“投票控制类”TicketControl

属性:

privateListcanList;//统计候选人

privateListticList;//统计票数

方法:

publicTicketControl()//实例化初始数据

publicvoidvote()//进行投票结果的统计

publicvoidshowResult()//进行统计结果的显示

2.2类图设计

3动态模型设计

4程序截图

5程序代码

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

namespacevoteDemo

{

classTicket

{

privateint[]id;//投递的人

publicint[]Id

{

get{returnid;}

set{id=value;}

}

publicTicket(int[]id){

this.id=id;

}

publicTicket(){

}

}

}usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

namespacevoteDemo

{

classCandidate:

IComparable

{

privatestringname;//姓名

publicstringName

{

get{returnname;}

set{name=value;}

}

privateintid;//编号

publicintId

{

get{returnid;}

set{id=value;}

}

privateintage;//年龄

privatestringphone;//电话

privatestringposition;//职位

privateintcount=0;//票数

publicintCount

{

get{returncount;}

set{count=value;}

}

publicvoidAddTicket(){

this.count++;

}

publicCandidate(intid,stringname,intage,stringphone,stringposition){

this.id=id;this.name=name;this.age=age;

this.phone=phone;this.position=position;

}

publicCandidate(intid){

this.id=id;

}

publicoverridestringToString()

{

stringstr="候选人姓名:

"+name+"\n年龄:

"+age+"\n电话:

"+phone

+"\n职位是:

"+position+"\n最后总票数为:

"+count;

returnstr;

}

publicoverrideboolEquals(objectobj)

{

Candidatec=(Candidate)obj;

if(c.id==this.id){

returntrue;

}

returnfalse;

}

publicoverrideintGetHashCode()

{

returnbase.GetHashCode();

}

publicintCompareTo(objectobj){

Candidatec=(Candidate)obj;

if(c.count>this.count){

return1;

}

if(c.count==this.count)

{

return0;

}

if(c.count

{

return-1;

}

return0;

}

}

}usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Collections;

namespacevoteDemo

{

classTicketControl

{

privateListcanList;//统计候选人

privateListticList;//统计票数

privateintindex=1;

publicTicketControl(){

canList=newList();

//初始化6个候选人

Candidatec=newCandidate(index,"张三1",12,"123456","职位"+index);

canList.Add(c);index++;

c=newCandidate(index,"张三2",12,"123456","职位"+index);

canList.Add(c);index++;

c=newCandidate(index,"张三3",12,"123456","职位"+index);

canList.Add(c);index++;

c=newCandidate(index,"张三4",12,"123456","职位"+index);

canList.Add(c);index++;

c=newCandidate(index,"张三5",12,"123456","职位"+index);

canList.Add(c);index++;

c=newCandidate(index,"张三6",12,"123456","职位"+index);

canList.Add(c);index++;

ticList=newList();

Tickett=newTicket(newint[]{1,2,3,4});

ticList.Add(t);

t=newTicket(newint[]{1,2,3,4,5});

ticList.Add(t);

t=newTicket(newint[]{1,2});

ticList.Add(t);

t=newTicket(newint[]{1,2,6,7});

ticList.Add(t);

t=newTicket(newint[]{2,3,4});

ticList.Add(t);

t=newTicket(newint[]{2,4,8});

ticList.Add(t);

t=newTicket(newint[]{6,5,4});

ticList.Add(t);

t=newTicket(newint[]{5,6});

ticList.Add(t);

t=newTicket(newint[]{2,3});

ticList.Add(t);

t=newTicket(newint[]{4,7});

ticList.Add(t);

}

//进行结果的显示

publicvoidshowResult(){

//对数据进行排序

canList.Sort();

Candidatec1=canList[0];

Console.WriteLine();

Console.WriteLine("当选人是:

"+canList[0].Name+",票数为:

"+canList[0].Count);

foreach(CandidatecincanList)

{

Console.WriteLine();

Console.WriteLine(c.ToString());

}

}

//进行投票选举过程

publicvoidvote(){

foreach(TickettinticList){

int[]tArr=t.Id;

foreach(intidintArr){

foreach(CandidatecincanList){

if(c.Id==id){

c.AddTicket();

}

}

}

}

}

}

}usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

namespacevoteDemo

{

classProgram

{

staticvoidMain(string[]args)

{

TicketControlt=newTicketControl();

t.vote();

t.showResult();

Console.ReadLine();

}

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 工学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1