-
March 26th, 2011, 02:36 PM
#1
transform from C# to C++
Last edited by sadam; March 28th, 2011 at 05:10 PM.
-
March 26th, 2011, 10:35 PM
#2
Re: transform from C# to C++
(I realize that this conversion is not complete - you'll have to make some adjustments)
Code:
using namespace System;
private ref class Program
{
private:
value class Point
{
private:
double X;
double Y;
public:
//C# TO C++ CONVERTER TODO TASK: Calls to same-class constructors are not supported in C++:
//ORIGINAL LINE: public Point(double x, double y) : this()
Point(double x, double y)
{
X = x;
Y = y;
}
double DistanceFrom(Point other)
{
return Math::Sqrt(Math::Pow(X - other.X, 2) + Math::Pow(Y - other.Y, 2));
}
static Point FromLine(String ^line)
{
array<String^> ^parts = line->Split(' ');
return Point(double::Parse(parts[0]), double::Parse(parts[1]));
}
virtual String ^ToString() override
{
return "(" + X + "," + Y + ")";
}
};
static void Main(array<String^> ^args)
{
TextReader ^reader = gcnew StreamReader("..\\CatAndDog.dat");
Point cat = Point::FromLine(reader->ReadLine());
Point dog = Point::FromLine(reader->ReadLine());
int numTrees = int::Parse(reader->ReadLine());
array<Point> ^trees = gcnew array<Point>(numTrees);
for (int i = 0; i < numTrees; ++i)
trees[i] = Point::FromLine(reader->ReadLine());
reader->Close();
Console::WriteLine("Cat is at: {0}", cat);
Console::WriteLine("Dog is at: {0}", dog);
Console::Write("There are {0} trees:", trees->Length);
for each (Point tree in trees)
Console::Write(" {0}", tree);
Console::WriteLine();
Point closestTree = trees->OrderBy([&] (Object ^t)
{
t::DistanceFrom(cat);
}).First();
Console::WriteLine("The closest tree to the cat is: {0}", closestTree);
Console::WriteLine("The dog is {0} units from the cat.", dog.DistanceFrom(cat));
Console::WriteLine("The cat is {0} units from the closest tree.", cat.DistanceFrom(closestTree));
Console::WriteLine("The cat will{0} escape from the dog.", (cat.DistanceFrom(closestTree) * 2 < dog.DistanceFrom(cat)) ? "" : " not");
}
};
Last edited by David Anton; March 26th, 2011 at 10:59 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|