Você está na página 1de 9

Bi tp thc hnh Nhp mn lp trnh

Mc tiu
Xy dng chng trnh bng cch thnh lp cc n th (phng thc).
Ci t cc thnh phn n th gm vo (input), x l (process), ra (output) ng vi
truyn tham s kiu tham tr, truyn tham s kiu tham chiu, gi tr tr v.
Hiu c tm vc ca bin, tham s hnh thc (argument) v tham s thc (parameter).
Thc thi cc n th v kim tra n th.
A. Bi tp mu
ngha s dng n th
Bi 1. In 3 hnh hnh vung In3HinhVuong.*
Nhp vo chiu cao h. Hy in hnh vung ng vi chiu cao h, h + 2, h + 4.
Moi ban nhap chieu cao h: 3
h=3
* * *
* *
* * *
h=5
* * * * *
* *
* *
* *
* * * * *
h=7
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
Chng trnh mu:
using System;

namespace NMLT.Buoi07
{
class In3HinhVuong
{
/// Ghi ch dng XML (nhn 3 du ///)
/// <summary>
/// n th in hnh vung vi chiu cao h
/// </summary>
/// <param name="h">Chiu cao h</param>
/// <param name="vtx">Ta x khi in ch</param>
/// <param name="vty">Ta y khi in ch</param>
/// <example>
/// vtx = 0, vty = 0, h = 5
/// *
/// * *

Trang 42
Bi tp thc hnh Nhp mn lp trnh

/// * *
/// * *
/// * * * * * * * * *
/// </example>
public static void InHinhVuong(int vtx, int vty, int h)
{
int i, j;
// Thit lp mu ch
Console.ForegroundColor = ConsoleColor.Red;

// In hnh vung
// Dng 1: // Thit lp ta u dng, In h du *
Console.SetCursorPosition(vtx, vty);
for (i = 1; i <= h; i++) Console.Write("{0, 2}", '*');
// Dng 2 n h-1:
for (i = 2; i <= h - 1; i++)
{
// Thit lp u dng i
Console.SetCursorPosition(vtx, vty + i - 1);
// In 1 u *, h - 2 khong trng, 1 du *
Console.Write("{0, 2}", '*');
for (j = 1; j <= h - 2; j++) Console.Write("{0, 2}", ' ');
Console.Write("{0, 2}", '*');
}
// Dng h: // Thit lp u dng h, In h du *
Console.SetCursorPosition(vtx, vty + h - 1);
for (i = 1; i <= h; i++) Console.Write("{0, 2}", '*');

// Nhy ti u dng kt thc, phc hi mu ch


Console.SetCursorPosition(0, vty + h);
Console.ResetColor();
}
public static void Main(string[] args)
{
// Khai bo bin
int h;

// Nhp d liu
Console.Write("Moi ban nhap chieu cao h: ");
h = int.Parse(Console.ReadLine());

// In hnh vung
Console.WriteLine("h = {0}", h);
InHinhVuong(Console.CursorLeft, Console.CursorTop, h);
Console.WriteLine("h = {0}", h + 2);
InHinhVuong(Console.CursorLeft, Console.CursorTop, h + 2);
Console.WriteLine("h = {0}", h + 4);
InHinhVuong(Console.CursorLeft, Console.CursorTop, h + 4);
}
}
}

Truyn tham tr v gi tr tr v
Bi 2. Kim tra s nguyn t KiemTraNguyenTo.*
Nhp s nguyn dng n. Hy kim tra xem n c phi l s nguyn t? Bit s nguyn t l s
ch chia ht cho 1 v chnh n.
Moi ban nhap n: 7
So 7 la so nguyen to.
Chng trnh mu:
using System;
Trang 43
Bi tp thc hnh Nhp mn lp trnh

namespace NMLT.Buoi07
{
public class KiemTraNguyenTo
{
/// <summary>
/// n th kim tra s nguyn t
/// </summary>
/// <param name="n">u vo: s nguyn n cn kim tra (tham s hnh
thc)</param>
/// <returns>true (l s nguyn t), false (khng l s nguyn t)</returns>
/// <example>
/// n = 3 --> return = true
/// n = 6 --> return = false
/// </example>
public static bool laNguyenTo(int n)
{
int i;
bool laNguyenTo;// c hiu nguyn t (true / false)

laNguyenTo = true; // Gi s n l nguyn t


for (i = 2; i * i <= n && laNguyenTo == true; i++)
{
if (n % i == 0) laNguyenTo = false;
}
return laNguyenTo;
}
public static void Main(string[] args)
{
// Khai bo bin
int n;
bool kq;

// Nhp d liu
Console.Write("Moi ban nhap n: ");
n = int.Parse(Console.ReadLine());

// X l
kq = laNguyenTo(n);// S dng n th

// Xut kt qu
if(kq)
Console.WriteLine("So {0} la nguyen to.", n);
else
Console.WriteLine("So {0} khong la nguyen to.", n);
}
}
}

Bi 3. Tnh giai tha GiaiThua.*


