C#上机习题

在这里插入图片描述

using System;
namespace test
{
    class Program
    {
        public static int jiecheng(int n)
        {
            int fac= 1;
            for (int i = n; i >= 1; i--)
            {
                fac *= i;
            }
            return fac;
        }
            static void Main(string[] args)
        {
            double sum= 1;
            for (int i=1;i<=10; i++)
            {
                sum=sum+(double)(1.0/jiecheng(i))+i*i;
                Console.WriteLine(sum);
            }
            Console.ReadLine();
        }
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

using System;
namespace test
{
    class Program
    {
            static void Main(string[] args)
        {
            string filename = @"hello world.txt";
            int indexDot = filename.LastIndexOf('.');
            string extendName = "dat";
            filename = filename.Substring(0, indexDot+1);
            String[] splitStrings = filename.Split(' ');//Split返回值是字符串数组
            filename = string.Join("_", splitStrings);//返回值是字符串
            filename = filename + extendName;
            Console.WriteLine(filename);
            Console.ReadLine();
        }
    }
}

在这里插入图片描述

在这里插入图片描述
抽象类的继承
在这里插入图片描述
在这里插入图片描述

using System;
namespace test
{
    public abstract class Father
    {
        public abstract double Surfacearea();
        public abstract double Volume();
    }

    public class Ball : Father
    {
        public double r;
        public Ball(double r)
        {
            this.r = r;
        }
        public override double Surfacearea()
        {
            return 4 * Math.PI * r * r;
        }

        public override double Volume()
        {
            return 4.0/3.0 * Math.PI * r * r*r;
        }
    }
    public class Cylinder : Father
    {
        public double r;
        public double h;
        public Cylinder(double r, double h)
        {
            this.r = r;
            this.h = h;
        }
        public override double Surfacearea()
        {
            return 2 * Math.PI * r * (r+h);
        }

        public override double Volume()
        {
            return Math.PI * r * r * h;
        }
    }

    public class Cone : Father
    {
        public double r;
        public double h;
        public Cone(double r, double h)
        {
            this.r = r;
            this.h = h;
        }
        public override double Surfacearea()
        {
            return  Math.PI * r * (r + Math.Sqrt(h*h+r*r));
        }

        public override double Volume()
        {
            return (1.0/3.0)*Math.PI * r * r * h;
        }
    }

    class Program
    {
            static void Main(string[] args)
        {
            Ball ball = new Ball(10.0);
            Console.WriteLine("半径为{0}的球表面积是{1},体积是{2}",ball.r,ball.Surfacearea(),ball.Volume());
            Cylinder cylinder = new Cylinder(10.0,10);
            Console.WriteLine("半径为{0},高为{1}的圆柱表面积是{2},体积是{3}", cylinder.r, cylinder.h, cylinder.Surfacearea(), cylinder.Volume());
            Cone cone = new Cone(10.0, 10);
            Console.WriteLine("半径为{0},高为{1}的圆锥表面积是{2},体积是{3}", cone.r, cone.h, cone.Surfacearea(), cone.Volume());
            Console.ReadLine();
        }
    }
}

在这里插入图片描述
类的继承
在这里插入图片描述

using System;
namespace test
{
    public class Ball
    {
        public double r;

        public Ball()//派生类调用构造函数时,会先调用基类的构造函数。默认调用没有参数的构造函数
        {

        }
        public Ball(double r)
        {
            this.r = r;
        }
        public double Surfacearea()
        {
            return 4 * Math.PI * r * r;
        }

        public double Volume()
        {
            return 4.0/3.0 * Math.PI * r * r*r;
        }
    }
    public class Cylinder :Ball
    {
        public new double r;
        public double h;
        public  Cylinder(double r, double h)
        {
            this.r = r;
            this.h = h;
        }
        public new double Surfacearea()
        {
            return 2 * Math.PI * r * (r+h);
        }

