OJ問題3485インターフェイスインスタンス(C#、IShape)

タイトル説明

インターフェイスインスタンス。次の図に、インターフェイスとクラスを示します。指定されたコードに従って、不足しているコードを入力し、Programクラスの静的Mainメソッドで実装されたクラスを確認します。 
 
using System;
namespace Myinterface
{     public interface IShape     {         double Perimeter();         double Area();     }     class Circle:IShape     {         public double Radius {get; set;}         public Circle(double r)         {             Radius = r;         }         public double Area ()         {             return Math.PI * Radius * Radius;         }         public double Perimeter()         {             return 2 * Math.PI * Radius;         }




















    }
    クラス矩形:IShape
    {             /                     //出力矩形の面積と周囲長を実現するためのコードを記入してください              /     }     クラスプログラム     {         静的な無効メイン(文字列[] args)         {             ダブルW、Hと、             double.TryParse(コンソール。 ReadLine()、out w);             double.TryParse(Console.ReadLine()、out h);             Rectangle r = new Rectangle(w、h);             Console.WriteLine( "area = {0}、Perimeter = {1}" 、r。Area()、r.Perimeter());         }     }















入る

など、長方形の長さと高さを入力し 
10 
3

出力

面積= 30、周囲長= 26

サンプル入力

10 
3

サンプル出力

面積= 30、周囲長= 26

促す

数字以外、負の数などの入力を検討する必要があります。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;
namespace ConsoleApplication1
{
    public interface IShape
    {
        double Perimeter();
        double Area();
    }
    class Circle : IShape
    {
        public double Radius { get; set; }
        public Circle(double r)
        {
            Radius = r;
        }
        public double Area()
        {
            return Math.PI * Radius * Radius;
        }
        public double Perimeter()
        {
            return 2 * Math.PI * Radius;
        }
    }
    class Rectangle : IShape
    {
        public double Height { get; set; }
        public double Length { get; set; }
        public Rectangle(double l,double h)
        {
            Height = h;
            Length = l;
        }
        public double Area()
        {
            if (Height <= 0 || Length <= 0)
            {
                return 0;
            }
            return Height * Length;
        }
        public double Perimeter()
        {
            if (Height <= 0 || Length <= 0)
            {
                return 0;
            }
            return (Height + Length) * 2;
        }
        
    }
    class Program
    {
        static void Main(string[] args)
        {
            double w, h;
            double.TryParse(Console.ReadLine(), out w);
            double.TryParse(Console.ReadLine(), out h);
            Rectangle r = new Rectangle(w, h);
            Console.WriteLine("area={0},Perimeter={1}", r.Area(), r.Perimeter());
        }
    }
} 

 

おすすめ

転載: blog.csdn.net/wangws_sb/article/details/105114712