Kỹ thuật lập trình - Làm quen với ngôn ngữ lập trình C#

Tiếp cận bài toán-vấn đề

Thiết kế giải thuật và cấu trúc dữ liệu

Viết chương trình (dựa trên 1 NNLT)

Nhập chương trình

Biên dịch/thông dịch, thi hành,gỡ rối

Hoàn chỉnh

 

ppt36 trang | Chia sẻ: Mr Hưng | Lượt xem: 747 | Lượt tải: 0download
Bạn đang xem trước 20 trang nội dung tài liệu Kỹ thuật lập trình - Làm quen với ngôn ngữ lập trình C#, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
LÀM QUEN VỚI NGÔN NGỮ LẬP TRÌNH C#8/28/2016Các bước tổng quátTiếp cận bài toán-vấn đềThiết kế giải thuật và cấu trúc dữ liệuViết chương trình (dựa trên 1 NNLT)Nhập chương trìnhBiên dịch/thông dịch, thi hành,gỡ rốiHoàn chỉnhDateƯu điểm của C#Các ưu điểm này đứng trên quan điểm 1 người sử dụng NNLT như 1 công cụ thể hiện giải thuật-Gần gủi với các NNLT thông dụng (Pascal,C/C++,Java)-“3 in 1 “ : C++ / C# và Java-Cải tiến các khuyết điểm của C/C++ :con trỏ, các hiệu ứng phụ,-“Copy” các ưu điểm từ các ngôn ngữ lập trình khác : dọn rác, fall-through, kiểu dữ liệu string-bool,.- Và 1 số ưu điểm khácDateMột số ví dụ làm quen với C#DateCấu trúc 1 chương trình C# đơn giảnusing class { public static void Main ( ) { ; } }DateVí dụ 1:/* Ghi chú : Chương trình in ra dòng chữ Hello,my friends*/class Vidu1_1{ static public void Main() { System.Console.WriteLine("Hello,my friends"); } // chấm dứt khai báo hàm} // chấm dứt khai báo lớpDateNên trùng tênDateDateDateDateDateMột số đề nghịNên lưu ý và đặt tên phần mở rộng của tập tin là cs (mặc dù phần mở rộng khác cs cũng được. Ví dụ nếu tập tin là Test.txt thì dòng lệnh biên dịch là csc Test.txt)Do có khả năng xảy ra lỗi cú pháp trong lúc viết chương trìnhMở cùng lúc 2 cửa sổ cmd và Notepad cùng lúcDateDateTheo dõi kỹ các thông báo lỗiDateBài tập tại lớpViết chương trình in ra màn hình 3 dòngDòng 1 : Họ tên của anh chịDòng 2 : Địa chỉ cư ngụDòng 3 : Số điện thọaiDateVí dụ 2 :Ký tự đặc biệt, phát biểu returnusing System;//Lam quen voi cac ky tu dac biet, Write va WriteLine//Lam quen returnclass Vidu1_2{static public void Main(){Console.Write("Hello,\t everyone\n"); // \n là xuống hàngConsole.WriteLine("How are you ?"); Console.WriteLine("\tSee\tYou\tSoon"); // \t là ký tự Tabreturn;}}DateVí dụ 3 : Làm quen màu sắcusing System;class Vidu1_3{static public void Main(){Console.ForegroundColor = ConsoleColor.DarkRed;Console.Write("What is your name ");String name=Console.ReadLine();Console.ForegroundColor=ConsoleColor.Green;Console.WriteLine("Hello, "+name);return;} }DateNhận xétThay đổi màu chữConsole.ForegroundColor = ConsoleColor.;Có thể ghép chuỗi khi in ra bằng Write hay WriteLinename : biến  vùng thông tin chứa dữ liệu trung gianDateDateDateDateDateDateDateDateVí dụ 4 : Di chuyển cursorusing System;class Vidu1_4{static public void Main(){Console.Clear();Console.ForegroundColor=ConsoleColor.Green;Console.SetCursorPosition(10,2);Console.Write("What is your name ");String name=Console.ReadLine();Console.SetCursorPosition(10,4);Console.WriteLine("Hello ,"+name);return;} }DateVí dụ 5:kiểu int, chuyển chuỗi sang sốusing System;class Vidu1_5 {static public void Main() {Console.Clear();Console.ForegroundColor=ConsoleColor.Green;Console.SetCursorPosition(10,2);Console.Write("What is your name ");String name=Console.ReadLine();Console.SetCursorPosition(10,4);Console.Write("When were you born , "+name+" : ");String sborn=Console.ReadLine();int born =Int32.Parse(sborn);int age = 2007 - born ; Console.SetCursorPosition(10,5);Console.WriteLine(name +" , you're " + age + "years old");return; } }DateVí dụ 6: “Hằng” , “biến”using System;class Vidu1_6{static public void Main() {const int thisyear=2007;int age=0,born=0;String name ="";Console.Clear();Console.ForegroundColor=ConsoleColor.Green;Console.SetCursorPosition(10,2);Console.Write("What is your name ");Datename=Console.ReadLine();Console.SetCursorPosition(10,4);Console.Write("When were you born , "+name + " : ");String sborn=Console.ReadLine();born =Int32.Parse(sborn);age = thisyear - born ; Console.SetCursorPosition(10,5);Console.WriteLine(name +" , you're " + age + "years old");return; } }DateTham số trên dòng lệnhDateVí dụ 7:Tham số dòng lệnhusing System;class Vidu1_7 {static public int Main(String[ ] danhsachthamso){const int thisyear=2007;int age=0,born=0;String name ="";Console.Clear();if (danhsachthamso.Length != 2 ) { Console.WriteLine("Cu phap su dung : VIDU1_7 "); return 1; }Datename=danhsachthamso[0];born =Int32.Parse(danhsachthamso[1]);age = thisyear - born ; Console.SetCursorPosition(10,5);Console.WriteLine(name +" , you're " + age + " years old");return 0;}}Datedanhsachthamso3.5117.5601DateVí dụ 8: Tham số dòng lệnhusing System;class Vidu1_7 {static public void Main(String[] args) {int count = args.Length;if (count !=2 ) return;float num1 = Single.Parse(args[0]);float num2 = Single.Parse(args[1]);Console.WriteLine(num1 + " + " + num2 + " = " + (num1+num2) );Console.WriteLine(num1 + " - " + num2 + " = " + (num1-num2) );Console.WriteLine(num1 + " x " + num2 + " = " + (num1*num2) );Console.WriteLine(num1 + " / " + num2 + " = " + (num1/num2) ); } }DateVí dụ 9: Phát âm thanhusing System;public class Vidu1_9 {public static void Main() { int time=500, f=500; for (int i=0;i<30;i++) { Console.Beep(f,time); f = f+50; } Console.Beep( ); }}DateMột số bài tập đơn giảnViết chương trình nhập bán kính hình tròn, sau đó in ra chu vi và diện tích hình trònViết chương trình nhập cạnh 1 hình vuông, sau đó in ra bán kính của hình tròn có diện tích bằng diện tích của hình vuôngDate

Các file đính kèm theo tài liệu này:

  • pptcosolaptrinh_csharp_bai1_4671.ppt
Tài liệu liên quan