通大c#第四次作业
Question
编写程序完成以下内容
1.定义自定义异常类ABCExption,重写起Message属性的get方法,使其返回值为字符串“方程没有实根!”
2.定义一元二次方程类ABC,为其定义三个私有的整形字段a、b、c表示一元二次方程的洗漱,一个私有实型字段delta用于判断方程是否有实根。定义一个构造函数,对三个私有字段进行初始化并计算delta的值。定义一个共有方法GetRoot()判断delta的值,若delta<0,那么就抛出ABCExption异常,否则分别输出该一元二次方程的根。
3.在Main()方法中从键盘输入一元二次方程的三个系数,创建医院二次方程类ABC的实例对象,调用该对象的GetRoot()方法,输出该医院二次方程的根并对程序中可能抛出的异常进行全面的处理。
Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 习题7_03 { class ABCException : Exception { public override string Message { get { return "方程没有实根!"; } } } class ABC { private int a, b, c; private double delta; public ABC(int a, int b, int c) { this.a = a; this.b = b; this.c = c; delta = b * b - 4 * a * c; } public void GetRoot()//求方程的根 { double x1, x2; if (delta < 0) { throw new ABCException(); } else if (delta == 0) { x1 = x2 = -b / (2.0 * a); Console.WriteLine("方程有两个相等的实根:x1=x2={0}", x1); } else { x1 = (-b + delta) / (2.0 * a); x2 = (-b - delta) / (2.0 * a); Console.WriteLine("方程有两不等的实根:x1={0} x2={1}", x1,x2); } } } class Program { static void Main(string[] args) { try { Console.Write("请输入二元一次方程的系数a:"); int a = int.Parse(Console.ReadLine()); Console.Write("请输入二元一次方程的系数b:"); int b = int.Parse(Console.ReadLine()); Console.Write("请输入二元一次方程的系数c:"); int c = int.Parse(Console.ReadLine()); ABC t = new ABC(a, b, c); t.GetRoot(); } catch (ABCException e) { Console.WriteLine(e.Message); } catch (Exception e) { Console.WriteLine(e.Message); } } } }
Question
编写程序完成以下内容
1.定义股票类Stock,为其定义两个事件PriceRiseEvent和PriceDownEvent,分别表示股票价格上涨和股票价格下跌。这两个事件都具有一个参数,表示价格上涨或下跌的钱数。定义两个自动生成的属性StockNameheStockPrice,分别表示股票名字和股票价格,并在构造函数中对其初始化。定义两个表示股票价格上升或者下降的方法PriceRise和PriseDown,在这两个方法中分别打印输出类股票上升或者下降的百分比,并分别处罚表示价格上升的的PriseRiseEvent和表示价格下降的PriseDownEvent事件,最后分别修改上升或下降后的股票加个
2.定义股票买家类Buyer,为其定义一个私有的Stock类型字段stock,表示卖家要抛售的股票,定义一个构造函数对该私有字段初始化,定义方法public void StockPriceRise,在方法中判断股票价格上升率,若上升率多余5%则打印输出“股票上涨已经超过5%,买家谨慎购买”,否则则输出“股票正小幅上涨,买家可以买入”。定义方法public。。。。等等等
懒得打了
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 习题7_04
{
class Stock
{
public delegate void PriceChange(double price);
public event PriceChange PriceRiseEvent;
public event PriceChange PriceDownEvent;
public string StockName { get; set; }
public double StockPrice { get; set; }
public Stock(string stockName, double price)
{
StockName=stockName;
StockPrice = price;
}
public void PriceRise(double up)
{
Console.WriteLine ("股票价格升高了{0:##.00}%",up*100.0/StockPrice);
PriceRiseEvent(up);
StockPrice += up; }
public void PriceDown(double down)
{
Console.WriteLine("股票价格降低了{0:##.00}%", down * 100.0 / StockPrice);
PriceDownEvent(down);
StockPrice -= down;
}
}
}
//对应第一小问
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 习题7_04
{
class Buyer
{
private Stock stock;
public void StockPriceRise(double up)
{
if (up / stock.StockPrice >= 0.05)
{
Console.WriteLine("股票上涨已经超过5%,买家请谨慎买入");
}
else
{
Console.WriteLine("股票正小幅上涨,买家可以买入");
}
}
public void StockPriceDown(double down)
{
if (down / stock.StockPrice >= 0.05)
{
Console.WriteLine("股票下跌已经超过5%,买家可以逢低买入");
}
else
{
Console.WriteLine("股票正小幅下跌,买家暂时请观望");
}
}
public Buyer(Stock stock)
{
this.stock = stock;
}
}
}
//对应第二小问
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 习题7_04
{
class Seller
{
private Stock stock;
public void StockPriceRise(double up)
{
if (up / stock.StockPrice >= 0.05)
{
Console.WriteLine("股票上涨已经超过5%,卖家请赶快抛售");
}
else
{
Console.WriteLine("股票正小幅上涨,卖家请继续观望");
}
}
public void StockPriceDown(double down)
{
if (down / stock.StockPrice >= 0.05)
{
Console.WriteLine("股票下跌已经超过5%,卖家请逢低补仓");
}
else
{
Console.WriteLine("股票正小幅下跌,卖家请谨慎操作");
}
}
public Seller(Stock stock)
{
this.stock = stock;
}
}
}
//对应第三小
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 习题7_04
{
class Program
{
static void Main(string[] args)
{
Stock stock = new Stock("中国银行", 10.3);
Buyer buyer = new Buyer(stock);
Seller seller = new Seller(stock);
stock.PriceDownEvent += buyer.StockPriceDown;
stock.PriceDownEvent += seller.StockPriceDown;
stock.PriceRiseEvent += buyer.StockPriceRise;
stock.PriceRiseEvent += seller.StockPriceRise;
stock.PriceRise(0.8);
stock.PriceDown(1.2);
}
}
}
//对应第四小问
Writer
通大咸鱼