Tải bản đầy đủ (.docx) (14 trang)

Bài tập lập trình hướng đối tượng c#

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (117.93 KB, 14 trang )

Bài tập Ứng dụng kỹ thuật lập trình hướng đối tượng để:
1. Tính diện tích và chu vi của một hình chữ nhật với việc nhập dữ liệu từ bàn phím.
2. Hiển thị lên màn hình tài khoản gồm các thông tin sau:
Chủ tài khoản / Số tài khoản / Số dư
3. Xây dựng lịch dương cho một năm bất kỳ, hiển thị lên màn hình theo dạng sau:
Tháng 1
CN Thứ 2 Thứ 3 Thứ 4 Thứ 5 Thứ 6 Thứ 7
1 2 3 4 5 6 7
8 9….
4. Lập danh sách sinh viên lớp KTĐT&THCN - K54 thi môn Lập trình hướng đối
tượng với các thông tin sau:
STT Họ và tên MSV Điểm thành phần Điểm thi Điểm kết thúc
Sau đó in ra danh sách các sinh viên phải học lại môn học này.
5. Lập danh sách quản lý hàng hóa trong siêu thị với các thông tin: Mã hàng hóa, tên
hàng hóa, số lượng, đơn giá. Sau đó in ra màn hình danh sách các loại hàng hóa có
sô lượng nhỏ hơn 5 để nhập hàng về.
6. In ra màn hình hóa đơn mua hàng của một khách hàng với các thông tin: Mã hàng
hóa, tên hàng hóa, số lượng, đơn giá, tiền trả cho từng mặt hàng và tổng tiền phải
thanh toán cho cả hóa đơn, biết nếu tổng tiền phải trả cho cả hóa đơn từ 1 triệu trở
lên thì được giảm 30%đơn hàng, lớn hơn 500 nghìn thì được giảm 10%.
7. Lập danh sách cán bộ công nhân viên trường Đại học GTVT với các thông tin sau:
STT Họ và tên MCB Ngày sinh Giới tính Quê quán
Sau đó in ra danh sách cán bộ công nhân viên quê ở Hà Nội.
8. Lập danh sách quản lý sách trong thư viện với các thông tin: Mã sách, chủng loại
sách, tên sách, năm xuất bản, nhà xuất bản, số lượng, đơn giá. Sau đó in ra màn
hình danh sách các loại sách do Nhà xuất bản Khoa học kỹ thuật xuất bản.
9. Lập danh sách quản lý xe oto trong gara với các thông tin: Tên xe, số máy, hãng
sản xuất, năm sản xuất, số lượng. Sau đó in ra màn hình danh sách các loại xe có
trong gara do hãng Toyota sản xuất.
Giải :
Câu 1 :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace bai_tap
{

public class chunhat
{
float dai;
float rong;
public float dientich(float dai, float rong)
{
return (dai * rong);
}
public float chuvi(float d, float r)
{
return (d + r) * 2;
}
}
class Program
{
static void Main(string[] args)
{
lap:
string h;
float m, n;
Console.Write("nhap gia tri chieu dai :");
m = float.Parse(Console.ReadLine());
Console.Write("nhap gia tri chieu rong :");

n = float.Parse(Console.ReadLine());
chunhat a = new chunhat();
Console.WriteLine(" ");
Console.Write("dien tich hinh la " + a.dientich(m,n));
Console.Write("\nchu vi hinh la {0}", a.chuvi(m,n));
Console.WriteLine("\n ");
Console.WriteLine("\n\n\nban co muon tiep tuc ?\n chon \" y \" de tiep tuc
chon ki tu khac de ket thuc");
Console.Write("moi nhap :");
h = Console.ReadLine();
if (h == "y")
goto lap;
Console.ReadLine();
}
}
}
Câu 2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace bai2
{
class thuebao
{
string chutaikhoan;
float sotaikhoan, sodu;
public void nhap()
{
Console.WriteLine("hay nhap chu tai khoan :");

chutaikhoan = Console.ReadLine();
Console.WriteLine("hay nhap so tai khoan :");
sotaikhoan = float.Parse(Console.ReadLine());
Console.WriteLine("hay nhap so du tai khoan :");
sodu = float.Parse(Console.ReadLine());
}
public void xuat()
{
Console.WriteLine("{0}\t{1}\t{2}\n", chutaikhoan, sotaikhoan, sodu);
}
}
class Program
{
static void Main(string[] args)
{
int n ;
thuebao[] a = new thuebao[100];
Console.WriteLine("DANH SACH TAI KHOAN NGAN HANG CONGTHUONGVN");
Console.Write("Nhap so tai khoan trong danh sach :");
n = int.Parse(Console.ReadLine());
for (int f = 1; f <= n; f++)
{
a[f] = new thuebao();
a[f].nhap();
}
Console.WriteLine("CHUTAIKHOAN\tSOTAIKHOAN\tSODU");
for (int f = 1; f <= n; f++)
{
a[f].xuat();
}

Console.ReadLine();
}
}
}
Câu 3:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace inlich2
{
class Program
{
static void Main(string[] args)
{
Console.Write("LICH DUOC SU DUNG TU NAM 1900 TRO DI\n");
int t, n;
do
{
Console.Write("nhap nam can xem : ");
n = int.Parse(Console.ReadLine());
}
while (n < 1900);
t = 1;
Console.WriteLine("\nLICH NAM {0}", n);
lap:
int namnhuan, namko, songay, ngaydu;
ngaydu = 0;
int[] a = new int[13] { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
namnhuan = (n - 1900-1) / 4;

if (namnhuan < 0)
namnhuan = 0;
namko = n - 1900 - namnhuan;
for (int i = 0; i < t; i++)
{

ngaydu = ngaydu + a[i];
if (((n - 1900) % 4) == 0 && i == 2)
{
ngaydu = ngaydu + 1;
}
if (n == 1900 && i==2)
ngaydu = ngaydu - 1;
}
songay = (namnhuan) * 366 + (namko) * 365 + ngaydu;
int b;
b = songay - (songay / 7) * 7;
Console.WriteLine("\nTHANG {0}", t);
Console.Write("CHUNHAT\tTHU2\tTHU3\tTHU4\tTHU5\tTHU6\tTHU7\n");
if (t == 4 || t == 6 || t == 9 || t == 11)
{
for (int p = 1; p <= b + 1; p++)
{
Console.Write("\t");
}
if (b == 6)
Console.WriteLine();
for (int m = 1; m <= 30; m++)
{
Console.Write("{0}\t", m);

if ((m - (7 - b - 1)) % 7 == 0)
Console.WriteLine();
}
}
else if (((n - 1900) % 4 )== 0 && t == 2)
{
for (int p = 1; p <= b + 1; p++)
{
Console.Write("\t");
}
if (b == 6)
Console.WriteLine();
for (int m = 1; m <= 29; m++)
{
if (n == 1900 && m == 29)
continue;
Console.Write("{0}\t", m);
if ((m - (7 - b - 1)) % 7 == 0)
Console.WriteLine();

}
}

else if (t == 2)
{

for (int p = 1; p <= b + 1; p++)
{
Console.Write("\t");
}

if (b == 6)
Console.WriteLine();
for (int m = 1; m <= 28; m++)
{
Console.Write("{0}\t", m);
if ((m - (7 - b - 1)) % 7 == 0)
Console.WriteLine();
}
}
else
{
for (int p = 1; p <= b + 1; p++)
{
Console.Write("\t");
}
if (b == 6)
Console.WriteLine();
for (int m = 1; m <= 31; m++)
{
Console.Write("{0}\t", m);
if ((m - (7 - b - 1)) % 7 == 0)
Console.WriteLine();
}
}
t++;
if (t <= 12)
goto lap;
Console.ReadLine();
}
}

}
Câu 4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace bai_thongtinsinhvien
{
class sinhvien
{
string hoten;
int masv;
float diemtp,diemthi, diemkt;
public void thongtin()
{

Console.Write("hay nhap ho ten sinh vien : ");
hoten = Console.ReadLine();
Console.Write("hay nhap ma sinh vien : ");
masv = int.Parse(Console.ReadLine());
do
{
Console.Write("hay nhap diem thanh phan cua sinh vien : ");
diemtp = float.Parse(Console.ReadLine());
}
while(diemtp<0||diemtp>10);
do
{
Console.Write("hay nhap diem thi cua sinh vien : ");
diemthi = float.Parse(Console.ReadLine());

}
while (diemthi > 10 || diemthi < 0);
diemkt = diemthi * 7 / 10 + diemtp * 3 / 10;
}
public void lai()
{
if (diemkt<=4)
Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4:0.0}", hoten, masv, diemtp,
diemthi, diemkt);
}
public void chitiet()
{

Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4:0.0}", hoten, masv,
diemtp,diemthi ,diemkt);
}

}
class Program
{
static void Main(string[] args)
{
int n;
Console.Write("nhap so sinh vien trong lop : ");
n= int.Parse(Console.ReadLine());
sinhvien h = new sinhvien();
sinhvien [] a = new sinhvien[100];
sinhvien[] b = new sinhvien[100];
for (int i = 1; i <= n; i++)
{

Console.Write("nhap thong tin cho sinh vien thu {0}\n", i);
a[i] = new sinhvien();
a[i].thongtin();
Console.Write(" \n");
}
Console.WriteLine("\n\nSTT\tHOTEN\tMASV\tDIEMTP\tDIEMTHI\tDIEMKT");
for (int i = 1; i <= n; i++)
{
Console.Write("{0}\t", i);
a[i].chitiet();
}
Console.WriteLine("\n\n\nDANH SACH SINH VIEN PHAI HOC LAI:");
Console.WriteLine("\nSTT\tHOTEN\tMASV\tDIEMTP\tDIEMTHI\tDIEMKT");
for (int i = 1; i <= n; i++)
{

Console.Write("{0}\t", i);
a[i].lai();

}
Console.ReadLine();
}
}
}
Câu 5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace bai5

