实验一
实验目的
1. 熟练掌握C#的各种数据类型,以及常量、变量的表达形式。
2. 熟练掌握C#的运算符和表达式。
3. 熟练掌握C#的语句,会使用顺序、选择、循环等语句结构编写程序。
4. 熟练掌握C#的数组,学会数组的定义、初始化,以及数组的应用。
5. 初步了解类、对象、方法、接口等最基本的面向对象语言的要素。
实验内容
①实验2-3
编程进行卡布列克运算。所谓卡布列克运算,是指任意一个4位数,只要它们各个位上的数字不全相同,就有这样的规律:
(1)把组成这个4位数的4个数字由大到小排列,形成由这4个数字构成的最大的4位数;
(2)把组成这个4位数的4个数字由小到大排列,形成由这4个数字构成的最小的4位数(如果4个数字中含有0,则此数不足4位);
(3)求出以上两数之差,得到一个新的4位数。
重复以上过程,最后的结果总是6174。
例如:当n=2456时,程序运行结果如图T2.3所示。
完善程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace shiyan2_3._1
{
class Program
{
static void Main(string[] args)
{
Console.Write("请输入一个四位数的整数:");
string s = Console.ReadLine();
int num = Convert.ToInt32(s);
int[] each = new int[4];
int max, min, i, j, temp;
while (num != 6174 && num != 0)
{
i = 0;
while (num != 0)
{
each[i++] = num % 10;
num = num / 10;
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3 - i; j++)
{
if (each[j] > each[j + 1])
{
temp = each[j];
each[j] = each[j + 1];
each[j + 1] = temp;
}
}
}
min = each[0] * 1000 + each[1] * 100 + each[2] * 10 + each[3];
max = each[3] * 1000 + each[2] * 100 + each[1] * 10 + each[0];
num = max - min;
Console.WriteLine("{0}-{1}={2}", max, min, num);
}
Console.Read();
}
}
}
实验结果
自己练习
修改程序,对输入字符串中的每个字符进行判断,只有输入的4个字符全为数字,方可继续执行。
修改程序把输入字符串中的4个数字直接保存到数组中。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace shiyan2_3._1
{
class Program
{
public static bool testInput(string s)
{
for(int n=0; n<s.Length;n++)
{
if (!Char.IsNumber(s[n]))
return false;
}
return true;
}
static void Main(string[] args)
{
Console.Write("请输入一个四位数的整数:");
string s = Console.ReadLine();
char[] array = new char[s.Length];
for (int m = 0; m < s.Length; m++)
array[m] = s[m];
while (!testInput(s))
{
Console.WriteLine("输入错误,请重新输入4位整数");
s = Console.ReadLine();
}
int num = Convert.ToInt32(s);
int[] each = new int[4];
int max, min, i, j, temp;
while (num != 6174 && num != 0)
{
i = 0;
while (num != 0)
{
each[i++] = num % 10;
num = num / 10;
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3 - i; j++)
{
if (each[j] > each[j + 1])
{
temp = each[j];
each[j] = each[j + 1];
each[j + 1] = temp;
}
}
}
min = each[0] * 1000 + each[1] * 100 + each[2] * 10 + each[3];
max = each[3] * 1000 + each[2] * 100 + each[1] * 10 + each[0];
num = max - min;
Console.WriteLine("{0}-{1}={2}", max, min, num);
}
Console.Read();
}
}
}
实验结果
②实验2-4
阅读下列程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test2_4
{
class CRect
{
private int top, bottom, left, right;
public static int total_rects = 0;
public static long total_rect_area = 0;
public CRect()
{
left = top = right = bottom = 0;
total_rects++;
total_rect_area += getHeight() * getWidth();
Console.WriteLine("CRect() Constructing rectangle number {0}", total_rects);
Console.WriteLine("Total rectangle areas is:{0}", total_rect_area);
}
public CRect(int x1, int y1, int x2, int y2)
{
left = x1; top = y1;
right = x2; bottom = y2;
total_rects++;
total_rect_area += getHeight() * getWidth();
Console.WriteLine("CRect(int,int,int,int) Constructing rectangle number {0}", total_rects);
Console.WriteLine("Total rectangle area is:{0}", total_rect_area);
}
public CRect(CRect r)
{
left = r.left; right = r.right;
top = r.top; bottom = r.bottom;
total_rects++;
total_rect_area += getHeight() * getWidth();
Console.WriteLine("CRect(CRect&) Constructing rectangle number {0}", total_rects);
Console.WriteLine("Total rectangle area is : {0}", total_rect_area);
}
public int getHeight()
{
return top > bottom ? top - bottom : bottom - top;
}
public int getWidth()
{
return right > left ? right - left : left - right;
}
public static int getTotalRects()
{
return total_rects;
}
public static long getTotalRectArea()
{
return total_rect_area;
}
}
class Test2_4
{
static void Main(string[] args)
{
CRect rectl = new CRect(1, 3, 6, 4), rect2 = new CRect(rectl);
Console.Write("Rectangle2: Height: {0}", rect2.getHeight());
Console.WriteLine(", Width: {0}", rect2.getWidth());
{//注释(1)
CRect rect3 = new CRect();
Console.Write("Rectangle3: Height: {0}", rect3.getHeight());
Console.WriteLine(", Width: {0}", rect3.getWidth());
}// 注释(2)
Console.Write("total_rects={0},", CRect.total_rects);
Console.WriteLine("total_rects_area={0}", CRect.total_rect_area);
Console.Read();
}
}
}
实验结果
自己思考
分析静态成员total_rects和total_rect_area的值及构造函数的调用次序。
答:①CRect rectl = new CRect(1, 3, 6, 4), rect2 = new CRect(rectl);
首先调用了public CRect(int x1, int y1, int x2, int y2)函数,在内部同时调用了 public int getHeight()和public int getWidth(),此时total rects为1, toatl rect areas为 5;
②接着调用了public CRect(CRect r),同样也调用了 public int getHeight()和public int getWidth(),此时 total_rects 为2,total_rect_areas为10。
③Console.Write("Rectangle2: Height: {0}", rect2.getHeight());
Console.WriteLine(", Width: {0}", rect2.getWidth());
调用了 public int getHeight()和public int getWidth(),此时 total_rects为2,total rect areas 为 10。
④CRect rect3 = new CRect();
调用了public CRect(CRect r),同样也调用了 public int getHeight()和public int getWidth(),此时 total_rects为3,total_rect_areas为10。
⑤ Console.Write("Rectangle3: Height: {0}", rect3.getHeight());
Console.WriteLine(", Width: {0}", rect3.getWidth());
调用了public int getHeight()和public int getWidth(),此时 total_rects为3,total_rect_areas=10
⑥ Console.Write("total_rects={0},", CRect.total_rects);
Console.WriteLine("total_rects_area={0}", CRect.total_rect_area);
调用了public static int getTotalRects()和public static long getTotalRectArea(),此时total_rects为3, total_rect_areas为10。
③实验2_5
编写IEnglishDimensions 和 IMetricDimensions 两个接口,同时分别以公制单位和英制单位显示框的尺寸。Box 类继承 IEnglishDimensions和IMetricDimensions两个接口,它们表示不同的度量衡系统。两个接口有相同的成员名Length和Width。
跟着学习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test2_5
{
interface IEnglishDimensions
{
float Length();
float Width();
}
interface IMetricDimensions
{
float Length();
float Width();
}
class Box : IEnglishDimensions, IMetricDimensions
{
float lengthInches;
float widthInches;
public Box(float length, float width)
{
lengthInches = length;
widthInches = width;
}
float IEnglishDimensions.Length()
{
return lengthInches;
}
float IEnglishDimensions.Width()
{
return widthInches;
}
float IMetricDimensions.Length()
{
return lengthInches * 2.54f;
}
float IMetricDimensions.Width()
{
return widthInches * 2.54f;
}
static void Main(string[] args)
{
Box myBox = new Box(30.0f, 20.0f);
IEnglishDimensions eDimensions = (IEnglishDimensions)myBox;
IMetricDimensions mDimensions = (IMetricDimensions)myBox;
Console.WriteLine(" Length(in):{0}", eDimensions.Length());
Console.WriteLine(" Width (in): {0}", eDimensions.Width());
Console.WriteLine(" Length(cm):{0}", mDimensions.Length());
Console.WriteLine(" Width (cm):{0}", mDimensions.Width());
Console.Read();
}
}
}
实验结果
自己思考
用隐式接口实现方法重新实现Box类。
实验代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test2_5
{
interface IEnglishDimensions
{
float Length0();
float Width0();
}
interface IMetricDimensions
{
float Length1();
float Width1();
}
class Box : IEnglishDimensions, IMetricDimensions
{
float lengthInches;
float widthInches;
public Box(float length, float width)
{
lengthInches = length;
widthInches = width;
}
public float Length0()
{
return lengthInches;
}
public float Width0()
{
return widthInches;
}
public float Length1()
{
return lengthInches*2.54f;
}
public float Width1()
{
return widthInches*2.54f;
}
static void Main(string[] args)
{
Box myBox = new Box(30.0f, 20.0f);
Console.WriteLine(" Length(in):{0}", myBox.Length0());
Console.WriteLine(" Width (in): {0}", myBox .Width0());
Console.WriteLine(" Length(cm):{0}", myBox.Length1());
Console.WriteLine(" Width (cm):{0}", myBox.Width1());
Console.Read();
}
}
}
实验结果
2.比较显式接口实现和隐式接口实现的异同。
答:隐示实现对象声明为接口和类都可以访问到其行为,显示实现只有声明为接口可以访问:隐式和显式接口实现的关键区别显然并不在于方法声明,而是在于从类外部的可访问性。