Last active
May 27, 2021 07:59
-
-
Save yostane/9b9906b0c820460214400a2802b89276 to your computer and use it in GitHub Desktop.
Exos C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // source: https://www.w3resource.com/csharp-exercises/for-loop/csharp-for-loop-exercise-38.php | |
| int num, r, sum = 0, t; | |
| Console.Write("\n\n"); | |
| Console.Write("Check whether a number is a palindrome or not:\n"); | |
| Console.Write("------------------------------------------------"); | |
| Console.Write("\n\n"); | |
| Console.Write("Input a number: "); | |
| num = 123115; | |
| for (t = num; num != 0; num = num / 10) | |
| { | |
| r = num % 10; | |
| sum = sum * 10 + r; | |
| } | |
| if (t == sum) | |
| { | |
| Console.Write("{0} is a palindrome number.\n", t); | |
| } | |
| else | |
| { | |
| Console.Write("{0} is not a palindrome number.\n", t); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var r = new Random(); | |
| var a = r.Next(50); | |
| var b = r.Next(50); | |
| var c = r.Next(50); | |
| Console.WriteLine($"a => {a}, b => {b}, c => {c}"); | |
| if (a > b) | |
| { | |
| if (b > c) | |
| { | |
| Console.WriteLine($"{c}, {b}, {a}"); | |
| } | |
| else if (a > c) // a > b et c > b | |
| { | |
| Console.WriteLine($"{b}, {c}, {a}"); | |
| } | |
| else | |
| { | |
| Console.WriteLine($"{b}, {a}, {c}"); | |
| } | |
| } | |
| else | |
| { | |
| if (a > c) // b >= a et a > c | |
| { | |
| Console.WriteLine($"{c}, {a}, {b}"); | |
| } | |
| else if (b > c) // b >= a et c >= a | |
| { | |
| Console.WriteLine($"{a}, {c}, {b}"); | |
| } | |
| else // b >= a et c >= a et b <= c | |
| { | |
| Console.WriteLine($"{a}, {b}, {c}"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var r2 = new Random(); | |
| var val1 = r2.Next(10, 20); | |
| Console.WriteLine($"First value {val1}"); | |
| var newValue = 0; | |
| do | |
| { | |
| newValue = r2.Next(10, 20); | |
| Console.WriteLine($"new value: {newValue}"); | |
| } while (newValue != val1); | |
| Console.WriteLine("Fin"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var r3 = new Random(); | |
| var nb = r3.Next(9); | |
| Console.WriteLine($"Table de {nb}:"); | |
| for (int i = 0; i <= 10; i++) | |
| { | |
| Console.WriteLine($"{nb} x {i} = {nb * i}"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| // Dans la suite, la valeur max des nombres aléatoire est de 10 | |
| /*Ecrire un programme qui génère un tableau 2D éparse de 4 lignes. | |
| Pour chaque ligne, le nombre de colonnes est un nombre | |
| aléatoire. | |
| Remplir cette matrice par des nombres aléatoires */ | |
| int[][] t1 = new int[4][]; | |
| var r = new Random(); | |
| for (int i = 0; i < t1.Length; i++) | |
| { | |
| t1[i] = new int[r.Next(10)]; | |
| } | |
| for (int i = 0; i < t1.Length; i++) | |
| { | |
| for (int j = 0; j < t1[i].Length; j++) | |
| { | |
| t1[i][j] = r.Next(1000); | |
| } | |
| } | |
| /*Afficher cette matrice comme sur excel | |
| A B C... | |
| 1| 4 6... | |
| 2| 5 4.. | |
| 3| -2 5... */ | |
| int padding = 6; | |
| Console.Write("l\\c".PadRight(padding)); | |
| for (char i = 'A'; i <= 'Z'; i++) | |
| { | |
| Console.Write(i.ToString().PadRight(padding)); | |
| } | |
| Console.WriteLine(); | |
| for (int i = 0; i < t1.Length; i++) | |
| { | |
| Console.Write($"{i}|".PadRight(padding)); | |
| for (int j = 0; j < t1[i].Length; j++) | |
| { | |
| Console.Write(t1[i][j].ToString().PadRight(padding)); | |
| } | |
| Console.WriteLine(); | |
| } | |
| /*Ecrire un programme qui génère deux tableau | |
| de tailles aléatoires et de contenus aléatoire. | |
| Calcule le schtroumpf des deux tableaux et afficher le détail. | |
| Pour calculer le schtroumpf, | |
| il faut multiplier chaque élément du tableau 1 | |
| par chaque élément du tableau 2, et additionner le tout. | |
| Par exemple si l'on a : [4, 8, 7, 12] et [3, 6], | |
| le programme va afficher: | |
| Le Schtroumpf sera : | |
| 3 x 4 + 3 x 8 + 3 x 7 + 3 x 12 + 6 x 4 + 6 x 8 + 6 x 7 + 6 x 12 = 279 | |
| */ | |
| int[] s1 = new int[r.Next(10)]; | |
| int[] s2 = new int[r.Next(10)]; | |
| for (int i = 0; i < s1.Length; i++) | |
| { | |
| s1[i] = r.Next(10); | |
| } | |
| for (int i = 0; i < s2.Length; i++) | |
| { | |
| s2[i] = r.Next(10); | |
| } | |
| int somme = 0; | |
| for (int i = 0; i < s1.Length; i++) | |
| { | |
| for (int j = 0; j < s2.Length; j++) | |
| { | |
| somme += s1[i] * s2[j]; | |
| Console.Write($"{s1[i]} x {s2[j]} "); | |
| if (!(i + 1 == s1.Length && j + 1 == s2.Length)) | |
| { | |
| Console.Write($"+ "); | |
| } | |
| } | |
| } | |
| Console.WriteLine($" = {somme}"); | |
| Console.WriteLine($"Somme avec LINQ = {s1.SelectMany(x => s2, (x, y) => x * y).Sum()}"); | |
| // ! à faire, ne pas afficher le dernier + | |
| /* | |
| écrivez un programme permettant de générer aléatoirement | |
| 10 notes comprises entre 0 et 20. | |
| Le programme, renvoie le nombre de ces notes supérieures à la moyenne de la classe. | |
| */ | |
| Console.WriteLine("--------- notes > moyennes -------"); | |
| var notes = new int[10]; | |
| int sommeExo1Lot4 = 0; | |
| for (int i = 0; i < notes.Length; i++) | |
| { | |
| notes[i] = r.Next(20); | |
| sommeExo1Lot4 += notes[i]; | |
| Console.WriteLine($"notes[{i}] -> {notes[i]}"); | |
| } | |
| var moyenne = sommeExo1Lot4 / notes.Length; | |
| // var: le type est "fixé" implicitement par le compilateur c# | |
| Console.WriteLine($"Moyenne -> {moyenne}"); | |
| for (int i = 0; i < notes.Length; i++) | |
| { | |
| if (notes[i] > moyenne) | |
| { | |
| Console.WriteLine($"note {i} -> {notes[i]}"); | |
| } | |
| } | |
| List<int> notesSupMoyenne = notes.Where(note => note > notes.Average()).ToList(); | |
| Console.WriteLine($"LINQ = {string.Join(", ", notesSupMoyenne)}"); | |
| Console.WriteLine("--------- FIN notes > moyennes -------"); | |
| /* | |
| Ecrire une fonction isPalindrome(string) qui permet de dire si une chaine de caractères est un palindrome | |
| par exemple: isPalindrome("toto") -> false (-> toto != <- otot) | |
| isPalindrome("totot") -> true | |
| ! Sans utiliser de fonction reverse | |
| */ | |
| Console.WriteLine("--------- Palindrome -------"); | |
| string palInput = "abcd"; | |
| // solution 1 | |
| string palReverse = ""; | |
| // 0 -> length: a, b, c, d | |
| // length - 1 -> -1: d, c, b, a | |
| // parcours inverse 1 | |
| for (int i = palInput.Length; i > 0; i--) | |
| { | |
| Console.Write($"{i}: "); | |
| Console.WriteLine($"palInput[{i - 1}] -> {palInput[i - 1]}"); | |
| } | |
| // parcours inverse 2 | |
| for (int i = palInput.Length - 1; i >= 0; i--) | |
| { | |
| Console.Write($"{i}: "); | |
| Console.WriteLine($"palInput[{i}] -> {palInput[i]}"); | |
| palReverse += palInput[i]; | |
| } | |
| Console.WriteLine($"Reverse -> {palReverse}"); | |
| Console.Write(palInput); | |
| if (palReverse == palInput) | |
| { | |
| Console.WriteLine($" est un palindrome"); | |
| } | |
| else | |
| { | |
| Console.WriteLine($" n'est pas un palindrome"); | |
| } | |
| Console.WriteLine("--------- Palindrome optimal -------"); | |
| string palOptimalInput = "abcba"; | |
| Console.WriteLine(palOptimalInput); | |
| // Le nombre d'itérations correspond au nombres de test minimal | |
| int i = 0; | |
| for (i = 0; i < palOptimalInput.Length / 2; i++) | |
| { | |
| Console.WriteLine($"{i}, {palOptimalInput.Length - i - 1}"); | |
| if (palOptimalInput[i] == palOptimalInput[palOptimalInput.Length - i - 1]) | |
| { | |
| Console.WriteLine($"Kifkif"); | |
| } | |
| else | |
| { | |
| Console.WriteLine($"Pas kifkif"); | |
| break; | |
| } | |
| } | |
| Console.WriteLine($"i=> {i}, palOptimalInput.Length / 2 => {palOptimalInput.Length / 2}"); | |
| Console.WriteLine($"{i >= palOptimalInput.Length / 2}"); | |
| Console.WriteLine("--------- Fin palindrome -------"); | |
| /* | |
| Ecrire une fonction isPalindrome(int) qui permet de dire si une entier est un palindrome | |
| isPalindrome(1234) -> false | |
| isPalindrome(12321) -> true | |
| ! Sans utiliser de fonction reverse | |
| */ | |
| Console.WriteLine("--------- Palindrome int -------"); | |
| int palInt = 1234321; | |
| string palIntAsString = palInt.ToString(); | |
| Console.WriteLine($"pal int facile {palIntAsString == palIntAsString.Reverse().ToString()}"); | |
| Console.WriteLine($"palInt {palInt}"); | |
| Console.WriteLine($"nb chiffres {(int)Math.Log10(palInt) + 1}"); | |
| // Console.WriteLine($"{palInt / 10}"); | |
| // Console.WriteLine($"{palInt / 10 / 10}"); | |
| // Console.WriteLine($"{palInt / 10 / 10 / 10}"); | |
| // Console.WriteLine($"{palInt / 10 / 10 / 10 / 10}"); | |
| // Console.WriteLine($"{palInt / 10 / 10 / 10 / 10 / 10}"); | |
| int GetNbDigits(int input) | |
| { | |
| int k = 0; | |
| for (k = 0; input > 0; k++) | |
| { | |
| input /= 10; | |
| } | |
| return k == 0 ? 1 : k; | |
| } | |
| // récup un chiffre à la pos i à partir de la droite => ('nb' / (int)Math.Pow(10, i)) % 10 | |
| int GetDigitAtIndex(int input, int index) | |
| { | |
| int originalIndex = index; | |
| int lastIndex = GetNbDigits(input) - 1; | |
| index = lastIndex - index; // pour aller de g à d | |
| if (index < 0 || index > lastIndex) | |
| { | |
| throw new IndexOutOfRangeException($"Indice {originalIndex} incorrect!"); | |
| } | |
| int div = input / (int)Math.Pow(10, index); | |
| return div % 10; | |
| } | |
| int nbDigits = GetNbDigits(palInt); | |
| Console.WriteLine($"nb chiffres avec une boucle: {nbDigits}"); | |
| // Le nombre d'itérations correspond au nombres de test minimal | |
| int j = 0; | |
| for (j = 0; j < nbDigits / 2; j++) | |
| { | |
| int rightIndex = nbDigits - j - 1; | |
| Console.WriteLine($"{j}, {rightIndex}"); | |
| if (GetDigitAtIndex(palInt, j) == GetDigitAtIndex(palInt, rightIndex)) | |
| { | |
| Console.WriteLine($"Kifkif"); | |
| } | |
| else | |
| { | |
| Console.WriteLine($"Pas kifkif"); | |
| break; | |
| } | |
| } | |
| Console.WriteLine($"i=> {j}, nbdigits / 2 => {nbDigits / 2}"); | |
| Console.WriteLine($"{j == nbDigits / 2}"); | |
| Console.WriteLine("--------- Fin palindrome int -------"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| void PrintShapeInfo(Shape shape) | |
| { | |
| Console.WriteLine($"print shape {shape}"); | |
| if (shape is Circle circle) | |
| { | |
| Console.WriteLine($"C'est un cercle rond de diametre {circle.Diameter}"); | |
| } | |
| else if (shape is Rectangle r) | |
| { | |
| Console.WriteLine($"C'est un rectangle pas rond {r}"); | |
| } | |
| else | |
| { | |
| Console.WriteLine($"Inconnu"); | |
| } | |
| Console.WriteLine(shape switch | |
| { | |
| Circle c => $"C'est un cercle rond rond {c}", | |
| Rectangle r => $"C'est un rectangle pas rond {r}", | |
| _ => $"Inconnu", | |
| }); | |
| } | |
| List<Shape> GenerateShapesMethod1(int count) | |
| { | |
| List<Shape> shapes = new(); | |
| Random r = new(); | |
| for (int i = 0; i < count; i++) | |
| { | |
| Shape s = r.NextDouble() >= 0.5 ? new Circle(r.Next(5, 20)) : new Rectangle(r.Next(5, 10), r.Next(11, 21)); | |
| shapes.Add(s); | |
| } | |
| return shapes; | |
| } | |
| List<Shape> GenerateShapes(int count) | |
| { | |
| Random r = new(); | |
| return Enumerable.Range(0, count).Select((item) => | |
| { | |
| Shape s = r.NextDouble() >= 0.5 ? new Circle(r.Next(5, 20)) : new Rectangle(r.Next(5, 10), r.Next(11, 21)); | |
| return s; | |
| }).ToList(); | |
| } | |
| var shapes = GenerateShapes(10); | |
| Console.WriteLine($"shapes: {string.Join("\n- ", shapes)}"); | |
| //Linq | |
| var sumArea = shapes.Where((shape) => shape is Circle).Select((c) => c.Area).Sum(); | |
| var sumAreaBis = shapes.Where((shape) => shape is Circle).Sum((c) => c.Area); | |
| //Linq query | |
| var sumAreaQuery = (from s in shapes where s is Circle select s.Area).Sum(); | |
| var sumAreaTris = shapes.Sum((c) => c is Circle ? c.Area : 0); | |
| var sumArea2 = shapes.OfType<Circle>().Aggregate(0.0, (acc, currentCircle) => acc + currentCircle.Area); | |
| var maxWidth = shapes.OfType<Rectangle>().Select((r) => r.Width).Max(); | |
| var maxWidth2 = shapes.OfType<Rectangle>().Max((r) => r.Width); | |
| Console.WriteLine($"sumArea2 {sumArea2}, maxWidth2 {maxWidth2}"); | |
| var result = new { SumArea = sumArea2, MaxWidth = maxWidth2 }; | |
| Console.WriteLine($"result object: {result.SumArea} - {result.MaxWidth}"); | |
| var c1 = new Circle(1.0); | |
| Circle c2 = new(1.0); | |
| Console.WriteLine(c1); | |
| Console.WriteLine(c1.Equals(c2)); | |
| Console.WriteLine(c1 == c2); | |
| PrintShapeInfo(c1); | |
| PrintShapeInfo(new Rectangle(10, 20)); | |
| abstract record Shape | |
| { | |
| public abstract double Area { get; } | |
| public abstract double Circumference { get; } | |
| } | |
| record Circle(double Radius) : Shape | |
| { | |
| public override double Area { get => Math.Pow(Radius, 2) * Math.PI; } | |
| public override double Circumference => Radius * Math.PI * 2; | |
| public double Diameter => 2 * Radius; | |
| } | |
| record Rectangle(double Width, double Height) : Shape | |
| { | |
| public override double Area { get => Width * Height; } | |
| public override double Circumference => (Width + Height) * 2; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment