0% found this document useful (0 votes)
25 views20 pages

Tuan 6 - Lap Trinh CS

bài tâp C# cơ bản chương 6.1
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)
25 views20 pages

Tuan 6 - Lap Trinh CS

bài tâp C# cơ bản chương 6.1
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/ 20

Name: Tào Nguyên Văn

Class: 21SE4 – ID: 21IT118

1.Generics

a)

// Using <> to specify Parameter type

public class MyClass<T>

private T data;

public T Value

get => data;

set => data = value;


}

public override string ToString() => $"Value:{data}";

class Program

static void Main(string[] args)

// Instance of string type

MyClass<string> name = new MyClass<string>() { Value =


"Jack" };

Console.WriteLine(name);

// Instance of float type

MyClass<float> version = new MyClass<float>() { Value = 5.5f };

Console.WriteLine(version);

// Instance of dynamic type

dynamic obj = new { Id = 1, Name = "David" };

MyClass<dynamic> myClass = new MyClass<dynamic>() { Value


= obj };

Console.WriteLine(myClass);

Console.ReadLine();

}
b)

public class MyClass

// Generics method with two types T and U

public void Display<T, U>(T msg, U value)

Console.WriteLine($"{msg}: {value}");

}
class Program

static void Main(string[] args)

// Creating object of MyClass

MyClass obj = new MyClass();

// Calling Generics method

obj.Display<string, int>("Integer", 2050);

obj.Display<double, char>(155.9, 'A');

obj.Display<float, double>(358.9F, 255.67);

Console.ReadLine();

}
c)

// Declare an interface with constraint: struct (Value type)

interface IBasic<T> where T : struct

T Add(T a, T b);

// Implement interface IBasic with int type

class MyFirstClass : IBasic<int>

public int Add(int a, int b) => a + b;

}
// Implement interface IBasic with double type

class MySecondClass : IBasic<double>

public double Add(double a, double b) => a + b;

class Program

static void Main(string[] args)

MyFirstClass firstClass = new MyFirstClass();

dynamic r = firstClass.Add(10, 20);

Console.WriteLine(r);

MySecondClass secondClass = new MySecondClass();

r = secondClass.Add(10.5, 20.5);

Console.WriteLine(r);

Console.ReadLine();

}
2. Collections

a) List < T >

public class Person

public int Age { get; set; }

public string FirstName { get; set; }

public string LastName { get; set; }

public override string ToString() =>

$"Name: {FirstName} {LastName}, Age: {Age}";

class Program
{

static void Main(string[] args)

List<Person> people = new List<Person>()

new Person { FirstName = "David", LastName = "Simpson", Age = 50


},

new Person { FirstName = "Marge", LastName = "Simpson", Age =


45 },

new Person { FirstName = "Lisa", LastName = "Simpson", Age =


19 },

new Person { FirstName = "Jack", LastName = "Simpson", Age = 16 }

};

// Print out # of items in List.

Console.WriteLine("Items in list: {0}", people.Count);

// Enumerate over list.

foreach (Person p in people)

Console.WriteLine(p);

Console.ReadLine();

}
b) SortedSet

using System;

using System.Collections.Generic;

class Program

static void Main(string[] args)

// using collection initializer to initialize SortedSet

SortedSet<int> mySet = new SortedSet<int>() { 8, 7, 9, 1, 3 };

// Add the elements in SortedSet using Add method

mySet.Add(5);

mySet.Add(4);
mySet.Add(6);

mySet.Add(2);

Console.WriteLine("Elements of mySet:\n");

// Accessing elements of SortedSet using foreach loop

foreach (var val in mySet)

Console.Write($"{val,3}");

Console.ReadLine();

}
c) IEnumerable <T>

using System;

using System.Collections;

using System.Collections.Generic;

public class Person

public int Age { get; set; }

public string FirstName { get; set; }

public string LastName { get; set; }

public Person() { }
public override string ToString() => $"Name: {FirstName} {LastName},
Age: {Age}";

public class MyCollection<T> : IEnumerable where T : class, new()

private List<T> myList = new List<T>();

public void AddItem(params T[] item) => myList.AddRange(item);

IEnumerator IEnumerable.GetEnumerator() => myList.GetEnumerator();

class Program

static void Main(string[] args)

MyCollection<Person> collection = new MyCollection<Person>();

var p1 = new Person { FirstName = "David", LastName = "Simpson",


Age = 50 };

var p2 = new Person { FirstName = "Marge", LastName = "Simpson",


Age = 45 };

var p3 = new Person { FirstName = "Lisa", LastName = "Simpson", Age


= 19 };

var p4 = new Person { FirstName = "Jack", LastName = "Simpson", Age


= 16 };
collection.AddItem(p1, p2, p3, p4);

foreach (var p in collection)

Console.WriteLine(p);

3. Problem

a) Gauss' Trick

class Program

{
static List<int> SumPairs(List<int> numbers)

List<int> result = new List<int>();

int i = 0;

int j = numbers.Count - 1;

while (i <= j)

if (i == j)

result.Add(numbers[i]);

else

result.Add(numbers[i] + numbers[j]);

i++;

j--;

return result;

static void Main(string[] args)

// Example 1

List<int> list1 = new List<int> { 1, 2, 3, 4, 5 };


var result1 = SumPairs(list1);

Console.WriteLine(string.Join(" ", result1)); // Output: 6 6 3

// Example 2

List<int> list2 = new List<int> { 1, 2, 3, 4 };

var result2 = SumPairs(list2);

Console.WriteLine(string.Join(" ", result2)); // Output: 5 5

b) Sum Adjacent Equal Numbers


static void Main()

List<int> inputs = new List<int> { 1,2, 2, 4, 6 };

for (int i = 0; i < inputs.Count - 1; i++)

if (inputs[i + 1] == inputs[i])

inputs[i] += inputs[i + 1];

inputs.RemoveAt(i + 1);

i = -1;

for (int i = 0; i < inputs.Count; i++)

Console.Write(inputs[i]+" ");

}
c) Merging Lists

static void Main()

List<int> list1 = new List<int> { 1, 2, 3 };

List<int> list2 = new List<int> { 4, 5, 6, 7, 8 };

List<int> list3 = new List<int> { };

int min = Math.Min(list1.Count, list2.Count);

int max = Math.Max(list1.Count, list2.Count);

int n = list1.Count - list2.Count;


if (n > 0)

for (int i = 0; i < min; i++)

list3.Add(list1[i]);

list3.Add(list2[i]);

for (int i = min; i < max; i++)

list3.Add(list1[i]);

else if (n < 0)

for (int i = 0; i < min; i++)

list3.Add(list1[i]);

list3.Add(list2[i]);

for (int i = min; i < max; i++)


{

list3.Add(list2[i]);

else

for (int i = 0; i < min; i++)

list3.Add(list1[i]);

list3.Add(list2[i]);

for (int i = 0; i < list3.Count; i++)

Console.Write(list3[i] + " ");

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy