0% found this document useful (0 votes)
48 views4 pages

Bài Tập Programming

The document provides examples of using built-in .NET classes and defining simple classes in C#. It includes examples of using DateTime to display dates and calculate date differences, randomizing an array of words, and calculating factorials. It also defines Song and Student classes with properties and demonstrates filtering lists of objects based on property values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views4 pages

Bài Tập Programming

The document provides examples of using built-in .NET classes and defining simple classes in C#. It includes examples of using DateTime to display dates and calculate date differences, randomizing an array of words, and calculating factorials. It also defines Song and Student classes with properties and demonstrates filtering lists of objects based on property values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

BÀI TẬP PROGRAMMING

1.Using the built-in.net classes


a. Day of week
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace School3_8
{
internal class Program
{
static void Main(string[] args)
{
DateTime myBirthday = new DateTime(2003, 9, 23);
Console.WriteLine($"{myBirthday: dd/MM/yyyy}");
DateTime mySonDate = new DateTime(1965, 6, 24);
Console.WriteLine(myFatherDate);
DateTime newmySonDate = mySonDate.AddDays(100);
Console.WriteLine(newmySonDate);
Console.ReadLine();
}
}
}
b. Ramdomise Words
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
string[] words = Console.ReadLine().Split(' ');
Random rnd = new Random();
for (int pos1 = 0; pos1 < words.Length; pos1++)
{
int pos2 = rnd.Next(words.Length);
}
Console.WriteLine(string.Join(Environment.NewLine, words));
}
}
}
c.Big Factorial
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
internal class Program
{
static void Main(string[] args)
{
int i, f = 1, n;
Console.Write("\n");
Console.Write("Tim giai thua: \n");
Console.Write("------------------------");
Console.Write("\n\n");
Console.Write("Nhap mot so bat ky: ");
n = Convert.ToInt32(Console.ReadLine());
for (i = 2; i <= n; i++)
f = f * i;
Console.Write("Giai thua cua {0} la: {1}\n", n, f);
Console.ReadKey();
}
}
}
2. Defining simple classes
a. Songs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Songs
{
class Song
{
public string TypeList { get; set; }
public string Name { get; set; }
public string Time { get; set; }
}
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the number of song");
int n = int.Parse(Console.ReadLine());
List<Song> songs = new List<Song>();
for (int i = 0; i < n; i++)
{
string[] data = Console.ReadLine().Split('_');
string type = data[0];
string name = data[1];
string time = data[2];
Song song = new Song();
song.TypeList = type;
song.Name = name;
song.Time = time;
songs.Add(song);
}
Console.WriteLine("Enter the type list: ");
string typeList = Console.ReadLine();
if (typeList == "ALL")
{
foreach (Song song in songs)
{
Console.WriteLine(song.Name);
}
}
else
{
foreach (Song song in songs)
{
if (song.TypeList == typeList)
{
Console.WriteLine(song.Name);
}
}
}
List<Song> filleredSongs = songs.Where(s => s.TypeList == typeList).ToList();
foreach (Song song in filleredSongs)
{
Console.Write(song.Name);
}
}
}
}
b. Students
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Students
{
class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string City { get; set; }
}
public class Program
{
static void Main()
{
List<Student> students = new List<Student>();
Console.WriteLine("Enter list of student: ");
string line = Console.ReadLine();
while (line != "end")
{
string[] tokens = line.Split();
string firstName = tokens[0];
string lastName = tokens[1];
int age = int.Parse(tokens[2]);
string city = tokens[3];
Student student = new Student()
{
FirstName = firstName,
LastName = lastName,
Age = age,
City = city
};
students.Add(student);
line = Console.ReadLine();
String filerCity = Console.ReadLine();
foreach (Student Student in students)
{
if (student.City == filerCity)
{
Console.WriteLine($"{student.FirstName} {student.LastName} is {student.Age}years
old.");
}
List<Student> filteredStudents = students.Where(x => x.City == filerCity).ToList();
foreach (Student Students in filteredStudents)
{
Console.WriteLine($"{student.FirstName} {student.LastName} is {student.Age} years
old.");
}
}
}
}
}
}

You might also like