using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入第一个数");
double numb1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("请输入第二个数");
double numb2 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("请输入要进行的运算");
string oprationstr = Console.ReadLine();
double result = 0;
#region MyRegion
/*switch (oprationstr)
{
case "+":
result = numb1 + numb2;
break;
case "-":
result = numb1 - numb2;
break;
case "*":
result = numb1 * numb2;
break;
case "/":
result = numb1 / numb2;
break;
}*/
#endregion
//使用类
OprationLibrary.Opration op = new OprationLibrary.Opration();
result = op.GetResult(numb1, numb2, oprationstr);
/*OprationLibrary.OprationSqrt sqrt = new OprationLibrary.OprationSqrt();
result= sqrt.Sqrt(numb1, numb2, oprationstr);*/
OprationLibrary.OprationSin sin = new OprationLibrary.OprationSin();
result= sin.Sin(numb1, numb2, oprationstr);
Console.WriteLine(result);
Console.ReadKey();
}
}
}
OprationLibrary类库
Opration.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OprationLibrary
{
public class Opration
{
public double GetResult(double numb1, double numb2, string oprationstr)
{
double result = 0;
switch (oprationstr)
{
case "+":
result = numb1 + numb2;
break;
case "-":
result = numb1 - numb2;
break;
case "*":
result = numb1 * numb2;
break;
case "/":
result = numb1 / numb2;
break;
}
return result;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OprationLibrary
{
public class OprationSin:OprationSqrt
{
public double Sin(double numb1, double numb2, string oprationstr)
{
if (oprationstr == "sin")
{
return Math.Sin(numb1);
}
else
{
OprationSqrt op = new OprationSqrt();
return op.GetResult(numb1, numb2, oprationstr);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OprationLibrary
{
public class OprationSqrt:Opration
{
public double Sqrt(double numb1, double numb2, string oprationstr)
{
if (oprationstr == "sqrt")
{
return Math.Sqrt(numb1);
}
else
{
return GetResult(numb1, numb2, oprationstr);
}
}
}
}
WindowsFormsApplication1:
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
double numb1 = Convert.ToDouble(this.textBox1.Text);
double numb2 = Convert.ToDouble(this.textBox3.Text);
string oprationstr = this.textBox2.Text;
double result = 0;
#region MyRegion
/*switch (oprationstr)
{
case "+":
result = numb1 + numb2;
break;
case "-":
result = numb1 - numb2;
break;
case "*":
result = numb1 * numb2;
break;
case "/":
result = numb1 / numb2;
break;
}*/
#endregion
OprationLibrary.Opration op = new OprationLibrary.Opration();
result= op.GetResult(numb1, numb2, oprationstr);
MessageBox.Show(result.ToString());
}
}
}