Сделать перегрузку любого метода - C#
Формулировка задачи:
необходимо сделать перегрузку любого метода
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Foo = System.Func<float, float>;
class Program
{
const float Eps = 1e-5f;
static float Solve(Foo f, float a, float b, float eps = Eps, int count = 0)
{
float fa = f(a), fb = f(b), c, fc;
if (fa * fb > 0) throw new ArgumentException();
do
{
c = (a + b) / 2; fc = f(c);
if (fc * fa < 0) { b = c; fb = fc; }
else { a = c; fa = fc; }
} while (Math.Abs(fc) > eps && --count != 0);
return c;
}
static float SolveR(Foo f, float x, float dx, float eps = Eps)
{
return Math.Abs(f(x)) < eps ? x :
SolveR(f, f(x + dx / 2) * f(x) < 0 ? x : x + dx / 2, dx / 2, eps);
}
static void Main(string[] args)
{
// x^4 + 3x^3 + 3x^2 - x - 6 = 0
float[] a = { 1, 3, 3, -1, -6 };
Foo f = x => a.Aggregate((s, q) => s * x + q);
Console.WriteLine("Korni:");
Console.WriteLine("X1 = {0:0.00}", Solve(f, 0, 10));
Console.WriteLine("X2 = {0:0.00}", Solve(f, -10, 0));
Console.ReadLine();
}
}Решение задачи: «Сделать перегрузку любого метода»
textual
Листинг программы
public int Calculate(int a, int b)
{
return a + b;
}
public int Calculate(params int[] input)
{
if(input.Length < 2)
throw new ArgumentException("Мало параметров xD");
int res = 0;
foreach(var item in input)
res += item;
return intem;
}