实验2 C#基础类与集合
【实验目的】
1.掌握常用基础类,如math、random、DateTime、String、StringBuilder、Array等类使用方法。
2.掌握常用集合使用方法。
【实验内容】
1、3442 使用C#编写一个控制台应用。输入10个正整数存入数组中,输出最大值、最小值和平均值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Helloworld
{
class Program
{
static void Main(string[] args)
{
double[] num = new double[10];
for (int i = 0; i < 10; i++)
{
num[i] = double.Parse(Console.ReadLine());
}
double max1 = max(num);
Console.WriteLine(max1);
double min1 = min(num);
Console.WriteLine(min1);
double ave1 = ave(num);
Console.Write(ave1);
Console.ReadKey();
}
static double max(double[] num)
{
double max = num[0];
for (int i = 0; i < 10; i++)
{
if (num[i] > max)
{
max = num[i];
}
}
return max;
}
static double min(double[] num)
{
double min = num[0];
for (int i = 0; i < 10; i++)
{
if (num[i] < min)
{
min = num[i];
}
}
return min;
}
static double ave(double[] num)
{
double sum = 0;
double ave = 0;
for (int i = 0; i < 10; i++)
{
sum += num[i];
}
ave = sum / 10;
return ave;
}
}
}
2、3443 使用C#编写一个控制台应用。输入若干个正整数存入数组中(输入exit表示输入结束),输出最大值、最小值和平均值。
using System;
using System.Collections;
class Program
{
static void Main(string[] args)
{
try
{
ArrayList al = new ArrayList();
while (true)
{
string s = Console.ReadLine();
if (s.ToLower().Equals("exit")) break;
int x;
bool flag = int.TryParse(s, out x);
if (!flag || x < 0 || (x == 0 && (s.Trim().Length <= 0 || !s.Equals("0"))))
{
Console.WriteLine("输入数字无效!");
continue;
}
al.Add(x);
}
int[] arr = new int[al.Count];
for (int i = 0; i < al.Count; i++)
{
arr[i] = int.Parse(al[i].ToString());
}
int a, b;
double c;
MyClass.MyPross(out a, out b, out c, arr);
string str1 = String.Format("{0:F}", c);
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(str1);
Console.ReadKey(false);
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
}
class MyClass
{
public static void MyPross(out int max, out int min, out double avg, params int[] arr)
{
try
{
max = 0;
min = arr[0];
avg = 0;
if (arr.Length <= 0) return;
foreach (int item in arr)
{
if (item > max) max = item;
if (item < min) min = item;
avg += item;
}
avg = avg / arr.Length;
}
catch (Exception ex)
{
throw ex;
}
}
}
3、3444 假设有一个字符串包含了文件名、扩展名和路径,如strFileName=@“D \C#程序设计\实验3\MyFile.TXT”。请使用C#编写一个静态方法,该方法能够取出路径中的文件名“MyFile.TXT”。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 提取文件名
{
class Program
{
static void Main(string[] args)
{
try
{
string s = Console.ReadLine();
int start = s.LastIndexOf("MyFile.TXT");
//Console.WriteLine(start);
string new_s = s.Substring(start,10);
Console.WriteLine(new_s);
} catch(Exception)
{
Console.WriteLine("error");
}
Console.ReadKey();
}
}
}
4、3447 使用C#编写一个静态方法。该方法能够根据出生日期,(1)计算此人的年龄;(2)计算从现在到其60周岁期间,总共多少天。
using System;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
DateTime dt;
DateTime.TryParse(Console.ReadLine(), out dt);
DateTime nowtime;
DateTime.TryParse("2019-12-4", out nowtime);
TimeSpan ts1 = nowtime - dt;
Console.WriteLine(ts1.Days / 365);
dt = dt.AddYears(60);
TimeSpan ts = dt - nowtime;
Console.WriteLine(ts.Days - 1);
}
}
}
5、3452 使用C#编写一个静态方法。该方法能够判断字符串是否是“回文”(即顺读和逆读相同的字符串)。
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(f(Console.ReadLine()));
Console.ReadKey();
}
static String f(string str)
{
for (int i = 0; i < str.Length / 2; i++)
{
if (str[i] != str[str.Length - i - 1])
{
return "no";
}
}
return "yes";
}
}
}
6、3484 编写一个控制台程序。以控制台方式输入整数,且调用Class1类CompareNum方法判断是否猜中,给出大了、小了、猜中三种提示。输入exit表示输入结束。若输入的既不是数字,又不是exit,应给出合理提示。如请输入数字!
using System;
namespace practicerandom
{
class Program
{
static void Main(string[] args)
{
Random rdm = new Random();
int guess = rdm.Next(0, 101);
bool temp = false;
try
{
do
{
string sGuess = Console.ReadLine();
int Guess = int.Parse(sGuess);
if (Guess < 0 || Guess > 100)
throw new Exception("The integer does not meet the requirement!");
else if (Guess == guess)
{
temp = true;
Console.WriteLine("猜中了");
Console.ReadKey();
}
else if (Guess > guess)
{
Console.WriteLine("太大了");
}
else if (Guess < guess)
{
Console.WriteLine("太小了");
}
} while (temp != true);
}
catch (Exception e)
{
Console.WriteLine("发生的错误为:" + e.Message);
}
}
}
}