{
class hanghoa
{
string mahanghoa, ten;
float dongia ;
int soluong;
public void nhap()
{
Console.WriteLine("nhap ma hang hoa :");
mahanghoa = Console.ReadLine();
Console.WriteLine("nhap ten hang hoa :");
ten = Console.ReadLine();
Console.WriteLine("nhap gia hang hoa :");
dongia = float.Parse(Console.ReadLine());
Console.WriteLine("nhap so luong hang hoa :");
soluong = int.Parse(Console.ReadLine());
}
public void xuat()
{
Console.WriteLine("{0}\t\t{1}\t\t{2}\t\t{3}", mahanghoa, ten, dongia,
soluong);
}
public void nhaphang()
{
if (soluong<5)
Console.WriteLine("{0}\t\t{1}\t\t{2}\t\t{3}", mahanghoa, ten, dongia,
soluong);
}
}
class Program

{
static void Main(string[] args)
{
int n;
Console.WriteLine("nhap so luong loai hang hoa :");
n = int.Parse(Console.ReadLine());
hanghoa[] a = new hanghoa[100];
for (int i = 1; i <= n; i++)
{
Console.WriteLine("nhap thong tin cho loai hang thu {0}", i);
a[i] = new hanghoa();
a[i].nhap();
}
Console.WriteLine("STT\tMAHANGHOA\tTENHANGHOA\tDONGIA\tSOLUONG");
for (int i = 1; i <= n; i++)
{
Console.Write("{0}\t", i);
a[i].xuat();
}
Console.WriteLine("\n\nDANH SACH HANG HOA CAN NHAP VE LA :\n");
Console.WriteLine("STT\tMAHANGHOA\tTENHANGHOA\tDONGIA\tSOLUONG");
for (int i = 1; i <= n; i++)
{
Console.Write("{0}\t", i);
a[i].nhaphang();
}
Console.ReadLine();
}
}
}

