A. 输入输出练习

在一行内输入5个整数,空格分隔。输出5个整数,逗号分隔。英文符号。(输入的接收请参考课本例题1-5,注意Split()的用法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
// Written by Sheauhaw Jang
// 2020-10-14 13:10:22
namespace CSharp_Week2_A
{
public static class InputConvert
{
public static string[] GetInputStrings(char SplitOperator)
{
return Console.ReadLine().Split(SplitOperator);
}
public static int[] GetInputArray(char SplitOperator)
{
string[] instr = GetInputStrings(SplitOperator);
int[] invar = new int[instr.Length];
for (int i = 0; i < invar.Length; ++i)
invar[i] = Convert.ToInt32(instr[i]);
return invar;
}
}
class Program
{
static void Main(string[] args)
{
string[] numstr = InputConvert.GetInputStrings(' ');
Console.WriteLine(string.Join(",", numstr));
}
}
}

B. 三角形的类和方法

定义一个三角型类,具有3个public的字段(double)为三角形的3边的长度。提供2个public的方法:

bool IsTriAngle()判断这3边能否构成一个三角形; double Area() 求三角形的面积(如果不能构成三角形,返回值为-1)

输入三角形的3条边,用空格分隔。如果能构成三角形,直接输出三角形面积;否则输出-1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
// Written by Sheauhaw Jang
// 2020-10-14 13:19:31
namespace CSharp_Week2_B
{
class TriAngle
{
static int sidenumber = 3;
double[] sides = new double[sidenumber];
bool vaild;
double totlen, area;
bool TriAngleVaildCheck()
{
for (int i = 0; i < sidenumber; ++i)
if (totlen <= 2 * sides[i])
return false;
return true;
}
double AreaCalculate()
{
double barlen = totlen / 2;
double ans = barlen;
for (int i = 0; i < sidenumber; ++i)
ans *= barlen - sides[i];
return Math.Sqrt(ans);
}
void update(double[] x)
{
totlen = 0;
for (int i = 0; i < x.Length && i < sidenumber; ++i)
sides[i] = x[i];
for (int i = 0; i < sidenumber; ++i)
totlen += sides[i];
vaild = TriAngleVaildCheck();
area = vaild ? AreaCalculate() : 0;
}
public TriAngle(double[] x) { update(x); }
public TriAngle(double a, double b, double c) { update(new double[]{ a, b, c }); }
public bool IsTriAngle() { return vaild; }
public double Area() { return area; }
}
public static class InputConvert
{
public static double[] GetInputArray(char SplitOperator)
{
string[] instr = Console.ReadLine().Split(SplitOperator);
double[] invar = new double[instr.Length];
for (int i = 0; i < invar.Length; ++i)
invar[i] = Convert.ToDouble(instr[i]);
return invar;
}
}

class Program
{
static void Main(string[] args)
{
TriAngle tri = new TriAngle(InputConvert.GetInputArray(' '));
if (tri.IsTriAngle())
Console.WriteLine(tri.Area());
else
Console.WriteLine(-1);
}
}
}

C. 日期Date类

创建一个Date类,要求能输入以下格式的日期: 第一种:MM/YYYY 构造函数接收2个整数 第二种:June,1992 构造函数接收一个字符串和一个整数

当用户输入其出生年月日时,能够计算出用户的年龄(到年即可。向下取整)。当用户输入的日期无意义或未来时间,输出invalid。(当前时间取系统时间,请查C#自带的取时间的函数)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
// Written by Sheauhaw Jang
// 2020-10-19 11:36:13
namespace CSharp_Week2_C
{
class Date
{
static string[] engname = new string[]
{
"",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
public int Year { get; private set; }
public int Month { get; private set; }
public static int NowYear() { return DateTime.Now.Year; }
public static int NowMonth() { return DateTime.Now.Month; }
void SetTypeNum(string date)
{
string[] spdate = date.Split('/');
if (spdate.Length != 2) return;
Year = Convert.ToInt32(spdate[1]);
Month = Convert.ToInt32(spdate[0]);
}
void SetTypeEng(string date)
{
string[] spdate = date.Split(',');
if (spdate.Length != 2) return;
Year = Convert.ToInt32(spdate[1]);
for (int i = 1; i <= 12; ++i)
if (spdate[0] == engname[i])
Month = i;
}
void SetDefault() { Year = Month = 0; }
public Date() { SetDefault(); }
public Date(string date)
{
SetDefault();
if (date[2] == '/')
SetTypeNum(date);
else
SetTypeEng(date);
}
public bool Vaild()
{
if (Month < 1 || Month > 12)
return false;
if (Year > NowYear())
return false;
if (Year == NowYear() && Month > NowMonth())
return false;
return true;
}
public int GetAge() { return NowYear() - Year; }
}
class Program
{
static void Main(string[] args)
{
string date = Console.ReadLine();
Date birth = new Date(date);
if (birth.Vaild())
Console.WriteLine("{0}岁", birth.GetAge());
else
Console.WriteLine("invalid");
}
}
}

D. 心率检查

运动时,可以利用心率监测仪来查看心率是否处于安全范围内。其中最高心率=220-年龄;目标心率是最高心率的50%-85%(向下取整);

创建一个名称为HeartRates的类。这个类的的属性应该包含人的姓名、出生年份和当前年份。

类中还包含一个计算并返回年龄(以整年计算)的属性;一个计算并返回最高心率分方法;以及2个分别计算最低和最高目标心率的方法;

编写程序,实例化HeartRates类,输入个人姓名,出生年月日(空格分隔)。并输出对象的信息,包括姓名,出生年份;年龄;最高心率,最低目标心率,最高目标心率(空格分隔)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
// Written by Sheauhaw Jang
// 2020-10-19 11:55:43
namespace CSharp_Week2_D
{
public static class InputConvert
{
public static int[] GetInputArray(char SplitOperator)
{
string[] instr = Console.ReadLine().Split(SplitOperator);
int[] invar = new int[instr.Length];
for (int i = 0; i < invar.Length; ++i)
invar[i] = Convert.ToInt32(instr[i]);
return invar;
}
}
class HeartRates
{
public string Name { get; private set; }
public int Year { get; private set; }
public int Month { get; private set; }
public int Days { get; private set; }
public static int NowYear() { return DateTime.Now.Year; }
void SetTypeNum(string date)
{
string[] spdate = date.Split('/');
Year = Convert.ToInt32(spdate[0]);
Month = Convert.ToInt32(spdate[1]);
Days = Convert.ToInt32(spdate[2]);
}
void SetDefault() { Year = Month = Days = 0; }
public HeartRates() { SetDefault(); }
public HeartRates(string[] info)
{
SetDefault();
Name = info[0];
SetTypeNum(info[1]);
}
public int GetAge() { return NowYear() - Year; }
public int MaxRates() { return 220 - GetAge(); }
public int MinGoalRates() { return (int)(MaxRates() * 0.5); }
public int MaxGoalRates() { return (int)(MaxRates() * 0.85); }
public string GetInfoLine()
{
string[] info = new string[6];
info[0] = Name;
info[1] = Convert.ToString(Year) + "年";
info[2] = Convert.ToString(GetAge()) + "岁";
info[3] = "最高心率" + Convert.ToString(MaxRates());
info[4] = "最低目标心率" + Convert.ToString(MinGoalRates());
info[5] = "最高目标心率" + Convert.ToString(MaxGoalRates());
return string.Join(" ", info);
}
public void WriteInfoLine() { Console.WriteLine(GetInfoLine()); }
}

class Program
{
static void Main(string[] args)
{
string[] infos = Console.ReadLine().Split(' ');
HeartRates peo = new HeartRates(infos);
peo.WriteInfoLine();
}
}
}