实验内容

  1. 扑克洗牌。
  2. 求三角形的面积,要求用“方法的参数数组”、“接口”、“构造函数”、“类继承”、至少四种方法实现三角形的面积的方法。 最简单的就是根据长方形的面积=长×宽推断出平行四边形的面积=底×高,因为两个一样的三角形可组成一个平行四边形,可得面积计算公式: 三角形的面积=底×高÷2 [S=ah÷2] 或者是: 三角形任意两边之积×这两边的夹角的正弦值÷2 [S=ab×sin×1/2]
  3. 实验耗材管理系统 设计一个耗材管理类,用来保存和记录实验室耗材分类和记录。这个类的成员包括耗材名、使用者、实验室存量。至少提供两个方法: store 耗材的入库处理 show 显示耗材信息 程序运行时,可以从控制台上输入需入库耗材总数,根据这个总数创建耗材类对象数组,然后输入数据,最后可按照耗材名、使用者或实验室存量排序。

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

namespace Shuffle
{
   class Program
  {
       static void Main(string[] args)
      {
           int i, j, temp;
           int k;
           int[] Card = new int[52];   //用于存储52张牌
           int[,] Player = new int[4, 13]; //用于存放4位玩家的13张牌
           Random Rnd = new Random();
           for (i = 0; i < 52; i++)
               Card[i] = i;            //按照顺序存入0~51
           do
          {
               Console.WriteLine("How many times for card:");
               string s = Console.ReadLine();      //读取洗牌次数
               int times = Convert.ToInt32(s);     //将洗牌次数由字符串类型转为整型
               /*
               for (i = 0;i < times;i++)
                   for(j = 0;j <52;j++)
                   {
                       k = Rnd.Next(51 - j + 1) + j;           //产生j到52之间的随机数
                       temp = Card[i];
                       Card[i] = Card[j];
                   }
   */
               for (i = 0; i < times; i++)
                   for (j = 0; j < 52; j++)
                  {
                       k = Rnd.Next(51);               //产生一个0到51的随机数
                       temp = Card[j];
                       Card[j] = Card[k];
                       Card[k] = temp;
                  }

               //下面将打乱的牌分给4个玩家
               for (i = 0; i < 4; i++)
                   for (j = 0; j < 13; j++)
                  {
                       Player[i, j] = Card[i * 13 + j];
                  }

               //分好牌后分别显示4个玩家的牌
               //红桃:'\x0003'
               //方块:'\x0004'
               //梅花:'\x0005'
               //黑桃:'\x0006'

               for (i = 0; i < 4; i++)
              {
                   Console.WriteLine("玩家{0}的牌为:", i + 1);
                   for (j = 0; j < 13; j++)
                  {
                       switch (Player[i, j] / 13)
                      {
                           case 0:
                               s = Convert.ToString('\x0006');
                               break;
                           case 1:
                               s = Convert.ToString('\x0003');
                               break;
                           case 2:
                               s = Convert.ToString('\x0005');
                               break;
                           case 3:
                               s = Convert.ToString('\x0004');
                               break;
                      }

                       switch (Player[i, j] % 13)
                      {
                           case 0:
                               s = s + 'A';
                               break;
                           case 10:
                               s = s + 'J';
                               break;
                           case 11:
                               s = s + 'Q';
                               break;
                           case 12:
                               s = s + 'K';
                               break;
                           default:
                               s = s + Convert.ToString((Player[i, j] % 13) + 1);
                               break;
                      }

                       Console.Write(s + " ");

                  }
                   Console.WriteLine();
              }
          } while (Console.ReadLine() != "txw");
          }
  }

}

image-20220213004400390

2. 求三角形面积

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

namespace Triangle_Method
{
   class Program
  {
       static void Main(string[] args)
      {
           Console.WriteLine("请输入三角形的第一条边:");
           float a = float.Parse(Console.ReadLine());
           Console.WriteLine("请输入三角形的第二条边:");
           float b = float.Parse(Console.ReadLine());
           Console.WriteLine("请输入三角形的第三条边:");
           float c = float.Parse(Console.ReadLine());
           double angleA = Math.Acos((Math.Pow(b, 2) + Math.Pow(c, 2) - Math.Pow(a, 2)) / (2 * b * c));
           double angleB = Math.Acos((Math.Pow(a, 2) + Math.Pow(c, 2) - Math.Pow(b, 2)) / (2 * a * c));
           double angleC = Math.PI - angleA - angleB;
           float height = a * (float)Math.Sin(angleC);
           float baseSide = b;
           float r = a / (2*(float)Math.Sin(angleA));

           Console.WriteLine("三角形面积为:{0:f3}", area(baseSide, height));
           Console.WriteLine("三角形面积为:{0:f3}",area(a, b, angleC));
           Console.WriteLine("三角形面积为:{0:f3}", area(a, b, c));
           Console.WriteLine("三角形面积为:{0:f3}", area(r, angleA, angleB, angleC));
      }