Câu 6
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace bai5
{
class hanghoa
{
string mahanghoa, ten;
float dongia;
int soluong;
float gia;
public void nhap()
{
Console.WriteLine("nhap ma hang hoa :");
mahanghoa = Console.ReadLine();
Console.WriteLine("nhap ten hang hoa :");
ten = Console.ReadLine();
Console.WriteLine("nhap gia hang hoa :");
dongia = float.Parse(Console.ReadLine());
Console.WriteLine("nhap so luong hang hoa :");
soluong = int.Parse(Console.ReadLine());
gia = soluong * dongia;
}
public float giahang()
{
return soluong * dongia;
}
public void xuat()

{
Console.WriteLine("{0}\t\t{1}\t\t{2}\t{3}\t{4}", mahanghoa, ten, dongia,
soluong, gia);
}
}
class Program
{
static void Main(string[] args)
{
float tong;
tong = 0;
int n;
Console.WriteLine("nhap so luong loai hang hoa :");
n = int.Parse(Console.ReadLine());
hanghoa[] a = new hanghoa[100];
for (int i = 1; i <= n; i++)
{
Console.WriteLine("nhap thong tin cho loai hang thu {0}", i);
a[i] = new hanghoa();
a[i].nhap();
a[i].giahang();
tong = tong + a[i].giahang();
}
Console.WriteLine("STT\tMAHANGHOA\tTENHANGHOA\tDONGIA\tSOLUONG\tGIA");
for (int i = 1; i <= n; i++)
{
Console.Write("{0}\t", i);
a[i].xuat();
}
float k = 0;

if (tong >= 1000000)
k = tong * 70 / 100;
if (tong < 1000000 && k > 500000)
k = tong * 90 / 100;
Console.WriteLine("\n TOnNG TIEN PHAI THANH TOAN LA :");
Console.Write("{0}\t",k);
Console.ReadLine();
}
}
}
Câu 7
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace bai7
{
class gtvt
{
string hoten;
int mcb;
string ngayde;
string gioitinh, quequan;
public void nhap()
{
Console.Write("nhap ten can bo :");
hoten = Console.ReadLine();
Console.Write("nhap mcb :");
mcb = int.Parse(Console.ReadLine());
Console.Write("nhap ngay / thang /nam sinh :");

ngayde = Console.ReadLine();
Console.Write("nhap gioi tinh :");
gioitinh = Console.ReadLine();
Console.Write("nhap que quan :");
quequan = Console.ReadLine();
}
public void xuat()
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", hoten, mcb, ngayde, gioitinh,
quequan);
}
public void hanoi()
{
if (quequan == "ha noi")
Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", hoten, mcb, ngayde,
gioitinh, quequan);
}
}
class Program
{
static void Main(string[] args)
{
int n;
Console.Write("nhap so can bo trong truong gia0 thong:");
n = int.Parse(Console.ReadLine());
gtvt[] a = new gtvt[10000];
for (int i = 1; i <= n; i++)
{
Console.WriteLine("nhap thong tin cho can bo thu {0}", i);
a[i] = new gtvt();

a[i].nhap();
}
Console.WriteLine("STT\tHOTEN\t\tMCB\tNGAYSINH\tGIOITINH\tQUEQUAN");
for (int i = 1; i <= n; i++)
{
Console.Write("{0}\t", i);
a[i].xuat();
}
Console.WriteLine("\n\nDANH SACH NHUNG NGUOI O \" ha noi \" LA :\n\n");
Console.WriteLine("STT\tHOTEN\tMCB\tNGAYSINH\tGIOITINH\tQUEQUAN");
for (int i = 1; i <= n; i++)
{
Console.Write("{0}\t", i);
a[i].hanoi();
}
Console.ReadLine();
}
}
}
Câu 8
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace bai8
{
class sach
{ string ma, chungloai, ten, nhasb;
int namsx, solg;
float dongia;

public void nhap()
{ Console.Write("nhap ma sach");
ma = Console.ReadLine();
Console.Write("nhap chung loai sach :");
chungloai = Console.ReadLine();
Console.Write("nhap ten sach:");
ten = Console.ReadLine();
Console.Write("nhap nam san xuat:");
namsx = int.Parse(Console.ReadLine());
Console.Write("nhap nha xuat ban :");
nhasb = Console.ReadLine();
Console.Write("nhap so luong:");
solg = int.Parse(Console.ReadLine());
Console.Write("nhap don gia :");
dongia = float.Parse(Console.ReadLine());
}
public void xuat()
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}", ma, chungloai, ten, namsx,
nhasb, solg, dongia);
}
public void trang()
{ if (nhasb=="Nha xuat ban Khoa hoc ki thuat")
Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}", ma, chungloai, ten, namsx,
nhasb, solg, dongia);
}
}
class Program
{
static void Main(string[] args)

{
int n;
Console.WriteLine("nhap so loai sach co trong thu vien ");
n = int.Parse(Console.ReadLine());
sach[] a = new sach[1000];
for (int i = 1; i <= n; i++)
{
Console.WriteLine("nhap thong tin cho loai {0}", i);
a[i] = new sach();
a[i].nhap();
}
Console.WriteLine("danh sach sach co trong thu vien :");
Console.WriteLine("MA\tCHUNGLOAI\tTEN\tNAMSX\tNHAXB\tSOLG\tDONGIA");
for (int i = 1; i <= n; i++)
{

a[i].xuat();
}
Console.WriteLine("\n\ndanh sach sach co trong thu vien do nha xuat ban khoa
hoc ki thuat xuat ban :\n\n");
Console.WriteLine("MA\tCHUNGLOAI\tTEN\tNAMSX\tNHAXB\tSOLG\tDONGIA");
for (int i = 1; i <= n; i++)
{
a[i].trang();
}
Console.ReadLine();
}
}
}
Câu 9 :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace bai9
{
public class xe
{
string tenxe , hangsx ;
float somay,namsx,soluong ;
public void nhap()
{
Console.WriteLine("nhap ten xe :");
tenxe = Console.ReadLine();
Console.WriteLine("Nhap so may :");
somay = float.Parse(Console.ReadLine());
Console.Write("nhap hang san xuat :");
hangsx = Console.ReadLine();
Console.Write("nhap nam san xuat :");
namsx = float.Parse(Console.ReadLine());
Console.Write("nhap so luong xe:");
soluong = float.Parse(Console.ReadLine());
}
public void xuat()
{

Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}",tenxe,somay,hangsx,namsx,soluong);
}
public void toki()
{

if (hangsx == "Toyota")
Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", tenxe, somay, hangsx, namsx,
soluong);
}
}
class Program
{
static void Main(string[] args)
{
int n ;
Console.Write("nhap so xe co trong gara :");
n = int.Parse(Console.ReadLine());
xe[] a = new xe[100];
for (int i = 1; i<=n; i++ )
{
Console.Write("nhap thong tin cho xe thu {0}\n",i);
a[i] = new xe();
a[i].nhap();
Console.WriteLine(" ");
}
Console.WriteLine("STT\tTENXE\tSOMAY\tHANGSX\tNAMSX\tSOLUONG");
for (int i = 1; i<=n; i++ )
{
Console.Write("{0}\t",i);
a[i].xuat();
}
Console.WriteLine ("\n\ndanh sach xe do hang\" Toyota \" san xuat la \n:");
Console.WriteLine("STT\tTENXE\tSOMAY\tHANGSX\tNAMSX\tSOLUONG");
for (int i = 1; i <= n; i++)
{

Console.Write("{0}\t", i);
a[i].toki();
}
Console.ReadLine();
}
}
}

×