、设计一个控制台应用程序,定义一个MyPoint类,该类能表示二维平面空间的点,完成点类及运算符重载等相关功能。具体要求如下:
()MyPoint类中定义2个私有字段x和y及相应的构造函数。
()MyPoint类中重载ToString方法输出坐标x和y的值。
()MyPoint类中重载一元运算符“-”。
()MyPoint类中重载二元运算符“+”和“-”。
()MyPoint类中重载二元运算符“>”和“<”。
()MyPoint类中重载二元运算符“==”和“!=”。
()在Main方法中测试上述运算符。
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Myproject
{
class Program
{
static void Main(string[] args)
{
Mypoint a = new Mypoint(, );
Mypoint b = new Mypoint(, );
Mypoint c = new Mypoint(, );
Console.WriteLine( "Check: a+b " + (a + b) );
Console.WriteLine( "Check: a-b " + (a - b) ); Console.WriteLine( "Check: < , > , != , == " );
if (a < c) Console.WriteLine("a < c ");
if( c > a) Console.WriteLine("c > a" ); if( a + b != a)
{
Console.WriteLine("a + b != a");
} if ( a + b == c)
{
Console.WriteLine("a + b == c");
}
}
}
public class Mypoint
{
int x, y; public Mypoint(int x, int y)
{
this.x = x;
this.y = y;
}
public Mypoint() : this(, ) { }
public int X { get { return x; } set { x = value; } }
public int Y { get { return y; } set { y = value; } }
public override string ToString()
{
return string.Format("({0},{1})", x, y);
} public static Mypoint operator - (Mypoint u)
{
Mypoint p = new Mypoint();
p.X = -u.X;
p.Y = -u.Y;
return p;
} public static Mypoint operator + (Mypoint u, Mypoint v)
{
Mypoint p = new Mypoint();
p.X = u.X + v.X;
p.Y = u.Y + v.Y;
return p;
} public static Mypoint operator -(Mypoint u, Mypoint v)
{
Mypoint p = new Mypoint();
p.X = u.X - v.X;
p.Y = u.Y - v.Y;
return p;
} public static bool operator >(Mypoint u, Mypoint v)
{
bool status = false ;
if( u.X >= v.X && u.Y >= v.Y)
{
status = true;
}
return status;
} public static bool operator <(Mypoint u, Mypoint v)
{
bool status = false;
if (u.X <= v.X && u.Y <= v.Y)
{
status = true;
}
return status;
} public static bool operator ==(Mypoint u, Mypoint v)
{
bool status = false;
if (u.X == v.X && u.Y == v.Y)
{
status = true;
}
return status;
} public static bool operator !=(Mypoint u, Mypoint v)
{
bool status = false;
if (u.X != v.X || u.Y != v.Y)
{
status = true;
}
return status;
} } }

、设计一个控制台应用程序,定义一个Box类,该类表示一个立方体,完成该类运算符重载等相关功能。具体要求如下:
()Box类中定义3个私有字段(代表长、宽、高)及相应的构造函数
()MyPoint类中重载ToString方法输出长宽高的值。
()Box类中重载一元运算符“-”。
()Box类中重载二元运算符“+”和“-”。
()Box类中重载二元运算符“>”和“<”。
()Box类中重载二元运算符“==”和“!=”。
()在Main方法中测试上述运算符。
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Myproject
{
class Program
{
static void Main(string[] args)
{
Box b1 = new Box(, , );
Box b2 = new Box(, , );
Box b3 = new Box(, , ); Console.WriteLine("Check: b1 + b2 " + (b1 + b2));
Console.WriteLine("Check: -b1 " + b1);
Console.WriteLine("Check: > , < , == , != ");
if (b2 > b1) Console.WriteLine("b2 > b1");
if (b1 < b2) Console.WriteLine("b1 < b2"); if (b1 + b2 == b3) Console.WriteLine("b1 + b2 == b3 ");
if (b1 + b2 != b1) Console.WriteLine("b1 + b2 != b1 ");
}
}
class Box
{
private int length, width, height; public Box(int length, int width, int height)
{
this.length = length;
this.width = width;
this.height = height;
} public Box() : this(, , ) { }
public int Length { get { return length; } set { length = value; } }
public int Width { get { return width; } set { width = value; } }
public int Height { get { return height; } set { height = value; } } public override string ToString()
{
return string.Format("( {0} , {1} , {2} )",length,width,height);
}
public static Box operator +(Box rhs)
{
Box res = new Box();
res.height = - rhs.height;
res.length = - rhs.length;
res.width = - rhs.width;
return res;
} public static Box operator + (Box lhs , Box rhs)
{
Box res = new Box();
res.height = lhs.height + rhs.height;
res.length = lhs.length + rhs.length;
res.width = lhs.width + rhs.width;
return res;
} public static Box operator - (Box lhs, Box rhs)
{
Box res = new Box();
res.height = lhs.height - rhs.height;
res.length = lhs.length - rhs.length;
res.width = lhs.width - rhs.width;
return res;
} public static bool operator >(Box lhs, Box rhs)
{
bool status = false;
if (lhs.length > rhs.length &&
lhs.width > rhs.width &&
lhs.height > rhs.height)
{
status = true;
}
return status;
} public static bool operator <(Box lhs, Box rhs)
{
bool status = false;
if (lhs.length < rhs.length &&
lhs.width < rhs.width &&
lhs.height < rhs.height)
{
status = true;
}
return status;
} public static bool operator == (Box lhs, Box rhs)
{
bool status = false;
if (lhs.length == rhs.length &&
lhs.width == rhs.width &&
lhs.height == rhs.height)
{
status = true;
}
return status;
} public static bool operator !=(Box lhs, Box rhs)
{
bool status = false;
if (lhs.length != rhs.length ||
lhs.width != rhs.width ||
lhs.height != rhs.height)
{
status = true;
}
return status;
}
}
}