        public new  double Volume()
        {
            return Math.PI * r * r * h;
        }
    }

    public class Cone : Ball
    {
        public new double r;
        public double h;
        public Cone(double r, double h)
        {
            this.r = r;
            this.h = h;
        }
        public new  double Surfacearea()
        {
            return  Math.PI * r * (r + Math.Sqrt(h*h+r*r));
        }

        public new double Volume()
        {
            return (1.0/3.0)*Math.PI * r * r * h;
        }
    }

    class Program
    {
            static void Main(string[] args)
        {
            Ball ball = new Ball(10.0);
            Console.WriteLine("半径为{0}的球表面积是{1},体积是{2}",ball.r,ball.Surfacearea(),ball.Volume());
            Cylinder cylinder = new Cylinder(10.0,10);
            Console.WriteLine("半径为{0},高为{1}的圆柱表面积是{2},体积是{3}", cylinder.r, cylinder.h, cylinder.Surfacearea(), cylinder.Volume());
            Cone cone = new Cone(10.0, 10);
            Console.WriteLine("半径为{0},高为{1}的圆锥表面积是{2},体积是{3}", cone.r, cone.h, cone.Surfacearea(), cone.Volume());
            Console.ReadLine();
        }
    }
}

在这里插入图片描述

接口继承
在这里插入图片描述
在这里插入图片描述

using System;
namespace test
{
    public interface Father
    {
        double Surfacearea(); //接口成员访问权限为public,但不能加访问修饰符 接口成员不能有定义
        double Volume();
    }

    public class Ball : Father
    {
        public double r;
        public Ball(double r)
        {
            this.r = r;
        }
        public double Surfacearea()
        {
            return 4 * Math.PI * r * r;
        }

        public double Volume()
        {
            return 4.0 / 3.0 * Math.PI * r * r * r;
        }
    }
    public class Cylinder : Father
    {
        public double r;
        public double h;
        public Cylinder(double r, double h)
        {
            this.r = r;
            this.h = h;
        }
        public double Surfacearea()
        {
            return 2 * Math.PI * r * (r + h);
        }

        public double Volume()
        {
            return Math.PI * r * r * h;
        }
    }

    public class Cone : Father
    {
        public double r;
        public double h;
        public Cone(double r, double h)
        {
            this.r = r;
            this.h = h;
        }
        public double Surfacearea()//类中接口实现必须显示声明为public
        {
            return Math.PI * r * (r + Math.Sqrt(h * h + r * r));
        }

        public  double Volume()
        {
            return (1.0 / 3.0) * Math.PI * r * r * h;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Ball ball = new Ball(10.0);
            Console.WriteLine("半径为{0}的球表面积是{1},体积是{2}", ball.r, ball.Surfacearea(), ball.Volume());
            Cylinder cylinder = new Cylinder(10.0, 10);
            Console.WriteLine("半径为{0},高为{1}的圆柱表面积是{2},体积是{3}", cylinder.r, cylinder.h, cylinder.Surfacearea(), cylinder.Volume());
            Cone cone = new Cone(10.0, 10);
            Console.WriteLine("半径为{0},高为{1}的圆锥表面积是{2},体积是{3}", cone.r, cone.h, cone.Surfacearea(), cone.Volume());
            Console.ReadLine();
        }
    }
}

虚函数
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

using System;
namespace test
{
    public class Ball
    {
        public double r;

        public Ball()//派生类调用构造函数时,会先调用基类的构造函数。默认调用没有参数的构造函数
        {

        }
        public Ball(double r)
        {
            this.r = r;
        }
        public virtual double Surfacearea()
        {
            return 4 * Math.PI * r * r;
        }

        public virtual double Volume()
        {
            return 4.0 / 3.0 * Math.PI * r * r * r;
        }
    }
    public class Cylinder : Ball
    {
        public new double r;
        public double h;
        public Cylinder(double r, double h)
        {
            this.r = r;
            this.h = h;
        }
        public new double Surfacearea()
        {
            return 2 * Math.PI * r * (r + h);
        }

