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

更新时间:2024-03-08 12:12:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

实验一、C#面向对象编程

一、 实验目的

1. 2. 3. 4. 5. 6.

熟悉 Visual Studio.net 2008开发环境 了解 Visual C#编程特性 掌握类的创建和对象声明

掌握通过属性访问对象中的数据 掌握继承实现方法

熟悉委托、事件的定义极其使用

二、 实验内容

1. 创建C#控制台应用程序。设计一个简单的密码验证程序,若密码正确,则显示“欢

迎进入本系统!”,否则显示“密码输入错误,请重新输入!”。若连续三次密码输入错误,则显示“对不起,超过最多输入次数,取消服务!”,程序退出。

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

namespace ConsoleApplication1 {

class Program {

static void Main(string[] args) {

int i = 0;

string mima = \; bool k = true;

Console.WriteLine(\ \);

Console.WriteLine(\ 》?¤》?¤》?¤》?¤欢?迎?-使o1用??本à?系|ì统a3《?《?《?《?\\n\\n\);

Console.WriteLine(\请?输o?入¨?您¨2的ì?服¤t务?秘?密¨1 \); while (k) {

string get = Console.ReadLine(); if (get != mima) { i++;

if (i == 3) {

Console.WriteLine(\对?不?起e,ê?您¨2输o?入¨?的ì?密¨1码?错?¨a误¨?次??数oy已°?经-超?过y三¨y次??,ê?\\n\\n已°?取¨?消?服¤t务?,ê?请?按???任¨?意°a键¨1结¨¢束o?!ê? !ê?\); Console.ReadLine(); break; } else

Console.WriteLine(\对?不?起e,ê?您¨2输o?入¨?的ì?密¨1码?有?D误¨?,ê?请?重?新?输o?入¨? !ê?\); } else {

Console.WriteLine(\欢?迎?-进?入¨?本à?系|ì统a3!ê?!ê?\); Console.ReadLine(); break; } }

} } }

2. 创建一个点Point类,属性包括横坐标、纵坐标。要求能够完成点的移动操作、求

两点距离操作,并利用运算符重载,对两个点进行比较(相等和不等)依据是两坐标点相等指它们横坐标和纵坐标分别相等。编写一个测试程序对产生的类的功能进行验证。

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

namespace _1_2 {

class point

{

public double x, y;

public point(double a, double b) {

x = a; y = b; }

public void move(double a, double b) {

x = x + a; y = y + b; }

public static bool operator ==(point a, point b) {

if ((a.x == b.x) && (a.y == b.y)) return true; else

return false; }

public static bool operator !=(point a, point b) {

if ((a.x != b.x) || (a.y != b.y)) return true; else

return false; }

public double distance(point a, point b) {

return Math.Sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } }

class Program {

static void Main() {

point a=new point(1,1) ; point b = new point(2, 2);

Console.WriteLine(\点ì?的ì?坐á?标à¨o:êo(ê?§{0},ê?{1})ê?\,a.x,a.y); Console.WriteLine(\点ì?的ì?坐á?标à¨o:êo(ê?§{0},ê?{1})ê?\, b.x, b.y);

Console.WriteLine(\现?在¨2将?对?a点ì?的ì?坐á?标à¨o进?行D移°?动?¥2和¨a3,ê?请?按???enter键¨1开a始o?移°?动?¥!ê?\);

//string get1 = Console.ReadLine();

// Console.WriteLine(\现?在¨2将?对?a点ì?的ì?纵áY坐á?标à¨o进?行D移°?动?¥,ê?请?输o?

入¨?您¨2要°a移°?动?¥的ì?Y坐á?标à¨o数oy\ // string get2 = Console.ReadLine(); //a.move((double)get1,(double)get2); a.move(2, 3); Console.ReadLine();

Console.WriteLine(\移°?动?¥后¨?a点ì?得ì?坐á?标à¨o是o?:êo({0},{1})\, a.x, a.y); Console.WriteLine(\点ì?坐á?标à¨o移°?动?¥后¨?与??b点ì?坐á?标à¨o的ì?距¨¤离¤?是o?:êo{0}\, a.distance(a,b)); if (a == b)

Console.WriteLine(\点ì?和¨ab点ì?相¨¤等쨨\\n\); else

Console.WriteLine(\点ì?和¨ab点ì?不?相¨¤等쨨\\n\);

Console.WriteLine(\现?将?对?b点ì?坐á?标à¨o分¤?别àe移°?动?¥3和¨a4,ê?按???enter确¨?¤认¨?!ê?\);

b.move(1, 2); Console.ReadLine();

Console.WriteLine(\移°?动?¥后¨?b点ì?得ì?坐á?标à¨o是o?:êo({0},{1})\, b.x, b.y); //Console.WriteLine(\点ì?坐á?标à¨o移°?动?¥后¨?与??a点ì?坐á?标à¨o的ì?距¨¤离¤?是o?:êo{0}\ if (a == b)

Console.WriteLine(\点ì?和¨ab点ì?相¨¤等쨨\); else

Console.WriteLine(\点ì?和¨ab点ì?不?相¨¤等쨨\); Console.ReadLine();

} } }