Nhp vo s nguyn dng n (1 n 12). Hy tnh n! bit: ! = 1.2.3.4 .
Moi ban nhap so n: 5
5! = 1.2.3.4.5 = 120.
Chng trnh mu:
using System;

namespace NMLT.Buoi07
{
public class KiemTraNguyenTo
{

Trang 44
Bi tp thc hnh Nhp mn lp trnh

/// <summary>
/// Tnh giai tha n!
/// </summary>
/// <param name="n">n</param>
/// <returns>n!=1.2.3.4...n</returns>
/// <example>
/// n = 5! --> return = 120
/// </example>
public static int tinhGiaiThua(int n)
{
int gt, i;

gt = 1;
for (i = 1; i <= n; i++)
{
gt = gt * i;
}

return gt;
}

public static void Main(string[] args)


{
// Khai bo bin
int i, n;

// Nhp d liu
Console.Write("Moi ban nhap n: ");
n = int.Parse(Console.ReadLine());

// X l

// Xut kt qu
Console.Write("{0}! = 1", n);
for (i = 2; i <= n; i++)
{
Console.Write(".{0}", i);
}
// Li gi n th lng trong biu thc
Console.WriteLine(" = {0}.", tinhGiaiThua(n));
}
}
}

Truyn tham chiu


Bi 4. Gii phng trnh bc 2 PhuongTrinhBac2.*
Nhp 3 s thc a, b, c. Hy tm nghim ca phng trnh bc 2: 2 + + = 0
Moi ban nhap he so a, b, c: 1 5 6
Phuong trinh bac 2 1x^2 + 5x + 6 = 0 co: 2 nghiem, x1 = -2.00,
x2 = -3.00.
Chng trnh mu:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NMLT.Buoi07
{
class GiaiPhuongTrinhBac2

Trang 45
Bi tp thc hnh Nhp mn lp trnh

{
/// <summary>
/// Gii phng trnh bc 2: ax^2 + bx + c = 0
/// </summary>
/// <param name="a">h s a</param>
/// <param name="b">h s b</param>
/// <param name="c">h s c</param>
/// <param name="x1">Kt qu: nghim x1</param>
/// <param name="x2">Kt qu: nghim x2</param>
/// <returns>-1 (v s nghim), 0 (v nghim), 1 (1 nghim, x1), 2 (2 nghim
x1, x2)</returns>
/// <example>
/// a = 1, b = 5, c = 6 --> return = 2, x1 = -3, x2 = -2
/// </example>
public static int giaiPhuongTrinhBac2(double a, double b, double c, ref double
x1, ref double x2)
{
int co;// -1 (v s nghim), 0 (v nghim), 1 (1 nghim), 2 (2 nghim)

if (a == 0)
{
if (b == 0)
{
if (c == 0)
co = -1;
else
co = 0;
}
else
{
co = 1;
x1 = -b / a;
}
}
else
{
double delta = b * b - 4 * a * c;
if (delta < 0)
co = -1;
else if (delta == 0)
{
co = 1;
x1 = -b / (2 * a);
}
else
{
co = 2;
x1 = (-b - Math.Sqrt(delta)) / (2 * a);
x2 = (-b + Math.Sqrt(delta)) / (2 * a);
}
}

return co;
}

public static void Main(string[] args)


{
// Khai bo bin
string[] tam;
double hsa, hsb, hsc, n1 = 0, n2 = 0;
int songhiem;

// Nhp d liu

Trang 46
Bi tp thc hnh Nhp mn lp trnh

Console.Write("Moi ban nhap he so a, b, c: ");


tam = Console.ReadLine().Split(new char[] { ' ', '\t' });
hsa = double.Parse(tam[0]);
hsb = double.Parse(tam[1]);
hsc = double.Parse(tam[2]);

// X l
songhiem = giaiPhuongTrinhBac2(hsa, hsb, hsc, ref n1, ref n2);

// Xut d liu
Console.Write("Phuong trinh bac 2 {0}x^2 + {1}x + {2} = 0 co: ", hsa, hsb,
hsc);
if (songhiem == -1)
Console.WriteLine(" vo so nghiem.");
else if (songhiem == 0)
Console.WriteLine(" vo nghiem.");
else if (songhiem == 1)
Console.WriteLine(" 1 nghiem, x = {0: #.00}.", n1);
else if (songhiem == 2)
Console.WriteLine(" 2 nghiem, x1 = {0: #.00}, x2 = {1:#.00}.", n1, n2);
}
}
}

Bi 5. X l ch s XuLyChuSo.*
Nhp s nguyn n. Hy (a) s ch s ca n (b) tng cc ch s ca n (c) tm ch s ln nht
ca n.
Moi ban nhap so nguyen n: 23457
So 23457 co 5 chu so, tong chu so la 21, chu so lon nhat la 7.
Chng trnh mu:
using System;