        public new double Volume()
        {
            return Math.PI * r * r * h;
        }
    }

    public class Cone : Ball
    {
        public new double r;
        public double h;
        public Cone(double r, double h)
        {
            this.r = r;
            this.h = h;
        }
        public new double Surfacearea()
        {
            return Math.PI * r * (r + Math.Sqrt(h * h + r * r));
        }

        public new double Volume()
        {
            return (1.0 / 3.0) * Math.PI * r * r * h;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Ball ball = new Ball(10.0);
            Console.WriteLine("半径为{0}的球表面积是{1},体积是{2}", ball.r, ball.Surfacearea(), ball.Volume());
            Cylinder cylinder = new Cylinder(10.0, 10);
            Console.WriteLine("半径为{0},高为{1}的圆柱表面积是{2},体积是{3}", cylinder.r, cylinder.h, cylinder.Surfacearea(), cylinder.Volume());
            Cone cone = new Cone(10.0, 10);
            Console.WriteLine("半径为{0},高为{1}的圆锥表面积是{2},体积是{3}", cone.r, cone.h, cone.Surfacearea(), cone.Volume());
            Console.ReadLine();
        }
    }
}
using System;
namespace test
{
    public class NoDescException : ApplicationException
    {
        public NoDescException() { }
        public NoDescException(string message) : base(message) { }
        public NoDescException(string message, Exception ex) : base(message, ex) { }
    }

    public interface IFun1
    { 
        string ShowMe();
    }

    public interface IFun2
    { 
        string ShowMe();
    }

    class Circle : IFun1
    {
        public string ShowMe()
        { 
            return "Circle-IFun1"; 
        }
    }

    public class ObjShowMe
    {
        public static void ShowMe(object obj)
        {
            try
            {
                if (!(obj is IFun1 && obj is IFun2))
                throw new NoDescException("Interface not implemented for " + obj.ToString());
            }
            catch (NoDescException ex)
            {
                Console.WriteLine("ex.Message: {0}", ex.Message);
                Console.WriteLine("ex.Source: {0}", ex.Source);
                Console.WriteLine("ex.TargetSite: {0}", ex.TargetSite.ToString());
                Console.WriteLine("ex.StackTrace: {0}", ex.StackTrace);
            }
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            Circle myCir = new Circle();
            ObjShowMe.ShowMe(myCir);
            Console.ReadLine();
        }
    }

}

第六题
在这里插入图片描述

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;

namespace hello
{
    class Program
    {
        public class Chair
        {
            public double myPrice;
            public string myVendor, myID;
            public Chair() { }
            public Chair(double price, string vendor, string sku)
            {
                myPrice = price;
                myVendor = vendor;
                myID = sku;
            }
        }

        public class MyCompareClass : IComparer
        {
            public int Compare(Object x, Object y)
            {
                if (x is Chair && y is Chair)
                {
                    Chair castObjX = (Chair)x;
                    Chair castObjY = (Chair)y;
                    int result = string.CompareOrdinal(castObjX.myID, castObjY.myID);
                    if (result>0) return 1;
                    else if (result<0) return -1;
                    else return 0;
                }
                throw new ArgumentException("object is not a chair");
            }
        }

        static void Main(string[] args)
        {
            Chair[] chairs = new Chair[4];
            chairs[0] = new Chair(150.0, "Lane", "99-88");
            chairs[1] = new Chair(250.0, "Lane", "99-00");
            chairs[2] = new Chair(100.0, "Lane", "98-88");
            chairs[3] = new Chair(120.0, "Harris", "93-9");
            Array.Sort(chairs, new MyCompareClass());
            foreach (Chair c in chairs)
            {
                Console.WriteLine(c.myPrice + " " + c.myVendor + " " + c.myID);
            }


            Console.ReadLine();
        }
    }
}

在这里插入图片描述

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值