Как перевести в процедуры и функции - C#
Формулировка задачи:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication32
{
class Program
{
static void Main(string[] args)
{
{
label1: Console.WriteLine("Имеют вид: \n(1) F = a * Math.Pow(x, 2) + b * x + c при a < 0 и c != 0; \n(2) F = -a / (x - c) при a > 0 и c = 0; \n(3) F = a * (x + c) в остальных случаях.");
Console.WriteLine("Введите следующие значения, для подтвер-ждения результата нажмите Enter");
double F;
Console.WriteLine("Введите x:");
int x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Введите a");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Введите b");
int b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Введите c");
int c = Convert.ToInt32(Console.ReadLine());
if (a < 0 && c != 0)
{
F = a * Math.Pow(x, 2) + b * x + c;
}
else if (a > 0 && c == 0)
{
F = -a / (x - c);
Console.WriteLine("F= " + F);
}
else
{
F = a * (x + c);
Console.WriteLine("F= " + F);
Console.WriteLine("Хотите повторить? Нажмите 1, для выхода нажмите любую клавишу");
string j = Console.ReadLine();
if (j == "1")
goto label1;
}
}
}
}
}Решение задачи: «Как перевести в процедуры и функции»
textual
Листинг программы
using System;
class Program
{
static int Get(string ParamName)
{
Console.Write("Введите {0}: ", ParamName);
return int.Parse(Console.ReadLine());
}
static double F(double x, double a, double b, double c)
{
if (a < 0 && c != 0) return a * x * x + b * x + c;
if (a > 0 && c == 0) return -a / (x - c);
return a * (x + c);
}
static void Iteration()
{
Console.WriteLine("F = a * x * x + b * x + c при a < 0 и c != 0");
Console.WriteLine("F = -a / (x - c) при a > 0 и c = 0");
Console.WriteLine("F = a * (x + c) в остальных случаях");
Console.WriteLine("F = {0}", F(Get("x"), Get("a"), Get("b"), Get("c")));
}
static void Main(string[] args)
{
do
{
Console.Clear();
try { Iteration(); }
catch { Console.WriteLine("Ошибка"); }
Console.WriteLine("Для повтора нажмите 1");
}
while (Console.ReadKey().KeyChar == '1');
}
}