zoukankan      html  css  js  c++  java
  • 【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}	", item);
    37             }
    38             Console.WriteLine("
    ______________________________________");
    39 
    40             /*(2)对数组的成员进行升序排序,输出排序后的数组元素*/
    41             Console.WriteLine("(2)  数组升序排序后");
    42             Array.Sort(a);
    43             foreach (var item in a)
    44             {
    45                 Console.Write("{0}	", item);
    46             }
    47             Console.WriteLine("
    ______________________________________");
    48 
    49             /*(3)从键盘上输入一个整数,查找该整数是否存在,若存在输出其所在的下标,
    50              * 若不存在给出提示信息“不存在此数据”。*/
    51 
    52             Console.WriteLine("(3)  请输入一个待寻找的数: 
    ");
    53             int x = int.Parse(Console.ReadLine());
    54 
    55             if (a.Contains(x))
    56             {
    57                 Console.WriteLine("
    所寻找的数的下标为: {0} (下标从0开始)",Array.IndexOf(a, x));
    58             }
    59             else
    60             {
    61                 Console.WriteLine("
    不存在此数据");
    62             }
    63             Console.WriteLine("
    ______________________________________");
    64 
    65             /*(4)将数组逆置,并输出排序后的数组元素。*/
    66             Console.WriteLine("(4)  数组逆序:");
    67             Array.Reverse(a);
    68             foreach (var item in a)
    69             {
    70                 Console.Write("{0}	", item);
    71             }
    72             Console.WriteLine("
    ______________________________________");
    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 }
    字符串的分割
  • 相关阅读:
    AWK只打印某个域后的所有域
    Apache配置文件httpd.conf内容翻译
    DOM事件类型详解
    DOM中的事件处理概览与原理的全面剖析
    JavaScript实战(带收放动画效果的导航菜单)
    (转)高性能JavaScript:加载和运行(动态加载JS代码)
    (转)网页性能管理详解
    (转)JavaScript-性能优化之函数节流(throttle)与函数去抖(debounce)
    你真的知道setTimeout是如何运行的吗
    用原生JS读写CSS样式的方法总结
  • 原文地址:https://www.cnblogs.com/Osea/p/11610990.html
Copyright © 2011-2022 走看看