Переписать код с Pascal - C#
Формулировка задачи:
Вот, собственно, код. Нужно переделать его на C#.
var xx,yy:array[1..100] of real; a1,b1,c1,st,s_mn:real; i,n:integer; procedure line(x0,y0,x,y:real;var l:real); begin l:=sqrt(sqr(x-x0)+sqr(y-y0)); end; procedure pl(a,b,c:real; var s:real); var p:real; begin p:=(a+b+c)/2; s:=sqrt(p*(p-a)*(p-b)*(p-c)); end; begin readln(n); for i:=1 to n do readln(xx[i],yy[i]); s_mn:=0; for i:=1 to n-2 do begin line(xx[1],yy[1],xx[i+1],yy[i+1],a1); line(xx[i+1],yy[i+1],xx[i+2],yy[i+2],b1); line(xx[i+2],yy[i+2],xx[1],yy[1],c1); pl(a1,b1,c1,st); s_mn:=s_mn+st; end; writeln(s_mn); end. end.
Решение задачи: «Переписать код с Pascal»
textual
Листинг программы
using System;
class Program
{
public static void Line(double x0, double y0, double x, double y, ref double l)
{
l = Math.Sqrt((x - x0) * (x - x0) + (y - y0) * (y - y0));
}
public static void Pl(double a, double b, double c, ref double s)
{
double p = (a + b + c) * 0.5;
s = Math.Sqrt(p * (p - a) * (p - b) * (p - c));
}
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
var xx = new int[101];
var yy = new int[101];
for (int i = 1; i <= n; i++)
{
var aData = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
xx[i] = Convert.ToInt32(aData[0]);
yy[i] = Convert.ToInt32(aData[1]);
}
double a1 = 0, b1 = 0, c1 = 0, st = 0, s_mn = 0;
for (int i = 1; i <= n - 2; i++)
{
Line(xx[1], yy[1], xx[i + 1], yy[i + 1], ref a1);
Line(xx[i + 1], yy[i + 1], xx[i + 2], yy[i + 2], ref b1);
Line(xx[i + 2], yy[i + 2], xx[1], yy[1], ref c1);
Pl(a1, b1, c1, ref st);
s_mn += st;
}
Console.WriteLine(s_mn);
}
}