C#实践(五)--------类的学习与实践(一)

题目:
1.定义一个复数Complex类,包含:①两个实型字段,分别代表复数的实部和虚部;②重载“ +、- ”运算符,实现两个复数的加法、减法运算;③定义一个不带参的DispComplex方法,返回一个复数形式的字符串。编写一个测试类对Complex类的功能进行验证。要求带异常处理。
代码:
Complex类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test2
{
    class Complex
    {
        private double real;
        private double image;

        public double Real
        {
            get { return real; }  //读属性
            set { real = value; } //写属性
        }
        public double Image
        {
            get { return image; }
        }
        public Complex()
        { }
        public Complex(double real,double image)
        {
            this.real = real;
            this.image = image;
        }

        public static Complex operator +(Complex c1,Complex c2)
        {
            Complex c = new Complex();
            c.real = c1.real + c2.real;
            c.image = c1.image + c2.image;
            return c;
        }
        public static Complex operator -(Complex c1, Complex c2)
        {
            Complex c = new Complex();
            c.real = c1.real - c2.real;
            c.image = c1.image - c2.image;
            return c;
        }
        public string DispComplex()
        {
            string str = "";
            if (image > 0)
                str = real.ToString() + " + " + image.ToString() + "i";
            else if (image == 0)
                str = real.ToString();
            else if (image < 0)
                str = real.ToString() + " - " + (-image).ToString() + "i";
            return str;
        }
    }
}

测试类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test2
{
    class Program
    {
        static void Main(string[] args)
        {
            double r1, i1, r2, i2;
            try
            {
                Console.WriteLine("请输入第一个复数的实部");
                r1 = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("请输入第一个复数的虚部");
                i1 = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("请输入第二个复数的实部");
                r2 = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("请输入第二个复数的虚部");
                i2 = Convert.ToDouble(Console.ReadLine());
                Complex c1 = new Complex(r1,i1);
                Console.WriteLine("第一个复数为:"+c1.DispComplex());
                Complex c2 = new Complex(r2, i2);
                Console.WriteLine("第二个复数为:" + c2.DispComplex());
                Console.WriteLine("两个复数的和为:" + (c1 + c2).DispComplex());
                Console.WriteLine("两个复数的差为:" + (c1 - c2).DispComplex());
            }
            catch (Exception e1)
            {
                Console.WriteLine(e1.Message);
            }
            Console.WriteLine("输入回车符结束程序的运行");
            Console.ReadLine();
        }
    }
}

运行结果:
在这里插入图片描述
总结:
本实验中使用了两个类,一个是Complex,在这个类中主要实现了复数的算法,采用运算符的重载,
需要注意的是:在这个运算符重载必须使用关键字operator.另一个是测试类。

题目:
编写圆形Circle类,包含创建对象、计算面积、计算周长、统计个数的操作。定义一个圆柱体Cylinder类,该类能够计算圆柱体的表面积和体积。编写一个测试类对上述功能进行测试,要求带异常处理。
代码:

Circle类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test2
{
    class Circle
    {
        static int count = 0;
        private int center_x = 0;
        private int center_y = 0;
        private double radius;

        public double Radius
        {
            get { return radius; }
            set { radius = value; }
        }
        public Circle()
        {
            count++;
        }
        public Circle(double radius)
        {
            this.radius = radius;
            count++;
        }
        public string area(double radius)
        {
            double area;
            area = Math.PI * radius * radius;
            area = Math.Round(area,2);
            return area.ToString();
        }
        public string zhoucahng(double radius)
        {
            double l;
            l = 2 * Math.PI * radius;
            l = Math.Round(l, 2);
            return l.ToString();
        }
        public string Count()
        {
            return count.ToString();
        }
    }
}

Cylinder类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test2
{
    class Cylinder
    {
        private int center_x = 0;
        private int center_y = 0;
        private double radius;
        private double height;
        public double Radius
        {
            get {return radius; }
            set {radius = value; }
        }
        public double Height
        {
            get {return height; }
            set {height = value; }
        }
        public Cylinder()
        { }
        public Cylinder(double radius,double height)
        {
            this.radius = radius;
            this.height = height;
        }
        public  static double square(double radius,double height)
        {
            double s;
            s = (2 * Math.PI * radius * radius) + (2 * Math.PI * radius * height);
            s = Math.Round(s, 2);
            return s;
        }
        public static double cube(double radius, double height)
        {
            double c;
            c = (Math.PI * radius * radius) * height;
            c = Math.Round(c, 2);
            return c;
        }
    }
}

测试:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test2
{
    class Program
    {
        static void Main(string[] args)
        {
            double r1, r2, h;
            Circle circle = new Circle();
            try
            {
                Console.WriteLine("请输入圆的半径");
                r1 = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("------------------------------");
                Console.WriteLine("圆的面积为:" + circle.area(r1));
                Console.WriteLine("------------------------------");
                Console.WriteLine("圆的周长为:" + circle.zhoucahng(r1));
                Console.WriteLine("------------------------------");
                Console.WriteLine("统计个数:" + circle.Count());
                Console.WriteLine("------------------------------");
                Console.WriteLine("请输入圆柱的半径");
                r2 = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("请输入圆柱的高");
                h = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("------------------------------");
                Console.WriteLine("圆的表面积为:" + Cylinder.square(r2,h));
                Console.WriteLine("------------------------------");
                Console.WriteLine("圆的体积为:" + Cylinder.cube(r2,h));
                Console.WriteLine("------------------------------");

            }
            catch(Exception e1)
            {
                Console.WriteLine(e1.Message);
            }
            Console.WriteLine("请按回车结束");
            Console.ReadLine();
        }
    }
}

运行结果:
在这里插入图片描述

总结:
分别做了3个类,一个是圆的,一个是圆柱的,最后是测试类,Circle中在构造方法中做了一个计数用用来实现定义了几个对象。采用了基础的算法算出了圆的面积和周长的以及圆柱的表面积和体积。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值