3. 定义一个顺序表SqlList类,要求能够完成在顺序表中插入元素和删除元素,确

定元素在顺序表中位置,检索元素,清空表,判断表是否为空等操作。编写一个测试程序进行验证。

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

namespace shiyan13 {

class SqlList {

private int[] list; private int len;

public SqlList(int[] a, int b) {

list = a; len = b; }

public void print() {

//Console.WriteLine(\顺3序¨°表à¨a序¨°列¢D是o?:êo\ for (int i = 0; i < len; i++) {

Console.Write(\, list[i]);

} }

public bool insert(int c, int d) {

int temp = 0, i = d - 1; for (; i < len; i++) {

temp = list[i]; list[i] = c; c = temp; }

if (d < len || d < 1) {

Console.WriteLine(\对?不?起e,ê?您¨2的ì?插?入¨?位?置?有?D误¨?,ê?请?重?新?输o?入¨?插?入¨?位?置?!\); return false;

}

return true; }

public bool delete(int e) {

int j = e; for (; j < len; j++) list[j - 1] = list[j]; len--;

if (e < len || e < 1)

{

Console.WriteLine(\对?不?起e,ê?没?有?D您¨2要°a删|?除y的ì?元a素?,ê?请?重?新?输o?入¨?您¨2要°a删|?除y的ì?位?置?!\\n\); return false; }

return true; }

public int lookup(int a) {

int i = 0;

for (i = 0; i < len; i++) if (list[i] == a) break; if (i == len) return -1; else

return (i + 1);

}

public int reserch(int a) {

int i = 0, j = 0; for (; i < len; i++) if (list[i] == a) j++; return j; }

public void clear() {

len = 0; }

public void show() {

if (len == 0)

Console .WriteLine(\顺3序¨°表à¨a已°?清?空?!ê?\\n\); else

Console.WriteLine(\顺3序¨°表à¨a未??清?空?!ê?\\n\); } }

