A. 属性的使用

定义Rectangle类,类中的两个属性Length和Width(其对应的私有字段为length和width,默认值为均为1)。

Length属性的set方法中验证其值必须在0~20(开区间)之间的浮点数。如果不满足保持其默认值。

方法:

计算长方形的周长public double Perimeter()

计算长方形的面积public double Area()

输入长方形的长和宽的值,输出长方形的周长、面积。

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
using System;
// Written by Sheauhaw Jang
// 2020-10-20 10:15:50
namespace CSharp_Week4_A
{
class Rectangle
{
double length = 1;
double width = 1;
double Length
{
get { return length; }
set
{
if (value > 0 && value < 20)
length = value;
}
}
double Width
{
get { return width; }
set { width = value; }
}
public Rectangle() { }
public Rectangle(double l, double w)
{
Length = l;
Width = w;
}
public double Perimeter() { return 2 * (length + width); }
public double Area() { return length * width; }
}
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)
{
double[] lenwid = InputConvert.GetInputArray(' ');
Rectangle tim = new Rectangle(lenwid[0], lenwid[1]);
Console.WriteLine(tim.Perimeter());
Console.WriteLine(tim.Area());
}
}
}

B. 利用一维数组求解问题。

读入若干(1-15个)整数(一行输入,空格分隔),每个数在10-100之间的整数包括10和100。

在读入每个数时,确认这个数的有效性(在10到100之间),并且若它和之前读入的数不一样,就把它存储到数组中,无效的数不存储。

读完所有数之后,仅显示用户输入的不同的数值。

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
using System;
using System.Collections;
// Written by Sheauhaw Jang
// 2020-10-20 10:48:45
namespace CSharp_Week4_B
{
class NumberPile
{
static readonly int[] range = { 10, 100 };
readonly bool[] check = new bool[range[1] - range[0] + 1];
ArrayList save = new ArrayList();
public NumberPile()
{
for (int i = 0; i < check.Length; ++i)
check[i] = false;
}
public bool CheckIn(int x)
{
if (x < range[0] || x > range[1])
return false;
return check[x - range[0]];
}
public bool CheckChange(int x)
{
if (x < range[0] || x > range[1])
return false;
return check[x - range[0]] = true;
}
public void Push(params int[] xs)
{
foreach (int x in xs)
if (x >= range[0] && x <= range[1])
if (!CheckIn(x))
{
save.Add(x);
CheckChange(x);
}
}
public void Write() { Console.WriteLine(string.Join(" ", save.ToArray())); }
}
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 Program
{
static void Main(string[] args)
{
NumberPile p = new NumberPile();
p.Push(InputConvert.GetInputArray(' '));
p.Write();
}
}
}

C. 成绩排序

创建一个学生类。

字段:学号(string类型)、成绩(用一个一维数组,存储一个学生的5门课成绩)

方法:将学生的5门课成绩由小到大顺序输出。

主函数中,声明3个学生对象,从控制台给每个学生的学号和5门课成绩赋值,调用方法输出。

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
using System;

namespace CSharp_Week4_C
{
class Student
{
string name;
const int subnum = 5;
int[] score = new int[subnum];
public Student()
{
name = "NoName";
for (int i = 0; i < subnum; ++i)
score[i] = 0;
}
public Student(params string[] info)
{
if (info == null)
return;
if (info.Length > 0)
name = info[0];
for (int i = 0; i < subnum; ++i)
if (info.Length > i + 1)
score[i] = Convert.ToInt32(info[i + 1]);
else
score[i] = 0;
}
public Student(string na, params int[] info)
{
name = na;
for (int i = 0; i < subnum; ++i)
if (info.Length > i)
score[i] = info[i];
else
score[i] = 0;
}
public void Sort() { Array.Sort(score); }
public void Write()
{
string[] info = new string[subnum + 1];
info[0] = name;
for (int i = 0; i < subnum; ++i)
info[i + 1] = Convert.ToString(score[i]);
Console.WriteLine(string.Join(" ", info));
}
}
class Program
{
static void Main(string[] args)
{
const int stunum = 3;
for (int i = 0; i < stunum; ++i)
{
Student stu = new Student(Console.ReadLine().Split(' '));
stu.Sort();
stu.Write();
}
}
}
}

D. 洗牌与发牌

参照课本第五章【案例研究】——模拟洗牌与发牌。

此题目涉及多种概念,请大家参照课本(或MOOC视频)完成代码编写。