       //传入三角形底和高
       static float area(float baseSide,float height)
      {
           return baseSide * height / 2;
      }

       //传入三角形三边边长
       static float area(float a,float b,float c)
      {
           float p = (a + b + c) / 2;
           return (float)Math.Sqrt(p * (p - a) * (p - b) * (p - c));
      }

       //传入三角形两边及夹角
       static float area(float a,float b,double angleC)
      {
           return a * b * (float)Math.Sin(angleC) / 2;
      }

       //传入三角形的外接圆半径r和三个角的角度
       static float area(float r,double angleA,double angleB,double angleC)
      {
           return (float)(2 * Math.Pow(r,2) *Math.Sin(angleA) * Math.Sin(angleB) * Math.Sin(angleC));
      }

  }
}
v

image-20220213004411916

(2)接口实现

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

namespace Triangle_IO
{

   public interface ITriangle
  {
       float area(float baseSide, float height);
       float area(float a, float b, float c);
       float area(float a, float b, double angleC);
       float area(float r, double angleA, double angleB, double angleC);
  }

   public class Triangle : ITriangle
  {

       //传入三角形底和高
       public float area(float baseSide, float height)
      {
           return baseSide * height / 2;
      }

       //传入三角形三边边长
       public float area(float a, float b, float c)
      {
           float p = (a + b + c) / 2;
           return (float)Math.Sqrt(p * (p - a) * (p - b) * (p - c));
      }

       //传入三角形两边及夹角
       public float area(float a, float b, double angleC)
      {
           return a * b * (float)Math.Sin(angleC) / 2;
      }

       //传入三角形的外接圆半径r和三个角的角度
       public float area(float r, double angleA, double angleB, double angleC)
      {
           return (float)(2 * Math.Pow(r, 2) * Math.Sin(angleA) * Math.Sin(angleB) * Math.Sin(angleC));
      }
     
       
       class Test
      {
           static void Main()
          {
                   Console.WriteLine("请输入三角形的第一条边:");
                   float a = float.Parse(Console.ReadLine());
                   Console.WriteLine("请输入三角形的第二条边:");
                   float b = float.Parse(Console.ReadLine());
                   Console.WriteLine("请输入三角形的第三条边:");
                   float c = float.Parse(Console.ReadLine());
                   double angleA = Math.Acos((Math.Pow(b, 2) + Math.Pow(c, 2) - Math.Pow(a, 2)) / (2 * b * c));
                   double angleB = Math.Acos((Math.Pow(a, 2) + Math.Pow(c, 2) - Math.Pow(b, 2)) / (2 * a * c));
                   double angleC = Math.PI - angleA - angleB;
                   float height = a * (float)Math.Sin(angleC);
                   float baseSide = b;
                   float r = a / (2 * (float)Math.Sin(angleA));

                   Triangle triangle = new Triangle();

                   Console.WriteLine("三角形面积为:{0:f3}", triangle.area(baseSide, height));
                   Console.WriteLine("三角形面积为:{0:f3}", triangle.area(a, b, angleC));
                   Console.WriteLine("三角形面积为:{0:f3}", triangle.area(a, b, c));
                   Console.WriteLine("三角形面积为:{0:f3}", triangle.area(r, angleA, angleB, angleC));
              }
      }
       
  }
}

image-20220213004420464

(3)不同构造方法实现

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;

namespace Triangle_Construct
{
   public class Triangle
  {
       
       protected float area;

       public Triangle(float baseSide,float height)
      {
           area = baseSide * height / 2;
      }

       public Triangle(float a,float b,float c)
      {
           float p = (a + b + c) / 2;
           area = (float)Math.Sqrt(p * (p - a) * (p - b) * (p - c));
      }

