C#程序设计实践与分析实验一-参考答案

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

实验一、C#面向对象编程一、实验目的1.熟悉VisualStudio.net2008开发环境2.了解VisualC#编程特性3.掌握类的创建和对象声明4.掌握通过属性访问对象中的数据5.掌握继承实现方法6.熟悉委托、事件的定义极其使用二、实验内容1.创建C#控制台应用程序。设计一个简单的密码验证程序,若密码正确,则显示“欢迎进入本系统!”,否则显示“密码输入错误,请重新输入!”。若连续三次密码输入错误,则显示“对不起,超过最多输入次数,取消服务!”,程序退出。usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication1{classProgram{staticvoidMain(string[]args){inti=0;stringmima=123321;boolk=true;Console.WriteLine();Console.WriteLine(》¡¤》¡¤》¡¤》¡¤欢?迎®-使º1用®?本À?系¦Ì统ª3《?《?《?《?\n\n);Console.WriteLine(请?输º?入¨?您¨²的Ì?服¤t务?秘?密¨¹);while(k){stringget=Console.ReadLine();if(get!=mima){i++;if(i==3){Console.WriteLine(对?不?起e,ê?您¨²输º?入¨?的Ì?密¨¹码?错䨪误¨®次ä?数ºy已°?经-超?过y三¨y次ä?,ê?\n\n已°?取¨?消?服¤t务?,ê?请?按ã¡ä任¨?意°a键¨¹结¨¢束º?!ê?!ê?);Console.ReadLine();break;}elseConsole.WriteLine(对?不?起e,ê?您¨²输º?入¨?的Ì?密¨¹码?有®D误¨®,ê?请?重?新?输º?入¨?!ê?);}else{Console.WriteLine(欢?迎®-进?入¨?本À?系¦Ì统ª3!ê?!ê?);Console.ReadLine();break;}}}}}2.创建一个点Point类,属性包括横坐标、纵坐标。要求能够完成点的移动操作、求两点距离操作,并利用运算符重载,对两个点进行比较(相等和不等)依据是两坐标点相等指它们横坐标和纵坐标分别相等。编写一个测试程序对产生的类的功能进行验证。usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespace_1_2{classpoint{publicdoublex,y;publicpoint(doublea,doubleb){x=a;y=b;}publicvoidmove(doublea,doubleb){x=x+a;y=y+b;}publicstaticbooloperator==(pointa,pointb){if((a.x==b.x)&&(a.y==b.y))returntrue;elsereturnfalse;}publicstaticbooloperator!=(pointa,pointb){if((a.x!=b.x)||(a.y!=b.y))returntrue;elsereturnfalse;}publicdoubledistance(pointa,pointb){returnMath.Sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}}classProgram{staticvoidMain(){pointa=newpoint(1,1);pointb=newpoint(2,2);Console.WriteLine(a点Ì?的Ì?坐Á?标À¨º:êo(ꡧ{0},ê?{1})ê?,a.x,a.y);Console.WriteLine(b点Ì?的Ì?坐Á?标À¨º:êo(ꡧ{0},ê?{1})ê?,b.x,b.y);Console.WriteLine(现?在¨²将?对?a点Ì?的Ì?坐Á?标À¨º进?行D移°?动¡¥2和¨ª3,ê?请?按ã¡äenter键¨¹开a始º?移°?动¡¥!ê?);//stringget1=Console.ReadLine();//Console.WriteLine(现?在¨²将?对?a点Ì?的Ì?纵ÁY坐Á?标À¨º进?行D移°?动¡¥,ê?请?输º?入¨?您¨²要°a移°?动¡¥的Ì?Y坐Á?标À¨º数ºy);//stringget2=Console.ReadLine();//a.move((double)get1,(double)get2);a.move(2,3);Console.ReadLine();Console.WriteLine(移°?动¡¥后¨®a点Ì?得Ì?坐Á?标À¨º是º?:êo({0},{1}),a.x,a.y);Console.WriteLine(a点Ì?坐Á?标À¨º移°?动¡¥后¨®与®?b点Ì?坐Á?标À¨º的Ì?距¨¤离¤?是º?:êo{0},a.distance(a,b));if(a==b)Console.WriteLine(a点Ì?和¨ªb点Ì?相¨¤等̨¨\n);elseConsole.WriteLine(a点Ì?和¨ªb点Ì?不?相¨¤等̨¨\n);Console.WriteLine(现?将?对?b点Ì?坐Á?标À¨º分¤?别Àe移°?动¡¥3和¨ª4,ê?按ã¡äenter确¨¡¤认¨?!ê?);b.move(1,2);Console.ReadLine();Console.WriteLine(移°?动¡¥后¨®b点Ì?得Ì?坐Á?标À¨º是º?:êo({0},{1}),b.x,b.y);//Console.WriteLine(b点Ì?坐Á?标À¨º移°?动¡¥后¨®与®?a点Ì?坐Á?标À¨º的Ì?距¨¤离¤?是º?:êo{0},b.distance(a,b));if(a==b)Console.WriteLine(a点Ì?和¨ªb点Ì?相¨¤等̨¨);elseConsole.WriteLine(a点Ì?和¨ªb点Ì?不?相¨¤等̨¨);Console.ReadLine();}}}3.定义一个顺序表SqlList类,要求能够完成在顺序表中插入元素和删除元素,确定元素在顺序表中位置,检索元素,清空表,判断表是否为空等操作。编写一个测试程序进行验证。usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceshiyan13{classSqlList{privateint[]list;privateintlen;publicSqlList(int[]a,intb){list=a;len=b;}publicvoidprint(){//Console.WriteLine(顺3序¨°表À¨ª序¨°列¢D是º?:êo);for(inti=0;ilen;i++){Console.Write({0}\0,list[i]);}}publicboolinsert(intc,intd){inttemp=0,i=d-1;for(;ilen;i++){temp=list[i];list[i]=c;c=temp;}if(dlen||d1){Console.WriteLine(\n对?不?起e,ê?您¨²的Ì?插?入¨?位?置?有®D误¨®,ê?请?重?新?输º?入¨?插?入¨?位?置?!);returnfalse;}returntrue;}publicbooldelete(inte){intj=e;for(;jlen;j++)list[j-1]=list[j];len--;if(elen||e1){Console.WriteLine(\n对?不?起e,ê?没?有®D您¨²要°a删¦?除y的Ì?元a素?,ê?请?重?新?输º?入¨?您¨²要°a删¦?除y的Ì?位?置?!\n);returnfalse;}returntrue;}publicintlookup(inta){inti=0;for(i=0;ilen;i++)if(list[i]==a)break;if(i==len)return-1;elsereturn(i+1);}publicintreserch(inta){inti=0,j=0;for(;ilen;i++)if(list[i]==a)j++;returnj;}publicvoidclear(){len=0;}publicvoidshow(){if(len==0)Console.WriteLine(顺3序¨°表À¨ª已°?清?空?!ê?\n);elseConsole.WriteLine(顺3序¨°表À¨ª未¡ä清?空?!ê?\n);}}classProgram{staticvoidMain(string[]args){int[]a={1,2,3,4,5,6,7,8,9};intb=9;SqlListlist=newSqlList(a,b);Console.WriteLine(顺3序¨°表À¨ª初?始º?化¡¥为a:êo);list.print();list.insert(4,6);Console.WriteLine(\n\n在¨²顺3序¨°表À¨ª的Ì?第̨²6个?数ºy据Y前¡ã插?入¨?数ºy据Y4后¨®为a:êo);list.print();Console.WriteLine(\n\n在¨²顺3序¨°表À¨ª检¨¬索¡Â数ºy据Y3,ê?得Ì?到Ì?的Ì?个?数ºy:êo{0},list.reserch(3));Console.WriteLine(\n\n在¨²顺3序¨°表À¨ª检¨¬索¡Â数ºy据Y4,ê?得Ì?到Ì?的Ì?个?数ºy:êo{0},list.reserch(4));Console.WriteLine(\n\n在¨²顺3序¨°表À¨ª找¨°到Ì?第̨²一°?个?数ºy据Y3所¨´在¨²位?置?:êo{0},list.lookup(3));Console.WriteLine(\n\n在¨²顺3序¨°表À¨ª找¨°到Ì?第̨²一°?个?数ºy据Y4所¨´在¨²位?置?:êo{0},list.lookup(4));list.delete(3);Console.WriteLine(\n\n删¦?除y顺3序¨°表À¨ª的Ì?第̨²3个?数ºy据Y:êo);list.print();Console.WriteLine(\n\n判D断?顺3序¨°表À¨ª是º?否¤?为a空?:êo);list.show();list.clear();Console.WriteLine(\n清?空?顺3序¨°表À¨ª后¨®判D断?:êo);list.show();Console.Read();}}}4.定义一个shape抽象类,利用它作为基类派生出Rectangle、Circle等具体形状类,已知具体形状类均具有两个方法GetArea和GetPerim,分别用来求形状的面积和周长。最后编写一个测试程序对产生的类的功能进行验证。usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceshiyan14{publicabstractclassShape{publicdoubleGetArea(){return0;}publicdoubleGetPerim(){return0;}}publicclassCircle:Shape{privatedoubler;publicCircle(doublea){r=a;}publicnewdoubleGetArea(){retur

1 / 12
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功