Bài tập lập trình c sharp cơ bản năm 2024

Bài tập lập trình c sharp cơ bản năm 2024

1.746 lượt xem 366 download

Bài tập lập trình c sharp cơ bản năm 2024
DownloadVui lòng tải xuống để xem tài liệu đầy đủ

Bài tập lập trình c sharp cơ bản năm 2024

Nội dung Text: Bài tập Lập trình C# căn bản

  1. Bài tập Lập trình C# căn bản
  2. L p trình C# - Căn b n KI U S L nh nh p 1 s t bàn phím Console.WriteLine(“Câu thông báo…”); Int So = Int.Parse(Console.ReadLine()); Int so01 = 23; Int so02 = 7; → so01 = s01 + 1 So01 += so02; Console.WriteLine(“so : {0}”, so01); Console.ReadLine(); --------- Int so = 10; so + +; so = so + 1 Console.WriteLine(“so : {0}”, so); Console.ReadLine(); --------------- Lưu ý: So + + Console.WriteLine(“So : {0}, so + + ); Console.WriteLine(“So :{0}”, so); So = so + 1 + + So Console.WriteLine(“So : {0}, + + so ); So = so + 1 Console.WriteLine(“So :{0}”, so); --------------- Int so01 = 23; Int so02 = 7; Tính: So01 += so02 – –; So01 = so01 + so02; So02 = so02 – 1; KQ: 30/6 Int so01 = 23; Int so02 = 7; Tính: So01 += – – so02; So02 = so02 – 1; So01 = so01 + so02; KQ: 29/6 Trang 1 Sangit.design.officelive.com – Email: [email protected]
  3. L p trình C# - Căn b n LAB TH C HÀNH Lab 1: Hàm enum using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace lab01 { class Program { enum ngaytrongtuan { thuhai, thuba, thutu, thunam, thusau, thubay, chunhat } static void Main(string[] args) { Console.WriteLine("hom nay la : {0}",ngaytrongtuan.thutu); Console.WriteLine("hai ngay sau la : {0}",ngaytrongtuan.thutu + 2); Console.ReadLine(); } } } Lab 2 – Ki u s using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace lab02 { class Program { static void Main(string[] args) { int so = 10; so += 5; //so = so + 5 ; Console.WriteLine("gia tri cua so la : {0}",so); Console.ReadLine(); Trang 2 Sangit.design.officelive.com – Email: [email protected]
  4. L p trình C# - Căn b n int so01 = 23; int so02 = 7; so01 -= so02; so01 += so01; // so01 = so01 + so02 Console.WriteLine("gia tri cua so01 la : {0}",so01); Console.ReadLine(); } } } Lab 3 – Ki u s using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace lab03 { class Program { static void Main(string[] args) { int so = 10; so++; Console.WriteLine("so : {0}",so++);//viet cw truoc roi + sau Console.WriteLine("so : {0}",++so);//+ truoc roi viet cw sau Console.ReadLine(); } } } Lab 4 - Main using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace lab04 { class Program { Trang 3 Sangit.design.officelive.com – Email: [email protected]
  5. L p trình C# - Căn b n static void Main(string[] args) {// re nhanh chuong trinh : submain se viet truoc , roi toi main // khi khong goi "Submain" thi chuong trinh k chay SubMain(); Console.WriteLine("goi tu Main"); Console.ReadLine(); } static void SubMain() { Console.WriteLine("goi tu SubMain"); } } } Lab 5 - Tính using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace lab05 { class Program { static void Main(string[] args) { PTbac1(); TinhCVDTHinhCN(); TinhCVDTHinhTron(); DoiKgsangPound(); DoiPoundsangKg(); Tinhsogiay(); Console.WriteLine("------- ----"); Console.ReadLine(); } static void TinhCVDTHinhCN() { Console.WriteLine("------- ----"); Console.WriteLine("CHUONG TRINH TINH CHU VI VA DIEN TICH HINH CHU NHAT"); Console.WriteLine("------- ----"); Console.WriteLine("Nhap chieu dai hinh chu nhat"); float chieudai = float.Parse(Console.ReadLine()); Trang 4 Sangit.design.officelive.com – Email: [email protected]
  6. L p trình C# - Căn b n Console.WriteLine("Nhap chieu rong hinh chu nhat"); float chieurong = float.Parse(Console.ReadLine()); Console.WriteLine("Chu vi hinh chu nhat la : {0}\nDien tich hinh chu nhat la : {1}", (chieudai + chieurong) * 2, chieurong * chieudai); Console.ReadLine(); } static void PTbac1() { Console.WriteLine("------- --\nCHUONG TRINH TINH PHUONG TRINH BAC NHAT\n----- ----"); Console.WriteLine("Phuong trinh ax + b = 0 "); Console.WriteLine("Nhap he so a: "); float a = float.Parse(Console.ReadLine()); Console.WriteLine("Nhap he so b: "); float b = float.Parse(Console.ReadLine()); Console.WriteLine("Nghiem x cua phuong trinh la: "); Console.WriteLine("x:{0}", -b / a); Console.ReadLine(); } static void TinhCVDTHinhTron() { Console.WriteLine("------- --\nCHUONG TRINH TINH CHU VI VA DIEN TICH HINH TRON\n- ----"); Console.WriteLine("nhap ban kinh duong tron"); float bankinh = float.Parse(Console.ReadLine()); const float pi = 3.1416f; Console.WriteLine("Chu vi hinh tron la :{0}", bankinh * 2 * pi); Console.WriteLine("Dien tich hinh tron la : {0}", bankinh * bankinh * pi); Console.ReadLine(); } static void DoiKgsangPound() { Console.WriteLine("------- --\nCHUONG TRINH DOI KG SANG POUND \n----- ----"); Console.WriteLine("Nhap so kg can doi sang pound"); float kg = float.Parse(Console.ReadLine()); const float hesokg = 2.2046f; Console.WriteLine("So pound se la : {0} P", kg * hesokg); Console.ReadLine(); } static void DoiPoundsangKg() { Console.WriteLine("====================================”); Console.WriteLine(“ CHUONG TRINH DOI POUND SANG KG “); Console.WriteLine("====================================”); Console.WriteLine("Nhap so pound can doi sang kg"); float pound = float.Parse(Console.ReadLine()); Trang 5 Sangit.design.officelive.com – Email: [email protected]
  7. L p trình C# - Căn b n const float hesopound = 0.4535f; Console.WriteLine("So kg la : {0}kg", pound * hesopound); Console.ReadLine(); } static void Tinhsogiay() { Console.WriteLine(“====================================”); Console.WriteLine(“ CHUONG TRINH TINH SO GIAY ”); Console.WriteLine(“====================================”); Console.WriteLine("Nhap vao so gio"); float sogio = float.Parse(Console.ReadLine()); Console.WriteLine("Nhap vao so phut"); float sophut = float.Parse(Console.ReadLine()); Console.WriteLine("Nhap vao so giay"); float sogiay = float.Parse(Console.ReadLine()); Console.WriteLine("So giay la : {0}giay", (sogio * 3600) + (sophut * 60) + sogiay); Console.ReadLine(); } } } Trang 6 Sangit.design.officelive.com – Email: [email protected]
  8. L p trình C# - Căn b n Lab6 – T ng hai s using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Lab6 { class Program { static void Main(string[] args) { //int so01 = 7; Console.Write("Nhap so thu 1: "); int so01 = int.Parse(Console.ReadLine()); //int so02 = 3; Console.Write("Nhap so thu 2: "); int so02 = int.Parse(Console.ReadLine()); Console.WriteLine("Tong hai so {0} va {1} la {2}", so01, so02, so01 + so02); Console.ReadLine(); } } } Lab 7 – ð i Inch using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Lab7 { class Program { static void Main(string[] args) { Console.Write("Viet so inch can doi:"); float soinch = float.Parse(Console.ReadLine()); const float hesocm = 2.54f; Console.WriteLine(" {0}inch = {1}cm", soinch,soinch* hesocm); Console.ReadLine(); } } } Trang 7 Sangit.design.officelive.com – Email: [email protected]
  9. L p trình C# - Căn b n KI U LOGIC Khai báo: Bool VD: Bool b = true; 1. Ph ñ nh: ký hi u ! Ví d : bool b – true; b = !b ;→ không ph ñ nh Ghi chú: 2 l n ph ñ nh → khôngph ñ nh 2. Phép And (&&) – Or (||): A B A&B A or B ð ð ð ð ð S S S ð S S S S S S S 3. S k t h p gi a phép ph ñ nh và And, Or: Bool a =true; Bool b = false; a = a && !b; → b: false !b: true a && !b = true && true → true Quan sát b ng sau: A B A&B A or B !(A & B) !(A or B) !A !B !A or !B !A & !B ð ð ð ð S S S S S S ð ð ð ð ð S S S S S ð ð ð ð ð S S S S S ð ð ð ð ð ð ð S S S Ta có công th c: !(A and B) = !A or !B !(A or B) = !A and !B !A and B = !(A or !B) A or !B = !(!A) or !B = !(!A and B) !(A and !B) = !A or B Trang 8 Sangit.design.officelive.com – Email: [email protected]
  10. L p trình C# - Căn b n 4. Phép so sánh trong ki u s : - Phép so sánh hay ñ i lư ng so sánh trong ki u s là 1 phép toán 2 ngôi mã k t qu là ki u bool. VD: 5 > 7 tr ra giá tr False - Có 6 phép so sánh: >, =, hay không b ng) 5. Phép ph ñ nh trong phép so sánh Ta có: !> là
  11. L p trình C# - Căn b n bool a = true; bool b = true; Console.WriteLine("A\tB\tA and B\t\tA or B"); Console.WriteLine("{0}\t{1}\t{2}\t\t{3}", a, b, a && b, a || b); b = false; Console.WriteLine("{0}\t{1}\t{2}\t\t{3}", a, b, a && b, a || b); a = false; b = true; Console.WriteLine("{0}\t{1}\t{2}\t\t{3}", a, b, a && b, a || b); b = false; Console.WriteLine("{0}\t{1}\t{2}\t\t{3}", a, b, a && b, a || b); Console.ReadLine(); } } } Lab 10 – Bool1 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BaiLab3 { class Program { static void Main(string[] args) { int so01 = 25; int so02 = 12; bool a = so01 > so02; bool b = so01
  12. L p trình C# - Căn b n Lab 11 – Bool 2 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BaiLab4 { class Program { static void Main(string[] args) { Console.WriteLine("Nhap vao so a:"); int a = int.Parse(Console.ReadLine()); Console.WriteLine("Nhap vao so b:"); int b = int.Parse(Console.ReadLine()); string s = (a >= b) ? "a lon hon hay bang b" : "a nho hon b"; Console.WriteLine(s); Console.ReadLine(); } } } Lab 12 - CMND Nh p vào 1 tu i. Hãy in ra màn hình tu i ñó có h p l ñ làm CMND hay không? Bi t r ng tu i làm CMND ph i t 15 tu i tr lên? using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Lab12 { class Program { static void Main(string[] args) { TuoihoplelamCMND(); Cachkhac(); Console.WriteLine("\nChi tiet: [email protected]\n"); Console.ReadLine(); } static void TuoihoplelamCMND() { Console.Write(" Xin cho biet tuoi cua ban: "); Trang 11 Sangit.design.officelive.com – Email: [email protected]
  13. L p trình C# - Căn b n int tuoi = int.Parse(Console.ReadLine()); string Kq = tuoi >= 15 ? "Tuoi hop le de lam CMND" : "Khong du tuoi lam CMND"; Console.WriteLine(Kq); Console.ReadLine(); } static void Cachkhac() { Console.Write(" Xin cho biet tuoi cua ban: "); int tuoi = int.Parse(Console.ReadLine()); //string Kq = tuoi >= 15 ? "Tuoi hop le de lam CMND" : "Khong du tuoi lam CMND"; //Console.WriteLine(Kq); Console.WriteLine("Ban {0} tuoi, ban {1} du tuoi lam CMND ", tuoi, tuoi >= 15?"":"khong "); Console.ReadLine(); } } } Lab13 – So sánh Nh p vào 2 s a, b. Hãy cho bi t a > b, a = b hay a < b using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Lab14 { Trang 12 Sangit.design.officelive.com – Email: [email protected]
  14. L p trình C# - Căn b n class Program { static void Main(string[] args) { Console.Write("Nhap vao so a: "); int a = int.Parse(Console.ReadLine()); Console.Write("Nhap vao so b: "); int b = int.Parse(Console.ReadLine()); string kq = a >= b ? a > b ? " a > b" : "a = b" : "a < b"; Console.WriteLine(kq); Console.ReadLine(); } } } Lab 14 – Tr giá tr chu i using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Bai14 { class Program { static void Main(string[] args) { Console.Write("Nhap vao 1 chuoi: "); string s = Console.ReadLine(); int i = s.Length; //Length tra ra chieu dai chuoi s Console.WriteLine("Chuoi co chieu dai la: {0} ky tu", i); s = s.ToUpper(); //Ham ToUpper tra ra chu Hoa Console.WriteLine(s); s = s.ToLower(); //Ham ToLowr tra ra chu Thuong Console.WriteLine(s); Console.ReadLine(); } } } Lab 15 – C t, b chu i using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Bai8 Trang 13 Sangit.design.officelive.com – Email: [email protected]
  15. L p trình C# - Căn b n { class Program { static void Main(string[] args) { Console.Write("Nhap vao chuoi s (co chieu dai > 2 ky tu): "); string s = Console.ReadLine(); s = s.Insert(2, "abcdef"); //chen chuoi " abcdef" vao chuoi s tai sau vi tri 2 Console.WriteLine("Chuoi sau khi duoc them vo: {0}", s); s = s.Remove(3, 5); //Xoa chuoi trong s tu vi tri thu 3 va co chieu dai 5 ky tu Console.WriteLine("Chuoi sau khi bi xoa: {0}", s); string sleft = s.PadLeft(30, '.'); //Them '.' vao ben trai cho du 30 ky tu Console.WriteLine(sleft); string sright = s.PadRight(30, '.'); //Them '.' vao ben phai cho du 30 ky tu Console.WriteLine(sright); string s1 = " Nguyen Van Ba "; s1 = s1.Trim(); //Trim la ham cat bo nhung khoang trang ben trai va ben phai cua chuoi Console.WriteLine(s1); Console.ReadLine(); } } } * Bài t p 1: Cho a, b, c là ba s b t kỳ. Hãy b d u ngo c c a các phép so sánh sau: 1. !(a c)) !( (a > b) and (b b and b < c 5. !((a>=b) or (c==b)) (a < b) or c != b * Bài t p 2: Vi t chương trình xu t b ng 10 c t v i các trư ng h p True/False c a A, B. * Bài t p 3: 3.1. Nh p vào 1 s nguyên n. Xu t ra s ñó là s âm hay dương. 3.2. Nh p vào 1 s nguyên n. Xu t ra s ñó là s ch n hay l . 3.3. Tu i lao ñ ng ñư c quy ñ nh là t 18 – 65 ñ i v i Nam. Hãy nh p vào tu i, thông báo tu i ñó có n m trong ñ tu i lao ñ ng hay không? 3.4. Vi t công th c nh p vào H Tên, ñi m Toán, Lý, Hóa. Hãy xu t ra tên h c sinh ñư c in Hoa và ñi m trung bình 1 (DTB1) = (ði m T + L + H) / 3. Tính ñi m trung bình 2 (DTB2) = (ñi m T*2 + L + H) / 4 3.5. Vi t công th c nh p vào b c lương, ngày công, ph c p. Tính ti n lãnh = b c lương * Ngày công + Ph c p. Xu t ra màn hình Trang 14 Sangit.design.officelive.com – Email: [email protected]
  16. L p trình C# - Căn b n Tham Kh o Các thao tác Chu i trong C# I / Gi i thi u : - Chu i trong C# là m t ki u d ng s n như int , long ... có ñ y ñ tính ch t m m d o , m nh m và d dùng - ð khai báo m t ñ i tư ng chu i ta s d ng t khóa string 1 . T o chu i m i : - Cú pháp : string = " Khai báo và gán m t chu i " 2 . Phương th c ToString () : - Dùng ñ chuy n ñ i m t ñ i tư ng b t kỳ sang ki u chu i Ví d : int n = 5 ; string s = n.ToString(); Bây gi chu i s s có giá tr là 5 . B ng cách này ta cũng có th t o ra m t chu i m i . II / Các thao tác chu i : 1 . Các hàm thành viên : : Bi n thành viên tĩnh ñ i di n cho m t chu i r ng - Empty : Phương th c tĩnh dùng so sánh hai chu i - Compare( ) : So sánh hai chu i không quan tâm ñ n ngôn ng - Compare Ordinal ( ) - Concat ( ) : T o chu i m i t nhi u chu i - Copy ( ) : T o m t b n sao - Equals ( ) : So sánh hai chu i có gi ng nhau - Join ( ) : Ghép n i nhi u chu i - Chars ( ) : Indexer c a chu i - Length ( ) : Chi u dài chu i - Insert( ) : Chèn m t chu i khác vào chu i - SubString ( ) : L y m t chu i con : T o chu i ch thư ng - ToLower ( ) - ToUpper ( ) : T o chu i ch hoa : C t b kho ng tr ng hai ñ u chu i - Trim ( ) VD1: Ghép chu i : PHP Code: string a = " Xin "; string b = " Chào "; string c = a + " " + b ; // Ket qua Xin Chào VD 2 : L y ký t PHP Code: string = " VIETPRO " ; char c = s [ 1 ] ; // k t qu là V VD3 : L y chu i con : PHP Code: string s = " VIETPRO ".Substring(4); // k t qu s = "VIET" string s = " VIETPRO ".Substring(4,3) ; // k t qu s = "PRO" VD 4: Thay th chu i con PHP Code: string s = " vietpro ".Replace("v","V") ; // K t qu s = "Vietpro" 1. Các c u trúc ñi u khi n: C# cung c p hai c u trúc ñi u khi n th c hi n vi c l a ch n ñi u ki n th c thi chương trình ñó là c u trúc if và switch...case Trang 15 Sangit.design.officelive.com – Email: [email protected]
  17. L p trình C# - Căn b n * C u trúc if…else: ðư c mô t như sau: if (bi u th c ñi u ki n) { // câu l nh th c thi n u bi u th c ñi u ki n ñúng } [else { // câu l nh th c thi n u bi u th c ñi u ki n sai }] Ví d : if (20 % 4 > 0) { Console.WriteLine("S 20 không chia h t cho 4"); } else { Console.WriteLine("S 20 chia h t cho s 4"); } C u trúc switch … case // switch ... case switch (Bi n ñi u ki n) { case giá tr 1: Câu l nh th c thi break; case giá tr 2: Câu l nh th c thi break; case giá tr 3: Câu l nh th c thi break; case giá tr n: Câu l nh th c thi default: Câu l nh th c thi break; } Trang 16 Sangit.design.officelive.com – Email: [email protected]
  18. L p trình C# - Căn b n Ví d : int x = 20 % 4; switch (x) { case 1: Console.WriteLine("20 chia cho 4 ñư c s dư là 1"); break; case 0: Console.WriteLine("20 chia h t cho 4"); break; default: Console.WriteLine("Không thu c t t c các trư ng h p trên"); break; } 2. C u trúc vòng l p trong l p trình C# C# cung c p các c u trúc vòng l p chương trình  While  Do… while  For  Foreach Sau ñây, tôi xin gi i thi u công th c và ví d s d ng các vòn l p trên Vòng l p While C u trúc vòng l p while while (bi u th c ñi u ki n) { // câu l nh } Th c thi câu l nh ho c m t lo t nh ng câu l nh ñ n khi ñi u ki n không ñư c th a mãn. Trang 17 Sangit.design.officelive.com – Email: [email protected]
  19. L p trình C# - Căn b n Ví d : using System; class WhileTest { public static void Main() { int n = 1; while (n < > { Console.WriteLine("Current value of n is {0}", n); n++; } } } Vòng l p do C u trúc vòng l p while do { // câu l nh } While (bi u th c ñi u ki n) Th c thi câu l nh ít nh t m t l n ñ n khi ñi u ki n không ñư c th a mãn. Trang 18 Sangit.design.officelive.com – Email: [email protected]
  20. L p trình C# - Căn b n Ví d : using System; public class TestDoWhile { public static void Main () { int x; int y = 0; do { x = y++; Console.WriteLine(x); } while(y } } Vòng l p for C u trúc vòng l p for for ([ ph n kh i t o] ; [bi u th c ñi u ki n]; [bư c l p] ) { // th c thi câu l nh } Ví d : using System; public class ForLoopTest { public static void Main() { for (int i = 1; i

Bài tập lập trình c sharp cơ bản năm 2024