       public Triangle(float a, float b, double angleC)
      {
           area = a * b * (float)Math.Sin(angleC) / 2;
      }

       public Triangle(float r, double angleA, double angleB, double angleC)
      {
           area = (float)(2 * Math.Pow(r, 2) * Math.Sin(angleA) * Math.Sin(angleB) * Math.Sin(angleC));

      }

       public float Getarea()
      {
           return this.area;
      }
  }

   class Test
  {
       static void Main(string[] args)
      {
           Console.WriteLine("请输入三角形的第一条边:");
           float a = float.Parse(Console.ReadLine());
           Console.WriteLine("请输入三角形的第二条边:");
           float b = float.Parse(Console.ReadLine());
           Console.WriteLine("请输入三角形的第三条边:");
           float c = float.Parse(Console.ReadLine());
           double angleA = Math.Acos((Math.Pow(b, 2) + Math.Pow(c, 2) - Math.Pow(a, 2)) / (2 * b * c));
           double angleB = Math.Acos((Math.Pow(a, 2) + Math.Pow(c, 2) - Math.Pow(b, 2)) / (2 * a * c));
           double angleC = Math.PI - angleA - angleB;
           float height = a * (float)Math.Sin(angleC);
           float baseSide = b;
           float r = a / (2 * (float)Math.Sin(angleA));
           Triangle triangle1 = new Triangle(baseSide,height);
           Triangle triangle2 = new Triangle(a,b,angleC);
           Triangle triangle3 = new Triangle(a,b,c);
           Triangle triangle4 = new Triangle(r, angleA, angleB, angleC);

           Console.WriteLine("三角形的面积为:{0:f3}", triangle1.Getarea());
           Console.WriteLine("三角形的面积为:{0:f3}", triangle2.Getarea());
           Console.WriteLine("三角形的面积为:{0:f3}", triangle3.Getarea());
           Console.WriteLine("三角形的面积为:{0:f3}", triangle4.Getarea());

      }
  }
}

image-20220213004429436

(4)继承实现

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

namespace Triangle_Inherit
{
   class Shape
  {
       protected float baseSide;
       protected float height;
       
       public Shape() {}

       public Shape(float baseSide,float height)
      {
           this.baseSide = baseSide;
           this.height = height;
      }
       public float area()
      {
           return baseSide * height / 2;
      }
  }

   class Triangle:Shape
  {
       public float a;
       public float b;
       public float c;

       public double angleA;
       public double angleB;
       public double angleC;

       public float r;
       public float area;

       public Triangle(float baseSide, float height) : base(baseSide, height)
      {
           area = area();
      }
       
       public Triangle(float a,float b,float c)
      {
           this.a = a;
           this.b = b;
           this.c = c;
           float p = (a + b + c) / 2;
           area = (float)Math.Sqrt(p * (p - a) * (p - b) * (p - c));
      }

       public Triangle(float a, float b, double angleC)
      {
           this.a = a;
           this.b = b;
           this.angleC = angleC;
           area = a * b * (float)Math.Sin(angleC) / 2;
      }


       public Triangle(float r,double angleA,double angleB,double angleC)
      {
           this.r = r;
           this.angleA = angleA;
           this.angleB = angleB;
           this.angleC = angleC;
           area = (float)(2 * Math.Pow(r, 2) * Math.Sin(angleA) * Math.Sin(angleB) * Math.Sin(angleC));

      }

  }

   class Test
  {
       static void Main(string[] args)
      {
           Console.WriteLine("请输入三角形的第一条边:");
           float a = float.Parse(Console.ReadLine());
           Console.WriteLine("请输入三角形的第二条边:");
           float b = float.Parse(Console.ReadLine());
           Console.WriteLine("请输入三角形的第三条边:");
           float c = float.Parse(Console.ReadLine());
           double angleA = Math.Acos((Math.Pow(b, 2) + Math.Pow(c, 2) - Math.Pow(a, 2)) / (2 * b * c));
           double angleB = Math.Acos((Math.Pow(a, 2) + Math.Pow(c, 2) - Math.Pow(b, 2)) / (2 * a * c));
           double angleC = Math.PI - angleA - angleB;
           float height = a * (float)Math.Sin(angleC);
           float baseSide = b;
           float r = a / (2 * (float)Math.Sin(angleA));

           Triangle triangle1 = new Triangle(baseSide, height);
           Triangle triangle2 = new Triangle(a, b, c);
           Triangle triangle3 = new Triangle(a, b, angleC);
           Triangle triangle4 = new Triangle(r, angleA, angleB, angleC);

           Console.WriteLine("三角形的面积为:{0:f3}", triangle1.area);
           Console.WriteLine("三角形的面积为:{0:f3}", triangle2.area);
           Console.WriteLine("三角形的面积为:{0:f3}", triangle3.area);
           Console.WriteLine("三角形的面积为:{0:f3}", triangle4.area);
      }
  }
}

