PDA

View Full Version : سوال: ارسال علامت هاي مقايسه ای به عنوان رشته به تابع



armm1388
شنبه 07 اردیبهشت 1392, 11:49 صبح
با سلام
می خواهم علامت مقایسه ای را به دو عدد به تابع بفرستم. حال در تابع چگونه دستورش را بنویسم؟
مثال:

(int d1, int d2, string operator)
(12, 14, “>”).

حال شرط را چگونه در تابع پیاده سازی کنیم؟
if (d1 operator d2) // is incorrect
if (d1 + operator + d2) // is incorrect
if (convert.toBolean (d1+ operator + d2)) // is incorrect

tooraj_azizi_1035
شنبه 07 اردیبهشت 1392, 13:53 عصر
سلام
با delegate باید کار کنید:



delegate bool CompareDelegate(int x, int y);
static void Main()
{
CompareDelegate lessThan = delegate(int x, int y) { return x < y; };
CompareDelegate moreThan = delegate(int x, int y) { return x y; };

bool isLess = Compare(2, 5, lessThan); // true
bool isMore = Compare(2, 5, moreThan); // false
}
static bool Compare(int a, int b, CompareDelegate c)
{
return c(a, b);
}