Отладка (C): различия между версиями
Перейти к навигации
Перейти к поиску
(Новая страница: «== Отладка == * [https://docs.microsoft.com/ru-ru/visualstudio/debugger/?view=vs-2019 Отладка в Visual Studio] * [https://www.jetbrains.com/help/cl...») |
|||
Строка 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/clion/debugging-code.html Отладка в CLion] | * [https://www.jetbrains.com/help/clion/debugging-code.html Отладка в CLion] | ||
+ | |||
+ | == Примеры == | ||
+ | === Простое число === | ||
+ | <syntaxhighlight lang="с" line> | ||
+ | #include <stdio.h> | ||
+ | #include <stdbool.h> | ||
+ | #include <math.h> | ||
+ | |||
+ | bool IsSimple(int num) { | ||
+ | bool simple = true; | ||
+ | for (int i = 2; i < trunc(sqrt(num)); ++i) { | ||
+ | if (num % i == 0) { | ||
+ | simple = false; | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | return simple; | ||
+ | } | ||
+ | |||
+ | int main() { | ||
+ | int n; | ||
+ | printf("Please, enter a number:\n"); | ||
+ | fflush(stdout); | ||
+ | scanf_s("%d", &n); | ||
+ | |||
+ | bool simple = IsSimple(n); | ||
+ | |||
+ | printf("Number %d is %s.", n, simple ? "simple" : "composite"); | ||
+ | fflush(stdout); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === Числа Фибоначчи === | ||
+ | <syntaxhighlight lang="с" line> | ||
+ | #include <stdio.h> | ||
+ | |||
+ | int main() { | ||
+ | int n; | ||
+ | printf("Please, enter a number:\n"); | ||
+ | fflush(stdout); | ||
+ | scanf_s("%d", &n); | ||
+ | |||
+ | int a = 0; | ||
+ | int b = 1; | ||
+ | |||
+ | while(b < n) { | ||
+ | int tmp = a; | ||
+ | a = b; | ||
+ | b = tmp + b; | ||
+ | } | ||
+ | |||
+ | printf("Number %d is %sa fibonacci number.", n, (b == n) ? "" : "not "); | ||
+ | fflush(stdout); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === Доброе утро мистер Иван === | ||
+ | <syntaxhighlight lang="с" line> | ||
+ | #include <stdio.h> | ||
+ | #include <string.h> | ||
+ | |||
+ | char *GoodMorning(char *n, char *t) { | ||
+ | char* intro = "Good morning "; | ||
+ | |||
+ | strcat(intro, " "); | ||
+ | strcat(intro, t); | ||
+ | strcat(intro, " "); | ||
+ | strcat(intro, n); | ||
+ | strcat(intro, "!"); | ||
+ | |||
+ | return intro; | ||
+ | } | ||
+ | |||
+ | int main() { | ||
+ | char* n = "Ivan"; | ||
+ | char* t = "mr."; | ||
+ | |||
+ | printf("%s", GoodMorning(n, t)); | ||
+ | fflush(stdout); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === Факториал === | ||
+ | <syntaxhighlight lang="с" 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 | ||
+ | #include <stdio.h> | ||
+ | |||
+ | int factorial(int num) { | ||
+ | return (num == 1) ? 1 : num * factorial(num - 1); | ||
+ | } | ||
+ | |||
+ | int main() { | ||
+ | int n; | ||
+ | printf("Please, enter a number:\n"); | ||
+ | fflush(stdout); | ||
+ | scanf_s("%d", &n); | ||
+ | |||
+ | int f = factorial(n); | ||
+ | |||
+ | printf("%d! = %d", n, f); | ||
+ | fflush(stdout); | ||
+ | } | ||
+ | </syntaxhighlight> |
Версия 10:19, 15 июня 2021
Отладка
Примеры
Простое число
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
bool IsSimple(int num) {
bool simple = true;
for (int i = 2; i < trunc(sqrt(num)); ++i) {
if (num % i == 0) {
simple = false;
break;
}
}
return simple;
}
int main() {
int n;
printf("Please, enter a number:\n");
fflush(stdout);
scanf_s("%d", &n);
bool simple = IsSimple(n);
printf("Number %d is %s.", n, simple ? "simple" : "composite");
fflush(stdout);
}
Числа Фибоначчи
#include <stdio.h>
int main() {
int n;
printf("Please, enter a number:\n");
fflush(stdout);
scanf_s("%d", &n);
int a = 0;
int b = 1;
while(b < n) {
int tmp = a;
a = b;
b = tmp + b;
}
printf("Number %d is %sa fibonacci number.", n, (b == n) ? "" : "not ");
fflush(stdout);
}
Доброе утро мистер Иван
#include <stdio.h>
#include <string.h>
char *GoodMorning(char *n, char *t) {
char* intro = "Good morning ";
strcat(intro, " ");
strcat(intro, t);
strcat(intro, " ");
strcat(intro, n);
strcat(intro, "!");
return intro;
}
int main() {
char* n = "Ivan";
char* t = "mr.";
printf("%s", GoodMorning(n, t));
fflush(stdout);
}
Факториал
// 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
#include <stdio.h>
int factorial(int num) {
return (num == 1) ? 1 : num * factorial(num - 1);
}
int main() {
int n;
printf("Please, enter a number:\n");
fflush(stdout);
scanf_s("%d", &n);
int f = factorial(n);
printf("%d! = %d", n, f);
fflush(stdout);
}