本文主要是介绍C# 实验课,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、实验目的
熟悉string、StringBuilder、math、random类用法;
熟悉常用集合使用
熟悉泛型技术二、实验内容
2、定义一个静态成员方法,该方法实现字符串反转。自行设计程序验证上述方法正确性。public static string Reverse(string str){//方法主体中使用StringBuilder}
3、假定已经获取题库中的试题号,并存放在数组arrayKT中。例如, int [] arrayKT={10,13,18,19,20,22,30,31...}。定义一个静态成员方法,该方法实现从上述数组中随机抽出给定数量(n,1<=n<=arrayKT.Length)的考题,并组成一个考题字符串。比如,随机从arrayKT中抽取5题组成考题字符串:“10,18,20,22,30”。要求,组成考题字符串中考题不重复,且一定在数组中存在。自行设计程序验证上述方法正确性。public static string getKTH(int n,param int [] arrayKT){//提示:主体中使用random类}
4、数列a的各项表达式为:a1= ,a2= ,a3= ,…。编写控制台应用程序,计算数列的极限值(n=1000)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;//include StringBuilder
using System.Collections;
using System.Threading;namespace ConsoleApplication1
{class Program{static void Main(string[] args){Console.WriteLine("Experience_1");Console.WriteLine(Exam.getFileNames("c:\\program files\\Maths\\all.dat"));Console.WriteLine("Experience_2");string str = "abcdcd";Console.WriteLine(Exam.Reverse(str));Console.WriteLine("Experience_3");int[] arrayKT = { 10, 13, 18, 19, 20, 22, 30, 31 };for (int i = 0; i < 3; i++){str = Exam.getKTH(3, arrayKT);Console.WriteLine(str);}Console.WriteLine("Experience_4");Console.WriteLine(Exam.cal());Console.Read();}}class Exam{/* 获取文件名* string strFilePaht="文件路径"; * Path.GetFileNameWithoutExtension(strFilePath);* 这个就是获取文件名的还有的就是用Substring截取 * strFilePaht.Substring(path.LastIndexOf("\\") + 1, path.Length - 1 - path.LastIndexOf("\\")); * strFilePaht.Substring(path.LastIndexOf("."), path.Length - path.LastIndexOf("."));* 提问者评价或者用openFileDialog1.SafeFileName*/public static string getFileNames(string file)//1{return file.Substring(file.LastIndexOf("\\") + 1);}public static string Reverse(string str)//2{solution 1使用ctrl+E && ctrl+U删除注释//stringbuilder sbuilder = new stringbuilder();//sbuilder.append(str);//int length = str.length;//char temp;//for (int i = 0; i < length / 2; i++)//{// temp = sbuilder[i];// sbuilder[i] = sbuilder[length - i - 1];// sbuilder[length - i - 1] = temp;//}//str = sbuilder.tostring();//return str;//solution 2int length = str.Length;StringBuilder sbuilder = new StringBuilder();Stack stack = new Stack();for (int i = 0; i < length; i++){stack.Push(str[i]);}for (int i = 0; i < length; i++){sbuilder.Append((char)stack.Pop());}str = sbuilder.ToString();return str;}public static string getKTH(int n, params int[] arrayKT)//3{//question : why the num is same; the function of foreachRandom rand = new Random();string str;int tmp, length = arrayKT.Length;bool [] used = new bool[length];for (int i = 0; i < length; i++){used[i] = true;}tmp = rand.Next(length);str = arrayKT[tmp].ToString();used[tmp] = false;for (int i = 0; i < n - 1; i++){while (true){tmp = rand.Next(length);if (used[tmp]){str += "," + arrayKT[tmp].ToString();used[tmp] = false;break;}}}//using Random()return str;}public static double cal()//4{double[] a = new double[1001];a[1] = Math.Sqrt(2);for (int i = 2; i <= 1000; i++){a[i] = Math.Sqrt(a[i - 1] + 2d);Console.WriteLine(Math.Sqrt(a[i] + 2d));}Console.WriteLine(Math.Sqrt(a[1] + 2d));return a[1000];} }
}
这篇关于C# 实验课的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!