class Program {

static void Main(string[] args) {

int[] a={1,2,3,4,5,6,7,8,9}; int b = 9;

SqlList list = new SqlList(a,b);

Console.WriteLine(\顺3序¨°表à¨a初?始o?化?¥为a:êo\); list.print(); list.insert(4, 6);

Console.WriteLine(\在¨2顺3序¨°表à¨a的ì?第ì¨26个?数oy据Y前??插?入¨?数oy据Y4后¨?为a:êo\);

list.print();

Console.WriteLine(\在¨2顺3序¨°表à¨a检¨?索??数oy据Y3,ê?得ì?到ì?的ì?个?数oy:êo{0}\, list.reserch(3));

Console.WriteLine(\在¨2顺3序¨°表à¨a检¨?索??数oy据Y4,ê?得ì?到ì?的ì?个?数oy:êo{0}\, list.reserch(4));

Console.WriteLine(\在¨2顺3序¨°表à¨a找¨°到ì?第ì¨2一°?个?数oy据Y3所¨′在¨2位?置?:êo{0}\, list.lookup(3));

Console.WriteLine(\在¨2顺3序¨°表à¨a找¨°到ì?第ì¨2一°?个?数oy据Y4所¨′在¨2位?置?:êo{0}\, list.lookup(4)); list.delete(3);

Console.WriteLine(\删|?除y顺3序¨°表à¨a的ì?第ì¨23个?数oy据Y:êo\); list.print();

Console.WriteLine(\判D断?顺3序¨°表à¨a是o?否¤?为a空?:êo\); list.show(); list.clear();

Console.WriteLine(\清?空?顺3序¨°表à¨a后¨?判D断?:êo\); list.show(); Console.Read();

} } }

4. 定义一个shape抽象类,利用它作为基类派生出Rectangle、Circle等具体形状

类,已知具体形状类均具有两个方法GetArea和GetPerim,分别用来求形状的面积和周长。最后编写一个测试程序对产生的类的功能进行验证。

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

namespace shiyan14 {

public abstract class Shape {

public double GetArea() {

return 0; }

public double GetPerim() {

return 0; } }

public class Circle:Shape {

private double r; public Circle(double a) { r=a; }

public new double GetArea() {

return Math.PI*r*r; }

public new double GetPerim() {

return Math.PI*2*r; } }

public class Rectangle : Shape {

private double a, b;

public Rectangle(double c, double d) {

a = c; b = d; }

public new double GetArea() {

return a*b; }

public new double GetPerim() {

return (2*(a+b)); } }

class Program {

static void Main(string[] args) {

Circle str = new Circle(3);

Rectangle ch = new Rectangle(5, 5);

Console.WriteLine(\圆2的ì?面?积y为a:êo{0}\\n\\n\, str.GetArea()); Console.WriteLine(\圆2的ì?周¨1长?è为a:êo{0}\\n\\n\, str.GetPerim()); Console.WriteLine(\长?è方¤?形?面?积y:êo{0}\\n\\n\, ch.GetArea()); Console.WriteLine(\长?è方¤?形?周¨1长?è:êo{0}\\n\\n\, ch.GetPerim()); Console.Read(); } } }

5. 编程实现一个模拟闹铃的程序,具有闹铃、继续闹铃、打会盹儿,停止闹铃的功能。

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

namespace first5 {

class Program {

static void Main(string[] args) { int i;

clock a = new clock();

Console.WriteLine(\输o?入¨?1、?é2、?é3、?é4分¤?别àe表à¨a示o?闹?铃¢?、?é继¨?续?闹?铃¢?、?é稍|?后¨?闹?铃¢?、?é停a?ê止1闹?铃¢?!ê?\\n\\n\); Console.WriteLine(\输o?入¨?:\); i = Console.Read(); Console.ReadLine(); i =i-'0';

if (i == 4) Environment.Exit(0); a.alarm(i); a.again(i); a.rest(i);

i = Console.Read(); i = i - '0'; Console.ReadLine();

if (i == 4) Environment.Exit(0);

a.alarm(i); a.again(i); a.rest(i);

i = Console.Read(); i = i - '0'; Console.ReadLine();

if (i == 4) Environment.Exit(0); a.alarm(i); a.again(i); a.rest(i);

Console.ReadLine(); } } class clock {

System.Media.SoundPlayer music; public void alarm(int i) {

if (i == 1) {

music = new System.Media.SoundPlayer(\); music.Play(); } }

public void again(int i) {

if (i == 2) {

music = new System.Media.SoundPlayer(\); music.Play(); } }

public void rest(int i) {

if (i== 3) {

music.Stop();

System.Threading.Thread.Sleep(10000);

music = new System.Media.SoundPlayer(\); music.Play(); } } }

本文来源:https://www.bwwdw.com/article/pg7a.html

Top