【C#】上机实验二
发布时间:2020-12-16 01:19:06 所属栏目:百科 来源:网络整理
导读:? 实验1: 求解 1/1 + 1 / 2 ?+ 1 / 3 ?+ 1 / 4 …… + 1 / i = ? 确保精度在 1e-6内。 ? 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace MyProject1 7 { 8 class Program 9 { 10 static vo
? 实验1: 求解 1/1 + 1 / 2 ?+ 1 / 3 ?+ 1 / 4 …… + 1 / i = ? 确保精度在 1e-6内。 ? 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace MyProject1 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 double eps = Math.Pow(10,-6); 13 double Eps = 1e-6; 14 double ans = 0.0; 15 int Last = 0; 16 for (int i = 1; 1.0 / i >= Eps; i++) 17 { 18 ans = ans + 1.0 / i; 19 Last = i; 20 } 21 double C = 0.57721566490153286060651209; 22 Console.WriteLine( "Last = {0}",Last); 23 Console.WriteLine( "1/1 + 1/2 + .... + 1/i = {0} ",ans ); 24 Console.WriteLine( "check : ln({0}) = {1}",Last+1,Math.Log(Last,Math.E)+C ); 25 } 26 } 27 } ? ? ? 实验2: 练习以下方法的使用: Array类进行操作,Sort , Reverse,IndexOf,Contains() 随机数的范围生成 :Next(A,B) ?[ A,B ) ? 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace MyProject2 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 /* 13 定义一个一维数组,其元素个数从键盘中输入,元素的值为[100,200]的随机整数。 14 (1)输出数组的每个成员值 15 (2)对数组的成员进行升序排序,输出排序后的数组元素 16 (3)从键盘上输入一个整数,查找该整数是否存在,若存在输出其所在的下标,若不存在给出提示信息“不存在此数据”。 17 (4)将数组逆置,并输出排序后的数组元素。 18 */ 19 20 /*第一步: 输入一个n值*/ 21 Console.WriteLine("请输入一个整数n:"); 22 int n = Convert.ToInt32(Console.ReadLine()); 23 int[] a = new int[n]; 24 25 /*第二步:随机生成[100,200]的随机整数*/ 26 Random R = new Random(); 27 for (int i = 0; i < n; i++) 28 { 29 a[i] = R.Next(100,201); 30 } 31 32 /* (1)输出数组的每个成员值*/ 33 Console.WriteLine("(1) 输出数组每个成员值"); 34 foreach (var item in a) 35 { 36 Console.Write("{0}t",item); 37 } 38 Console.WriteLine("n______________________________________"); 39 40 /*(2)对数组的成员进行升序排序,输出排序后的数组元素*/ 41 Console.WriteLine("(2) 数组升序排序后"); 42 Array.Sort(a); 43 foreach (var item in a) 44 { 45 Console.Write("{0}t",item); 46 } 47 Console.WriteLine("n______________________________________"); 48 49 /*(3)从键盘上输入一个整数,查找该整数是否存在,若存在输出其所在的下标, 50 * 若不存在给出提示信息“不存在此数据”。*/ 51 52 Console.WriteLine("(3) 请输入一个待寻找的数: n"); 53 int x = int.Parse(Console.ReadLine()); 54 55 if (a.Contains(x)) 56 { 57 Console.WriteLine("n所寻找的数的下标为: {0} (下标从0开始)",Array.IndexOf(a,x)); 58 } 59 else 60 { 61 Console.WriteLine("n不存在此数据"); 62 } 63 Console.WriteLine("n______________________________________"); 64 65 /*(4)将数组逆置,并输出排序后的数组元素。*/ 66 Console.WriteLine("(4) 数组逆序:"); 67 Array.Reverse(a); 68 foreach (var item in a) 69 { 70 Console.Write("{0}t",item); 71 } 72 Console.WriteLine("n______________________________________"); 73 } 74 } 75 } ? ? ? 实验3: 练习字符串的分割 Split 及其相应的StringSplitOptions.RemoveEmptyEntries参数使用 ? 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace MyProject3 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 /* 13 给定字符串“The quick brown box jumped over the lazy dog. An apple a day keeps the doctor away. Can a fox and a dog be friends?” 14 统计单词“the”在字符串中出现的次数。 15 */ 16 string S = "The quick brown box jumped over the lazy dog. An apple a day keeps the doctor away. Can a fox and a dog be friends?"; 17 18 string[] str = S.Split(new char[]{‘ ‘,‘.‘,‘?‘},StringSplitOptions.RemoveEmptyEntries); 19 const string text = "the"; 20 21 int cnt = 0; 22 foreach (var item in str ) 23 { 24 if (item.ToLower() == text) 25 { 26 cnt++; 27 } 28 } 29 Console.WriteLine(" 统计单词“the”在字符串中出现的次数为:{0}次",cnt); 30 } 31 } 32 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |