Advertisement
Guest User

TD 1

a guest
Oct 28th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace TD1
  8. {
  9.     class Program
  10.     {
  11.         static void Exo2()
  12.         {
  13.             Console.WriteLine("Saisis ton nom :");
  14.             String nom = Console.ReadLine();
  15.             Console.WriteLine("Bonjour " + nom + "!");
  16.             Console.WriteLine("Comment vas-tu ?");
  17.             Console.Read();
  18.         }
  19.  
  20.         static void Exo3()
  21.         {
  22.             int cp = 78;
  23.             string nom = "Yvelines";
  24.             string phrase = String.Format("Le code postal de {0} est le {1}", nom, cp.ToString());
  25.             Console.WriteLine(phrase);
  26.             Console.Read();
  27.         }
  28.  
  29.         static void Exo4()
  30.         {
  31.             Console.WriteLine("Quelle est la longueur du premier cote ?");
  32.             double cote1 = Double.Parse(Console.ReadLine());
  33.             Console.WriteLine("Quelle est la longueur du second cote ?");
  34.             double cote2;
  35.             cote2 = float.Parse(Console.ReadLine());
  36.             double perimetre = cote1 * 2 + cote2 * 2;
  37.             Console.WriteLine("Le périmètre du rectangle est " + perimetre);
  38.             Console.Read();
  39.         }
  40.  
  41.         static void Exo5()
  42.         {
  43.             Console.WriteLine("Saisis une syllable :");
  44.             string syllable = Console.ReadLine();
  45.             Console.WriteLine(syllable + syllable);
  46.             Console.Read();
  47.         }
  48.  
  49.         static void Exo6()
  50.         {
  51.             Console.WriteLine("Saisir la chaine 1 :");
  52.             string c1 = Console.ReadLine();
  53.             Console.WriteLine("Saisir la chaine 2 :");
  54.             string c2 = Console.ReadLine();
  55.             Console.WriteLine("c1 = " + c1 + " c2 = " + c2);
  56.             string c3 = c2;
  57.             c2 = c1;
  58.             c1 = c3;
  59.             Console.WriteLine("c1 = " + c1 + " c2 = " + c2);
  60.             Console.Read();
  61.         }
  62.  
  63.         static void Exo7()
  64.         {
  65.             Console.WriteLine("Sairsir entier 1");
  66.             int nb1 = int.Parse(Console.ReadLine());
  67.             Console.WriteLine("Sairsir entier 2");
  68.             int nb2 = int.Parse(Console.ReadLine());
  69.             double moyenne = ((double)nb1 + (double)nb2) / 2;
  70.             Console.WriteLine("La moyenne de ces deux nombres est : " + moyenne);
  71.             Console.Read();
  72.         }
  73.  
  74.         static void Exo8()
  75.         {
  76.             Console.WriteLine("Saisir la température en degré celcius");
  77.             double tempC = double.Parse(Console.ReadLine());
  78.             double tempF = tempC * 9 / 5 + 32;
  79.             string phrase = String.Format("{0} °C = {1} °F", tempC, tempF);
  80.             Console.WriteLine(phrase);
  81.             Console.Read();
  82.         }
  83.  
  84.         static void Exo9()
  85.         {
  86.             int i = 0;
  87.             while (i < 20)
  88.             {
  89.                 Console.WriteLine("ça tourne");
  90.                 i++;
  91.             }
  92.             for (int j = 0; j < 20; j++)
  93.             {
  94.                 Console.WriteLine("ça tourne");
  95.             }
  96.             Console.Read();
  97.         }
  98.  
  99.         static void Exo10()
  100.         {
  101.  
  102.             int somme = 0;
  103.             while (somme < 100)
  104.             {
  105.                 Console.WriteLine("Saisis un nombre");
  106.                 somme += int.Parse(Console.ReadLine());
  107.             }
  108.             Console.WriteLine("La somme des nombres a dépassé 100");
  109.         }
  110.  
  111.         static void Exo11()
  112.         {
  113.             Console.WriteLine("Saisis un nombre (0 pour arrêter)");
  114.             int n = int.Parse(Console.ReadLine());
  115.             while (n != 0)
  116.             {
  117.                 Console.WriteLine(String.Format("Le carré de {0} est {1}", n, n * n));
  118.                 n = int.Parse(Console.ReadLine());
  119.             }
  120.         }
  121.  
  122.         static void Exo12()
  123.         {
  124.             Console.WriteLine("Saisis un nombre");
  125.             int n = int.Parse(Console.ReadLine());
  126.             for (int i = 1; i <= n; i++)
  127.             {
  128.                 Console.WriteLine(i);
  129.             }
  130.             for (int i = n; i > 0; i--)
  131.             {
  132.                 Console.WriteLine(i);
  133.             }
  134.             Console.Read();
  135.         }
  136.  
  137.         static void Exo13()
  138.         {
  139.             Console.WriteLine("Saisis un nombre");
  140.             int n = int.Parse(Console.ReadLine());
  141.             int somme = 0;
  142.             for (int i = 0; i <= n; i++)
  143.             {
  144.                 somme += i;
  145.             }
  146.             Console.WriteLine(somme);
  147.             Console.Read();
  148.         }
  149.  
  150.         static void Exo14()
  151.         {
  152.             int n;
  153.             do
  154.             {
  155.                 Console.WriteLine("Saisis un nombre");
  156.                 n = int.Parse(Console.ReadLine());
  157.             } while (n < 0);
  158.  
  159.             int somme = 0;
  160.             for (int i = 0; i <= n; i++)
  161.             {
  162.                 somme += i;
  163.             }
  164.             Console.WriteLine(somme);
  165.             Console.Read();
  166.         }
  167.  
  168.         static void Exo15()
  169.         {
  170.             Console.WriteLine("Saisis un premier nombre");
  171.             int a = int.Parse(Console.ReadLine());
  172.             Console.WriteLine("Saisis un second nombre");
  173.             int b = int.Parse(Console.ReadLine());
  174.             Console.WriteLine("Saisis la somme de ces deux nombres");
  175.             int r = int.Parse(Console.ReadLine());
  176.  
  177.             if (r == a + b) Console.WriteLine("Bravo !");
  178.             else Console.WriteLine("Retourne en primaire :-(");
  179.             Console.Read();
  180.         }
  181.  
  182.         static void Exo16()
  183.         {
  184.             int[] jours = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  185.             int m;
  186.             do
  187.             {
  188.                 Console.WriteLine("Saisis un numéro de mois");
  189.                 m = int.Parse(Console.ReadLine());
  190.             } while (m >= 1 && m <= 12);
  191.  
  192.             Console.WriteLine(String.Format("Le mois n°{0} à {1} jours", m, jours[m - 1]));
  193.             Console.Read();
  194.         }
  195.  
  196.         static void Exo17()
  197.         {
  198.             string input;
  199.             bool fin = false;
  200.             int i = 0;
  201.             int somme = 0;
  202.             while (!fin)
  203.             {
  204.                 Console.WriteLine("Saisis un nombre (fin pour arrêter)");
  205.                 input = Console.ReadLine();
  206.                 if (input == "fin")
  207.                 {
  208.                     fin = true;
  209.                 }
  210.                 else
  211.                 {
  212.                     somme += int.Parse(input);
  213.                     i++;
  214.                 }
  215.             }
  216.             Console.WriteLine((double)somme / (double)i);
  217.             Console.Read();
  218.         }
  219.  
  220.         static void Exo18()
  221.         {
  222.             Random rnd = new Random();
  223.             int toFind = rnd.Next(101);
  224.             int i = 0;
  225.             Console.WriteLine("Saisis un nombre");
  226.             int input = int.Parse(Console.ReadLine());
  227.             while (input != toFind)
  228.             {
  229.                 i++;
  230.                 if (toFind > input)
  231.                 {
  232.                     Console.WriteLine("Plus grand !");
  233.                 }
  234.                 else
  235.                 {
  236.                     Console.WriteLine("Plus petit !");
  237.                 }
  238.                 input = int.Parse(Console.ReadLine());
  239.             }
  240.             Console.WriteLine(String.Format("Bravo ! Tu as gagné en {0} coups, le nombre a deviné était {1}", i, toFind));
  241.             Console.Read();
  242.         }
  243.  
  244.         static void Exo18AI()
  245.         {
  246.             Console.WriteLine("Indique un intervale (séparé par un espace)");
  247.             string input = Console.ReadLine();
  248.             int[] minmax = new int[] { int.Parse(input.Split(' ')[0]), int.Parse(input.Split(' ')[1]) };
  249.             int i = 0;
  250.             Console.WriteLine(String.Format("[{0};{1}]", minmax[0], minmax[1]));
  251.             Console.WriteLine("Choisis un nombre dans l'intervalle que tu viens de donner");
  252.             Console.Read();
  253.             int nombre;
  254.             do
  255.             {
  256.                 nombre = (minmax[0] + minmax[1]) / 2;
  257.                 Console.WriteLine("Votre nombre est-il " + nombre + " ? [oui/+/-]");
  258.                 input = Console.ReadLine();
  259.                 if(input == "+")
  260.                 {
  261.                     minmax[0] = nombre;
  262.                     i++;
  263.                 } else if(input == "-")
  264.                 {
  265.                     minmax[1] = nombre;
  266.                     i++;
  267.                 }
  268.             } while (input.ToLower() != "oui");
  269.  
  270.             Console.WriteLine(String.Format("Votre nombre était {0} et a été trouvé en {1} coup(s)", nombre, i));
  271.             Console.Read();
  272.         }
  273.  
  274.         static void Menu()
  275.         {
  276.             List<Action> Actions = new List<Action> { Exo2, Exo3, Exo4, Exo5, Exo6, Exo7, Exo8, Exo9, Exo10, Exo11, Exo12, Exo13, Exo14, Exo15, Exo16, Exo17, Exo18, Exo18AI };
  277.             ConsoleKey key;
  278.             int i = 0;
  279.  
  280.             Console.Clear();
  281.             Console.WriteLine("Selectionner un exercice :");
  282.  
  283.             for (int j = 0; j < Actions.Count; j++)
  284.             {
  285.                 if (j == i)
  286.                 {
  287.                     Console.WriteLine("[*] Exercice " + (j + 2));
  288.                 }
  289.                 else
  290.                 {
  291.                     Console.WriteLine("[ ] Exercice " + (j + 2));
  292.                 }
  293.             }
  294.  
  295.             Console.WriteLine("\nEchap pour quitter");
  296.  
  297.             do
  298.             {
  299.                 key = Console.ReadKey().Key;
  300.                 if (key == ConsoleKey.UpArrow && i > 0)
  301.                 {
  302.                     i--;
  303.                 }
  304.                 else if (key == ConsoleKey.DownArrow && i < Actions.Count-1)
  305.                 {
  306.                     i++;
  307.                 }
  308.                 else if (key == ConsoleKey.Escape)
  309.                 {
  310.                     System.Environment.Exit(0);
  311.                 }
  312.  
  313.                 Console.Clear();
  314.  
  315.                 Console.WriteLine("Selectionner un exercice :");
  316.  
  317.                 for (int j = 0; j < Actions.Count; j++)
  318.                 {
  319.                     if (j == i)
  320.                     {
  321.                         Console.WriteLine("[*] Exercice " + (j + 2));
  322.                     }
  323.                     else
  324.                     {
  325.                         Console.WriteLine("[ ] Exercice " + (j + 2));
  326.                     }
  327.                 }
  328.  
  329.                 Console.WriteLine("\nEchap pour quitter");
  330.             } while (key != ConsoleKey.Enter);
  331.  
  332.             Console.Clear();
  333.             Console.WriteLine("======== Exercice " + (i + 2) + " ========");
  334.             Actions[i]();
  335.         }
  336.  
  337.         static void Main(string[] args)
  338.         {
  339.             while (true)
  340.             {
  341.                 Menu();
  342.             }
  343.         }
  344.     }
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement