接口实例(C#,IShape)【C#】

接口实例(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;
        }
    }
    class Rectangle : IShape
    {
            /
            
            //请填写代码,实现输出矩形的面积和周长

            /
        
    }

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

输入

输入矩形长、高,如
10
3
 

输出

area=30,Perimeter=26

样例输入

10

3

样例输出

area=30,Perimeter=26

提示

需要考虑输入非数字、负数等

        private double w, h;public Rectangle(double w,double h){this.w = w;this.h = h;}public double Perimeter(){if (w <= 0 || h <= 0) return 0;return 2.0 * w + 2.0 * h;}public double Area(){if (w <= 0 || h <= 0) return 0;return w * h;}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/536229.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

c#补充print(多态性问题)【C#】

c#补充print&#xff08;多态性问题&#xff09; 题目描述 根据给出代码&#xff0c;补写缺失代码&#xff0c;当print函数内为整数的时候&#xff0c;输出整数的三次方&#xff0c;为浮点数&#xff0c;输出其二次方&#xff0c;为字符串时&#xff0c;直接输出。 using Sys…

1439: 2.4.5 Fractions to Decimals 分数化小数

1439: 2.4.5 Fractions to Decimals 分数化小数 时间限制: 1 Sec 内存限制: 64 MB提交: 194 解决: 13题目描述 写一个程序&#xff0c;输入一个形如N/D的分数(N是分子&#xff0c;D是分母)&#xff0c;输出它的小数形式。 如果小数有循环节的话&#xff0c;把循环节放在一对圆…

Problem B: 求各位数字之和

#include <stdio.h> #include <stdlib.h> int main() { int n,sum0,m; while(~scanf("%d",&n)) { while(n>0) { mn%10; nn/10; summ; } printf("%d\n",sum); sum0; } return 0; }

Problem C: 判断字符串是否为回文

#include <stdio.h> #include <stdlib.h> int main() { int i,j,n; char str[10]; gets(str); nstrlen(str); for(i0,jn-1;i<j;i,j--) { if(str[i]!str[j]) { printf("No\n"); break; } } if(i>j)printf("Yes\n"); return 0; }

Problem A: 童年生活二三事

斐波那契数列:F(n)F(n-1)F(n-2) #include <stdio.h> #include <stdlib.h> int f(int n) {int b;if(n1)b1;if(n2)b2;if(n>2)bf(n-1)f(n-2);return b; }int main() {int a,n;while(~scanf("%d",&n)&&n!0){af(n);printf("%d\n",a…

Problem C: 01字串

#include <stdio.h> #include <stdlib.h>int main() {int i,j,n0,m0;char a[129][8];for(i0;i<128;i){for(j0;j<7;j){a[i][j]n%2;nn/2;}m;nm;}for(i0;i<128;i){for(j6;j>0;j--)printf("%d",a[i][j]);printf("\n");}return 0; }

汉诺塔III

#include <stdio.h> #include <stdlib.h> int main() {int fx(int );int n,m;while(~scanf("%d",&n)){mfx(n);printf("%d\n",m);}return 0; } int fx(int n) {int m;if(n1)m2;if(n>1)m3*fx(n-1)2;return m; }

Problem B: C语言习题 学生成绩输入和输出

Problem B: C语言习题 学生成绩输入和输出 Description 编写一个函数print&#xff0c;打印一个学生的成绩数组&#xff0c;该数组中有5个学生的数据&#xff0c;每个学生的数据包括num(学号)、name(姓名)、score3。编写一个函数input&#xff0c;用来输入5个学生的数据。 I…

Problem E:结构体---点坐标结构体

Problem E: 结构体—点坐标结构体 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 585 Solved: 395 [Submit][Status][Web Board] Description 定义一个表示点坐标的结构体&#xff0c;输入两个点的坐标&#xff0c;输出这两个点中点的坐标 Input 第一个点的坐标&…

Problem F: 结构体--学生信息排序

Problem F: 结构体–学生信息排序 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 378 Solved: 192 [Submit][Status][Web Board] Description 定义存放一个学生信息的结构体类型&#xff0c;学生信息包括&#xff1a;姓名&#xff0c;学号&#xff0c;性别&#xff0c;…

Problem D: 分数减法——结构体

Problem D: 分数减法——结构体 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 604 Solved: 462 [Submit][Status][Web Board] Description 分数可以看成是由字符’/’分割两个整数构成&#xff0c;可以用结构体类型表示。请用结构体类型变量计算两个分数的差。 注意&…

Problem G: C语言习题 医生值班

Problem G: C语言习题 医生值班 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 847 Solved: 102[Submit][Status][Web Board]Description 医院内科有A,B,C,D,E,F,G,H共七位医生&#xff0c;每人在一周内要值一次夜班&#xff0c;排班的要求是&#xff1a; &#xff08;1&a…

Problem A: 删除区间内的元素(线性表)

Problem A: 删除区间内的元素&#xff08;线性表&#xff09; Time Limit: 1 Sec Memory Limit: 2 MBSubmit: 75 Solved: 24[Submit][Status][Web Board]Description 若一个线性表L采用顺序存储结构&#xff0c;其中元素都为整数。设计一个算法&#xff0c;删除元素值在[x,y…

问题 B: 调整表中元素顺序(线性表)

问题 B: 调整表中元素顺序&#xff08;线性表&#xff09; 时间限制: 1 Sec 内存限制: 2 MB提交: 28 解决: 11[提交][状态][讨论版]题目描述 若一个线性表L采用顺序存储结构存储&#xff0c;其中所有元素都为整数。设计一个算法&#xff0c;将所有小于0的元素移到所有大于0的…

Problem C: 顺序表基本运算(线性表)

问题 C: 顺序表基本运算&#xff08;线性表&#xff09; 时间限制: 1 Sec 内存限制: 128 MBDescription 编写一个程序&#xff0c;实现顺序表的各种基本运算&#xff08;假设顺序表的元素类型为char&#xff09;&#xff0c;主函数已给出&#xff0c;请补充每一种方法。 1、初…

Problem D: 链表的基本运算(线性表)

Problem D: 链表的基本运算&#xff08;线性表&#xff09; Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 18 Solved: 10Description 编写一个程序&#xff0c;实现链表的各种基本运算&#xff08;假设顺序表的元素类型为char&#xff09;&#xff0c;主函数已给出&#…

Problem B: 结构体---职工信息结构体

Problem B: 结构体---职工信息结构体 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 504 Solved: 306编写程序&#xff0c;定义一个存放职工信息的结构体类型&#xff0c;职工信息包括职工姓名、工作年限、工资总额。初始化5名职工的信息&#xff0c;对工作年限超过30年的…

Problem C: 结构体---点坐标结构体

Problem C: 结构体---点坐标结构体 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 663 Solved: 444[Submit][Status][Web Board]Description 定义一个表示点坐标的结构体&#xff0c;输入两个点的坐标&#xff0c;输出这两个点中点的坐标 Input 第一个点的坐标&#xff08…

Problem D: 编程题B-向量的数量积

Problem D: 编程题B-向量的数量积 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 164 Solved: 148Description 已知两个非零向量a&#xff08;x1&#xff0c;y1&#xff09;&#xff0c;b&#xff08;x2&#xff0c;y2&#xff09;&#xff0c;则有abx1x2y1y2&#xff0c;…

Problem E: 高于均分的学生

Problem E: 高于均分的学生 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 67 Solved: 45Description 输入n(<100)个学生的姓名、学号和成绩&#xff0c;将其中高于平均成绩的姓名、学号和成绩输出。 Input 第一行为学生人数 n 第二行到第n1行&#xff0c;每行一个学生…