參考答案:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSD04
{
class Program
{
static void Main(string[] args)
{
// TODO: overall code must be implemented by candidate.
using (Equation e = new Equation(1,3,-6))
{
Console.WriteLine(e.Calculate());
}
Console.ReadLine();
}
}
class Equation :IDisposable
{
double a, b, c;
double s1, s2;
public Equation(double a,double b,double c)
{
this.a = a;
this.b = b;
this.c = c;
}
public string Calculate()
{
s1 = (-b + Math.Sqrt(Math.Pow(b, 2) - 4 * a * c)) / (2 * a);
s2 = (-b - Math.Sqrt(Math.Pow(b, 2) - 4 * a * c)) / (2 * a);
if ((s1.ToString()).Equals("非數值") || (s2.ToString()).Equals("非數值"))
return "This equation has one or more solution is invalid.";
else
return string.Format("S: {0:.00}, S: {1:.00}",s1,s2);
}
public void Dispose()
{
return;
}
}
}
執行結果:
留言列表