1. จงเขียนโปรแกรมหาค่า f(x)โดยสมการf(x)เป็นดังนี้
f(x) = 3x2 + 2x + 7 if x <0
= 0 if x = 0
= x – 2 if x >0
กำหนดให้ส่วนที่ใช้ในการคำนวณค่าf(x)อยู่ในฟังก์ชันget_Fxกำหนดให้ส่วนที่รับค่าตัวแปร Xจากคีย์บอร์ดและส่วนที่แสดงผลลัพธ์ของค่าf(x) อยู่ในฟังก์ชัน main
class Program
{
static
void Main(string[]
args)
{
int
first, second, third;
Console.Write("\n f(x) =
3x2 + 2x + 7 if x <0\n");
Console.Write(" f(x) = 0 if x = 0");
Console.Write("\n f(x) = x
- 2 if x >0");
Console.Write("\n\nEnter first value: ");
first = Int32.Parse(Console.ReadLine());
get_Fx(first);
Console.Write("\n\nEnter second value: ");
second = Int32.Parse(Console.ReadLine());
get_Fx(second);
Console.Write("\n\nEnter third value: ");
third = Int32.Parse(Console.ReadLine());
get_Fx(third);
}
static
void get_Fx(int
x)
{
if
(x>0)
Console.WriteLine("f({0}) is {1}", x, (x-2));
else
if (x==0)
Console.WriteLine("f({0}) is {1}", x, (x=0));
else
if (x<0)
Console.WriteLine("f({0}) is {1}", x, (3*x*2 + 2*x +
7));
}
}
|
2. จงเขียนโปรแกรมหาค่าf(x,y) โดยสมการ f(x,y)เป็นดังนี้
f(x, y) = x + y if x + y >0
= 0 if x + y = 0
= -x + y if x + y <0
กำหนดให้ส่วนที่ใช้ในการคำนวณค่า f(x,y) อยู่ในฟังก์ชันget_Fxyกำหนดให้ส่วนที่รับค่าตัวแปรx จากคีย์บอร์ดและส่วนที่แสดงผลลัพธ์ของค่า f(x,y) อยู่ในฟังก์ชันmain
class Program
{
static
void Main(string[]
args)
{
int
firstx, secondx, thirdx, firsty, secondy, thirdy;
Console.Write("\n f(x,y) =
x + y if x + y > 0");
Console.Write("\n f(x,y) = 0 if
x + y = 0");
Console.Write("\n f(x,y) =
-x + y if x + y < 0");
Console.Write("\n\nEnter first value X: ");
firstx = Int32.Parse(Console.ReadLine());
Console.Write("\nEnter first value Y: ");
firsty = Int32.Parse(Console.ReadLine());
get_Fxy(firstx,firsty);
Console.Write("\n\nEnter second value X: ");
secondx = Int32.Parse(Console.ReadLine());
Console.Write("\nEnter second value Y: ");
secondy = Int32.Parse(Console.ReadLine());
get_Fxy(secondx, secondy);
Console.Write("\n\nEnter third value X: ");
thirdx = Int32.Parse(Console.ReadLine());
Console.Write("\nEnter third value Y: ");
thirdy = Int32.Parse(Console.ReadLine());
get_Fxy(thirdx, thirdy);
}
static
void get_Fxy(int
x , int y)
{
if
(x + y > 0)
Console.WriteLine("f({0},{1}) is {2}", x,y, (x + y));
else
if (x + y == 0)
Console.WriteLine("f({0},{1}) is 0");
else
if (x + y < 0)
Console.WriteLine("f({0},{1}) is {2}", x,y, (-x + y));
}
}
|
ไม่มีความคิดเห็น:
แสดงความคิดเห็น
แสดงความเห็นเกี่ยวกับบทความตรงนี้นะ ^^