【注:】提交后,编译通过即可。

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Text;
// Written by Sheauhaw Jang
// 2020-10-19 11:57:10
namespace CSharp_Week4_D
{
class Card
{
public int Number { get; private set; }
public int Suit { get; private set; }
public int Id { get; private set; }
static string[] suits = new string[] { "♠", "♥", "♣", "♦" };
static string NumberName(int x)
{
switch (x)
{
case 1: return "A";
case 11: return "J";
case 12: return "Q";
case 13: return "K";
default: return Convert.ToString(x);
}
}
public Card(int id)
{
Id = id;
Number = id / 4 + 1;
Suit = id % 4;
}
public Card(int num, int type)
{
Id = 4 * Number - 4 + Suit;
Number = num;
Suit = type;
}
public string GetSuitS() { return suits[Suit]; }
public string GetNumberS() { return NumberName(Number); }
public string GetName() { return GetSuitS() + GetNumberS(); }
public int CompareTo(Card b)
{
if (Id < b.Id)
return -1;
if (Id > b.Id)
return 1;
return 0;
}
}
class PokerPile
{
static int cardnum = 52;
Card[] pcards = new Card[cardnum];
public PokerPile()
{
for (int i = 0; i < cardnum; ++i)
pcards[i] = new Card(i);
}
void swap(ref Card a, ref Card b) { Card tmp = b; b = a; a = tmp; }
public void Shuffle(params int[] seed)
{
Random tool;
if (seed.Length == 0)
{
Console.WriteLine("随机洗牌中......");
tool = new Random();
}
else
{
Console.WriteLine("作弊洗牌中......");
string dinfo = string.Format("{0:X}", seed[0]);
while (dinfo.Length < 8)
dinfo = "0" + dinfo;
dinfo = "0x" + dinfo;
Console.WriteLine("种子为:{0}", dinfo);
tool = new Random(seed[0]);
}
for (int i = 0; i < cardnum; ++i)
swap(ref pcards[i], ref pcards[tool.Next(i, cardnum)]);
Console.WriteLine("洗牌完毕!");
}
public void Sort()
{
Console.WriteLine("恢复牌堆中......");
Array.Sort(pcards, (a, b) => a.CompareTo(b));
Console.WriteLine("恢复完毕!");
}
const int playernum = 4;
readonly static string[] defname = new string[] { "甲", "乙", "丙", "丁" };
readonly static string[] align = new string[] { "第", "", "张:", "", "\t\t" };
const int alignlen = 7;
int GetPreStr(string org)
// 防止破坏格式, 字母1格, 非字母2格, 不得超过7格.
{
int cnt = alignlen;
for (int i = 0; i < cnt && i < org.Length; ++i)
if (org[i] > 127)
--cnt;
return cnt;
}
public void HandOut(params string[] name)
{
Console.WriteLine("发牌中......");
string[] rname = new string[playernum];
for (int i = 0; i < playernum; ++i)
if (name.Length <= i || name[i].Length < 1)
rname[i] = defname[i];
else
{
int remLen = GetPreStr(name[i]);
if (remLen < name[i].Length)
rname[i] = name[i].Remove(remLen);
else
rname[i] = name[i];
}
for (int i = 0; i < playernum; ++i)
Console.Write("\t{0}\t\t", rname[i]);
for (int i = 0; i < cardnum; ++i)
{
if (i % playernum == 0)
Console.WriteLine();
string[] s = new string[align.Length];
Array.Copy(align, s, align.Length);
s[1] = Convert.ToString(i + 1);
while (s[1].Length < 2)
s[1] = " " + s[1];
s[3] = pcards[i].GetName();
Console.Write(string.Join("", s));
}
Console.WriteLine();
Console.WriteLine("发牌完毕!");
}
public void DoAsk()
{
while (true)
{
Console.Write(">> ");
string[] csla = Console.ReadLine().Split(' ');
if (csla.Length == 0)
continue;
switch (csla[0])
{
case "洗牌":
try { Shuffle(Convert.ToInt32(csla[1])); }
catch { Shuffle(); }
break;
case "恢复": Sort(); break;
case "发牌":
string[] aft = new string[csla.Length - 1];
Array.Copy(csla, 1, aft, 0, aft.Length);
HandOut(aft);
break;
case "结束": return;
default: Console.WriteLine("命令未找到!"); break;
}
}
}
}
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = new UTF8Encoding();
PokerPile p = new PokerPile();
p.Shuffle();
p.HandOut();
p.Sort();
p.Shuffle(0x01315878);
p.HandOut("徐忠锋", "乔亚男", "平田一郎");
p.Sort();
p.HandOut("Zeondik,,Sheauhaw".Split(','));
p.DoAsk();
}
}
}