C# 实验课
来源:互联网 发布:淘宝母婴用品好做吗 编辑:程序博客网 时间:2024/05/20 15:40
一、实验目的熟悉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 StringBuilderusing 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 2 int 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 foreach Random 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]; } }}
0 0
- C# 实验课
- C#实验
- C#实验课:人民币和美元兑换
- C#实验[1]
- C#实验[2]
- C#实验[3]
- C#实验[4]
- c#实验6作业
- C#实验8
- C#中的编码实验
- C#实验题目
- C#实验笔记
- C#实验上机
- C#我文本框实验
- C#多线程实验
- C#上机实验
- C#实验十1.1
- C#实验十1.2
- 最近计划----总纲
- Android 优酷
- SICP 习题 (1.38)解题总结
- STL源码剖析 笔记
- 链表的19个功能
- C# 实验课
- CodingTrip - 携程编程大赛 (预赛第二场)1001剪刀石头布&1003 位图像素的颜色
- js点击button按钮跳转到页面代码
- CUDA-lite:为减低GPU编程的复杂性而生[1]
- SQLPLUS 中Autotrace的使用
- codec engine工程中使用ccs下编译的lib库
- 备战“软考”之路一
- VB把一个Excel中的部分数据Copy到另一个Excel表中
- CodingTrip - 携程编程大赛 (预赛第二场):位图像素的颜色