C#实验 C语言基础

实验一     C#语言基础

目录

实验一     

实验目的

实验内容、过程及结果

            


  • 实验目的

  1. 熟练掌握C#的各种数据类型,以及常量、变量的表达形式。
  2. 熟练掌握C#的运算符和表达式。
  3. 熟练掌握C#的语句,会使用顺序、选择、循环等语句结构编写程序。
  4. 熟练掌握C#的数组,学会数组点的定义、初始化,以及数组的应用。
  5. 初步了解类、对象、方法、接口等最基本的面向对象语言的要素。
  • 实验内容过程及结果

【实验2-3】

A.跟着学习

编程进行卡布列克运算,所谓卡布列克运算,是指任意一个4位数,只要它们各个位上的数字各不相同,就会有这样的规律:

(1)把组成这个4位数的四个数字由大到小排列,形成由这四个数字构成的最大的四位数;

(2)把组成这个4位数的四个数字由小到大排列,形成由这四个数字构成的最小的四位数;(如果4个数字中有0,则此类不足4位)

(3)求出以上两数之差,得到一个新的4位数。

重复以上过程,最后的结果总是6174。

源代码:

Console.Write("请输入一个4位整数");

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 /= 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();

运行结果截图:

当n=2456时,程序运行结果如图所示:

B.自己练习:
1.修改程序,对输入字符串中的每个字符进行判断,只有输入的4个字符全为数字,方可继续执行。

 

 namespace Chapter2_3_1

{

    internal class Program

    {

        static void Main(string[] args)

        {

            Console.Write("请输入一个4位整数!");

            int num2;

            string s = Console.ReadLine();//读取输入的字符

            if (int.TryParse(s, out num2))//判断是否是数字,是则进行卡布列克运算

            {

                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);

                }

            }

            else //若有的字符不是数字

            {

                Console.WriteLine("请正确输入!");

            }

            Console.Read();

        }

    }

}

运行截图:

若输入的字符均为数字

 

若输入的有的字符不为数字

 

2.修改程序,把输入字符串中的4个数字直接保存到数组中。

namespace Chapter2_3_2

{

    internal class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("请输入一个4位整数");

            //输入一位数字,将其放入数组中

            int[] each = new int[4];

            int num2;

            for (int n = 0; n < 4; n++)

            {

                string s = Console.ReadLine();

                if (int.TryParse(s, out num2) && (Convert.ToInt32(s) >= 0) && (Convert.ToInt32(s) <= 9))

                {

                    each[n] = Convert.ToInt32(s);

                }

                else

                {

                    Console.Write("请正确输入");

                }

            }

            int num = each[0] * 1000 + each[1] * 100 + each[2] * 10 + each[3];

            int max, min, i, j, temp;

            while (num != 6174 && num != 0)

            {

                i = 0;

                while (num != 0)

                {

                    each[i++] = num % 10;//将每一位存放在数组中

                    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

A.跟着学习

  1. 阅读下列程序

using Chapter2_4;

namespace Chapter2_4

{

    class CRect

    {

        private int top, bottom, left, right;

        public static int total_rects = 0;

        public static long total_rects_area = 0;

        public CRect()

        {

            left = top = right = bottom = 0;

            total_rects++;

            total_rects_area += getHeight() * getWidth();

            Console.WriteLine("CRect() Constructing rectangle number{0}", total_rects);

            Console.WriteLine("Total rectangle areas is:{0}", total_rects_area);

        }

        public CRect(int x1, int y1, int x2, int y2)

        {

            left = x1; top = y1; right = x2; bottom = y2;

            total_rects++;

            total_rects_area += getWidth() * getHeight();

            Console.WriteLine("CRect(int,int,int,int) Constructing rectangle number{0}", total_rects);

            Console.WriteLine("Total rectangle areas is:{0}", total_rects_area);

        }

        public CRect(CRect r)

        {

            left = r.left; right = r.right; bottom = r.bottom; top = r.top;

            total_rects++;

            total_rects_area += getWidth() * getHeight();

            Console.WriteLine("CRect(CRect&) Constructing rectangle number{0}", total_rects);

            Console.WriteLine("Total rectangle areas is:{0}", total_rects_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_rects_area;

        }

    }

}

internal class Program

{

    static void Main(string[] args)

    {

        CRect rect1 = new CRect(1, 3, 6, 4), rect2 = new CRect(rect1);

        Console.Write("Rectangle 2 :Height:{0}", rect2.getHeight());

        Console.WriteLine(",Width:{0}", rect2.getWidth());

        {

            CRect rect3 = new CRect();

            Console.Write("Rectangle 3 :Height:{0}", rect3.getHeight());

        }

        {

            Console.Write("total_rexts={0},", CRect.total_rects);

            Console.WriteLine("total_rect_area={0}", CRect.total_rects_area);

            Console.Read();

        }

    }

}

2编译和运行程序

 

B.自己思考

分析静态成员total_rects和total_rect_area的值及构造函数的调用次序。

total_rects:记录对三个主要函数的调用次数;

total_rect_area计算得到的面积的累和。

构造函数的调用次序:

首先创建了rect1和rect2两个对象。

调用rect2的getHeight()函数

调用rect2的getWidth()函数

创建rect3对象

调用rect3的getHeight()函数

【实验2-5】

  1. 跟着学习

编写IEnglishDimensions和IMetricDimensions两个接口,同时分别以公制单位和英制单位显示框的尺寸。Box类继承IEnglishDimensions和IMetricDimensions两个接口,他们表示不同的度量衡系统。两个接口有相同的成员名Length和Width。

  1. 阅读下列程序

namespace Chapter2_5

{

    internal class Program

    {

        interface IEnglishDimensions

        {

            float Length();

            float Width();

        }

        interface IMetricDimensions

        {

            float Length();

            float Width();

        }

        class Box : IEnglishDimensions, IMetricDimensions

        {

            float lengthInches;

            float widthInches;

            public Box()

            {

                lengthInches = 0.0f;

                widthInches = 0.0f;

            }

            public Box(float length, float width)

            {

                this.lengthInches = length;

                this.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("Length(cm):{0}", mDimensions.Width());

                Console.Read();

            }

        }

    }

}

  1.     编译和运行程序,观察运行结果。

 

B.自己思考

  1. 用隐式接口实现方法重现实现Box类

namespace Chapter2_5

{

    internal class Program

    {

        interface IEnglishDimensions

        {

            float Length();

            float Width();

        }

        interface IMetricDimensions

        {

            float Length();

            float Width();

        }

        public class Box : IEnglishDimensions, IMetricDimensions

        {

            float lengthInches;

            float widthInches;

            public Box()

            {

                lengthInches = 0.0f;

                widthInches = 0.0f;

            }

            public Box(float length, float width)

            {

                this.lengthInches = length;

                this.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("Length(cm):{0}", mDimensions.Width());

                Console.Read();

            }

        }

    }

}

 

    比较显示接口实现和隐式接口实现的异同。

相同点:都能通过接口进行调用

不同点:

1.当类实现一个接口时,通常使用隐式接口实现,这样可以方便的访问接口方法和类自身具有的方法和属性。

2.当类实现多个接口时,并且接口中包含相同的方法签名,此时使用显式接口实现。显示实现接口的类不能直接调用继承于接口的成员,必须先转换才行。

3.隐式接口实现,类和接口都可访问接口中方法。显式接口实现,只能通过接口访问。

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值