C#上机实验题目和答案8.doc_第1页
C#上机实验题目和答案8.doc_第2页
C#上机实验题目和答案8.doc_第3页
C#上机实验题目和答案8.doc_第4页
C#上机实验题目和答案8.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

(1)创建一个控制台应用程序,在程序中定义一个公共接口IMyInterface,该接口中包含两个方法,一个是DoSomething(),另一个是DoSomethingElse();另外再定义一个类MyClass,该类实现了接口IMyInterface,在DoSomething()方法中向控制台输出“Do something.”,在DoSomethingElse()方法中向控制台输出“Do something else.”在Program类中的Main()方法中实例化MyClass的对象和定义一个接口变量,通过对象和接口变量来访问这两个方法。(2)创建一个控制台应用程序,在程序中定义了一个接口IIfc1,该接口包含一个无返回值,且带一个字符串类型的参数的方法PrintOut();在程序中定义了另一个接口IIfc2,该接口中也包含一个无返回值,且带一个字符串类型的参数的方法PrintOut();程序中还定义了一个类MyClass,该类以类级别和显式接口成员两种方式实现了这两个接口。在Program类的Main()方法中分别以类对象的引用和两个接口对象的引用来调用PrintOut()方法。(3)创建一个控制台应用程序,求一个方阵的对角元之和。1.using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication1 public interface IMyInterface void DoSomething(); void DoSomethingElse(); class MyClass : IMyInterface public void DoSomething() Console.WriteLine(Do Something.); public void DoSomethingElse() Console.WriteLine(Do Something Else.); class Program static void Main(string args) MyClass MC = new MyClass(); MC.DoSomething(); IMyInterface imyinterface = (IMyInterface)MC; imyinterface.DoSomethingElse(); 2.using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2 public interface IIfc1 void PrintOut(string s); public interface IIfc2 void PrintOut(string s); public class MyClass : IIfc1, IIfc2 public void PrintOut(string s) Console.WriteLine(IIfc1:0, s); void IIfc2.PrintOut(string s) Console.WriteLine(IIfc2:0, s); class Program static void Main(string args) MyClass MC = new MyClass(); MC.PrintOut(类对象应用!); IIfc2 iifc2 = (IIfc2)MC; iifc2.PrintOut(接口对象应用!); 3.using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication3 class Program static void Main(string args) int i, j; int count = 0; int, a = new int3, 3 5,6,7 , 8,9,10 , 11,12,13 ; for (i = 0; i 3; i+) for (j = 0; j 3; j+) Console.Write(0,4:d, ai, j); count+; if (count / 3 = 0) Console.WriteLine(); int sum = a0, 0 + a1, 1 + a2, 2 + a2, 0 + a0, 2; Console.WriteLine(); Console.WriteLine(sum=0+1+2+3+4=5, a0, 0, a1, 1, a2, 2, a2, 0, a0, 2, sum); 1.创建一个控制台应用程序,生成5个100-200之间的随机数,并利用3种方法来进行排序,输出排序前和排序后的结果。(随机数用Random类的实例方法Next()方法生成。rnd.Next(100-5)+5得到的是5到100之间的数。)2.创建一个类,它存储一个int数据成员MyNumber,并给该数据成员创建属性,当该数据成员被存储时,将其乘以100;当其被读取时,将其除以100。3.编写一个控制台应用程序,要求完成写列功能。1)接收一个整数n。2)如果接收的值n为正数,输出1n间的全部整数。3)如果接收的值n为负值,用break或者return退出程序。4)转到A继续接收下一个整数。4.创建一个控制台应用程序,从键盘输入一个字符串,输出其中最长的单词。(提示:使用字符串的Split( )方法)1. using System.Collections.Generic;using System.Text;namespace ConsoleApplication1 /直接插入排序 public class InsertionSorter public void Sort(int list) for (int i = 1; i 0) & (listj - 1 t) listj = listj - 1; j-; listj = t; /冒泡排序 public class BubbleSorter public void Sort(int list) int i, j, temp; bool done = false; j = 1; while (j list.Length) & (!done) done = true; for (i = 0; i listi + 1) done = false; temp = listi; listi = listi + 1; listi + 1 = temp; j+; /选择排序 public class SelectionSorter private int min; public void Sort(int list) for (int i = 0; i list.Length - 1; i+) min = i; for (int j = i + 1; j list.Length; j+) if (listj listmin) min = j; int t = listmin; listmin = listi; listi = t; class Program static void Main(string args) int Arr = new int5; Random ram = new Random(); for (int i = 0; i 5; i+) Arri = ram.Next(200 - 100) + 100; Console.WriteLine(100200之间的随机数为:); for (int m = 0; m Arr.Length; m+) Console.Write(0,4:d, Arrm); Console.WriteLine(); Console.WriteLine(直接插入排序:); InsertionSorter IS = new InsertionSorter(); IS.Sort(Arr); for (int m = 0; m Arr.Length; m+) Console.Write(0,4:d, Arrm); Console.WriteLine(); Console.WriteLine(冒泡排序:); BubbleSorter BS = new BubbleSorter(); BS.Sort(Arr); for (int m = 0; m Arr.Length; m+) Console.Write(0,4:d, Arrm); Console.WriteLine(); Console.WriteLine(选择排序:); SelectionSorter SS = new SelectionSorter(); SS.Sort(Arr); for (int m = 0; m 0) Console.WriteLine(n为正数:); for (int i = 0; i n; i+) Console.Write(0,4:d, i); if (n 0) return; Console.WriteLine(); Console.WriteLine(请继续输入n:); x = Convert.ToChar(Console.ReadLine(); 4. using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication4 class Program static void Main(string args

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论