image-20220213004438293

3. 耗材管理

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

namespace MaterialSystem
{

   class MaterialManage
  {
       private string material, user;
       private float storage;
       public MaterialManage()
      {
           material = "";
           user = "";
           storage = 0;
      }

       public MaterialManage(string material,string user,float storage)
      {
           this.material = material;
           this.user = user;
           this.storage = storage;
      }

       public void store( MaterialManage materialManage)
      {
           material = materialManage.material;
           user = materialManage.user;
           storage = materialManage.storage;
      }

       public void show()
      {
           Console.WriteLine("耗材名:{0}\t使用者:{1}\t库存量:{2}\t", material, user, storage);
      }

       public string Material
      {
           get { return material; }
           set { material = value; }
      }

       public string User
      {
           get { return user; }
           set { user = value; }
      }
       public float Storage
      {
           get { return storage; }
           set { storage = value; }
      }

  }

   class Test
  {
       static void Main(string[] args)
      {
           MaterialManage[] materialManages;
           int[] index;
           int i;
           MaterialManage materialManage = new MaterialManage();
           Console.WriteLine("请输入要入库耗材的种类数:");
           string sline = Console.ReadLine();
           int num = int.Parse(sline);
           materialManages = new MaterialManage[num];
           for (i = 0; i < num; i++)
          {
               materialManages[i] = new MaterialManage();
          }
           index = new int[num];

           for (i = 0; i < num; i++)
          {
               Console.WriteLine("请输入耗材名:");
               materialManage.Material = Console.ReadLine();
               Console.WriteLine("请输入使用者:");
               materialManage.User = Console.ReadLine();
               Console.WriteLine("请输入耗材存入量:");
               materialManage.Storage = int.Parse(Console.ReadLine());
               materialManages[i].store(materialManage);
               index[i] = i;

          }
           
           Console.WriteLine("请选择按什么关键字排序(1.按耗材名 2.按使用者 3.按耗材入库量:");
           int choice = int.Parse(Console.ReadLine());
           switch (choice)
          {
               case 1:
                   sortMaterial(materialManages, index);
                   break;
               case 2:
                   sortUser(materialManages, index);
                   break;
               case 3:
                   sortStorage(materialManages, index);
                   break;

          }
           
           for (i = 0; i < num; i++)
          {
               materialManages[index[i]].show();
          }

      }

       static void sortMaterial(MaterialManage[] materialManages,int[] index)
      {
           int m, n, temp;
           for (int i = 0; i < index.Length - 1; i++)
               for(int j = i + 1; j < index.Length; j++)
              {
                   m = index[i];
                   n = index[j];
                   if (string.Compare(materialManages[m].Material, materialManages[n].Material) > 0)
                  {
                       temp = index[i];
                       index[i] = index[j];
                       index[j] = temp;
                  }
              }
      }

       static void sortUser(MaterialManage[] materialManages, int[] index)
      {
           int m, n, temp;
           for (int i = 0; i < index.Length - 1; i++)
               for (int j = i + 1; j < index.Length; j++)
              {
                   m = index[i];
                   n = index[j];
                   if (string.Compare(materialManages[m].User, materialManages[n].User) > 0)
                  {
                       temp = index[i];
                       index[i] = index[j];
                       index[j] = temp;
                  }
              }
      }

       static void sortStorage(MaterialManage[] materialManages, int[] index)
      {
           int m, n, temp;
           for (int i = 0; i < index.Length - 1; i++)
               for (int j = i + 1; j < index.Length; j++)
              {
                   m = index[i];
                   n = index[j];
                   if (materialManages[m].Storage<materialManages[n].Storage)
                  {
                       temp = index[i];
                       index[i] = index[j];
                       index[j] = temp;
                  }
              }
      }
  }
}

image-20220213004447929