【C#】上机实验二

 

实验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 }
Array类方法练习

 

 

 

实验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 }
字符串的分割

相关文章

项目中经常遇到CSV文件的读写需求,其中的难点主要是CSV文件...
简介 本文的初衷是希望帮助那些有其它平台视觉算法开发经验的...
这篇文章主要简单记录一下C#项目的dll文件管理方法,以便后期...
在C#中的使用JSON序列化及反序列化时,推荐使用Json.NET——...
事件总线是对发布-订阅模式的一种实现,是一种集中式事件处理...
通用翻译API的HTTPS 地址为https://fanyi-api.baidu.com/api...