Отладка (C-Sharp): различия между версиями
Перейти к навигации
Перейти к поиску
(Новая страница: «== Отладка == * [https://docs.microsoft.com/ru-ru/visualstudio/debugger/?view=vs-2019 Отладка в Visual Studio] * [https://www.jetbrains.com/help/ri...») |
|||
Строка 1: | Строка 1: | ||
+ | {{TOCRight}} | ||
== Отладка == | == Отладка == | ||
* [https://docs.microsoft.com/ru-ru/visualstudio/debugger/?view=vs-2019 Отладка в Visual Studio] | * [https://docs.microsoft.com/ru-ru/visualstudio/debugger/?view=vs-2019 Отладка в Visual Studio] | ||
* [https://www.jetbrains.com/help/rider/Debugging_Code.html Отладка в Rider] | * [https://www.jetbrains.com/help/rider/Debugging_Code.html Отладка в Rider] | ||
+ | |||
+ | == Примеры == | ||
+ | === Простое число === | ||
+ | <syntaxhighlight lang="c#" line> | ||
+ | using System; | ||
+ | |||
+ | namespace ConsoleApp1 | ||
+ | { | ||
+ | class Program | ||
+ | { | ||
+ | static bool IsSimple(int num) { | ||
+ | bool simple = true; | ||
+ | for (int i = 2; i < Math.Truncate(Math.Sqrt(num)); ++i) { | ||
+ | if (num % i == 0) { | ||
+ | simple = false; | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | return simple; | ||
+ | } | ||
+ | |||
+ | static void Main(string[] args) | ||
+ | { | ||
+ | Console.WriteLine("Please, enter a number:"); | ||
+ | int num = int.Parse(Console.ReadLine()); | ||
+ | |||
+ | bool simple = IsSimple(num); | ||
+ | |||
+ | Console.WriteLine($"Number {num} is {(simple ? "simple" : "composite")}."); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === Числа Фибоначчи === | ||
+ | <syntaxhighlight lang="c#" line> | ||
+ | using System; | ||
+ | |||
+ | namespace ConsoleApp1 | ||
+ | { | ||
+ | class Program | ||
+ | { | ||
+ | static void Main(string[] args) | ||
+ | { | ||
+ | Console.WriteLine("Please, enter a number:"); | ||
+ | int num = int.Parse(Console.ReadLine()); | ||
+ | |||
+ | int a = 0; | ||
+ | int b = 1; | ||
+ | |||
+ | while (b < num) | ||
+ | { | ||
+ | int tmp = a; | ||
+ | a = b; | ||
+ | b = tmp + b; | ||
+ | } | ||
+ | |||
+ | Console.WriteLine($"Number {num} is {((b == num) ? "" : "not ")}a fibonacci number."); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === Доброе утро мистер Иван === | ||
+ | <syntaxhighlight lang="c#" line> | ||
+ | using System; | ||
+ | |||
+ | namespace ConsoleApp1 | ||
+ | { | ||
+ | class Program | ||
+ | { | ||
+ | private static string GoodMorning(string name, string title) | ||
+ | { | ||
+ | return "Good morning " + title + " " + name + "!"; | ||
+ | } | ||
+ | |||
+ | static void Main(string[] args) | ||
+ | { | ||
+ | string n = "Ivan"; | ||
+ | string t = "mr."; | ||
+ | |||
+ | Console.WriteLine(GoodMorning(n, t)); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === Факториал === | ||
+ | <syntaxhighlight lang="c#" line> | ||
+ | // 5! = 1*2*3*4*5 | ||
+ | // 5! = 5*4*3*2*1 | ||
+ | // 4! = 4*3*2*1 | ||
+ | // 3! = 3*2*1 | ||
+ | // 2! = 2*1 | ||
+ | // 1! = 1 | ||
+ | // 5! = 5*4! | ||
+ | // 4! = 4*3! | ||
+ | // 3! = 3*2! | ||
+ | // 2! = 2*1! | ||
+ | // 1! = 1 | ||
+ | using System; | ||
+ | |||
+ | namespace ConsoleApp1 | ||
+ | { | ||
+ | class Program | ||
+ | { | ||
+ | private static int Factorial(int num) | ||
+ | { | ||
+ | return (num == 1) ? 1 : num * Factorial(num-1); | ||
+ | } | ||
+ | |||
+ | static void Main(string[] args) | ||
+ | { | ||
+ | Console.WriteLine("Please, enter a number:"); | ||
+ | int num = int.Parse(Console.ReadLine()); | ||
+ | |||
+ | Console.WriteLine($"{num}! = {Factorial(num)}"); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> |
Текущая версия на 10:33, 15 июня 2021
Отладка
Примеры
Простое число
1 using System;
2
3 namespace ConsoleApp1
4 {
5 class Program
6 {
7 static bool IsSimple(int num) {
8 bool simple = true;
9 for (int i = 2; i < Math.Truncate(Math.Sqrt(num)); ++i) {
10 if (num % i == 0) {
11 simple = false;
12 break;
13 }
14 }
15
16 return simple;
17 }
18
19 static void Main(string[] args)
20 {
21 Console.WriteLine("Please, enter a number:");
22 int num = int.Parse(Console.ReadLine());
23
24 bool simple = IsSimple(num);
25
26 Console.WriteLine($"Number {num} is {(simple ? "simple" : "composite")}.");
27 }
28 }
29 }
Числа Фибоначчи
1 using System;
2
3 namespace ConsoleApp1
4 {
5 class Program
6 {
7 static void Main(string[] args)
8 {
9 Console.WriteLine("Please, enter a number:");
10 int num = int.Parse(Console.ReadLine());
11
12 int a = 0;
13 int b = 1;
14
15 while (b < num)
16 {
17 int tmp = a;
18 a = b;
19 b = tmp + b;
20 }
21
22 Console.WriteLine($"Number {num} is {((b == num) ? "" : "not ")}a fibonacci number.");
23 }
24 }
25 }
Доброе утро мистер Иван
1 using System;
2
3 namespace ConsoleApp1
4 {
5 class Program
6 {
7 private static string GoodMorning(string name, string title)
8 {
9 return "Good morning " + title + " " + name + "!";
10 }
11
12 static void Main(string[] args)
13 {
14 string n = "Ivan";
15 string t = "mr.";
16
17 Console.WriteLine(GoodMorning(n, t));
18 }
19 }
20 }
Факториал
1 // 5! = 1*2*3*4*5
2 // 5! = 5*4*3*2*1
3 // 4! = 4*3*2*1
4 // 3! = 3*2*1
5 // 2! = 2*1
6 // 1! = 1
7 // 5! = 5*4!
8 // 4! = 4*3!
9 // 3! = 3*2!
10 // 2! = 2*1!
11 // 1! = 1
12 using System;
13
14 namespace ConsoleApp1
15 {
16 class Program
17 {
18 private static int Factorial(int num)
19 {
20 return (num == 1) ? 1 : num * Factorial(num-1);
21 }
22
23 static void Main(string[] args)
24 {
25 Console.WriteLine("Please, enter a number:");
26 int num = int.Parse(Console.ReadLine());
27
28 Console.WriteLine($"{num}! = {Factorial(num)}");
29 }
30 }
31 }