【C#】上机实验五的更多相关文章

  1. SDN第五次上机实验

    1.浏览RYU官网学习RYU控制器的安装和RYU开发入门教程,提交你对于教程代码的理解. 1.通过源码安装RYU控制器 sudo apt-get install python3-pip git clo ...

  2. 20145210 20145226 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验

    20145210 20145226 <信息安全系统设计基础>实验五 简单嵌入式WEB服务器实验 结对伙伴:20145226 夏艺华 实验报告封面 实验目的与要求 · 掌握在ARM开发板实现 ...

  3. 实验五 CC2530平台上ADC组件的TinyOS编程

    实验五 CC2530平台上ADC组件的TinyOS编程 实验目的: 加深和巩固学生对于TinyOS编程方法的理解和掌握 让学生初步掌握传感器的ADC组件应用方法 学生通过本实验能够初步的了解和掌握CC ...

  4. oracle上机实验内容

    这是oracle实验的部分代码,我花了一中午做的. 第一次上机内容 实验目的:熟悉ORACLE11G的环境 实验内容: 第二次上机内容 实验目标:掌握oracle体系结构,掌握sqlplus的运行环境 ...

  5. SDN上机第五次作业

    2019 SDN上机第五次作业 1.浏览RYU官网学习RYU控制器的安装和RYU开发入门教程,提交你对于教程代码的理解,包括但不限于: 1.1描述官方教程实现了一个什么样的交换机功能? 答:官方教程实 ...

  6. 20145215&20145307《信息安全系统设计基础》实验五 网络通信

    小组成员:20145215卢肖明.20145307陈俊达 实验报告链接:信息安全系统设计基础--实验五实验报告

  7. 20145216 20145330 《信息安全系统设计基础》 实验五 简单嵌入式WEB 服务器实验

    20145216 20145330 <信息安全系统设计基础> 实验五 简单嵌入式WEB 服务器实验 实验报告封面 实验步骤 1.阅读理解源码 进入/arm2410cl/exp/basic/ ...

  8. 20145208《信息安全系统设计基础》实验五 简单嵌入式WEB 服务器实验

    20145208<信息安全系统设计基础>实验五 简单嵌入式WEB 服务器实验 20145208<信息安全系统设计基础>实验五 简单嵌入式WEB 服务器实验

  9. 20145315&20145307《信息安全系统设计基础》实验五

    20145315&20145307<信息安全系统设计基础>实验五 北京电子科技学院(BESTI) 实 验 报 告 课程:信息安全系统设计基础 班级:1453 1452 姓名:陈俊达 ...

随机推荐

  1. Redis的订阅、事务、持久化

    1.Redius的订阅: 运用关键字subscribe订阅: 关键字publish发布: 发布后,订阅的页面才会出现发布的内容. 2.Redis事务: Redis事务与mysql的事务不同,mysql ...

  2. [RN] React Native 使用 react-navigation 报错 "Unable to resolve module `react-native-gesture-handler`

    在React Native 使用 react-navigation 过程中,报错 "Unable to resolve module `react-native-gesture-handle ...

  3. Kafka 消费者到底是什么 以及消费者位移主题到底是什么(Python 客户端 1.01 broker)

    Kafka 中有这样一个概念消费者组,所有我们去订阅 topic 和 topic 交互的一些操作我们都是通过消费者组去交互的. 在 consumer 端设置了消费者的名字之后,该客户端可以对多个 to ...

  4. mysql locate()函数

    mysql> select * from test; +----+------------+-------+-----------+ | id | name | score | subject ...

  5. React_02_ECMAScript6

    1.let与const ES2015(ES6) 新增加了两个重要的 JavaScript 关键字: let 和 const. let 声明的变量只在 let 命令所在的代码块内有效,const 声明一 ...

  6. intellij ide调用一个对象所有的set方法

    1.下载地址:https://github.com/yoke233/genSets/releases/download/1.1/genSets.jar 2.plugin 从本地磁盘安装找到jar,并重 ...

  7. [web 前端] Npm package.json与package-lock.json文件的作用

    本文链接:https://blog.csdn.net/u013992330/article/details/81110018 最新版nodejs中,多了一个package-lock.json文件,刚开 ...

  8. flask 开发接口测试平台

    flask 开发接口测试平台 数据库,forms  views  视图, 数据库如下: # encoding: utf-8 ''' @author: lileilei @file: models.py ...

  9. 30段极简Python代码

    Python 是机器学习最广泛采用的编程语言,它最重要的优势在于编程的易用性.如果读者对基本的 Python 语法已经有一些了解,那么这篇文章可能会给你一些启发.作者简单概览了 30 段代码,它们都是 ...

  10. Swift编码总结4

    1.swift @discardableResult 声明: swift正常的方法如果有返回值的话,调用的时候必须有一个接收方,否则的话编译器会报一个警告,如果在方法前加上 @discardableR ...