You are on page 1of 6

LẤY 4 ĐIỂM

using System;
using System.Threading;

class Program
{
private static string s = ""; // Biến toàn cục để lưu chuỗi
private static bool isRunning = true; // Biến để kiểm tra xem chương trình có đang chạy hay không

static void Main(string[] args)


{
// Tạo luồng để đọc giá trị của biến s và hiển thị lên màn hình
Thread readerThread = new Thread(ReadAndDisplayString);
readerThread.Start();

// Tạo luồng để nhập chuỗi từ bàn phím và lưu vào biến s


Thread inputThread = new Thread(ReadInput);
inputThread.Start();

// Chờ cả hai luồng kết thúc trước khi thoát chương trình
inputThread.Join();
readerThread.Join();
}

static void ReadInput()


{
while (isRunning)
{
string input = Console.ReadLine();
if (input == "#" || input.ToLower() == "quit")
{
isRunning = false; // Đặt biến kiểm tra để dừng cả hai luồng
}
else
{
// Gán giá trị chuỗi vào biến toàn cục s
s = input;
}
}
}

static void ReadAndDisplayString()


{
while (isRunning)
{
if (!string.IsNullOrEmpty(s))
{
Console.WriteLine("Giá trị của biến s: " + s);
s = ""; // Đặt lại giá trị của biến s sau khi hiển thị
}
}
}
}
KIỂM TRA SỐ NGUYÊN TỐ
using System;
using System.Threading;

class Program
{
static int c = 1;
static bool check = false;
static void Main(string[] args)
{
// Bắt đầu hai luồng song song
Thread thread1 = new Thread(Function1);
Thread thread2 = new Thread(Function2);

thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
}

static void Function1()


{
while (true)
{
Console.WriteLine("Nhap mot so(0 la de thoat):");
int input = int.Parse(Console.ReadLine());
c = input;
check = true;
if (input == 0)
break;
Thread.Sleep(5); // Đợi 5ms
}
}

static void Function2()


{
do
{
if (check)
{
Console.WriteLine($"Gia tri cua bien c: {c}");
if (IsPrime(c))
{
Console.WriteLine($"{c} la so nguyen to.");
}
else
{
Console.WriteLine($"{c} khong phai la so nguyen to.");
}

check = false;
}
} while(c != 0);
Thread.Sleep(10); // Đợi 10ms
}

static bool IsPrime(int n)


{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;

for (int i = 5; i * i <= n; i += 6)


{
if (n % i == 0 || n % (i + 2) == 0)
return false;
}

return true;
}
}
ĐẢO NGƯỢC CHUỖI ĐƯỢC NHẬP
using System;
using System.Threading;

class Program
{
static string inputString = string.Empty;

static void Main(string[] args)


{
// Bắt đầu hai luồng song song
Thread thread1 = new Thread(ReadInput);
Thread thread2 = new Thread(ReverseString);

thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
}

static void ReadInput()


{
while (true)
{
Console.WriteLine("Nhap mot chuoi (nhap 'thoat' de thoat):");
string input = Console.ReadLine();
if (input.ToLower() == "thoat")
break;
inputString = input;
Thread.Sleep(5); // Đợi 5ms
}
}

static void ReverseString()


{
do
{
if (!string.IsNullOrEmpty(inputString))
{
string reversedString = Reverse(inputString);
Console.WriteLine($"Chuoi dao nguoc: {reversedString}");
inputString = string.Empty;
}
} while (true);
Thread.Sleep(10); // Đợi 10ms
}

static string Reverse(string str)


{
char[] charArray = str.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}
ĐẾM TỪ
using System;
using System.Threading;

class Program
{
static string inputString = string.Empty;

static void Main(string[] args)


{
// Bắt đầu hai luồng song song
Thread thread1 = new Thread(ReadInput);
Thread thread2 = new Thread(CountWords);

thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
}

static void ReadInput()


{
while (true)
{
Console.WriteLine("Nhap mot chuoi (nhap 'thoat' de thoat):");
string input = Console.ReadLine();
if (input.ToLower() == "thoat")
break;
inputString = input;
Thread.Sleep(5); // Đợi 5ms
}
}

static void CountWords()


{
do
{
if (!string.IsNullOrEmpty(inputString))
{
int wordCount = CountWordsInString(inputString);
Console.WriteLine($"So tu trong chuoi: {wordCount}");
inputString = string.Empty;
}
} while (true);
Thread.Sleep(10); // Đợi 10ms
}
static int CountWordsInString(string str)
{
string[] words = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
return words.Length;
}
}
TÌM N!
using System;
using System.Threading;

class Program
{
static int n = -1;
static long result = -1;

static void Main(string[] args)


{
// Bắt đầu hai luồng song song
Thread thread1 = new Thread(ReadInput);
Thread thread2 = new Thread(CalculateFactorial);

thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
}

static void ReadInput()


{
while (n < 0 || n > 20)
{
Console.WriteLine("Nhập một số nguyên n (0 <= n <= 20):");
if (int.TryParse(Console.ReadLine(), out n) && n >= 0 && n <= 20)
{
break;
}
Console.WriteLine("Số không hợp lệ. Hãy nhập lại.");
}
}

static void CalculateFactorial()


{
while (n == -1)
{
// Chờ cho đến khi n được cung cấp bởi người dùng
Thread.Sleep(100);
}

result = CalculateFactorialRecursive(n);
Console.WriteLine($"{n}! = {result}");
}

static long CalculateFactorialRecursive(int num)


{
if (num == 0)
{
return 1;
}
return num * CalculateFactorialRecursive(num - 1);
}
}

You might also like