Vinh La Kiến

48 bundles
2 files5 months ago
2

Day 2 - C#

Untitled

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp2 { class Program { static int OpenFile (string filePath) { if (filePath == null) { throw new Exception("File path cannot be null."); } else if (filePath == "") { throw new Exception("File path cannot be empty."); } return 0; } static void Main(string[] args) { int errCode = 0; try { errCode = OpenFile(null); } catch(Exception e) { Console.WriteLine(e.ToString()); } int number = -6; // 3.1. if (number < 0) number = -number; // number = Math.Abs(number); // 3.2. bool isOdd; if (number % 2 == 0) isOdd = true; else isOdd = false; // bool isOdd = number % 2 == 0; // 3.3. int SECOND_PER_DAY = 24 * 60 * 60; int seconds = 3545; bool isValid = false; if (seconds >= 0 && seconds <= SECOND_PER_DAY) { isValid = true; } // bool isValid = seconds >= 0 && seconds <= SECOND_PER_DAY; // a / b ; a % b int hours = seconds / 3600; int minutes = (seconds % 3600) / 60; int secs = seconds % 3600 % 60; // int minutes = (seconds - hours * 3600) / 60; // int secs = seconds - hours * 3600 - minutes * 60; // 3.4. int d = 29; int m = 2; int y = 2020; bool isLeapYear = (y % 400 == 0) || (y % 4 == 0 && y % 100 != 0); int maxDay = 0; if (m == 1 || m == 3 || m == 7 || m == 8 || m == 10 || m == 12) { maxDay = 31; } else if (m == 4 || m == 6 || m == 9 || m == 11) { maxDay = 30; } else if (m == 2) { maxDay = isLeapYear == true ? 29 : 28; } bool isValidDate = (d >= 1 && d <= maxDay); Console.WriteLine(d + "/" + m + "/" + y + ": " + (isValidDate == true ? "is valid" : "is not valid") ); int n = 3; int S = 0; for (int i = 1; i <= n; i++) { S += i; // S = S + i; } int N = 1; bool isPrime = N > 1; int testNumber = 2; for (int i = 2; i < testNumber; i++) { if (N % i == 0) { isPrime = false; break; // i = N; } } int N1 = 5; for (int r = 0; r < N1; r++) { for (int c = 0; c < N1; c++) { int value = c + (r + 1); if (value > N1) value -= N1; Console.Write(value); } Console.Write("\t"); for (int c = 0; c < N1; c++) { int value = c + r; if (value > N1) value -= N1; char character = (char)((int)'A' + value); Console.Write(character); } Console.Write("\n"); } int[] a = { 1, 1, 1, 1, 1 }; int myS = sumOfArray(a); while (true) ; } public static int FindMinIndexInArray(int[] arr) { int minIndex = 0; for (int i = 0; i < arr.Length; i++) { if (arr[minIndex] > arr[i]) { minIndex = i; } } return minIndex; } public static int sumOfArray(int[] arr) { int S = 0; for (int i = 0; i < arr.Length; i++) { S += arr[i]; } return S; } } }

Delegate

123456789101112131415161718192021222324252627282930313233343536373839404142434445
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace ConsoleApp2 { class Program { public delegate int FooDelegate(int r, float v); public static FooDelegate processDelegate; static void ProcessWeight(string ip, string port) { // Do something connect to weight, get value string raw = ""; int rfid = 9; float value = 5.5F; if (processDelegate != null) processDelegate(rfid, value); } ///////////////////////////// static int ProcessWeightData(int rfid, float value) { // write db return 0; } static void Main(string[] args) { processDelegate = ProcessWeightData; } } }