public class calculation
{
protected int x,y;
public calculation()
{
this.x = 0;
this.y = 0;
}
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
public virtual double GetResult(int xx,int yy)
{
double Result = 0;
return Result;
}
} public class operationAdd :calculation
{
public override double GetResult(int xx,int yy)
{ return xx+yy; }
} public class operationSub : calculation
{
public override double GetResult(int xx, int yy)
{ return xx - yy; }
} public class operationMultiply : calculation
{
public override double GetResult(int xx, int yy)
{ return xx * yy; }
} public class operationDivide : calculation
{
public override double GetResult(int xx, int yy)
{
if (yy == 0)
{
throw new Exception() ;
}
else return (double)xx / yy;
}
} public class CreateInstance
{
public static calculation Create(string operation)
{
switch (operation)
{
case "+":
return new operationAdd();
case "-":
return new operationSub();
case "*":
return new operationMultiply();
case "/":
return new operationDivide();
default:
return null;
}
}
} class Program
{
static void Main(string[] args)//编写一个类cal1实现加减运算,
{ //再编写一个派生类cal2实现乘除运算
calculation myInstance;
Console.WriteLine("Please input two integers with comma as seperator:");
string s=Console.ReadLine();
int x=0;
int y=0;
if (s.Contains(","))
{
myInstance = CreateInstance.Create("+");
int index = s.IndexOf(",");
try
{
string sNumber = s.Substring(0, index);
x = int.Parse(sNumber);
sNumber = s.Substring(index + 1, s.Length - index - 1);
y = int.Parse(sNumber); myInstance.X = x;
myInstance.Y = y;
}
catch
{
throw new Exception();
}
Console.WriteLine("两数之和为:{0}", myInstance.GetResult(x, y).ToString()); myInstance = CreateInstance.Create("-");
myInstance.X = x;
myInstance.Y = y;
Console.WriteLine("两数之差为:{0}", myInstance.GetResult(x, y).ToString()); myInstance = CreateInstance.Create("*");
myInstance.X = x;
myInstance.Y = y;
Console.WriteLine("两数之积为:{0}", myInstance.GetResult(x, y).ToString()); myInstance = CreateInstance.Create("/");
myInstance.X = x;
myInstance.Y = y;
Console.WriteLine("两数之商为:{0}", myInstance.GetResult(x, y).ToString());
}
}
}《大话设计模式》真是本好书!谢谢上次回帖的朋友推荐!也明白了为什么上个帖有朋友说“这个设计实在是...”,哈哈!改良了的代码好像不太符合题目要求,不过已经比以前的代码像样多了:P欢迎大家批评指正!
{
protected int x,y;
public calculation()
{
this.x = 0;
this.y = 0;
}
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
public virtual double GetResult(int xx,int yy)
{
double Result = 0;
return Result;
}
} public class operationAdd :calculation
{
public override double GetResult(int xx,int yy)
{ return xx+yy; }
} public class operationSub : calculation
{
public override double GetResult(int xx, int yy)
{ return xx - yy; }
} public class operationMultiply : calculation
{
public override double GetResult(int xx, int yy)
{ return xx * yy; }
} public class operationDivide : calculation
{
public override double GetResult(int xx, int yy)
{
if (yy == 0)
{
throw new Exception() ;
}
else return (double)xx / yy;
}
} public class CreateInstance
{
public static calculation Create(string operation)
{
switch (operation)
{
case "+":
return new operationAdd();
case "-":
return new operationSub();
case "*":
return new operationMultiply();
case "/":
return new operationDivide();
default:
return null;
}
}
} class Program
{
static void Main(string[] args)//编写一个类cal1实现加减运算,
{ //再编写一个派生类cal2实现乘除运算
calculation myInstance;
Console.WriteLine("Please input two integers with comma as seperator:");
string s=Console.ReadLine();
int x=0;
int y=0;
if (s.Contains(","))
{
myInstance = CreateInstance.Create("+");
int index = s.IndexOf(",");
try
{
string sNumber = s.Substring(0, index);
x = int.Parse(sNumber);
sNumber = s.Substring(index + 1, s.Length - index - 1);
y = int.Parse(sNumber); myInstance.X = x;
myInstance.Y = y;
}
catch
{
throw new Exception();
}
Console.WriteLine("两数之和为:{0}", myInstance.GetResult(x, y).ToString()); myInstance = CreateInstance.Create("-");
myInstance.X = x;
myInstance.Y = y;
Console.WriteLine("两数之差为:{0}", myInstance.GetResult(x, y).ToString()); myInstance = CreateInstance.Create("*");
myInstance.X = x;
myInstance.Y = y;
Console.WriteLine("两数之积为:{0}", myInstance.GetResult(x, y).ToString()); myInstance = CreateInstance.Create("/");
myInstance.X = x;
myInstance.Y = y;
Console.WriteLine("两数之商为:{0}", myInstance.GetResult(x, y).ToString());
}
}
}《大话设计模式》真是本好书!谢谢上次回帖的朋友推荐!也明白了为什么上个帖有朋友说“这个设计实在是...”,哈哈!改良了的代码好像不太符合题目要求,不过已经比以前的代码像样多了:P欢迎大家批评指正!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货