(28.218.81608.81609.81613.83749.83751.83854)

实验一 面向对象程序设计 班级: 16计科2 学号:2015329620062 姓名: 孙远昭 一、实验目的 掌握Visual Studio(C#编程界面)和一些工具的基本使用方法; 掌握C#类型系统; 掌握C#控制语句用法; 掌握数组的用法; 二、实验内容 (实验过程中编写的程序复制到本文件中,下课整理后上交) 填空并验证 将以下程序复制到Visual Studio中,运行(如有错则改正),填写空处。 struct SomeVal { public Int32 x; } class SomeRef { public Int32 x; } SomeVal v1; // 分配到栈(堆/栈) Console.WriteLine(v1.x); //能运行吗? 不能 v1 = new SomeVal(); Console.WriteLine(v1.x); //输出 0 v1.x =5; Console.WriteLine(v1.x); //输出 5 SomeRef r1; Console.WriteLine(r1.x); //能运行吗? 不能 r1 = new SomeRef(); // 分配到_堆 (堆/栈) Console.WriteLine(r1.x); //输出 0 r1.x =5; Console.WriteLine(r1.x); //输出 5 SomeVal v2 = v1; SomeRef r2 = r1; v1.x = 9; r1.x = 8; Console.WriteLine(r1.x); //输出 8 Console.WriteLine(r2.x); //输出 8 Console.WriteLine(v1.x); //输出 9 Console.WriteLine(v2.x); //输出 5 创建控制台应用程序。 1)在程序主方法中,由用户依次输入一个字符串text,以及一个加密字符key,其中变量key保存为int类型。 (提示:加密字符通过Console.ReadKey().KeyChar输入) 2)进行字符串加密。此处采用的加密方法是:将密钥字符与字符串中的每个字符进行异或运算。如’a’与’l’异或结果为字符’P’。 编译运行程序,查看字符串加密结果。 (提示:异或运算只能在整数之间进行,结果也是整数,注意类型转换。可用foreach(char c in text)循环处理字符串。用+=运算符可将字符添加到字符串中。) 3)进行字符串解密。 原理:异或运算具有可逆性,如字符’P’与’l’的异或仍为字符’a’。编写代码取回原字符串并输出。 输出格式参考: 请输入字符串: abcdefg 请输入加密字符: 1 加密后的字符串为:PSRUTWV 解码后的字符串为:abcdefg 参数修饰符的用法: 设计一个工具类,包含四个函数,尽可能重载: 函数一:输入3个参数值,返回其算术平均值 函数二:输入3个参数值,使用ref参数,输出几何平均值、均方根平均值。 函数三,输入3个参数值,使用out参数,同样输出几何平均值、均方根平均值。 函数四,输入n个参数,自行决定参数格式,要求输出几何平均值、均方根平均值。 在主函数中进行测试。 class MeanTool { public static double Mean(double a, double b, double c) { return …; } …… } class Program { public static void Main() { double value1 = 2.0, ……; Console.WriteLine("函数1:{0}", MeanTool.Mean(value1, ……)); double result1 = 0.0; …… } } 写一控制台应用程序,生成一个随机整数,将它转换为字符串,将阿拉伯数字替换为英文表示(即0àzero),输出替换前后的字符串长度,用字符‘o‘将字符串切分,输出切分后的字符串。 随机数生成方法: Random rd = new Random(); int value = rd.Next(); 应用数组、委托等知识,完成以下程序。 程序框架: class Delegates { //创建委托类型 public delegate bool NumberPredicate( int number ); static void Main( string[] args ) { //生成20个随机整数(不超过100),放在数组numbers中并打印这些数 int[] numbers; …code1… //生成委托实例 NumberPredicate evenPredicate = IsEven; //利用委托变量,对任意值调用IsEven Console.WriteLine( "通过委托变量调用IsEven方法: {0}",…code2…); //选出偶数并输出 Console.WriteLine("数组包含以下偶数:"); FilterArray(…code3…); //选出素数并输出 …code4… } //选择满足predicate的数组元素并打印出来 private static void FilterArray(int[] intArray, NumberPredicate predicate) { …code5… } //偶数判断函数 private static bool IsEven( int number ) { return ( number % 2 == 0 ); } //判断是否素数 private static bool IsPrime( int number ) { …code6… } } 三、实验心得与体会 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("请?输º?入¨?字Á?符¤?串ä?:êo"); string text = Console.ReadLine(); Console.WriteLine("请?输º?入¨?加¨®密¨¹字Á?符¤?:êo"); char c = Console.ReadKey().KeyChar; Console.WriteLine("加¨®密¨¹后¨®的Ì?字Á?符¤?串ä?为a:êo"); foreach (char i in text) { Console.Write(i ^ c); } Console.WriteLine(); Console.WriteLine("解a码?后¨®的Ì?字Á?符¤?串ä?为a:êo"); Console.WriteLine(text); Console.ReadKey(); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Random rd = new Random(); int value = rd.Next(); string s = value.ToString(); Console.WriteLine(s); s = s.Replace("0", "zero"); s = s.Replace("1", "one"); s = s.Replace("2", "two"); s = s.Replace("3", "three"); s = s.Replace("4", "four"); s = s.Replace("5", "five"); s = s.Replace("6", "six"); s = s.Replace("7", "sevev"); s = s.Replace("8", "eight"); s = s.Replace("9", "nine"); Console.WriteLine(s); foreach (string tmp in s.Split('o')) { Console.Write(tmp + " "); } Console.WriteLine(); Console.ReadKey(); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleTest { class Program { static void Main(string[] args) { double a = 1, b = 2, c = 3; Console.WriteLine("Mean:{0}", MeanTool.Mean(a, b, c)); double x1 = 0, y1 = 0, z1 = 0; //ref参?数ºy在¨²使º1用®?前¡ã必À?须?先¨¨赋3值¦Ì MeanTool.Mean(a, ref x1, ref y1); Console.WriteLine("Mean:{0} {1} {2}", x1, y1, z1); double x2, y2; //out参?数ºy在¨²使º1用®?前¡ã不?必À?赋3值¦Ì MeanTool.Mean(a, b, c, out x2, out y2); Console.WriteLine("Mean:{0} {1} {2}", x2, y2); double x3 = 0, y3 = 0, z3 = 0; MeanTool.Mean(ref x3, ref y3, 100, 200, 300, 400, 500); //参?数ºy不?定¡§ Console.WriteLine("Mean:{0} {1} {2}", x3, y3, z3); Console.ReadKey(); } } class MeanTool { public static double Mean(double a, double b, double c) //静2态¬?方¤?法¤¡§可¨¦以°?用®?类¤¨¤直¡À接¨®调Ì¡Â用®? { return (a + b + c) / 3; } public static void Mean(double a, ref double b, ref double c) { double tmp1 = (a + b + c) / 3; double tmp2 = Math.Sqrt(a * a + b * b + c * c) / 3; b = tmp1; c = tmp2; } public static void Mean(double a, double b, double c, out double x, out double y) { x = (a + b + c) / 3; y = Math.Sqrt(a * a + b * b + c * c) / 3; } public static void Mean(ref double x, ref double y, params double[] a) //params,ê?用®?于®¨²参?数ºy个?数ºy不?确¨¡¤定¡§,ê?必À?须?是º?方¤?法¤¡§的Ì?最Á?后¨®一°?个?参?数ºy { double tmp1 = 0, tmp2 = 0; foreach (double i in a) { tmp1 += i; tmp2 *= i; } tmp1 /= a.Length; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Delegates { //创ä¡ä建¡§委¡¥托ªD类¤¨¤型¨ª public delegate bool NumberPredicate(int number); static void Main(string[] args) { //生¦¨²成¨¦20个?随?机¨²整?数ºy(ê¡§不?超?过y100)ê?,ê?放¤?在¨²数ºy组Á¨¦numbers中D并¡é打䨰印®?这a些?数ºy int[] numbers = new int[20]; Random rd = new Random(); for (int i = 0; i < 20; i++) numbers[i] = rd.Next(100); //生¦¨²成¨¦委¡¥托ªD实º¦Ì例¤y NumberPredicate evenPredicate = IsEven; //利¤?用®?委¡¥托ªD变À?量¢?,ê?对?任¨?意°a值¦Ì调Ì¡Â用®?IsEven Console.WriteLine("通ª¡§过y委¡¥托ªD变À?量¢?调Ì¡Â用®?IsEven方¤?法¤¡§: {0}", IsEven(rd.Next())); //选?出?偶?数ºy并¡é输º?出? Console.WriteLine("数ºy组Á¨¦包㨹含?以°?下?偶?数ºy:êo"); FilterArray(numbers, evenPredicate); Console.WriteLine(); //选?出?素?数ºy并¡é输º?出? evenPredicate = IsPrime; FilterArray(numbers, evenPredicate); Console.ReadKey(); } //选?择?满¨²足Á?predicate的Ì?数ºy组Á¨¦元a素?并¡é打䨰印®?出?来¤¡ä private static void FilterArray(int[] intArray, NumberPredicate predicate) { foreach (int i in intArray) { if (predicate(i)) { Console.WriteLine(i); } } } //偶?数ºy判D断?函¡¥数ºy private static bool IsEven(int number) { return (number % 2 == 0); } //判D断?是º?否¤?素?数ºy private static bool IsPrime(int number) { for (int i = 2; i * i <= number; i++) if (number % i == 0) return false; return true; } } }

group status

👤 group joined: 0 ⏳ group pending: 0 🚫 group blocked: 0

ALL > Computer and Education > courses > university courses > undergraduate courses > > > > (28.218.81608.81609.81613.83749.83751.83854)

Sub Domains (0)