namespace NMLT.Buoi07
{
public class XuLyChuSo
{
public static void xuLy(int n, ref int dem, ref int tong, ref int vmax)
{
dem = 0; // m ch s
tong = 0;// Tng ch s
// ch s nh nht ban u
// (khng tn ti trong tp ch s [0,9])
vmax = -1;
// Duyt cc ch s ca n t phi qua tri
while (n != 0)
{
int cs = n % 10; // ch s ti v tr cui

dem++;
tong += cs;
if (vmax == -1)// nu cha tn ti
vmax = cs;
else// tn ti
{
// Cha ti u
if (vmax < cs) vmax = cs;
}

n = n / 10;// dch n qua phi


}

Trang 47
Bi tp thc hnh Nhp mn lp trnh

}
public static void Main(string[] args)
{
// Khai bo bin
int n;
int demcs = 0, tongcs = 0, csmax = 0;

// Nhp d liu
Console.Write("Moi ban nhap so nguyen n: ");
n = int.Parse(Console.ReadLine());

// X l
xuLy(n, ref demcs, ref tongcs, ref csmax);

// Xut kt qu
Console.WriteLine("So {0} co {1} chu so, tong chu so la {2}, chu so lon
nhat la {3}.", n, demcs, tongcs, csmax);
}
}
}

B. Bi tp c bn
Bi 1. In 3 hnh tam gic InHinhTamGiacVaVuong.*
Nhp vo chiu cao h. Hy in hnh tam gic v hnh vung ng vi chiu cao h.
Moi ban nhap chieu cao h: 3
h=3
*
* *
* * * * *
h=5
*
* *
* *
* *
* * * * * * * * *
h=7
*
* *
* *
* *
* *
* *
* * * * * * * * * * * * *
Bi 2. Kim tra s hon ho KiemTraSoHoanHao.*
S t nhin n gi l s hon ho nu tng cc c t nhin ca n khng k chnh n bng n.
V d: 6 l s hon ho v 6 = 1 + 2 + 3. Cho s t nhin n. Hy kim tra n c l s hon ho.
Moi ban nhap n: 6
6 = 1 + 2 + 3 la so hoan hao.

Moi ban nhap n: 8


8 khong la so hoan hao.
Bi 3. Tnh s pi TinhSoPi.*

Trang 48
Bi tp thc hnh Nhp mn lp trnh

Nhp vo sai s epsilon. Vit chng trnh tnh s vi sai s epsilon theo cng thc sau:
1 1 1 1 4
= 4 (1 3 + 5 7 + + (1) 2+1 + ) cho n khi 2+1 <
Moi ban nhap do sai so epsilon: 0.001
So pi tinh den do chinh xac 0.001 la ?.
Do sai so thuc te la ?.
Bi 4. Ngy trc NgayTruoc.*
Nhp vo ngy, thng, nm. Hi ngy trc l ngy no?
Moi ban nhap ngay: 1
Moi ban nhap thang: 2
Moi ban nhap nam: 2015
Ngay truoc ngay 1/2/2015 la ngay 31/1/2015.
Bi 5. X l ch s (1) XuLyChuSo1.*
Nhp s nguyn n. Hy (a) cho bit ch s th k (tnh t tri sang) (b) tng cc ch s l (c)
tm ch s l nh nht v ln nht.
Moi ban nhap so nguyen n: 182345
Moi ban nhap vi tri k: 2
Chu so o vi tri 2 (tinh tu trai) cua 182345 la 8.
Tong cac chu so le cua 182345 la 9.
182345 co chu so le nho nhat la 1 va lon nhat la 5.
C. Bi tp nng cao
Bi 1. In ch HUFLIT InHUFLIT.*
Nhp vo cao h. Hy in hnh ch F, H, L va T ng vi chiu cao h.
h = 5
* * * * * * * * * * * * * * * * * * * *
* * * * * * * *
* * * * * * * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * * *
Bi 2. Tnh ly tha LuyThua.*
Nhp s thc a v nguyn dng n. Hy tnh an (khng dng th vin ton hc).
Bi 3. Tm s Fibonancy TimSoFibonaci.*
Nhp vo s nguyn dng n. Hy tm s Fibonancy ln nht nh hn n cho trc. Bit s
Fibonaci l s c tnh theo cng thc sau:
1 = 1 = 2
= {
1 + 2 >2
i 1 2 3 4 5 6 7 8
Fi 1 1 2 3 5 8 13 21
V d: n = 9 th s Fibonacy gn n nht l 8.
Bi 4. Gii h phng trnh HePhuongTrinh.*
Nhp 6 s thc a, b, c, d, e, f. Hy tm nghim ca h phng trnh sau:
+ =
{
+ =
Bi 5. Gii phng trnh bc 3 PhuongTrinhBac3.*

Trang 49
Bi tp thc hnh Nhp mn lp trnh

Nhp bn h s a, b, c, d ca phng trnh bc 3 (vi a khc 0):


3 + 2 + + = 0
Hy gii v bin lun nghim phng trnh bc 3.
Tham kho trn vi.wikipedia.org vi t kha Phng trnh bc 3

Trang 50