【C#】上机实验二
实验1:
求解 1/1 + 1 / 2 + 1 / 3 + 1 / 4 …… + 1 / i = ?
确保精度在 1e-6内。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MyProject1
{
class Program
{
static void Main(string[] args)
{
double eps = Math.Pow(, -);
double Eps = 1e-;
double ans = 0.0;
int Last = ;
for (int i = ; 1.0 / i >= Eps; i++)
{
ans = ans + 1.0 / i;
Last = i;
}
double C = 0.57721566490153286060651209;
Console.WriteLine( "Last = {0}",Last);
Console.WriteLine( "1/1 + 1/2 + .... + 1/i = {0} " , ans );
Console.WriteLine( "check : ln({0}) = {1}" , Last+,Math.Log(Last,Math.E)+C );
}
}
}
循环求解
实验2:
练习以下方法的使用:
Array类进行操作,Sort , Reverse,IndexOf,Contains()
随机数的范围生成 :Next(A,B) [ A , B )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MyProject2
{
class Program
{
static void Main(string[] args)
{
/*
定义一个一维数组,其元素个数从键盘中输入,元素的值为[100,200]的随机整数。
(1)输出数组的每个成员值
(2)对数组的成员进行升序排序,输出排序后的数组元素
(3)从键盘上输入一个整数,查找该整数是否存在,若存在输出其所在的下标,若不存在给出提示信息“不存在此数据”。
(4)将数组逆置,并输出排序后的数组元素。
*/ /*第一步: 输入一个n值*/
Console.WriteLine("请输入一个整数n:");
int n = Convert.ToInt32(Console.ReadLine());
int[] a = new int[n]; /*第二步:随机生成[100,200]的随机整数*/
Random R = new Random();
for (int i = ; i < n; i++)
{
a[i] = R.Next(, );
} /* (1)输出数组的每个成员值*/
Console.WriteLine("(1) 输出数组每个成员值");
foreach (var item in a)
{
Console.Write("{0}\t", item);
}
Console.WriteLine("\n______________________________________"); /*(2)对数组的成员进行升序排序,输出排序后的数组元素*/
Console.WriteLine("(2) 数组升序排序后");
Array.Sort(a);
foreach (var item in a)
{
Console.Write("{0}\t", item);
}
Console.WriteLine("\n______________________________________"); /*(3)从键盘上输入一个整数,查找该整数是否存在,若存在输出其所在的下标,
* 若不存在给出提示信息“不存在此数据”。*/ Console.WriteLine("(3) 请输入一个待寻找的数: \n");
int x = int.Parse(Console.ReadLine()); if (a.Contains(x))
{
Console.WriteLine("\n所寻找的数的下标为: {0} (下标从0开始)",Array.IndexOf(a, x));
}
else
{
Console.WriteLine("\n不存在此数据");
}
Console.WriteLine("\n______________________________________"); /*(4)将数组逆置,并输出排序后的数组元素。*/
Console.WriteLine("(4) 数组逆序:");
Array.Reverse(a);
foreach (var item in a)
{
Console.Write("{0}\t", item);
}
Console.WriteLine("\n______________________________________");
}
}
}
Array类方法练习
实验3:
练习字符串的分割
Split 及其相应的StringSplitOptions.RemoveEmptyEntries参数使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MyProject3
{
class Program
{
static void Main(string[] args)
{
/*
给定字符串“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?”
统计单词“the”在字符串中出现的次数。
*/
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?"; string[] str = S.Split(new char[]{' ','.','?'},StringSplitOptions.RemoveEmptyEntries);
const string text = "the"; int cnt = ;
foreach (var item in str )
{
if (item.ToLower() == text)
{
cnt++;
}
}
Console.WriteLine(" 统计单词“the”在字符串中出现的次数为:{0}次",cnt);
}
}
}
字符串的分割
【C#】上机实验二的更多相关文章
- 《Java语言程序设计》上机实验
实验一 Java环境演练 [目的] ①安装并配置Java运行开发环境: ②掌握开发Java应用程序的3个步骤:编写源文件.编译源文件和运行应用程序: ③学习同时编译多个Java源文件. [内容 ...
- ORACLE 实验二
实验二:数据操纵 实验学时:4学时 实验类型:综合型 实验要求:必修 一.实验目的 1.掌握SQL数据查询语句: 2.掌握SQL聚集函数的使用. 3.掌握SQL插入.改动.删除语句的使用. 二.实验内 ...
- 算法课上机实验(一个简单的GUI排序算法比较程序)
(在家里的电脑上Linux Deepin截的图,屏幕大一点的话,deepin用着还挺不错的说) 这个应该是大二的算法课程上机实验时做的一个小程序,也是我的第一个GUI小程序,实现什么的都记不清了,只记 ...
- 软件测试技术lab2——Selenium上机实验
Selenium上机实验说明 1.安装SeleniumIDE插件 2.学会使用SeleniumIDE录制脚本和导出脚本 3.访问http://121.193.130.195:8080/使用学号登录系统 ...
- 20145215&20145307《信息安全系统设计基础》实验二 固件设计
20145215&20145307<信息安全系统设计基础>实验二 固件设计 实验目的与要求 了解多线程程序设计的基本原理,学习 pthread 库函数的使用. 了解在 linux ...
- FPGA与simulink联合实时环路系列——实验二LED
实验二LED 实验内容 在实验一的基础上,将simulink产生的测试信号输出到FPGA开发板上的LED灯进行显示,这里要在生成的硬件模型上进行修改,将传送到FPGA的信号输出到8个LED灯上,并且对 ...
- 20145204&20145212信息安全系统实验二
20145204&20145212信息安全系统实验二 链接
- 20145204&20145212实验二报告
实验二固件设计 步骤: 1.开发环境的配置,参考实验一 1.将实验代码拷贝到共享文件夹中. 2.在虚拟机中编译代码.对于多线程相关的代码,编译时需要加-lpthread的库.下载调试在超级终端中运行可 ...
- 20145215实验二 Java面向对象程序设计
一.实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 二.实验步骤 (一)单元测试 (1)三种代码 伪代码: ...
随机推荐
- 推荐一款分布式微服务框架 Surging
surging surging 是一个分布式微服务框架,提供高性能RPC远程服务调用,采用Zookeeper.Consul作为surging服务的注册中心,集成了哈希,随机,轮询,压力最小优先作为 ...
- P2624 [HNOI2008]明明的烦恼
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #inclu ...
- 第03组 Alpha冲刺(1/4)
队名:不等式方程组 组长博客 作业博客 团队项目进度 组员一:张逸杰(组长) 过去两天完成的任务: 文字/口头描述: 制定了初步的项目计划,并开始学习一些推荐.搜索类算法 GitHub签入纪录: 暂无 ...
- Python入门(一)-打开世界之Hello World
关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 本篇文章同步微信公众号 欢迎大家关注我的微信公众号:「醉翁猫咪」 今天我们来用Python向世界说声Hello World,人生 ...
- avalon数据已更新,视图未更新的bug修复
$computed: { pinlei() { var key = this.currentProduct.key || 'youpin'; console.log(key, "我是key& ...
- sprintf格式化字符串漏洞(转)
深入解析sprintf格式化字符串漏洞 特征: 如何利用: 可以看到, php源码中只对15种类型做了匹配, 其他字符类型都直接break了,php未做任何处理,直接跳过,所以导致了这个问题: 没做字 ...
- MongoDB笔记: 常见问题
系统配置 设置ulimit MongoDB的文件机制 每个Collection会单独创建一个数据文件(collection-xxxxxx.wt) 每个索引会单独创建一个文件(index-xxxxxx. ...
- CentOS7 安装特定版本的Docker
先卸载旧版本 sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-late ...
- Python爬虫笔记安装篇
目录 爬虫三步 请求库 Requests:阻塞式请求库 Requests是什么 Requests安装 selenium:浏览器自动化测试 selenium安装 PhantomJS:隐藏浏览器窗口 Ph ...
- tensorflow2.0手写数字识别
import tensorflow as tf import matplotlib.pyplot as plt import numpy as np datapath = r'D:\data\ml\m ...