commit cd6a6b268266417c849e2ee9e44c1c429ec379bd Author: Dmitry Sudarenko Date: Wed Nov 13 09:22:28 2024 +0300 first commit diff --git a/01_Lecture01/00_hello_world_p36.c b/01_Lecture01/00_hello_world_p36.c new file mode 100644 index 0000000..964c2c3 --- /dev/null +++ b/01_Lecture01/00_hello_world_p36.c @@ -0,0 +1,12 @@ +/* Это комментарий. + Он не влияет на работу программы */ + +// Это тоже комментарий только из одной строки + +#include // Подключаем библиотеку ввода-вывода + +int main (int argc, char **argv) // Программа начинается тут. Это точка выполнения программы +{ + printf ("Hello world!\n"); // Напечатать текст Hello World! + return 0; // Завершает программу с кодом 0, что все хорошо +} // Конец блока main diff --git a/01_Lecture01/01_sqrt_equation_p37.c b/01_Lecture01/01_sqrt_equation_p37.c new file mode 100644 index 0000000..fc512e9 --- /dev/null +++ b/01_Lecture01/01_sqrt_equation_p37.c @@ -0,0 +1,19 @@ +/* + Комментарий + */ +//Комментарий +#include +#include + +int main (int argc, char **argv) +{ + setlocale(LC_ALL, "en_US.UTF-8"); //устанавливаем русскую локализацию + printf("Вычисление корней квадратного уравнения \\ \"a*x*x+b*x+c=0\" \n"); + printf("Введите a: \n"); + printf("Введите b: \n"); + printf("Введите c:\n"); + printf("Корни квадратного уравнения \n"); + printf("X1 = \n"); + printf("X2 = \n"); + return 0; +} diff --git a/01_Lecture01/02_ASCII_art_p38.c b/01_Lecture01/02_ASCII_art_p38.c new file mode 100644 index 0000000..1d8968c --- /dev/null +++ b/01_Lecture01/02_ASCII_art_p38.c @@ -0,0 +1,14 @@ +#include +int main(int argc, char **argv) +{ + /* + (\___/) + (='.'=) + (")_(") + */ + printf("(\\___/)\n"); + printf("(='.'=)\n"); + printf("(\")_(\")\n"); + + return 0; +} diff --git a/01_Lecture01/03_ASCII_art_hard_p39.c b/01_Lecture01/03_ASCII_art_hard_p39.c new file mode 100644 index 0000000..12916b6 --- /dev/null +++ b/01_Lecture01/03_ASCII_art_hard_p39.c @@ -0,0 +1,18 @@ +#include +int main(int argc, char **argv) +{ + /* + ___ _____ ______________ ___ ____ ______ + / | / ___// ____/ _/ _/ / | / __ \/_ __/ + / /| | \__ \/ / / / / / / /| | / /_/ / / / + / ___ |___/ / /____/ /_/ / / ___ |/ _, _/ / / + /_/ |_/____/\____/___/___/ /_/ |_/_/ |_| /_/ + */ + + printf(" ___ _____ ______________ ___ ____ ______\n"); + printf(" / | / ___// ____/ _/ _/ / | / __ \\/_ __/\n"); + printf(" / /| | \\__ \\/ / / / / / / /| | / /_/ / / / \n"); + printf(" / ___ |___/ / /____/ /_/ / / ___ |/ _, _/ / / \n"); + printf("/_/ |_/____/\\____/___/___/ /_/ |_/_/ |_| /_/\n"); + return 0; +} diff --git a/03_Lecture03/00_example_p4.c b/03_Lecture03/00_example_p4.c new file mode 100644 index 0000000..8ebafad --- /dev/null +++ b/03_Lecture03/00_example_p4.c @@ -0,0 +1,10 @@ +#include + +int main() +{ + int i, sum=0; + for (i=1; i<10; i++) + sum*=i; + printf("%d",sum); + return 0; +} diff --git a/03_Lecture03/01_announcement_p10.c b/03_Lecture03/01_announcement_p10.c new file mode 100644 index 0000000..a187022 --- /dev/null +++ b/03_Lecture03/01_announcement_p10.c @@ -0,0 +1,13 @@ +#include + +int main() +{ + unsigned short a; //объявление целочисленной переменной + float f; //объявление вещественной переменной + int su7, prime=7, five=5; //объявление переменных с инициализацией + float pi=3.14; //объявление переменных с инициализацией + char c, c2='A', m=10; //объявление символьных переменных с инициализацией + double big_pi; //объявление вещественной переменной двойной точности + printf("%d",sizeof(a));//забегая вперед + return 0; +} diff --git a/03_Lecture03/02_announcement_spec_p13.c b/03_Lecture03/02_announcement_spec_p13.c new file mode 100644 index 0000000..4a24dab --- /dev/null +++ b/03_Lecture03/02_announcement_spec_p13.c @@ -0,0 +1,13 @@ +#include + +int main() +{ + unsigned int a; //объявление целочисленной переменной + float f; //объявление вещественной переменной + unsigned long long int su7, prime=7, five=5; //объявление переменных с инициализацией + float pi=3.14; //объявление переменных с инициализацией + unsigned char c, c2='A', m=10; //объявление символьных переменных с инициализацией + long double big_pi; //объявление вещественной переменной двойной точности + printf("%d",sizeof(su7));//забегая вперед + return 0; +} diff --git a/03_Lecture03/03_const_p15.c b/03_Lecture03/03_const_p15.c new file mode 100644 index 0000000..95133de --- /dev/null +++ b/03_Lecture03/03_const_p15.c @@ -0,0 +1,16 @@ +#include + +extern int Voltage; +int main() +{ + { // Операторные скобки обознаячают составной оператор + float Power = 0.5f; + } + { + static int Current; + } + Power = 0;// Ошибка! области видимости + const int SIZE=1000; // Объявление целочисленной константы. + //Обратите внимание, имена констант + // обычно пишут заглавными буквами. +} diff --git a/03_Lecture03/04_side_effect_p17.c b/03_Lecture03/04_side_effect_p17.c new file mode 100644 index 0000000..9f92e11 --- /dev/null +++ b/03_Lecture03/04_side_effect_p17.c @@ -0,0 +1,10 @@ +#include + +int main() +{ + int a, b, x, y, z; + a = 5; //положить целое число 5 в переменную a + x = x + 20; // x=25 побочный эффект + y = (x - 15) * (x + a); // y=300 + z = y = a+1; // y=6 z=6 +} diff --git a/03_Lecture03/05_calc_p19.c b/03_Lecture03/05_calc_p19.c new file mode 100644 index 0000000..f1a8535 --- /dev/null +++ b/03_Lecture03/05_calc_p19.c @@ -0,0 +1,10 @@ +#include + +int main() +{ + int a, x, y, z; + a = 27; //положить целое число 27 в переменную a + x = a / 5; // x=5 + y = 11 % 3; // y=2 + z = (x + 5) * y; // z=20 +} diff --git a/03_Lecture03/06_explicit_p21.c b/03_Lecture03/06_explicit_p21.c new file mode 100644 index 0000000..fe37948 --- /dev/null +++ b/03_Lecture03/06_explicit_p21.c @@ -0,0 +1,11 @@ +#include + +int main() +{ + int X; + int Y = 200; + char C = 255; //Чтобы избежать неоднозначностей, рекомендуется явно + //указывать знаковость для типа char -1 или 255? + X = C * 10 + Y; // переменная С приводится к типу int + printf("%d\n",X); +} diff --git a/03_Lecture03/07_implicit_p23.c b/03_Lecture03/07_implicit_p23.c new file mode 100644 index 0000000..a6ae348 --- /dev/null +++ b/03_Lecture03/07_implicit_p23.c @@ -0,0 +1,20 @@ +#include + +int main() +{ + float f; + f = 3/2; //целочисленное деление f=1.0 + printf("%f\n",f); + f = 3./2; //вещественное деление f=1.5 + printf("%f\n",f); + int a = 7; + float x; + x = a / 4; // 1 + printf("%f\n",x); + x = 4 / a; // 0 + printf("%f\n",x); + x = (float)a / 4; // 1.75 + printf("%f\n",x); + x = 1.*a / 4; // 1.75 + printf("%f\n",x); +} diff --git a/03_Lecture03/08_implicit_2_p24.c b/03_Lecture03/08_implicit_2_p24.c new file mode 100644 index 0000000..4726559 --- /dev/null +++ b/03_Lecture03/08_implicit_2_p24.c @@ -0,0 +1,10 @@ +#include + +int main() +{ + short i, x=5,y=-2; + i = x + y; //переменные x и y сначала расширяются до типа int, + // выполняется сложение а затем результат + //помещается в i + printf("%d",i); +} diff --git a/03_Lecture03/09_implicit_3_p25.c b/03_Lecture03/09_implicit_3_p25.c new file mode 100644 index 0000000..6e52c15 --- /dev/null +++ b/03_Lecture03/09_implicit_3_p25.c @@ -0,0 +1,12 @@ +#include + +int main() +{ + unsigned int u = 50; + int i = -500; + int answer = i / u; // answer = 85899335. + printf("%d\n",answer); + // i будет приведен к беззнаковому типу и его + //значение будет равно 2^32-500 вместо -500, а результат + //деления будет приведен обратно к знаковому типу int +} diff --git a/03_Lecture03/10_short_p27.c b/03_Lecture03/10_short_p27.c new file mode 100644 index 0000000..73c766d --- /dev/null +++ b/03_Lecture03/10_short_p27.c @@ -0,0 +1,8 @@ +#include + +int main() +{ + int a = 50, b=7; + a = a + b; // a=57 + a += b; // a=57 можно так +} diff --git a/03_Lecture03/11_postfix_p28.c b/03_Lecture03/11_postfix_p28.c new file mode 100644 index 0000000..3480b7d --- /dev/null +++ b/03_Lecture03/11_postfix_p28.c @@ -0,0 +1,8 @@ +#include + +int main() +{ + // Постфиксная форма + int a, b=7; + a = b++; // a=7 b=8 +} diff --git a/03_Lecture03/12_prefix_p28.c b/03_Lecture03/12_prefix_p28.c new file mode 100644 index 0000000..79d9b57 --- /dev/null +++ b/03_Lecture03/12_prefix_p28.c @@ -0,0 +1,8 @@ +#include + +int main() +{ + // Префиксная форма + int a, b=7; + a = ++b; // a=8 b=8 +} diff --git a/03_Lecture03/13_sequence_point_p30.c b/03_Lecture03/13_sequence_point_p30.c new file mode 100644 index 0000000..69d9aca --- /dev/null +++ b/03_Lecture03/13_sequence_point_p30.c @@ -0,0 +1,13 @@ +#include + +int main() +{ + //~ int a, b, c; + //~ a = (a+b) * (c + 3); // после вычисления всего выражения + + int a, b=10, c; + a = b++ + ++b; // ТАК НЕЛЬЗЯ дважды модифицируется одна переменная Внимание! Attention! Atención! + printf("%d %d\n",a,b); + a = (c=3) + (c=2); // ТАК НЕЛЬЗЯ дважды модифицируется одна переменная Внимание! Attention! Atención! + printf("%d %d\n",a,c); +} diff --git a/03_Lecture03/14_numbers_sum_p32.c b/03_Lecture03/14_numbers_sum_p32.c new file mode 100644 index 0000000..5f2ab00 --- /dev/null +++ b/03_Lecture03/14_numbers_sum_p32.c @@ -0,0 +1,11 @@ +#include + +int main() +{ + int n=123; + int sum; + sum = n%10; //sum = 3 + sum += (n/10)%10; //sum = 5 + sum += (n/100)%10; //sum = 6 + printf("%d\n",sum); +} diff --git a/03_Lecture03/15_sum_p35.c b/03_Lecture03/15_sum_p35.c new file mode 100644 index 0000000..4eba939 --- /dev/null +++ b/03_Lecture03/15_sum_p35.c @@ -0,0 +1,11 @@ +#include //Объявить библиотеки ввода-вывода + +int main(void) +{ + int a, b, c; //Объявить переменные + printf("Input number:\n"); //Вывести на экран подсказку + scanf ("%d%d", &a, &b); //Считать два целых числа и записать их по адресу a,b + c = a + b; //Сложить два числа и поместить сумму в c + printf("%d\n", c); //Вывести на экран значение в переменной c + return 0; //Завершить программу успешно +} diff --git a/03_Lecture03/16_16rich_p38.c b/03_Lecture03/16_16rich_p38.c new file mode 100644 index 0000000..841989c --- /dev/null +++ b/03_Lecture03/16_16rich_p38.c @@ -0,0 +1,10 @@ +#include + +int main(void) +{ + int a; + printf("Input number:"); + scanf ("%d", &a); + printf("%#5x", a); + return 0; +} diff --git a/03_Lecture03/17_mistakes_p40.c b/03_Lecture03/17_mistakes_p40.c new file mode 100644 index 0000000..2155f16 --- /dev/null +++ b/03_Lecture03/17_mistakes_p40.c @@ -0,0 +1,11 @@ +#include + +int main(void) +{ + int a, b; + scanf ("%d", a); // &a + scanf ("%d", &a, &b); // %d%d + scanf ("%d%d", &a); //&a, &b + scanf ("%d %d", &a, &b); // пробел не обязательно + scanf ("%f%f", &a, &b); // %d%d +} diff --git a/03_Lecture03/18_output_p41.c b/03_Lecture03/18_output_p41.c new file mode 100644 index 0000000..2ae4e8e --- /dev/null +++ b/03_Lecture03/18_output_p41.c @@ -0,0 +1,17 @@ +#include + +int main(void) +{ + int a=1, b=5, c=123; + printf ("%d\n", c); + //123 + + printf ("Result: %d\n", c); + //Result: 123 + + printf ("%d+%d=%d\n", a, b, c ); + //1+5=123 + + printf ("%d+%d=%d\n", a, b, a+b ); + //1+5=6 +} diff --git a/03_Lecture03/19_output_2_p42.c b/03_Lecture03/19_output_2_p42.c new file mode 100644 index 0000000..2bd92d6 --- /dev/null +++ b/03_Lecture03/19_output_2_p42.c @@ -0,0 +1,12 @@ +#include + +int main(void) +{ + int x = 1234; + printf ("%d\n", x); //минимальное число позиций под вывод числа + //1234 + + printf ("%9d\n", x);//под вывод числа выделено 9 позиций + printf ("%09d\n", x);//под вывод числа выделено 9 позиций + // 1234 +} diff --git a/03_Lecture03/20_output_3_p43.c b/03_Lecture03/20_output_3_p43.c new file mode 100644 index 0000000..6061cf3 --- /dev/null +++ b/03_Lecture03/20_output_3_p43.c @@ -0,0 +1,17 @@ +#include + +int main(void) +{ + float x = 123.4567; + printf ("%f\n", x); + //123.456700 + + printf ("%9.3f\n", x); + //123.456 + + printf ("%e\n", x); // стандартно 1.234567 * 102 + //1.234560e+02 + + printf ("%10.2e\n", x);// всего 10 знаков, 2 цифры под мантиссу + //1.23e+02 +} diff --git a/03_Lecture03/21_mistake_p45.c b/03_Lecture03/21_mistake_p45.c new file mode 100644 index 0000000..8785704 --- /dev/null +++ b/03_Lecture03/21_mistake_p45.c @@ -0,0 +1,12 @@ +#include + +int main(void) +{ + int a, b; + float x, y; + a = 5; + 10 = x;//константа + y = 7,8; //точка вместо запятой + b = 2.5; //неверный тип + a = b + x; //пропадет дробная часть +} diff --git a/03_Lecture03/22_print_p46.c b/03_Lecture03/22_print_p46.c new file mode 100644 index 0000000..e994d00 --- /dev/null +++ b/03_Lecture03/22_print_p46.c @@ -0,0 +1,11 @@ +#include + +int main(void) +{ + int a = 1, b= 3; + printf ("a+%d=a+b\n", b); + printf ("%d=F (%d)\n", a, b); + printf ("a=F (%d)\n", b); + printf ("%d>%d\n", a+b, b); + printf ("F(%d)==G(%d)\n", b, a); +} diff --git a/03_Lecture03/23_sqrt_equation_p49.c b/03_Lecture03/23_sqrt_equation_p49.c new file mode 100644 index 0000000..1992305 --- /dev/null +++ b/03_Lecture03/23_sqrt_equation_p49.c @@ -0,0 +1,26 @@ +#include +#include +#include + +int main(int argc, char **argv) +{ + float a,b,c; + float B,d; + float X1,X2; + setlocale(LC_ALL, ".utf-8"); + printf("Вычисление корней квадратного уравнения\"a*x*x+b*x+с=0\"\n"); + printf("Введите a:\n"); + scanf ("%f", &a); //1 + printf("Введите b:\n"); + scanf ("%f", &b); //18 + printf("Введите c:\n"); + scanf ("%f", &c); //32 + B = b/2; + d = sqrtf(B*B - a*c); + printf("Корни квадратного уравнения \n"); + X1 = (-B + d)/a; //-2 + printf("X1 = %f \n",X1); + X2 = (-B - d)/a; //-16 + printf("X2 = %f \n",X2); + return 0; +} diff --git a/04_Lecture04/00_inttypes_p5.c b/04_Lecture04/00_inttypes_p5.c new file mode 100644 index 0000000..0ddd378 --- /dev/null +++ b/04_Lecture04/00_inttypes_p5.c @@ -0,0 +1,12 @@ +#include +#include + +int main() +{ + int32_t a; + int8_t b;//char + uint8_t c;//unsigned char c; + scanf ("%" SCNd32, &a);//SCNu32 + printf("a = %" PRId32 " hex a = %" PRIx32"\n",a,a);//PRIu32 + return 0; +} diff --git a/04_Lecture04/01_compare_p7.c b/04_Lecture04/01_compare_p7.c new file mode 100644 index 0000000..8c1b580 --- /dev/null +++ b/04_Lecture04/01_compare_p7.c @@ -0,0 +1,11 @@ +#include +#include + +int main() +{ + int a, b; + a = 5 == 3; // a=0 - + b = 100 >= 1; // b=1 + printf("a=%d b=%d",a,b); + return 0; +} diff --git a/04_Lecture04/02_operations_p9.c b/04_Lecture04/02_operations_p9.c new file mode 100644 index 0000000..d88c36d --- /dev/null +++ b/04_Lecture04/02_operations_p9.c @@ -0,0 +1,10 @@ +#include +#include + +int main() +{ + int a, b=5, n=0; + a = ( n!=0 && b/n ); // Нет ошибки - деления на ноль + printf("%d %d",a,b); + return 0; +} diff --git a/04_Lecture04/03_solution_p14.c b/04_Lecture04/03_solution_p14.c new file mode 100644 index 0000000..b542bfe --- /dev/null +++ b/04_Lecture04/03_solution_p14.c @@ -0,0 +1,16 @@ +#include +#include + +int main() +{ + int a = 5, b = 7; + printf("%d\n",(a += 5) * (b -= 3)); + printf("a=%d,b=%d\n",a,b); + printf("%d\n",--b / (a++ - 3)); + printf("a=%d,b=%d\n",a,b); + printf("%d\n",(a -= 2) || 47 / (b - 7)); + printf("a=%d,b=%d\n",a,b);//a=9,b=3 + printf("%d\n",(a *= b) + (b *= a)); + printf("a=%d,b=%d\n",a,b);//неопределенность + return 0; +} diff --git a/04_Lecture04/04_max_p18.c b/04_Lecture04/04_max_p18.c new file mode 100644 index 0000000..57512d6 --- /dev/null +++ b/04_Lecture04/04_max_p18.c @@ -0,0 +1,13 @@ +#include +#include + +int main() +{ + int a,b; + scanf("%d%d",&a,&b); + if (a>b) + printf("%d",a); + else + printf("%d",b); + return 0; +} diff --git a/04_Lecture04/05_max_tern_p21.c b/04_Lecture04/05_max_tern_p21.c new file mode 100644 index 0000000..d4e7984 --- /dev/null +++ b/04_Lecture04/05_max_tern_p21.c @@ -0,0 +1,17 @@ +#include +#include + +int main() +{ + int a,b, max; + scanf("%d%d",&a,&b); + // короткая запись if else + //условная операция + max = a>b ? a : b; + //~ if(a>b) + //~ max = a; + //~ else + //~ max = b; + printf("%d",max); + return 0; +} diff --git a/04_Lecture04/06_example_tv_p22.c b/04_Lecture04/06_example_tv_p22.c new file mode 100644 index 0000000..d9ce58d --- /dev/null +++ b/04_Lecture04/06_example_tv_p22.c @@ -0,0 +1,16 @@ +#include + +const int TV_PAL = 1; +const int TV_SECAM = 0; +const int TV_WORK = 1; +const int TV_TEST = 0; + +int main(int argc, char **argv) +{ + int tv_system = TV_SECAM; + int tv_input = TV_TEST; + printf("TV Diagnostic %s %s", + tv_system == TV_PAL ? "PAL" : "SECAM", + tv_input ? "WORK" : "TEST"); + return 0; +} diff --git a/04_Lecture04/07_right_shift_p25.c b/04_Lecture04/07_right_shift_p25.c new file mode 100644 index 0000000..8f45adc --- /dev/null +++ b/04_Lecture04/07_right_shift_p25.c @@ -0,0 +1,11 @@ +#include +#include + +int main() +{ + uint32_t u=0xaabbccdd; + printf("0x%X\n",u); // 0aabbccdd + u = u>>4; // логический сдвиг + printf("0x%08X\n",u); // 0aabbccd + return 0; +} diff --git a/04_Lecture04/08_left_shift_p26.c b/04_Lecture04/08_left_shift_p26.c new file mode 100644 index 0000000..564ef65 --- /dev/null +++ b/04_Lecture04/08_left_shift_p26.c @@ -0,0 +1,10 @@ +#include +#include + +int main() +{ + int32_t a=0xaabbccdd; + printf("%x\n",a); // 0aabbccdd + a >>= 4; // арифметический сдвиг + printf("%x\n",a); // faabbccd +} diff --git a/04_Lecture04/09_shift_features_p27.c b/04_Lecture04/09_shift_features_p27.c new file mode 100644 index 0000000..3d9e2c8 --- /dev/null +++ b/04_Lecture04/09_shift_features_p27.c @@ -0,0 +1,9 @@ +#include +#include + +int main() +{ + int a=1,b=-1; + printf("a = %d b = %d", a>>32, b>>-1); + // a = 0 b = -1 +} diff --git a/04_Lecture04/10_bit operations_p29.c b/04_Lecture04/10_bit operations_p29.c new file mode 100644 index 0000000..a3c0feb --- /dev/null +++ b/04_Lecture04/10_bit operations_p29.c @@ -0,0 +1,10 @@ +#include +#include + +int main() +{ + unsigned int a=0xAABBCCFF; + a = a & ~7;//1111 1000 + printf("a = %x", a); + // a = aabbccf8 +} diff --git a/04_Lecture04/11_bit operations_2_p30.c b/04_Lecture04/11_bit operations_2_p30.c new file mode 100644 index 0000000..635d3fc --- /dev/null +++ b/04_Lecture04/11_bit operations_2_p30.c @@ -0,0 +1,10 @@ +#include +#include + +int main() +{ + unsigned int a=0xFFFFFF00; + a = a | 3; + printf("a = %x", a); + // a = ffffff03 +} diff --git a/04_Lecture04/12_bit operations_3_p31.c b/04_Lecture04/12_bit operations_3_p31.c new file mode 100644 index 0000000..572ab24 --- /dev/null +++ b/04_Lecture04/12_bit operations_3_p31.c @@ -0,0 +1,14 @@ +#include +#include + +int main() +{ + uint32_t n=0; + uint8_t a=0xaa,b=0xbb,c=0xcc,d=0xdd; + n = a;//n=0xaa + n = (n<<8) | b;//n=0xaa00 | 0xbb = 0xaabb + n = (n<<8) | c;// 0xaabb00| 0xcc = 0xaabbcc + n = (n<<8) | d; + printf("n = %x", n); + // n = aabbccdd +} diff --git a/04_Lecture04/13_bit operations_4_p32.c b/04_Lecture04/13_bit operations_4_p32.c new file mode 100644 index 0000000..5d67dd8 --- /dev/null +++ b/04_Lecture04/13_bit operations_4_p32.c @@ -0,0 +1,14 @@ +#include +#include + +int main() +{ + unsigned int n=0xaabbccdd; + unsigned char a,b,c,d; + d = n; + c = n>>8; + b = n>>16; + a = n>>24; + printf("a = %x, b = %x, c = %x, d = %x", a,b,c,d); + // a = aa, b = bb, c = cc, d = dd +} diff --git a/04_Lecture04/14_bit operations_5_p33.c b/04_Lecture04/14_bit operations_5_p33.c new file mode 100644 index 0000000..d3bf95f --- /dev/null +++ b/04_Lecture04/14_bit operations_5_p33.c @@ -0,0 +1,13 @@ +#include +#include + +int main() +{ + unsigned int a=5, b=7; + printf("a = %d, b = %d\n", a,b); + a = a+b; + b = a-b; + a = a-b; + printf("a = %d, b = %d\n", a,b); + // a = 7 b =5 +} diff --git a/04_Lecture04/15_bit operations_6_p33.c b/04_Lecture04/15_bit operations_6_p33.c new file mode 100644 index 0000000..38a817b --- /dev/null +++ b/04_Lecture04/15_bit operations_6_p33.c @@ -0,0 +1,13 @@ +#include +#include + +int main() +{ + unsigned int a=5, b=7; + printf("a = %d, b = %d\n", a,b); + a = a^b; + b = a^b; + a = a^b; + printf("a = %d, b = %d", a,b); + // a = 7 b =5 +} diff --git a/04_Lecture04/16_invert_p34.c b/04_Lecture04/16_invert_p34.c new file mode 100644 index 0000000..9528241 --- /dev/null +++ b/04_Lecture04/16_invert_p34.c @@ -0,0 +1,14 @@ +#include +#include + +int main() +{ + unsigned int mask, n=0xAB; // n = 1010 1011 + printf("n = %x\n", n); + mask = (1<<5)-1; + //~ mask = 0x1F; + //~ mask = 0b11111; + n = n ^ mask; + printf("n = %x\n", n); + // n = b4 1011 0100 +} diff --git a/04_Lecture04/17_sqrt_equation_p40_41.c b/04_Lecture04/17_sqrt_equation_p40_41.c new file mode 100644 index 0000000..57cc7f0 --- /dev/null +++ b/04_Lecture04/17_sqrt_equation_p40_41.c @@ -0,0 +1,49 @@ +#include +#include +#include + +int main(int argc, char **argv) +{ + float a,b,c; + float B,d; + float X1,X2; + printf("%s\n",setlocale(LC_ALL, ".utf-8")); + printf("Вычисление корней квадратного уравнения\"a*x*x+b*x+с=0\"\n"); + printf("Введите a:\n"); + scanf ("%f", &a); //1 + printf("Введите b:\n"); + scanf ("%f", &b); //18 + printf("Введите c:\n"); + scanf ("%f", &c); //32 + B = b/2; + if(a!=0) + { + d = B*B-a*c; + if(d<0) + { + printf("Корни квадратного уравнения комплексные\n"); + } + else + { + printf("Корни квадратного уравнения \n"); + d = sqrtf(d); + X1 = (-B + d)/a; //-2 + printf("X1 = %f \n",X1); + X2 = (-B - d)/a; //-16 + printf("X2 = %f \n",X2); + } + } + else + { + if(b!=0) + { + X1 = -c/b; + printf("Корень линейного уравнения %f\n",X1); + } + else + { + printf("Корней НЕТ!\n"); + } + } + return 0; +} diff --git a/05_Lecture05/00_switch_p6.c b/05_Lecture05/00_switch_p6.c new file mode 100644 index 0000000..6404a4a --- /dev/null +++ b/05_Lecture05/00_switch_p6.c @@ -0,0 +1,25 @@ +#include + +int main () +{ +int input; + scanf ("%d", &input); + switch (input) + { + case 1: + printf ("one\n"); + break; + case 2: + printf ("two\n"); + break; + case 3: + printf ("three\n"); + break; + case 4: + printf ("four\n"); + break; + default : + printf ("default\n"); + } + return 0; +} diff --git a/05_Lecture05/01_while_p11.c b/05_Lecture05/01_while_p11.c new file mode 100644 index 0000000..6ae8712 --- /dev/null +++ b/05_Lecture05/01_while_p11.c @@ -0,0 +1,12 @@ +#include + +int main() +{ + int n = 0; + while (n!=5)// лучше n<5 + { + printf("Hello\n"); + n++; // n = n+1 + } + return 0; +} diff --git a/05_Lecture05/02_while_2_p11.c b/05_Lecture05/02_while_2_p11.c new file mode 100644 index 0000000..ca2cbc2 --- /dev/null +++ b/05_Lecture05/02_while_2_p11.c @@ -0,0 +1,13 @@ +#include + +int main() +{ + int n; + n=5; + while (n!=0)//n>0 + { + printf("Hello %d\n",n); + n--; + } + return 0; +} diff --git a/05_Lecture05/03_while_example_p13.c b/05_Lecture05/03_while_example_p13.c new file mode 100644 index 0000000..a391942 --- /dev/null +++ b/05_Lecture05/03_while_example_p13.c @@ -0,0 +1,13 @@ +#include + +int main() +{ + int n; + n=1; + while (n <= 5) + { + printf("%d\n", n); + n++; + } + return 0; +} diff --git a/05_Lecture05/04_while_example_2_p15.c b/05_Lecture05/04_while_example_2_p15.c new file mode 100644 index 0000000..4930e6b --- /dev/null +++ b/05_Lecture05/04_while_example_2_p15.c @@ -0,0 +1,13 @@ +#include + +int main() +{ + int n; + n=2; + while (n != 5) + { + printf("%d\n", n); + n = n+2; + } + return 0; +} diff --git a/05_Lecture05/05_digits_p19.c b/05_Lecture05/05_digits_p19.c new file mode 100644 index 0000000..22e7e38 --- /dev/null +++ b/05_Lecture05/05_digits_p19.c @@ -0,0 +1,17 @@ +#include + +int main () +{ + int input, n, count; + printf ("Input number : "); + scanf ("%d", &input); + count = 0; + n = input; + while (n != 0)//n>0 + { + count++; + n = n / 10; // Отбросили одну цифру + } + printf ("In %d fount %d digits", input, count); + return 0; +} diff --git a/05_Lecture05/06_deviders_p21.c b/05_Lecture05/06_deviders_p21.c new file mode 100644 index 0000000..dd7fedf --- /dev/null +++ b/05_Lecture05/06_deviders_p21.c @@ -0,0 +1,16 @@ +#include + +int main(int argc, char **argv) +{ +int a = 14; +int b = 35; + printf("a=%d,b=%d,",a,b); + while (b != 0) + { + int t = b; + b = a % b; + a = t; + } + printf("NOD=%d\n",a); + return 0; +} diff --git a/05_Lecture05/07_do_while_p24.c b/05_Lecture05/07_do_while_p24.c new file mode 100644 index 0000000..75967d0 --- /dev/null +++ b/05_Lecture05/07_do_while_p24.c @@ -0,0 +1,11 @@ +#include + +int main(int argc, char **argv) +{ +int a = 4, b = 6; + do + { + a++; + } while (a < b); // Не забываем точку с запятой в конце + return 0; +} diff --git a/05_Lecture05/08_number_p29.c b/05_Lecture05/08_number_p29.c new file mode 100644 index 0000000..deececa --- /dev/null +++ b/05_Lecture05/08_number_p29.c @@ -0,0 +1,19 @@ +#include + +int main(int argc, char **argv) +{ + int i, a=1; + for(i=1; i<4; i++) + a++; + printf("%d\n",a); + + int s, b=1; + for(s=1; s<4; s++) + b = b+s; + printf("%d\n",b); + + int e, k=1, l=2; + for(e=3; e>=1; e--) + k+=l; + printf("%d\n",k); +} diff --git a/05_Lecture05/09_comtinue_p31.c b/05_Lecture05/09_comtinue_p31.c new file mode 100644 index 0000000..03b83e0 --- /dev/null +++ b/05_Lecture05/09_comtinue_p31.c @@ -0,0 +1,12 @@ +#include + +int main(int argc, char **argv) +{ + int i; + for(i=1; i<5; i++) + { + if (i==3) + continue; + printf("i = %d\n",i); + } +} diff --git a/05_Lecture05/10_break_p32.c b/05_Lecture05/10_break_p32.c new file mode 100644 index 0000000..9e646e9 --- /dev/null +++ b/05_Lecture05/10_break_p32.c @@ -0,0 +1,15 @@ +#include + +int main() { + char c; + for(;;)//while(1) + { + printf( "\nPress any key, Q to quit: " ); + // Convert to character value + scanf("%c", &c); + printf("%x\n",c); + if (c == 'Q') + break; + } + return 0; +} // Loop exits only when 'Q' is pressed diff --git a/05_Lecture05/11_sqrt_p36.c b/05_Lecture05/11_sqrt_p36.c new file mode 100644 index 0000000..026285d --- /dev/null +++ b/05_Lecture05/11_sqrt_p36.c @@ -0,0 +1,18 @@ +#include + +int main() { + // Используя цикл while + int i=1; + while (i<=10) { + printf("%d\n",i*i); + i++; + } +/* + // Используя цикл for + int i; + for(i=1; i<=10; i++) { + printf("%d\n",i*i); + } +*/ + return 0; +} diff --git a/05_Lecture05/12_count_p38.c b/05_Lecture05/12_count_p38.c new file mode 100644 index 0000000..9f25cc4 --- /dev/null +++ b/05_Lecture05/12_count_p38.c @@ -0,0 +1,19 @@ +#include + +int main() +{ + int i, count = 0; + for (i = 102; i <= 987; i++) + { + int d1 = i % 10; + int d2 = i % 100/10; + int d3 = i / 100; + if (d1!=d2 && d1!=d3 && d2!=d3) + //if (i / 100 != i % + //10 && i / 100 != i % 100 / 10 + //&& i % 100 / 10 != i % 10) + count++; + } + printf("%d", count); + return 0; +} diff --git a/05_Lecture05/13_fibonacci_p40.c b/05_Lecture05/13_fibonacci_p40.c new file mode 100644 index 0000000..e8f65bc --- /dev/null +++ b/05_Lecture05/13_fibonacci_p40.c @@ -0,0 +1,23 @@ +#include + +int main( void ) +{ + unsigned int n, i, f1, f2, m; + scanf("%d", &n); + f1 = 1; + f2 = 1; + if (n == 1) + printf("1"); + else + if (n != 0) + { + printf("1 1 "); + for (i = 2; i < n; ++i){ + m = f1 + f2; + f1 = f2; + f2 = m; + printf("%u ", f2); + } + } + return 0; +} diff --git a/05_Lecture05/14_goto_p42.c b/05_Lecture05/14_goto_p42.c new file mode 100644 index 0000000..d59f89d --- /dev/null +++ b/05_Lecture05/14_goto_p42.c @@ -0,0 +1,10 @@ +#include + +int main() +{ + printf("Hello"); + goto skip; + printf("World"); +skip: + return 0; +} diff --git a/05_Lecture05/15_global_p45.c b/05_Lecture05/15_global_p45.c new file mode 100644 index 0000000..8ddafea --- /dev/null +++ b/05_Lecture05/15_global_p45.c @@ -0,0 +1,11 @@ +#include + +/*Глобальная переменная. Видна внутри всего файла.*/ +int a=5; +int main() +{ +/*Локальная переменная видна только внутри main. */ + int a=10; + printf("a = %d",a); + return 0; +} diff --git a/05_Lecture05/16_example_1_p47.c b/05_Lecture05/16_example_1_p47.c new file mode 100644 index 0000000..cbf539f --- /dev/null +++ b/05_Lecture05/16_example_1_p47.c @@ -0,0 +1,11 @@ +#include + +int main() +{ + int i, sum=0; + for(i=0; i<5; i++) { + sum+=i; + } + printf("%d\n",i); // i = 5 + return 0; +} diff --git a/05_Lecture05/16_example_2_p47.c b/05_Lecture05/16_example_2_p47.c new file mode 100644 index 0000000..8276dcf --- /dev/null +++ b/05_Lecture05/16_example_2_p47.c @@ -0,0 +1,12 @@ +#include + +int main() +{ + int sum=0; + // переменная i видна только внутри for + for(int i=0; i<5; i++) { + sum+=i; + } + printf("%d\n",i); //ошибка + return 0; +} diff --git a/05_Lecture05/17_define_p52.c b/05_Lecture05/17_define_p52.c new file mode 100644 index 0000000..ffc4948 --- /dev/null +++ b/05_Lecture05/17_define_p52.c @@ -0,0 +1,11 @@ +#include + +// Плохо +unsigned int a, h; +char ln[25], f[25]; + +// Хорошо +#define NAME_LENGTH 100 +unsigned int age, height; +char lastName[NAME_LENGTH], + firstName[NAME_LENGTH]; diff --git a/05_Lecture05/18_sqrt_equation_p54.c b/05_Lecture05/18_sqrt_equation_p54.c new file mode 100644 index 0000000..08be4dc --- /dev/null +++ b/05_Lecture05/18_sqrt_equation_p54.c @@ -0,0 +1,35 @@ +#include +#include + +int main(int argc, char **argv) +{ +char Choice; + setlocale(LC_ALL, ".utf-8"); + while(1) + { + printf("1. Вычисление корней квадратного уравнения\n"); + printf("0. Выход\n"); + printf("Для выход нажмите Q\n"); +NO_PRINT: + scanf("%c",&Choice); + printf("%x\n",Choice); + switch(Choice) + { + case '1': + printf("SquarEquation()\n"); + break; + case '0': + case 'q': + case 'Q': + return 0; + break; + case 0xa://'\n': + goto NO_PRINT; + break; + default: + printf("Непонятный выбор %x\n",Choice); + break; + } + } + return 0; +} diff --git a/06_Lecture06/00_getchar_putchar_p5.c b/06_Lecture06/00_getchar_putchar_p5.c new file mode 100644 index 0000000..b01dbd3 --- /dev/null +++ b/06_Lecture06/00_getchar_putchar_p5.c @@ -0,0 +1,9 @@ +#include + +int main() +{ + char c; + while( (c=getchar())!='.') + putchar(c); + return 0; +} diff --git a/06_Lecture06/01_getchar_putchar_2_p6.c b/06_Lecture06/01_getchar_putchar_2_p6.c new file mode 100644 index 0000000..7502d5f --- /dev/null +++ b/06_Lecture06/01_getchar_putchar_2_p6.c @@ -0,0 +1,13 @@ +#include + +int main () +{ + char character; + puts("Введите символ, символ точки - выход('.'):"); + do + { + character = getchar(); // считать введённый со стандартного потока ввода символ + putchar (character); // вывести этот символ + } while (character != '.'); // пока введенный символ не точка + return 0; +} diff --git a/06_Lecture06/02_letter_change_p12.c b/06_Lecture06/02_letter_change_p12.c new file mode 100644 index 0000000..85bd94f --- /dev/null +++ b/06_Lecture06/02_letter_change_p12.c @@ -0,0 +1,13 @@ +#include + +int main() +{ + char c; + while( (c=getchar())!='\n') //спец символ новой строки + if(c>='a' && c<='z') //все символы лежат подряд ‘a’=97, ‘b’=98, ‘c’=99, ... + putchar(c-0x20); + //putchar('A' + (c-'a')); + else + putchar(c); + return 0; +} diff --git a/06_Lecture06/03_getch_p13.c b/06_Lecture06/03_getch_p13.c new file mode 100644 index 0000000..6d3a552 --- /dev/null +++ b/06_Lecture06/03_getch_p13.c @@ -0,0 +1,18 @@ +#include +#include +//https://sourceforge.net/p/msys2/mailman/msys2-users/thread/81B27E05-0DCC-4FB2-9615-5627895BED40@crelg.com/ + +int main () +{ + char character; + puts("Exit('.'):"); + do + { + character = _getch(); // считать введённый со стандартного потока ввода символ + if(character>='a' && character<='z') //все символы лежат подряд ‘a’=97, ‘b’=98, ‘c’=99, ... + putchar('A' + (character-'a')); + else + putchar(character); + } while (character != '.'); // пока введенный символ не точка + return 0; +}// не работает в https://www.onlinegdb.com/ diff --git a/06_Lecture06/03_getch_p13_ncurses.c b/06_Lecture06/03_getch_p13_ncurses.c new file mode 100644 index 0000000..8de8e03 --- /dev/null +++ b/06_Lecture06/03_getch_p13_ncurses.c @@ -0,0 +1,21 @@ +#include +//#include +#include + +int main () +{ + char character; + initscr(); + noecho(); + //~ puts("Exit('.'):"); + printw("Exit('.'):\n"); + do + { + character = getch(); // считать введённый со стандартного потока ввода символ + if(character>='a' && character<='z') //все символы лежат подряд ‘a’=97, ‘b’=98, ‘c’=99, ... + addch('A' + (character-'a')); + else + addch(character); + } while (character != '.'); // пока введенный символ не точка + return 0; +}// работает в https://www.onlinegdb.com/ #include diff --git a/06_Lecture06/03_letter_add_p15.c b/06_Lecture06/03_letter_add_p15.c new file mode 100644 index 0000000..d569a30 --- /dev/null +++ b/06_Lecture06/03_letter_add_p15.c @@ -0,0 +1,16 @@ +#include + +int main() +{ + char c; + int sum=0; + while( (c=getchar())!='\n') //спец символ новой строки + { + if(c>='0' && c<='9') + sum+=c-0x30; + //~ sum+=c-'0'; + //~ sum+=c&0x0F; + } + printf("%d",sum); + return 0; +} diff --git a/06_Lecture06/04_numbers_combine_p16.c b/06_Lecture06/04_numbers_combine_p16.c new file mode 100644 index 0000000..4d1230b --- /dev/null +++ b/06_Lecture06/04_numbers_combine_p16.c @@ -0,0 +1,14 @@ +#include + +int main() +{ + char c; + int Number=0;//unsigned long long + while( (c=getchar())!='\n') //спец символ новой строки проверка на переполнения + { + if(c>='0' && c<='9') + Number = Number*10+c-'0';//if Number + } + printf("%d",Number); + return 0; +} diff --git a/06_Lecture06/05_function_1_p19.c b/06_Lecture06/05_function_1_p19.c new file mode 100644 index 0000000..785ab90 --- /dev/null +++ b/06_Lecture06/05_function_1_p19.c @@ -0,0 +1,15 @@ +#include + +int max (int a, int b) +{ + if (a>b) + return a; + return b; // Здесь можно обойтись без else +} +int main () +{ + int a,b; + scanf ("%d%d", &a,&b); + printf ("max = %d\n", max(a,b)); + return 0; +} diff --git a/06_Lecture06/06_function_2_p19.c b/06_Lecture06/06_function_2_p19.c new file mode 100644 index 0000000..9859379 --- /dev/null +++ b/06_Lecture06/06_function_2_p19.c @@ -0,0 +1,18 @@ +#include + +int max (int a, int b) +{ + if (a>b) + return a; + return b; +} +int main () +{ + int a,b; + scanf ("%d%d", &a,&b); + // Чтобы объявлять переменные не в начале нужен С99 + int result = max (a,b); //Результат функции присваем maximum, имя функции не должно совпадать с именем переменной, int max = max (a,b); https : //www.onlinegbd.com + printf ("max = %d\n", result); + return 0; +} + diff --git a/06_Lecture06/07_function_void_1_p21.c b/06_Lecture06/07_function_void_1_p21.c new file mode 100644 index 0000000..58b5d30 --- /dev/null +++ b/06_Lecture06/07_function_void_1_p21.c @@ -0,0 +1,18 @@ +#include + +void max(void) +{ + int a,b; //локальные переменные + scanf("%d%d",&a,&b); + if (a>b) + printf("%d\n",a); + else + printf("%d\n",b); +} + +int main () +{ + max(); + return 0; +} + diff --git a/06_Lecture06/08_function_void_p22.c b/06_Lecture06/08_function_void_p22.c new file mode 100644 index 0000000..1e33a13 --- /dev/null +++ b/06_Lecture06/08_function_void_p22.c @@ -0,0 +1,16 @@ +#include + +int max (int a, int b) +{ + if (a>b) + return a; + return b; +} + +int main() +{ + int a,b; + scanf("%d%d",&a,&b); + printf("max = %d\n", max(a,b)); + return 0; +} diff --git a/06_Lecture06/09_function_void_1_p23.c b/06_Lecture06/09_function_void_1_p23.c new file mode 100644 index 0000000..4563409 --- /dev/null +++ b/06_Lecture06/09_function_void_1_p23.c @@ -0,0 +1,20 @@ +#include + +int max (int, int); + +int main() +{ + int a,b; + scanf("%d%d",&a,&b); + printf("max = %d\n", max(a,b)); + return 0; +} + +int max (int a, int b) +{ + if (a>b) + return a; + return b; +} + + diff --git a/06_Lecture06/10_function_void_2_p23.c b/06_Lecture06/10_function_void_2_p23.c new file mode 100644 index 0000000..5d8ccf6 --- /dev/null +++ b/06_Lecture06/10_function_void_2_p23.c @@ -0,0 +1,18 @@ +#include + +int max (int a, int b); + +int main() +{ + int a,b; + scanf("%d%d",&a,&b); + printf("max = %d\n", max(a,b)); + return 0; +} + +int max (int a, int b) +{ + if (a>b) + return a; + return b; +} diff --git a/06_Lecture06/11_module_p29.c b/06_Lecture06/11_module_p29.c new file mode 100644 index 0000000..14e36dd --- /dev/null +++ b/06_Lecture06/11_module_p29.c @@ -0,0 +1,14 @@ +#include + +int abs(int num) +{ + return (num<0)?-num:num; +} + +int main() +{ + int num; + scanf("%d",&num); + printf("%d",abs(num)); + return 0; +} diff --git a/06_Lecture06/12_prime_check_p30.c b/06_Lecture06/12_prime_check_p30.c new file mode 100644 index 0000000..984ec13 --- /dev/null +++ b/06_Lecture06/12_prime_check_p30.c @@ -0,0 +1,24 @@ +#include + +int prime(int n) +{ + //~ int i=2; + //~ while (i*i<=n) + for(int i=2;i*i<=n;i++) + { + if (n%i==0) + return 0; + //~ i++; + } + return 1; +} + +int main() +{ + int num; + scanf("%d",&num); + prime(num) ? printf("Prime") : printf("Not prime"); +//printf("%s",prime(num) ? "Prime" : "Not prime"); +//if (prime(num)) printf("Prime"); else printf("Not prime"); + return 0; +} diff --git a/06_Lecture06/13_sqrt_final_34_37.c b/06_Lecture06/13_sqrt_final_34_37.c new file mode 100644 index 0000000..c0f9740 --- /dev/null +++ b/06_Lecture06/13_sqrt_final_34_37.c @@ -0,0 +1,87 @@ +#include +#include +#include +#include + +float InputFloat(char* message) +{ +float number; +static int counter = 0; + counter++; + printf("%d,%s",counter,message); + scanf("%f",&number); + return number; +} + +void CalcRealRoots(float sqrD,float B,float a) +{ +float X1,X2; + printf("Корни квадратного уравнения \n"); + float d = sqrtf(sqrD); + X1 = (-B + d)/a; //2 + printf("X1 = %f \n",X1); + X2 = (-B - d)/a; //16 + printf("X2 = %f \n",X2); +} + +//Сделаем вычисление корней квадратного уравнения отдельной функцией. +void SquareEquation(void) +{ + printf("Вычисление корней квадратного уравнения \\ \"a*x*x+b*x+с=0\"\n"); + float a = InputFloat("Введите a:\n");//1 + float b = InputFloat("Введите b:\n");//18 + float c = InputFloat("Введите c:\n");//32 + float B = b/2; + if(a!=0) + { + float d = B*B-a*c; + if(d<0) + { + printf("Корни квадратного уравнения комплексные \n"); + } + else + { + CalcRealRoots(d,B,a); + } + } + else + { + if(b!=0) + { + float X1 = -c/b; + printf("Корень линейного уравнения %f\n",X1); + } + else + { + printf("Корней НЕТ!\n"); + } + } +} + +int main(int argc, char **argv) +{ +char Choice; + setlocale(LC_ALL, ".utf-8"); + while(1) + { + printf("1. Вычисление корней квадратного уравнения\n"); + printf("0. Выход\n"); + printf("Для выход нажмите Q\n"); + Choice = _getch(); + switch(Choice) + { + case '1': + SquareEquation(); + break; + case '0': + case 'q': + case 'Q': + return 0; + break; + default: + printf("Непонятный выбор %x\n",Choice); + break; + } + } + return 0; +} diff --git a/06_Lecture06/14_fsm_p40.c b/06_Lecture06/14_fsm_p40.c new file mode 100644 index 0000000..36d00a2 --- /dev/null +++ b/06_Lecture06/14_fsm_p40.c @@ -0,0 +1,13 @@ +#include + +int main() { + int c; + do { + c = getchar(); + while (c == ' ') c = getchar(); + while (c != ' ' && c != '\n' && c != EOF) putchar(c), c = getchar(); + putchar('\n'); + while (c != '\n' && c != EOF) c = getchar(); + } while (c != EOF); + return 0; +} diff --git a/06_Lecture06/15_fsm_2_p41.c b/06_Lecture06/15_fsm_2_p41.c new file mode 100644 index 0000000..a97d482 --- /dev/null +++ b/06_Lecture06/15_fsm_2_p41.c @@ -0,0 +1,31 @@ +#include + +int main() { + enum states { before, inside, after } state; + int c; + state = before; + while ((c = getchar()) != EOF) { + switch (state) { + case before: + if (c == '\n') + putchar('\n'); + else if (c != ' ') + putchar(c), state = inside; + break; + case inside: + switch (c) { + case ' ': + state = after; break; + case '\n': + putchar('\n'), state = before; + break; + default: putchar(c); + } + break; + case after: + if (c == '\n') + putchar('\n'), state = before; + } + } + return 0; +} diff --git a/06_Lecture06/16_coffee_p43.c b/06_Lecture06/16_coffee_p43.c new file mode 100644 index 0000000..7a89a4c --- /dev/null +++ b/06_Lecture06/16_coffee_p43.c @@ -0,0 +1,68 @@ +#include +#include + +int Rubl_1 = 0,Rubl_2 = 0,Cancel = 0; +enum signals { RUBL_1, RUBL_2, CANCEL, NONE}; + + +void GetUserSignal(void) +{ + char Choice; + while(1) + { + printf("1.Put 1 rubl\n2.Put 2 rubl\n0.Cancel\n"); + Choice = _getch(); + switch(Choice) + { + case '1': Rubl_1=1; return; + case '2': Rubl_2=1; return; + case '0': Cancel=1; return; + } + } + } + +int main() +{ + enum states { READY, PREPFRE, WAIT, CHANGE, RETURN } state = READY; +// enum signals signal; + while (1) + { + switch (state) + { + case READY: + printf("Ready\n"); + GetUserSignal(); + if(Rubl_2) + state = PREPFRE; + if(Rubl_1) + state = WAIT; + break; + case PREPFRE: + printf("Preapare cofee\n"); + state = READY; + break; + case WAIT: + printf("Wait\n"); + GetUserSignal(); + if(Rubl_2) + state = CHANGE; + if(Rubl_1) + state = PREPFRE; + if(Cancel) + state = RETURN; + break; + case CHANGE: + printf("Change 1 Rubl\n"); + state = PREPFRE; + break; + case RETURN: + printf("Change 1 Rubl\n"); + state = READY; + break; + } + Rubl_1 = 0; + Rubl_2 = 0; + Cancel = 0; + } + return 0; +} diff --git a/06_Lecture06/16_coffee_p43refact.c b/06_Lecture06/16_coffee_p43refact.c new file mode 100644 index 0000000..ca4b160 --- /dev/null +++ b/06_Lecture06/16_coffee_p43refact.c @@ -0,0 +1,78 @@ +#include +#include + +enum signals { RUBL_1, RUBL_2, CANCEL}; +enum states { READY, PREPFRE, WAIT, CHANGE, RETURN }; + +enum signals GetUserSignal(void) +{ + char Choice; + while(1) + { + printf("1.Put 1 rubl\n2.Put 2 rubl\n0.Cancel\n"); + Choice = _getch(); + switch(Choice) + { + case '1': + return RUBL_1; + case '2': + return RUBL_2; + case '0': + return CANCEL; + } + } +} + +int main() +{ + enum states state = READY; + while (1) + { + switch (state) + { + case READY: + printf("Ready\n"); + switch(GetUserSignal()) + { + case (RUBL_2): + state = PREPFRE; + break; + case(RUBL_1): + state = WAIT; + break; + case(CANCEL): + printf("Error signal CANCEL\n"); + break; + } + break; + case PREPFRE: + printf("Preapare cofee\n"); + state = READY; + break; + case WAIT: + printf("Wait\n"); + switch(GetUserSignal()) + { + case(RUBL_2): + state = CHANGE; + break; + case(RUBL_1): + state = PREPFRE; + break; + case(CANCEL): + state = RETURN; + break; + } + break; + case CHANGE: + printf("Change 1 Rubl\n"); + state = PREPFRE; + break; + case RETURN: + printf("Change 1 Rubl\n"); + state = READY; + break; + } + } + return 0; +} diff --git a/07_Lecture07/00_func_p5.c b/07_Lecture07/00_func_p5.c new file mode 100644 index 0000000..f0f93d4 --- /dev/null +++ b/07_Lecture07/00_func_p5.c @@ -0,0 +1,15 @@ +#include + +void func(void) { + static int a=5; //статическая память + a++; + printf("a = %d\n",a); +} + +int main(void) +{ + func(); + func(); + func(); + return 0; +} diff --git a/07_Lecture07/01_pointer_p9.c b/07_Lecture07/01_pointer_p9.c new file mode 100644 index 0000000..2a4c8f3 --- /dev/null +++ b/07_Lecture07/01_pointer_p9.c @@ -0,0 +1,11 @@ +#include + +int main(void) +{ + int *p, n=0; //объявление переменной p - указатель на целочисленный объект + printf("n = %d\n", n); // n = 10 + p = &n; //присвоение адреса n в p + *p = 10; //n = 10 или положить значение по адресу в переменной p + printf("n = %d\n", n); // n = 10 + return 0; +} diff --git a/07_Lecture07/02_pointer_use_p10.c b/07_Lecture07/02_pointer_use_p10.c new file mode 100644 index 0000000..4f5563a --- /dev/null +++ b/07_Lecture07/02_pointer_use_p10.c @@ -0,0 +1,15 @@ +#include + +int main() { + int x, y, + *ptr; // объявляем 3 переменные + ptr = NULL; // инициализируем указатель null, нулевым значением + //~ printf("x = %d y = %d ptr=%p\n",x,y,ptr); + x = -7; + ptr = &x; // адрес переменной х записываем в переменную ptr + y = *ptr; // Записываем в y значение на которое указывает указатель ptr + *ptr = 3; // Записываем в ячейку (х) на которую ссылается указатель ptr число 3 + //std::cout << "x = " << x << " y = " << y; // вывод на экран x = 3 y = -7 + printf("x = %d y = %d ptr=%p\n",x,y,ptr); + return 0; +} diff --git a/07_Lecture07/03_pointer_to_pointer_p12.c b/07_Lecture07/03_pointer_to_pointer_p12.c new file mode 100644 index 0000000..5be23b6 --- /dev/null +++ b/07_Lecture07/03_pointer_to_pointer_p12.c @@ -0,0 +1,14 @@ +//Можно объявлять указатели на указатели: +#include + +int main() { + int a = 77; + printf("%d\n",a);// << std::endl; // 88 + int *ptrA = &a; + int** ppA = &ptrA; + *ptrA = 88; + printf("%d\n",a);// << std::endl; // 88 + **ppA = 99; + printf("%d\n",a);// << std::endl; // 99 + return 0; +} diff --git a/07_Lecture07/04_swap_p13.c b/07_Lecture07/04_swap_p13.c new file mode 100644 index 0000000..15eb6ae --- /dev/null +++ b/07_Lecture07/04_swap_p13.c @@ -0,0 +1,16 @@ +#include + +void swap(int a, int b) { + int tmp; + tmp = a; + a = b; + b = tmp; +} + +int main(void) +{ + int n=7, m=5; + printf("n = %d m = %d\n",n,m); + swap(n, m); // Передаются значения + printf("n = %d m = %d\n",n,m); +} diff --git a/07_Lecture07/05_swap_2_p15.c b/07_Lecture07/05_swap_2_p15.c new file mode 100644 index 0000000..66b68c4 --- /dev/null +++ b/07_Lecture07/05_swap_2_p15.c @@ -0,0 +1,16 @@ +#include + +void swap(int *pa, int *pb) { + int tmp; + tmp = *pa; + *pa = *pb; + *pb = tmp; +} + +int main(void) +{ + int n=7, m=5; + printf("n = %d m = %d\n",n,m); + swap(&n, &m); // Передается адрес + printf("n = %d m = %d\n",n,m); +} diff --git a/07_Lecture07/06_rec_p19.c b/07_Lecture07/06_rec_p19.c new file mode 100644 index 0000000..db7974e --- /dev/null +++ b/07_Lecture07/06_rec_p19.c @@ -0,0 +1,15 @@ +#include + +void rec(int n) +{ + //~ printf("%5d",n); + if(n>0) + rec(n-1); + printf("%5d",n); +} + +int main(void) +{ + rec(3); + return 0; +} diff --git a/07_Lecture07/07_rec_fact_p21.c b/07_Lecture07/07_rec_fact_p21.c new file mode 100644 index 0000000..512cef8 --- /dev/null +++ b/07_Lecture07/07_rec_fact_p21.c @@ -0,0 +1,15 @@ +#include + +unsigned int factorial(unsigned int n) { + printf("%d\n",n); + if(n<=1) // Условие остановки + return 1; + int _f = n * factorial(n-1); + printf("%d*factorial(%d)=%d\n",n,n-1,_f); + return _f; // Шаг +} + +int main() { + factorial(3); + return 0; +} diff --git a/07_Lecture07/08_sum_p24.c b/07_Lecture07/08_sum_p24.c new file mode 100644 index 0000000..e023eb0 --- /dev/null +++ b/07_Lecture07/08_sum_p24.c @@ -0,0 +1,21 @@ +#include + +//Нерекурсивный способ: +int sumIter(int num) +{ + int sum = 0; + while(num > 0) + { + sum += num % 10; + num /= 10; + } + return sum; +} + +int main() +{ + int numb; + scanf("%d",&numb); + printf("%d\n",sumIter(numb)); + return 0; +} diff --git a/07_Lecture07/09_sum_rec_p25.c b/07_Lecture07/09_sum_rec_p25.c new file mode 100644 index 0000000..9b8993a --- /dev/null +++ b/07_Lecture07/09_sum_rec_p25.c @@ -0,0 +1,18 @@ +#include + +//Рекурсивный способ: +int sumRec(int num) +{ + if (num > 0) + return num % 10 + sumRec(num / 10); + else + return 0; +} + +int main() +{ + int numb; + scanf("%d",&numb); + printf("%d\n",sumRec(numb)); + return 0; +} diff --git a/07_Lecture07/10_fibonachi_25.c b/07_Lecture07/10_fibonachi_25.c new file mode 100644 index 0000000..0cf670c --- /dev/null +++ b/07_Lecture07/10_fibonachi_25.c @@ -0,0 +1,15 @@ +#include + +int fibonachi(int n) +{ + if(n<=0) + return 0; + if(n==1) + return 1; + return fibonachi(n - 1) + fibonachi(n - 2); +} + +int main(void) +{ + printf("%d\n",fibonachi(6)); +} diff --git a/07_Lecture07/11_2_for_rec_p33.c b/07_Lecture07/11_2_for_rec_p33.c new file mode 100644 index 0000000..8215404 --- /dev/null +++ b/07_Lecture07/11_2_for_rec_p33.c @@ -0,0 +1,38 @@ +#include + +void iterFor(int start,int n) +{ + for(int i=start; i + +int fibonachi(int n) +{ + printf("n=%d\n",n); + if(n <= 0) + { + //printf("F(%d)=0\n",n); + printf("return 0\n"); + return 0; + } + if(n == 1) + { + printf("F(1)=1\n"); + //printf("F(%d)=1\n",n); + return 1; + } + + int Fib = fibonachi(n - 1)+ fibonachi(n - 2); + printf("Fib(%d)=%d\n",n,Fib); + return Fib; +} + +int main(void) +{ + printf("%d\n",fibonachi(6)); + return 0; +} diff --git a/07_Lecture07/12_print_rev_p36.c b/07_Lecture07/12_print_rev_p36.c new file mode 100644 index 0000000..9123a8b --- /dev/null +++ b/07_Lecture07/12_print_rev_p36.c @@ -0,0 +1,18 @@ +#include + +void print_rev (void) +{ + char ch; + scanf ("%c", &ch); //ввод очередного символа c=getchar(); + if (ch != '.') + print_rev (); //рекурсивный вызов для обработки оставшихся символов + else + return; + printf ("%c", ch); //вывод символа +} + +int main() +{ + print_rev(); + return 0; +} diff --git a/07_Lecture07/13_gcd_p37.c b/07_Lecture07/13_gcd_p37.c new file mode 100644 index 0000000..16f1385 --- /dev/null +++ b/07_Lecture07/13_gcd_p37.c @@ -0,0 +1,21 @@ +#include + +int gcd ( int n, int m ) +{ + if(n == m) + return n ; + if (n < m) + return gcd(n,m - n ); + return gcd(n - m,m ); +} + +int main() +{ + int a,b; + printf("a="); + scanf("%d",&a); + printf("b="); + scanf("%d",&b); + printf("GCD=%d",gcd(a,b)); + return 0; +} diff --git a/07_Lecture07/14_dec_to_bin_p38.c b/07_Lecture07/14_dec_to_bin_p38.c new file mode 100644 index 0000000..33bf010 --- /dev/null +++ b/07_Lecture07/14_dec_to_bin_p38.c @@ -0,0 +1,21 @@ +#include + +void dec_to_bin(int n) +{ + if (n >= 2) + dec_to_bin(n / 2); + printf("%d", n % 2); + //std::cout << n % 2; +} +int main() +{ + int n; + printf("n -> "); + //std::cout << "\n\nn -> "; + scanf("%d",&n); + //std::cin >> n; + printf("Bin = "); + //std::cout << "\n\nBin = "; + dec_to_bin(n); + return 0; +} diff --git a/07_Lecture07/15_max_find_p39.c b/07_Lecture07/15_max_find_p39.c new file mode 100644 index 0000000..1d2c818 --- /dev/null +++ b/07_Lecture07/15_max_find_p39.c @@ -0,0 +1,20 @@ +#include +#include + +uint32_t max_find() +{ + uint32_t number, max; + scanf("%u", &number); + if(number == 0) + return 0; + max = max_find(); + if (max < number) + max = number; + return max; +} + +int main() +{ + printf("Max=%u",max_find()); + return 0; +} diff --git a/08_Lecture08/00_array_p8.c b/08_Lecture08/00_array_p8.c new file mode 100644 index 0000000..f12f3cd --- /dev/null +++ b/08_Lecture08/00_array_p8.c @@ -0,0 +1,15 @@ +#include + +int main(int argc, char **argv) +{ + int n; + scanf ("%d", &n); // ввод количества элементов массива + int arr[n]; + for (int i =0; i < n; i++) { // ввод массива + scanf ("%d", &arr[i]); + } + for (int i =0; i < n; i++) { // ввод массива + printf ("%d ", arr[i]); + } + return 0; +} diff --git a/08_Lecture08/01_arr_mult_p10.c b/08_Lecture08/01_arr_mult_p10.c new file mode 100644 index 0000000..57b03ea --- /dev/null +++ b/08_Lecture08/01_arr_mult_p10.c @@ -0,0 +1,37 @@ +#include +#include + +#define SIZE 5 + +int Input(int arr[], int n) +{ + int i; + for(i=0;i +#include + +#define SIZE 5 + +int Input(int* arr, int n) +{ + int i; + for(i=0;i arr[i]) + min = arr[i]; + } + return min; +} + +int main(int argc, char **argv) +{ + int arr[SIZE]={0}; + Input(arr,SIZE); + printf("Min = \t\t%d\n",Min(arr,SIZE)); + return 0; +} diff --git a/08_Lecture08/03_arr_swap_p14.c b/08_Lecture08/03_arr_swap_p14.c new file mode 100644 index 0000000..b960076 --- /dev/null +++ b/08_Lecture08/03_arr_swap_p14.c @@ -0,0 +1,60 @@ +#include +#include + +#define SIZE 5 + +int Input(int* arr, int n) +{ + int i; + for(i=0;i arr[i]) + { + min = arr[i]; + pos = i; + } + return pos; +} + +int PosMax(int *arr,int len) +{ + int max=arr[0],i,pos=0; + for (i = 1; i < len; i++) + if (max < arr[i]) + { + max = arr[i]; + pos = i; + } + return pos; +} + +void SwapArr(int *arr,int i,int j) +{ + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; +} + +int main(int argc, char **argv) +{ + int arr[SIZE]={0}; + Input(arr,SIZE); + SwapArr(arr,PosMin(arr,SIZE),PosMax(arr,SIZE)); + Print(arr,SIZE); + return 0; +} diff --git a/08_Lecture08/04_arr_bubblesort_p17.c b/08_Lecture08/04_arr_bubblesort_p17.c new file mode 100644 index 0000000..47fd184 --- /dev/null +++ b/08_Lecture08/04_arr_bubblesort_p17.c @@ -0,0 +1,55 @@ +#include +#include + +#define SIZE 5 + +int Input(int* arr, int n) +{ + int i; + for(i=0;i i; j--) + { + if(arr[j-1]>arr[j]) + { + SwapArr(arr,j-1,j); + noSwap = 0; + } + } + if(noSwap) + break; + } +} + +int main(int argc, char **argv) +{ + int arr[SIZE]={0}; + Input(arr,SIZE); + BubbleSort(arr,SIZE); + Print(arr,SIZE); + return 0; +} diff --git a/08_Lecture08/05_arr_bubblesort_2_p18.c b/08_Lecture08/05_arr_bubblesort_2_p18.c new file mode 100644 index 0000000..8bb4cd0 --- /dev/null +++ b/08_Lecture08/05_arr_bubblesort_2_p18.c @@ -0,0 +1,55 @@ +#include +#include + +#define SIZE 5 + +int Input(int* arr, int n) +{ + int i; + for(i=0;i=0; i--) + { + noSwap = 1; + for(j=0; jarr[j+1]) + { + SwapArr(arr,i,j); + noSwap = 0; + } + } + if(noSwap) + break; + } +} + +int main(int argc, char **argv) +{ + int arr[SIZE]={0}; + Input(arr,SIZE); + BubbleSort(arr,SIZE); + Print(arr,SIZE); + return 0; +} diff --git a/08_Lecture08/06_arr_all_p19.c b/08_Lecture08/06_arr_all_p19.c new file mode 100644 index 0000000..16bcc30 --- /dev/null +++ b/08_Lecture08/06_arr_all_p19.c @@ -0,0 +1,105 @@ +#include +#include + +#define SIZE 5 +#define COEF 3 + +int Input(int arr[], int n) +{ + //~ static int test[3] = {0}; + int i; + for(i=0;i arr[i]) + min = arr[i]; + } + return min; +} + +int PosMin(int *arr,int len) +{ + int min=arr[0],i,pos=0; + for (i = 1; i < len; i++) + if (min > arr[i]) + { + min = arr[i]; + pos = i; + } + return pos; +} + +int PosMax(int *arr,int len) +{ + int max=arr[0],i,pos=0; + for (i = 1; i < len; i++) + if (max < arr[i]) + { + max = arr[i]; + pos = i; + } + return pos; +} + +void SwapArr(int *arr,int i,int j) +{ + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; +} + +void BubbleSort(int* arr,int n) +{ + int noSwap; + for (int i = 0; i < n; i++) + { + noSwap = 1; + for (int j = n-1; j > i; j--) + { + if(arr[j-1]>arr[j]) + { + SwapArr(arr,j-1,j); + noSwap = 0; + } + } + if(noSwap) + break; + } +} + + +int main(void) +{ + int arr[SIZE]; + Input(&arr[0],SIZE); + Mult(arr,SIZE,COEF); + Print(arr,SIZE); + printf("Min = %d\n",Min(arr,SIZE)); + SwapArr(arr, PosMin(arr,SIZE), PosMax(arr,SIZE)); + Print(arr,SIZE); + BubbleSort(arr,SIZE); + Print(arr,SIZE); + return 0; +} diff --git a/08_Lecture08/07_float_bin_p27.c b/08_Lecture08/07_float_bin_p27.c new file mode 100644 index 0000000..13b6a59 --- /dev/null +++ b/08_Lecture08/07_float_bin_p27.c @@ -0,0 +1,22 @@ +#include + +//Перевод вещественного числа в двоичный вид +void print_float_bin(float num) { + unsigned int* fi = (unsigned int*)# + for(int i=31;i>=0;i--) { + if(i==30 || i==22) + putchar(' '); + if(*fi&(1< +#include + + +//Перевод вещественного числа в двоичный вид +void print_float_bin(float num) { + unsigned int* fi = (unsigned int*)# + for(int i=31; i>=0; i--) { + if(i==30 || i==22) + putchar(' '); + if(*fi&(1< +#define N 11 + +int main(int argc, char **argv) +{ + { + char s[10];// Строка из 9 значимых символов + } + { + char s[N];// Строка из N-1 значимых символов, в строке всегда есть символ \0 + } + { + char st[] = "hello"; + // st[0]='h' st[1]='e' st[2]=’l’ st[3]=’l’ st[4]='o' st[5]='\0' + printf("%llu\n", sizeof(st)); // 6 + } + { + char st[10] = "hello"; + // st[0]='h' st[1]='e' st[2]=’l’ st[3]=’l’ st[4]='o' st[5]='\0' + printf("%llu\n", sizeof(st)); // + } + return 0; +} diff --git a/09_Lecture09/00_st_scanf_printf_p7.c b/09_Lecture09/00_st_scanf_printf_p7.c new file mode 100644 index 0000000..8e057c8 --- /dev/null +++ b/09_Lecture09/00_st_scanf_printf_p7.c @@ -0,0 +1,9 @@ +#include + +int main(int argc, char **argv) +{ + char s[10]; + scanf("%s",s); // считать строку до первого пробельного символа или \n + printf("%s",s); // напечатать строку + return 0; +} diff --git a/09_Lecture09/01__p10.c b/09_Lecture09/01__p10.c new file mode 100644 index 0000000..5c41934 --- /dev/null +++ b/09_Lecture09/01__p10.c @@ -0,0 +1,13 @@ +#include + +int main(int argc, char **argv) +{ + char s[100]=""; + int r; + //~ r=scanf("%[a-z]",s);// считать стр буквы + //~ r=scanf("%[0-9]",s);// считать цифры + r=scanf("%[^\n]",s);// все кроме \n + + printf("%s %d\n",s,r); + +} diff --git a/09_Lecture09/01__p12.c b/09_Lecture09/01__p12.c new file mode 100644 index 0000000..9c6f928 --- /dev/null +++ b/09_Lecture09/01__p12.c @@ -0,0 +1,10 @@ +#include + +int main(int argc, char **argv) +{ + char s1[100]="", s2[100]=""; + int r; + r=scanf("%[0-9]=%[a-z]",s1,s2); + printf("r = %d\n",r); + +} diff --git a/09_Lecture09/01_getchar_p8.c b/09_Lecture09/01_getchar_p8.c new file mode 100644 index 0000000..73ad461 --- /dev/null +++ b/09_Lecture09/01_getchar_p8.c @@ -0,0 +1,14 @@ +#include + +int main(int argc, char **argv) +{ + char s[100], c; + int i=0; + while( (c=getchar())!='\n' ) + s[i++]=c; + s[i]='\0'; + i=0; + while( s[i] ) // s[i] != 0 + putchar(s[i++]); + printf("\n%s\n",s); // напечатать строку +} diff --git a/09_Lecture09/02_strlen_p15.c b/09_Lecture09/02_strlen_p15.c new file mode 100644 index 0000000..c2affa5 --- /dev/null +++ b/09_Lecture09/02_strlen_p15.c @@ -0,0 +1,9 @@ +#include +#include + +int main(void) { + char st[10] = "hello"; + printf("Sizeof = %llu\n", sizeof(st)); + printf("Strlen = %llu\n", strlen(st)); + return 0; +} diff --git a/09_Lecture09/03_strlen find_p16.c b/09_Lecture09/03_strlen find_p16.c new file mode 100644 index 0000000..668324c --- /dev/null +++ b/09_Lecture09/03_strlen find_p16.c @@ -0,0 +1,16 @@ +#include + +int my_strlen(const char *src) +{ + int len=0; + while (*src++)//*src++!=0 + len++; + return len; +} + +int main(int argc, char **argv) +{ +char* str={"Hello!"}; + printf("%d\n",my_strlen(str)); + return 0; +} diff --git a/09_Lecture09/04__p17.c b/09_Lecture09/04__p17.c new file mode 100644 index 0000000..6dcece0 --- /dev/null +++ b/09_Lecture09/04__p17.c @@ -0,0 +1,12 @@ +#include + +int main(int argc, char **argv) +{ + char st1[10] = "hello"; + char st2[10] = "hello"; + if(st1 == st2) + printf("Yes"); + else + printf("No"); + return 0; +} diff --git a/09_Lecture09/04_strcmp_optim_p18_19.c b/09_Lecture09/04_strcmp_optim_p18_19.c new file mode 100644 index 0000000..e4a2a98 --- /dev/null +++ b/09_Lecture09/04_strcmp_optim_p18_19.c @@ -0,0 +1,30 @@ +#include +#include + +int my_strcmp(const char *a, const char *b) +{ + while ( *a && *b && *a == *b ) + ++a, ++b; + return *a - *b; +} + +void Print(char* str,int res) +{ + printf(str, res==0 ? "equal to" : res<0 ? "less" : "greater than"); +} + +int main(void) +{ + char *a = "abcde", + *b = "xyz", + *c = "abcd", + *d = "xyz"; + printf("A = %s\nB = %s\nC = %s\nD = %s\n\n", a, b, c, d); + Print("A is %s B\n",my_strcmp(a,b)); + Print("A is %s D\n",my_strcmp(a,d)); + Print("A is %s C\n",my_strcmp(a,c)); + Print("B is %s C\n",my_strcmp(b,c)); + Print("B is %s D\n",my_strcmp(b,d)); + Print("C is %s D\n",my_strcmp(c,d)); + return 0; +} diff --git a/09_Lecture09/05_strcpy_p20.c b/09_Lecture09/05_strcpy_p20.c new file mode 100644 index 0000000..c51ea11 --- /dev/null +++ b/09_Lecture09/05_strcpy_p20.c @@ -0,0 +1,16 @@ +#include + +char *strcpy (char *dst, char *src) +{ +char *ptr = dst; + while(*dst++=*src++); + return ptr; +} +int main(int argc, char **argv) +{ +char str1[]={"Hello!"};//char* str1 = {"Hello!"}; +char str2[]={"World!"};//char* str2={"World!"} + printf("%s\n",strcpy(str2,str1)); + printf("%s\n",str2); + return 0; +} diff --git a/09_Lecture09/06_mystrcpy_p21.c b/09_Lecture09/06_mystrcpy_p21.c new file mode 100644 index 0000000..1dd323b --- /dev/null +++ b/09_Lecture09/06_mystrcpy_p21.c @@ -0,0 +1,27 @@ +#include + +char *my_strcpy2(char *dst, char *src) +{ +char *ptr = dst; + while((*dst++=*src++))//(*dst++=*src++)!=0 + {} + return ptr; +} + +char *my_strcpy1(char *dst, char *src) +{ + for (char* ptr = dst; *src; src++,ptr++) + *ptr = *src; + return dst; +} + + +int main(int argc, char **argv) +{ +char str1[]={"Hello!"};//char* str1 = {"Hello!"}; +char str2[]={"World!"};//char* str2={"World!"} + printf("%s\n",my_strcpy2(str2,str1)); + printf("%s\n",str2); + printf("%s\n",str1); + return 0; +} diff --git a/09_Lecture09/07_words_p23.c b/09_Lecture09/07_words_p23.c new file mode 100644 index 0000000..079bedf --- /dev/null +++ b/09_Lecture09/07_words_p23.c @@ -0,0 +1,14 @@ +#include + +int main(void) +{ + char s[100]; + int count=0; + while(scanf("%s",s)==1) + { + count++; + printf("In this text %d words\n",count); + } + //printf("In this text %d words\n",count); + return 0; +} diff --git a/09_Lecture09/08__p25.c b/09_Lecture09/08__p25.c new file mode 100644 index 0000000..7d51c6a --- /dev/null +++ b/09_Lecture09/08__p25.c @@ -0,0 +1,18 @@ +#include +#include + +int main(void) +{ + //~ int a[5]; + //~ int b[5]; + //~ a = b; // так делать нельзя + + int a[5]={1,2,3,4,5}; + //~ int a[5]={0}; + int *pa; + pa = a; // так можно + pa = &a[0]; // и так + printf("%d\n",pa[3]); + return 0; + +} diff --git a/09_Lecture09/08_sizeof_p27.c b/09_Lecture09/08_sizeof_p27.c new file mode 100644 index 0000000..bd03ede --- /dev/null +++ b/09_Lecture09/08_sizeof_p27.c @@ -0,0 +1,12 @@ +#include +#include + +int main(void) +{ + int a[5]; + int *pa; + pa = a; + printf("sizeof(a) = %llu\n", sizeof(a)); + printf("size a = %llu\n", sizeof(a)/sizeof(a[0])); + printf("sizeof(pa) = %llu\n", sizeof(pa)); +} diff --git a/09_Lecture09/09_StrToHexMas_p36_37.c b/09_Lecture09/09_StrToHexMas_p36_37.c new file mode 100644 index 0000000..876fe30 --- /dev/null +++ b/09_Lecture09/09_StrToHexMas_p36_37.c @@ -0,0 +1,59 @@ +#include +#include +#include + +//преобразование hex-цифры в dec-число +int CharToHex(char c) +{ + int result=-1; + if (c>='0' && c<='9') + result=c-'0'; + else if(c>='A' && c<='F') + result=c-'A'+10; + else if(c>='a' && c<='f') + result=c-'a'+10; + return result; +} +int StrToHexMas(char* Str,uint8_t* mas); + +int main(int argc, char **argv) +{ +uint8_t arr[10]; +int len = StrToHexMas("AAa a 1 15",arr); + printf("%s\n","AAa a 1 15"); + printf("%d\n",len); + for(int i=0;i=0) //если это значащий символ + { + Result = data; + if(i=0) //если это данные + { + Result *= 16; + Result += data; + } + } + mas[index++]=Result; //кладем число в массив + } + } + return index; +} diff --git a/09_Lecture09/09__p31.c b/09_Lecture09/09__p31.c new file mode 100644 index 0000000..d073d14 --- /dev/null +++ b/09_Lecture09/09__p31.c @@ -0,0 +1,35 @@ +#include +#include + +void print_matrix_1 (int m, int n, int *a) +{ + int i, j; + for (i = 0; i < m; i++) { + for (j = 0; j < n; j++) { + printf ("%d ", a[i * n + j]); + } + printf ("\n"); + } +} + +void print_matrix_2 (int m, int n, int a[m][n]) +{ + int i, j; + for (i = 0; i < m; i++) { + for (j = 0; j < n; j++) { + printf ("%d ", a[i][j]); + } + printf ("\n"); + } +} + + + + +int main(void) +{ + int matr[3][5]={{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5}}; // 3 строки и 5 столбцов + print_matrix_1(3,5,(int*)matr); + print_matrix_2(3,5,matr); + return 0; +} diff --git a/09_Lecture09/10_pointer_increase_p40.c b/09_Lecture09/10_pointer_increase_p40.c new file mode 100644 index 0000000..c0df4bb --- /dev/null +++ b/09_Lecture09/10_pointer_increase_p40.c @@ -0,0 +1,9 @@ +#include + +int main() +{ + int a[5]= {10,20,30,40,50}; + printf("&a[0] = %p\n", &a[0]); + printf("&a[1] = %p\n", &a[1]); + return 0; +} diff --git a/09_Lecture09/11_pointer_increase_p42.c b/09_Lecture09/11_pointer_increase_p42.c new file mode 100644 index 0000000..fa4b51d --- /dev/null +++ b/09_Lecture09/11_pointer_increase_p42.c @@ -0,0 +1,12 @@ +#include + +int main() +{ + int a[5] = {10,20,30,40,50}; + int *pa, n; + pa = a; + n = *(pa+2); // a[2] + //~ n = pa[2]; + printf("n = %d\n", n); + return 0; +} diff --git a/09_Lecture09/12_pointer_increase_p44.c b/09_Lecture09/12_pointer_increase_p44.c new file mode 100644 index 0000000..3914f96 --- /dev/null +++ b/09_Lecture09/12_pointer_increase_p44.c @@ -0,0 +1,11 @@ +#include + +int main() +{ + int a[5] = {10,20,30,40,50}; + int *pa, n=1; + pa = a; + *pa++ = n+3; + printf("%d,%d,%d,%d,%d *pa=%d",a[0],a[1],a[2],a[3],a[4],*pa); + return 0; +} diff --git a/09_Lecture09/12_pointer_increase_p45.c b/09_Lecture09/12_pointer_increase_p45.c new file mode 100644 index 0000000..d0991dc --- /dev/null +++ b/09_Lecture09/12_pointer_increase_p45.c @@ -0,0 +1,12 @@ +#include + +int main() +{ + int a[5] = {10,20,30,40,50}; + int *pa, n=10; + pa = a+2; + n = ++*pa; // ++(*pa) + //n = *(++pa);//n=40 + printf("%d\n",n); + return 0; +} diff --git a/09_Lecture09/13_scalar_p47-48.c b/09_Lecture09/13_scalar_p47-48.c new file mode 100644 index 0000000..2159351 --- /dev/null +++ b/09_Lecture09/13_scalar_p47-48.c @@ -0,0 +1,25 @@ +#include +#define SIZE 3 + +float scalar(float*arrA,float*arrB,int len) +{ + float result = 0; + for(int i=0;i + +int main(void){ + FILE *f; + f = fopen("in.txt","w"); // открытие файла in.txt на запись + fclose(f); //Закрытие файла. После окончания работы с +//файлом необходимо убедиться, что все записанные данные попали +//на диск, и освободить все ресурсы, связанные с ним. + return 0; +} diff --git a/10_Lecture10/01_file_sum_p15.c b/10_Lecture10/01_file_sum_p15.c new file mode 100644 index 0000000..6925148 --- /dev/null +++ b/10_Lecture10/01_file_sum_p15.c @@ -0,0 +1,12 @@ +#include + +int main(void){ +FILE *f; + int sum = 0, n; + f = fopen("in.txt", "r"); + while (fscanf (f, "%d", &n) == 1) + sum += n; + fclose (f); + printf ("%d\n", sum); + return 0; +} diff --git a/10_Lecture10/02_file_ftell_p17.c b/10_Lecture10/02_file_ftell_p17.c new file mode 100644 index 0000000..d61ccaf --- /dev/null +++ b/10_Lecture10/02_file_ftell_p17.c @@ -0,0 +1,19 @@ +#include + +int main(void){ +FILE *f; + static char filename[100]={0}; + size_t size; + printf("Input file name: "); + scanf("%s",filename); + f = fopen (filename, "r"); + if (f != NULL) { + fseek (f, 0, SEEK_END); + size = ftell (f); + fclose (f); + printf ("File size of '%s' - %lu bytes.\n",filename, size); + } else { + printf ("Can't open file %s\n", filename); + } + return 0; +} diff --git a/10_Lecture10/03_file_sum_end_p19.c b/10_Lecture10/03_file_sum_end_p19.c new file mode 100644 index 0000000..c964037 --- /dev/null +++ b/10_Lecture10/03_file_sum_end_p19.c @@ -0,0 +1,16 @@ +#include + +int main(void){ +FILE *f; + int sum = 0, n; + signed char c;// обязательно signed! иначе зациклится + f = fopen("in.txt", "r+"); // режим чтение и дозапись + while ( (c=fgetc(f))!=EOF ) { + if(c>='0' && c<='9') { + sum += c-'0'; + } + } + fprintf (f, " %d", sum); + fclose (f); + return 0; +} diff --git a/10_Lecture10/04_file_fread_p26.c b/10_Lecture10/04_file_fread_p26.c new file mode 100644 index 0000000..6fb2bd5 --- /dev/null +++ b/10_Lecture10/04_file_fread_p26.c @@ -0,0 +1,14 @@ +#include + +int main(void){ + unsigned int i = 0x31323334, u=0; + FILE *f = fopen ("out.bin", "wb"); + fwrite (&i, sizeof (int), 1, f); // данные запишутся в формате little-endian + fclose(f); + + f = fopen ("out.bin", "rb"); + fread (&u, 1, 1, f); + fclose(f); + printf("u = %x\n",u); // u = 34 + return 0; +} diff --git a/10_Lecture10/05_students_p36_38.c b/10_Lecture10/05_students_p36_38.c new file mode 100644 index 0000000..9a32288 --- /dev/null +++ b/10_Lecture10/05_students_p36_38.c @@ -0,0 +1,86 @@ +#include +#include +#include + +#define STR_SIZE 30 +#define STUDEN_NUMBER 200 + +struct student { + char surname[STR_SIZE]; + char name[STR_SIZE]; + uint8_t age; +}; +//возраст самого старшего человека; +int Eldest(struct student* course,int number){ + int max = course->age; + for(int i=1;iage) + max = (course+i)->age; + return max; +} + +//количество людей с заданным +//именем (имя также является +//параметром функции); +int SameNameNumber(struct student* course,int number,char* name) +{ + int counter = 0; + for(int i=0;i +#include +#include + +#define SIZE 30 + +struct sensor { + uint8_t day; + uint8_t month; + uint16_t year; + int8_t t; +}; + +void cgangeIJ(struct sensor* info,int i, int j){ +struct sensor temp; + temp=info[i]; + info[i]=info[j]; + info[j]=temp; +} +//упорядочивающую его по неубыванию температуры +void SortByT(struct sensor* info,int n){ + for(int i=0; i=info[j].t) + cgangeIJ(info,i,j); +} + +unsigned int DateToInt(struct sensor* info){ + return info->year << 16 | info->month << 8 | info->day; +} +//упорядочивающую его по дате +void SortByDate(struct sensor* info,int n){ + for(int i=0; i= DateToInt(info+j)) + cgangeIJ(info,i,j); +} + +void AddRecord(struct sensor* info,int number, +uint16_t year,uint8_t month,uint8_t day,int8_t t){ + info[number].year = year; + info[number].month = month; + info[number].day = day; + info[number].t = t; +} + +int AddInfo(struct sensor* info){ +int counter=0; + AddRecord(info,counter++,2021,9,16,9); + AddRecord(info,counter++,2022,9,2,-9); + AddRecord(info,counter++,2021,1,7,8); + AddRecord(info,counter++,2021,9,5,1); + return counter; +} + +void print(struct sensor* info,int number){ + printf("===================================\n"); + for(int i=0;i +extern int m; // Глобальная переменная. Видна из обоих файлов + +int max(int a, int b) +{ + m=5; + printf("m=%d\n",m); + return a>b? a : b; +} diff --git a/11_Lecture11/Module01_p7/func.o b/11_Lecture11/Module01_p7/func.o new file mode 100644 index 0000000..ffd166e Binary files /dev/null and b/11_Lecture11/Module01_p7/func.o differ diff --git a/11_Lecture11/Module01_p7/hello.c b/11_Lecture11/Module01_p7/hello.c new file mode 100644 index 0000000..4a42a83 --- /dev/null +++ b/11_Lecture11/Module01_p7/hello.c @@ -0,0 +1,7 @@ +#include + +int main(void) +{ + printf("Hello world!\n"); + return 0; +} diff --git a/11_Lecture11/Module01_p7/main.c b/11_Lecture11/Module01_p7/main.c new file mode 100644 index 0000000..4eeca8a --- /dev/null +++ b/11_Lecture11/Module01_p7/main.c @@ -0,0 +1,15 @@ +#include + +int max(int, int); // Функция описана в др файле +int m=0; // Глобальная переменная. Видна из обоих файлов + +int main(void) +{ + int a,b; + scanf("%d%d",&a,&b); + + printf("max(%d %d)=%d \n",a,b,max(a,b)); + printf("m=%d\n",m); + + return 0; +} diff --git a/11_Lecture11/Module01_p7/main.o b/11_Lecture11/Module01_p7/main.o new file mode 100644 index 0000000..22ccaf0 Binary files /dev/null and b/11_Lecture11/Module01_p7/main.o differ diff --git a/11_Lecture11/Module01_p7/makefile b/11_Lecture11/Module01_p7/makefile new file mode 100644 index 0000000..d263211 --- /dev/null +++ b/11_Lecture11/Module01_p7/makefile @@ -0,0 +1,14 @@ +all: prog + +prog: main.o func.o + gcc -g -o main main.o func.o + +main.o: main.c + gcc -g -c -o main.o main.c + +func.o: func.c + gcc -g -c -o func.o func.c + +clean: + del *.o + del prog.exe diff --git a/11_Lecture11/Module02_p9/.vscode/settings.json b/11_Lecture11/Module02_p9/.vscode/settings.json new file mode 100644 index 0000000..9090ba5 --- /dev/null +++ b/11_Lecture11/Module02_p9/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "func.h": "c", + "stdio.h": "c" + } +} \ No newline at end of file diff --git a/11_Lecture11/Module02_p9/func.c b/11_Lecture11/Module02_p9/func.c new file mode 100644 index 0000000..5110186 --- /dev/null +++ b/11_Lecture11/Module02_p9/func.c @@ -0,0 +1,6 @@ +#include "func.h" + +int max(int a, int b) +{ + return a > b ? a : b; +} diff --git a/11_Lecture11/Module02_p9/func.h b/11_Lecture11/Module02_p9/func.h new file mode 100644 index 0000000..fe64c04 --- /dev/null +++ b/11_Lecture11/Module02_p9/func.h @@ -0,0 +1 @@ +int max(int, int); diff --git a/11_Lecture11/Module02_p9/main.c b/11_Lecture11/Module02_p9/main.c new file mode 100644 index 0000000..a8f29fd --- /dev/null +++ b/11_Lecture11/Module02_p9/main.c @@ -0,0 +1,13 @@ +#include +#include "func.h" + +int m; + +int main(void) +{ + int a,b,c; + scanf("%d%d",&a,&b); + m = max(a,b); + printf("max(%d %d) = %d\n", a, b, m); + return 0; +} diff --git a/11_Lecture11/Module02_p9/makefile b/11_Lecture11/Module02_p9/makefile new file mode 100644 index 0000000..869f9e3 --- /dev/null +++ b/11_Lecture11/Module02_p9/makefile @@ -0,0 +1,13 @@ +all: prog + +prog: main.o func.o + gcc -o prog main.o func.o + +main.o: main.c func.h + gcc -c -o main.o main.c + +func.o: func.c + gcc -c -o func.o func.c + +clean: + rm -rf *.o prog \ No newline at end of file diff --git a/11_Lecture11/Module03_p11/.vscode/settings.json b/11_Lecture11/Module03_p11/.vscode/settings.json new file mode 100644 index 0000000..3f1de6b --- /dev/null +++ b/11_Lecture11/Module03_p11/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "mylibrary.h": "c" + } +} \ No newline at end of file diff --git a/11_Lecture11/Module03_p11/func.c b/11_Lecture11/Module03_p11/func.c new file mode 100644 index 0000000..3aa658f --- /dev/null +++ b/11_Lecture11/Module03_p11/func.c @@ -0,0 +1,11 @@ +#include "mylibrary.h" + +int max_3(int a, int b, int c) +{ + return max(max(a,b),c); +} + +static int max(int a, int b) +{ + return a > b ? a : b; +} diff --git a/11_Lecture11/Module03_p11/lib2.h b/11_Lecture11/Module03_p11/lib2.h new file mode 100644 index 0000000..d3a57f7 --- /dev/null +++ b/11_Lecture11/Module03_p11/lib2.h @@ -0,0 +1,4 @@ +#ifndef LIB_2_H +#define LIB_2_H +#include "mylibrary.h" +#endif \ No newline at end of file diff --git a/11_Lecture11/Module03_p11/main.c b/11_Lecture11/Module03_p11/main.c new file mode 100644 index 0000000..deaf79f --- /dev/null +++ b/11_Lecture11/Module03_p11/main.c @@ -0,0 +1,13 @@ +#include +#include "mylibrary.h" + +int m; + +int main(void) +{ + int a,b,c; + scanf("%d%d%d",&a,&b,&c); + m = max_3(a,b,c); + printf("max(%d %d %d) = %d\n", a, b, c, m); + return 0; +} diff --git a/11_Lecture11/Module03_p11/makefile b/11_Lecture11/Module03_p11/makefile new file mode 100644 index 0000000..53dba3c --- /dev/null +++ b/11_Lecture11/Module03_p11/makefile @@ -0,0 +1,14 @@ +all: prog + +prog: main.o func.o + gcc -o prog main.o func.o + +main.o: main.c mylibrary.h + gcc -c -o main.o main.c -I ./mylibrary.h + +func.o: func.c + gcc -c -o func.o func.c + +clean: + del *.o + del prog.exe diff --git a/11_Lecture11/Module03_p11/mylibrary.h b/11_Lecture11/Module03_p11/mylibrary.h new file mode 100644 index 0000000..d0edbb8 --- /dev/null +++ b/11_Lecture11/Module03_p11/mylibrary.h @@ -0,0 +1,7 @@ +#include "lib2.h" + +// Функция доступна только из func.c +static int max(int, int); + +// Функция доступна везде +int max_3(int, int, int); diff --git a/12_Lecture12/.vscode/settings.json b/12_Lecture12/.vscode/settings.json new file mode 100644 index 0000000..0f06797 --- /dev/null +++ b/12_Lecture12/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "stdio.h": "c" + } +} \ No newline at end of file diff --git a/12_Lecture12/00_argc_p7.c b/12_Lecture12/00_argc_p7.c new file mode 100644 index 0000000..17033e4 --- /dev/null +++ b/12_Lecture12/00_argc_p7.c @@ -0,0 +1,8 @@ +#include + +int main(int argc, char *argv[]) +{ + for(int i=0; i + +int main(int argc, char *argv[]) +{ + int size=0; + for(int i=0; i +#include + +int main(int argc, char *argv[]) +{ + int rez=0; + // opterr=0;//Можно отключить вывод сообщений об ошибках, для этого надо где-то в + //программе перед вызовом функции вставить opterr=0 + while ( (rez = getopt(argc,argv,"ab:C::d")) != -1) + { + switch (rez) + { + case 'a': printf("found argument \"a\".\n"); break; + case 'b': printf("found argument \"b = %s\".\n",optarg); break; + case 'C': printf("found argument \"C = %s\".\n",optarg); break; + case 'd': printf("found argument \"d\"\n"); break; + case '?': printf("Error found !\n");break; + } + } +} diff --git a/12_Lecture12/02_ls_p21.c b/12_Lecture12/02_ls_p21.c new file mode 100644 index 0000000..a42a3a2 --- /dev/null +++ b/12_Lecture12/02_ls_p21.c @@ -0,0 +1,34 @@ +#include +#include +#define PATH_LENGTH 255 + +int main(int argc, char *argv[]) +{ + char dir[PATH_LENGTH], buf[PATH_LENGTH]; + int rez = 0; + // opterr=0; + while ((rez = getopt(argc, argv, "hf:")) != -1) + { + switch (rez) + { + case 'h': + printf("This is example of list directory\n"); + printf("Usage: clear [options]\n\ + -h This help text\n\ + -f Specify folder.\n"); + printf("Example: %s -f /tmp\n", argv[0]); + return 0; + case 'f': + printf("folder is \"f = %s\".\n",optarg); + strcpy(dir, optarg); + break; + case '?': + printf("Unknown argument: %s Try -h for help\n", argv[optind - 1]); + return 1; + }; + }; + // convert_path_to_full(buf, dir); + // printf("ls for folder %s\n",buf); + // ls(dir); + return 0; +} diff --git a/12_Lecture12/03_macros_debugprint_p26.c b/12_Lecture12/03_macros_debugprint_p26.c new file mode 100644 index 0000000..a250266 --- /dev/null +++ b/12_Lecture12/03_macros_debugprint_p26.c @@ -0,0 +1,9 @@ +#include + +#define DEBUGPRINT fprintf (stderr, "debug in %d line\n", __LINE__) + +int main(void) +{ + DEBUGPRINT; + return 0; +} diff --git a/12_Lecture12/04_macros_degug_p27.c b/12_Lecture12/04_macros_degug_p27.c new file mode 100644 index 0000000..ccf393a --- /dev/null +++ b/12_Lecture12/04_macros_degug_p27.c @@ -0,0 +1,11 @@ +#include + +#define ONE printf ("DEBUG\n") +#define TWO ONE; ONE +#define FOUR TWO; TWO + +int main(void) +{ + FOUR; + return 0; +} diff --git a/12_Lecture12/05_macros_example_p28.c b/12_Lecture12/05_macros_example_p28.c new file mode 100644 index 0000000..ca2aaaf --- /dev/null +++ b/12_Lecture12/05_macros_example_p28.c @@ -0,0 +1,12 @@ +#include + +#define A B + 1 +#define B 2 + +int main(void) +{ + int a; + a = A + 2; + printf("a = %d\n", a); + return 0; +} diff --git a/12_Lecture12/06_macros_swap_p30.c b/12_Lecture12/06_macros_swap_p30.c new file mode 100644 index 0000000..cea6919 --- /dev/null +++ b/12_Lecture12/06_macros_swap_p30.c @@ -0,0 +1,18 @@ + +#include + +/* Открывающая скобка не должна быть отделена пробельными +символами от имени макроса. Если в списке параметров или в +тексте встречаются комментарии, каждый комментарий заменяется +на один символ пробела. */ + +#define SWAP(a,b) (a ^= b, b ^= a, a ^= b) + +int main(void) +{ + int a=1,b=2; + printf("%d %d\n",a,b); + SWAP(a,b); + printf("%d %d\n",a,b); + return 0; +} diff --git a/12_Lecture12/07_macros_half_p31.c b/12_Lecture12/07_macros_half_p31.c new file mode 100644 index 0000000..1ce5b05 --- /dev/null +++ b/12_Lecture12/07_macros_half_p31.c @@ -0,0 +1,11 @@ +#include + +#define HALF(x) x/2 + +int main(void) +{ + int a=5, b; + b = HALF(a + 5); + printf("a = %d b = %d\n",a, b); + return 0; +} diff --git a/12_Lecture12/08_macros_half_2_p33.c b/12_Lecture12/08_macros_half_2_p33.c new file mode 100644 index 0000000..c33622e --- /dev/null +++ b/12_Lecture12/08_macros_half_2_p33.c @@ -0,0 +1,11 @@ +#include + +#define HALF(x) ((x)/2) + +int main(void) +{ + int a=5, b; + b = HALF(a + 5); + printf("a = %d b = %d\n",a, b); + return 0; +} diff --git a/12_Lecture12/09_macros_tostr_p35.c b/12_Lecture12/09_macros_tostr_p35.c new file mode 100644 index 0000000..4605ea1 --- /dev/null +++ b/12_Lecture12/09_macros_tostr_p35.c @@ -0,0 +1,12 @@ +#include + +//Преобразование аргумента в строку +#define TOSTR(a) #a + +int main(void) +{ + printf("%s\n",TOSTR(hello world)); + printf("%s\n",TOSTR(123)); + printf("%s\n",TOSTR("hello world")); + return 0; +} diff --git a/12_Lecture12/10_macros_merge_p36.c b/12_Lecture12/10_macros_merge_p36.c new file mode 100644 index 0000000..b283c32 --- /dev/null +++ b/12_Lecture12/10_macros_merge_p36.c @@ -0,0 +1,12 @@ +#include + +//Операция склейки записывается как ## +#define MERGE(a,b) a##b + +int main(void) +{ + MERGE(d,o); // do + MERGE(a,2); // a2 + MERGE(+,+); // ++ + return 0; +} diff --git a/12_Lecture12/11_why_me_p37.c b/12_Lecture12/11_why_me_p37.c new file mode 100644 index 0000000..f4fa8a3 --- /dev/null +++ b/12_Lecture12/11_why_me_p37.c @@ -0,0 +1,19 @@ +#include + +void why_me(); + +int main(void){ + printf("File: %s\n", __FILE__); + printf("Date:Q %s\n", __DATE__); + printf("Time: %s\n", __TIME__); + printf("Version: %ld\n", __STDC_VERSION__); + printf("Line: %d\n", __LINE__); + printf("Function: %s\n", __func__); + why_me(); + return 0; +} + +void why_me() { + printf("Function: %s\n", __func__); + printf("Line: %d\n", __LINE__); +} diff --git a/12_Lecture12/12_if_endif_p41.c b/12_Lecture12/12_if_endif_p41.c new file mode 100644 index 0000000..94f568c --- /dev/null +++ b/12_Lecture12/12_if_endif_p41.c @@ -0,0 +1,17 @@ +#include + +#define DEBUG + +#if defined DEBUG +#define DEBUGPRINT fprintf(stderr, "debug in %d line func: %s\n", __LINE__, __func__) +#else +#define DEBUGPRINT +#endif + +int main(void) +{ + int a = 5, b = 7; + DEBUGPRINT; + printf("a = %d b = %d\n", a, b); + return 0; +} diff --git a/13_Lecture13/.vscode/launch.json b/13_Lecture13/.vscode/launch.json new file mode 100644 index 0000000..889a255 --- /dev/null +++ b/13_Lecture13/.vscode/launch.json @@ -0,0 +1,30 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Attach", + "type": "cppdbg", + "request": "attach", + "program": "enter program name, for example ${workspaceFolder}/a.exe", + "MIMode": "gdb", + "miDebuggerPath": "/path/to/gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + }, + { + "description": "Set Disassembly Flavor to Intel", + "text": "-gdb-set disassembly-flavor intel", + "ignoreFailures": true + } + ] + } + + + ] +} \ No newline at end of file diff --git a/13_Lecture13/.vscode/tasks.json b/13_Lecture13/.vscode/tasks.json new file mode 100644 index 0000000..865ba8b --- /dev/null +++ b/13_Lecture13/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: gcc.exe build active file", + "command": "C:\\msys64\\ucrt64\\bin\\gcc.exe", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}\\${fileBasenameNoExtension}.exe" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/13_Lecture13/00_logir_p7.c b/13_Lecture13/00_logir_p7.c new file mode 100644 index 0000000..97dd55a --- /dev/null +++ b/13_Lecture13/00_logir_p7.c @@ -0,0 +1,9 @@ +//Логирование вручную +#include + +int main(int argc, char *argv[]) +{ + fprintf(stderr,"Debug message\n"); + fprintf(stdout,"Stdout message\n"); + return 0; +} diff --git a/13_Lecture13/01_logir_sclog4c_p8.c b/13_Lecture13/01_logir_sclog4c_p8.c new file mode 100644 index 0000000..e4d7abc --- /dev/null +++ b/13_Lecture13/01_logir_sclog4c_p8.c @@ -0,0 +1,10 @@ +//Логирование sclog4c +#include +#include "sclog4c.c" + +int main(int argc, char *argv[]) +{ + sclog4c_level = SL4C_ALL; + logm(SL4C_DEBUG, "Program name: %s", argv[0]); + return 0; +} diff --git a/13_Lecture13/02_lifo_1_p14.c b/13_Lecture13/02_lifo_1_p14.c new file mode 100644 index 0000000..e2d55ab --- /dev/null +++ b/13_Lecture13/02_lifo_1_p14.c @@ -0,0 +1,43 @@ +// Пример организации стека как однонаправленного списка +#include +#include + +typedef int datatype; + +typedef struct list { + datatype value; + struct list * next; +}stack; + +void push(stack **p,datatype data) +{ + stack *ptmp; + ptmp=malloc(sizeof(stack)); + ptmp->value=data; + ptmp->next=*p; + *p=ptmp; +} + +_Bool empty_stack(stack *p) { + return p==NULL; +} + +datatype pop(stack **p){ + stack *ptmp=*p; + datatype c; + if(empty_stack(*p)) + exit (1); // Попытка взять из пустого стека + c=ptmp->value; + *p=ptmp->next; + free(ptmp); + return c; +} + +int main(){ + stack *p=NULL; // Важно для корректной работы присвоить NULL + for(int i=1;i<=5; i++) + push(&p,i); + for(int i=1;i<=5; i++) + printf("%d\n",pop(&p)); // 5 4 3 2 1 + return 0; +} diff --git a/13_Lecture13/02_lifo_1_p14.exe b/13_Lecture13/02_lifo_1_p14.exe new file mode 100644 index 0000000..5e7e815 Binary files /dev/null and b/13_Lecture13/02_lifo_1_p14.exe differ diff --git a/13_Lecture13/03_lifo_2_p15_16.c b/13_Lecture13/03_lifo_2_p15_16.c new file mode 100644 index 0000000..5612ff4 --- /dev/null +++ b/13_Lecture13/03_lifo_2_p15_16.c @@ -0,0 +1,71 @@ +// Пример организации стека через динамический массив +#include +#include + +typedef int datatype; + +typedef struct stack +{ + datatype *item; + int size; + int sp; +} stack; + +void init_stack(stack *st) +{ + st->size = 4; + st->sp = 0; + st->item = malloc(st->size * sizeof(datatype)); +} + +void delete_stack(stack *st) +{ + free(st->item); +} + +void push(stack *st, datatype value) +{ + if (st->sp == st->size - 1) + { + st->size = st->size * 2; + st->item = realloc(st->item, + st->size * sizeof(datatype)); + } + st->item[st->sp++] = value; +} + +void pop(stack *st, datatype *value) +{ + if (st->sp < 1) + { + printf("stack empty"); + exit(1); + } + *value = st->item[--(st->sp)]; +} + +int empty_stack(stack *st) +{ + return (st->sp < 1); +} + +int main() +{ + stack st; + + int a, i; + init_stack(&st); + do + { + scanf("%d", &a); + push(&st, a); + } while (a != 0); + for (i = 0; i < st.sp; i++) + printf("%d ", st.item[i]); + while (!empty_stack(&st)) + { + pop(&st, &a); + printf("%d ", a); + } + return 0; +} diff --git a/13_Lecture13/03_lifo_2_p15_16.exe b/13_Lecture13/03_lifo_2_p15_16.exe new file mode 100644 index 0000000..a325734 Binary files /dev/null and b/13_Lecture13/03_lifo_2_p15_16.exe differ diff --git a/13_Lecture13/04_fifo_p18_20.c b/13_Lecture13/04_fifo_p18_20.c new file mode 100644 index 0000000..ced5116 --- /dev/null +++ b/13_Lecture13/04_fifo_p18_20.c @@ -0,0 +1,83 @@ +// Пример реализации очереди при помощи однонаправленного списка +#include +#include + +typedef int datatype; + +typedef struct list +{ + datatype value; + struct list *next; +} queue; + +_Bool empty_queue(queue *p) +{ + return p == NULL; +} + +datatype dequeue(queue **p) +{ + queue *ptmp = *p; + datatype c; + if (empty_queue(*p)) + { // Попытка взять из пустой очереди + fprintf(stderr, "Error. Queue is empty\n"); + exit(1); + } + c = ptmp->value; + *p = ptmp->next; + free(ptmp); + return c; +} + +void enqueue(struct list **pl, datatype data) +{ + struct list *ptmp = *pl, + *elem; + elem = malloc(sizeof(struct list)); + elem->value = data; + elem->next = NULL; + if (*pl == NULL) + { + *pl = elem; + return; + } + while (ptmp->next) + ptmp = ptmp->next; + ptmp->next = elem; +} + +void enqueue_recurs(struct list **pl, datatype data) +{ + if (*pl == NULL) + { + (*pl) = malloc(sizeof(struct list)); + (*pl)->value = data; + (*pl)->next = NULL; + return; + } + else + { + enqueue_recurs(&((*pl)->next), data); + } +} + +void print_list(struct list *pl) +{ + while (pl) + { + printf("%d ", pl->value); + pl = pl->next; + } + printf("\n"); +} + +int main() +{ + queue *pq = NULL; + for (int i = 1; i <= 5; i++) + enqueue_recurs(&pq, i); + for (int i = 1; i <= 5; i++) + printf("%d\n", dequeue(&pq)); + return 0; +} diff --git a/13_Lecture13/04_fifo_p18_20.exe b/13_Lecture13/04_fifo_p18_20.exe new file mode 100644 index 0000000..86ab19a Binary files /dev/null and b/13_Lecture13/04_fifo_p18_20.exe differ diff --git a/13_Lecture13/04_fifo_p18_20.o b/13_Lecture13/04_fifo_p18_20.o new file mode 100644 index 0000000..92d6b22 Binary files /dev/null and b/13_Lecture13/04_fifo_p18_20.o differ diff --git a/13_Lecture13/test.c b/13_Lecture13/test.c new file mode 100644 index 0000000..504c12a --- /dev/null +++ b/13_Lecture13/test.c @@ -0,0 +1,79 @@ +// Пример реализации очереди при помощи однонаправленного списка +#include +#include + +// #define DEBUG +#ifdef DEBUG + #define D if(1) +#else + #define D if(0) +#endif + +FILE* log_func; +FILE* log_for; + + + +typedef int datatype; + +typedef struct list +{ + datatype value; + struct list *next; +} queue; + +_Bool empty_queue(queue *p) +{ + return p == NULL; +} + +void enqueue(queue **pl, datatype data) +{ + D printf("enqueue %p %d\n", *pl, data); + fprintf(log_func,"enqueue %p %d\n", *pl, data); + fflush(log_func); + queue *elem = malloc(sizeof(struct list)); + elem->value = data; + elem->next = NULL; + if(*pl==NULL) + { + *pl = elem; + } + else + { + queue *ptmp; + for (ptmp = *pl; ptmp; ptmp = ptmp->next)//error + // for (ptmp = *pl; ptmp->next; ptmp = ptmp->next) + {} + D printf("for %p %d\n", ptmp, data); + fprintf(log_for,"for %p %d\n", ptmp, data); + fflush(log_for);//если забыли, то пусто + ptmp->next = elem; + } +} + +void print_list(struct list *pl) +{ + while (pl) + { + printf("%d ", pl->value); + pl = pl->next; + } + printf("\n"); +} + +int main() +{ + queue *pq = NULL; + log_func = fopen("log_func.txt","w"); + log_for = fopen("log_for.txt","w"); + + + D printf("This is debug message\n"); + for (int i = 1; i <= 5; i++) + enqueue(&pq, i); + print_list(pq); + fclose(log_func); + fclose(log_for); + return 0; +} diff --git a/16_Seminar03/00_assign_p3.c b/16_Seminar03/00_assign_p3.c new file mode 100644 index 0000000..f011f75 --- /dev/null +++ b/16_Seminar03/00_assign_p3.c @@ -0,0 +1,14 @@ +#include + +int main(void) +{ + float f=123.767; + int k, fint; + fint = f; //fint = 123 + fint *= 10; //fint = 1230 + f = f*10; //f = 1235.67 + k = f - fint; //k=5 + printf ("%d",k); + return 0; +} + diff --git a/16_Seminar03/01_time_p5.c b/16_Seminar03/01_time_p5.c new file mode 100644 index 0000000..dde4f27 --- /dev/null +++ b/16_Seminar03/01_time_p5.c @@ -0,0 +1,14 @@ +#include + +int main(void) +{ + int s = 10000; + int h = s/3600; + s = s - h*3600; + int m = s/60; + s = s - m*60; + int s1 = h*60*60+m*60+s%60; + printf("s=%d m=%d h=%d s1=%d\n",s,m,h,s1); + return 0; +} + diff --git a/16_Seminar03/02_printf_p7.c b/16_Seminar03/02_printf_p7.c new file mode 100644 index 0000000..6ea13cb --- /dev/null +++ b/16_Seminar03/02_printf_p7.c @@ -0,0 +1,13 @@ +#include + +int main(void) +{ + int a = 1, b= 3; + printf ("a+%d=a+b\n", b); + printf ("%d=F (%d)\n", a, b); + printf ("a=F (%d)\n", b); + printf ("%d>%d\n", a+b, b); + printf ("F(%d)==G(%d)\n", b, a); + return 0; +} + diff --git a/16_Seminar03/02_sum_p_6.c b/16_Seminar03/02_sum_p_6.c new file mode 100644 index 0000000..bd38dc6 --- /dev/null +++ b/16_Seminar03/02_sum_p_6.c @@ -0,0 +1,9 @@ +#include +int main() +{ +int a, b, c, sum; + scanf("%d%d%d", &a, &b, &c); + sum = a+b+c; + printf ("%d+%d+%d=%d\n", a, b, c, sum); +return 0; +} diff --git a/16_Seminar03/03_mult_p_6.c b/16_Seminar03/03_mult_p_6.c new file mode 100644 index 0000000..d09eb96 --- /dev/null +++ b/16_Seminar03/03_mult_p_6.c @@ -0,0 +1,12 @@ +#include +int main() +{ +int a, b, c, sum, mult; + scanf("%d%d%d", &a, &b, &c); + sum = a+b+c; + mult = a*b*c; + printf ("%d+%d+%d=%d\n", a, b, c, sum); + printf ("%d*%d*%d=%d\n", a, b, c, mult); +return 0; +} + diff --git a/16_Seminar03/04_averege_p_6.c b/16_Seminar03/04_averege_p_6.c new file mode 100644 index 0000000..4e7293f --- /dev/null +++ b/16_Seminar03/04_averege_p_6.c @@ -0,0 +1,9 @@ +#include +int main(){ +int a, b, c; +float averege; + scanf("%d%d%d", &a, &b, &c); + averege = (a + b + c) / 3.; + printf("%.2f\n", averege); +return 0; +} diff --git a/16_Seminar03/05_sub_p_6.c b/16_Seminar03/05_sub_p_6.c new file mode 100644 index 0000000..d346809 --- /dev/null +++ b/16_Seminar03/05_sub_p_6.c @@ -0,0 +1,9 @@ +#include +int main() +{ +int x; +int y; + scanf("%d%d", &x, &y); + printf("%d", x - y); +return 0; +} diff --git a/16_Seminar03/06_printf_p7.c b/16_Seminar03/06_printf_p7.c new file mode 100644 index 0000000..6ea13cb --- /dev/null +++ b/16_Seminar03/06_printf_p7.c @@ -0,0 +1,13 @@ +#include + +int main(void) +{ + int a = 1, b= 3; + printf ("a+%d=a+b\n", b); + printf ("%d=F (%d)\n", a, b); + printf ("a=F (%d)\n", b); + printf ("%d>%d\n", a+b, b); + printf ("F(%d)==G(%d)\n", b, a); + return 0; +} + diff --git a/16_Seminar03/07_sum_digits_p8.c .c b/16_Seminar03/07_sum_digits_p8.c .c new file mode 100644 index 0000000..74b0a3d --- /dev/null +++ b/16_Seminar03/07_sum_digits_p8.c .c @@ -0,0 +1,11 @@ +#include +int main(int argc, char **argv) +{ // На вход подается произвольное трехзначное число, напечать сумму цифр + int a, d1, d2, d3; + scanf (" %d", &a); + d1 = (a / 100) % 10; + d2 = (a / 10) % 10; + d3 = a % 10; + printf("%d\n",d1+d2+d3); + return 0; +} diff --git a/16_Seminar03/08_scanf.c .c b/16_Seminar03/08_scanf.c .c new file mode 100644 index 0000000..f5fb489 --- /dev/null +++ b/16_Seminar03/08_scanf.c .c @@ -0,0 +1,8 @@ +#include +int main() +{ + int a=-1, b=-1; + int res = scanf("%d,%d", &a, &b); + printf ("res = %d a=%d b=%d\n", res , a, b); +return 0; +} diff --git a/17_Seminar04/00_yes_no_p5.c b/17_Seminar04/00_yes_no_p5.c new file mode 100644 index 0000000..1e923c0 --- /dev/null +++ b/17_Seminar04/00_yes_no_p5.c @@ -0,0 +1,11 @@ +#include + +int main(void) +{ + int x; + scanf("%d",&x); + printf("%s",(x == 0) ? "NO\n" : "YES\n"); + x==0 ? printf("NO\n") : printf("YES\n"); + return 0; +} + diff --git a/17_Seminar04/01_1_xor.c b/17_Seminar04/01_1_xor.c new file mode 100644 index 0000000..28ebf19 --- /dev/null +++ b/17_Seminar04/01_1_xor.c @@ -0,0 +1,8 @@ +#include + +int main(void) +{ + int a0=1,a1=1,a2=3,a3=5,a4=5; + printf ("%d\n", a0^a1^a2^a3^a4); + return 0; +} diff --git a/17_Seminar04/01_min_p6.c b/17_Seminar04/01_min_p6.c new file mode 100644 index 0000000..0ef033d --- /dev/null +++ b/17_Seminar04/01_min_p6.c @@ -0,0 +1,11 @@ +#include + +int main(void) +{ + int a=-1, b=-1; + scanf("%d%d", &a, &b); + int min = (a < b) ? a : b; + printf ("min = %d a=%d b=%d\n", min , a, b); + return 0; +} + diff --git a/17_Seminar04/02_reset_3_bits_p7.c b/17_Seminar04/02_reset_3_bits_p7.c new file mode 100644 index 0000000..369033f --- /dev/null +++ b/17_Seminar04/02_reset_3_bits_p7.c @@ -0,0 +1,12 @@ +#include +#include + +int main(void) +{ + uint32_t a=0xAABBCCFF; + a = a>>3; + a = a<<3; + printf("a = %x", a); + // a = aabbccf8 + return 0; +} diff --git a/17_Seminar04/03_rise_2_bits_p8.c b/17_Seminar04/03_rise_2_bits_p8.c new file mode 100644 index 0000000..3f2d347 --- /dev/null +++ b/17_Seminar04/03_rise_2_bits_p8.c @@ -0,0 +1,16 @@ +#include +#include + +#define BIT_ODD 0 +#define BIT_PARITY 1 + + +int main( ) +{ + unsigned int a=0xFFFFFF00; + //~ a = a |((1<<2) - 1); // чтобы не запоминать константу + a |= (1< +#include + +int main( ) +{ + unsigned int a=0xFFFFFF00, mask=-1; // mask = 0xffffffff + //~ mask = mask>>(32 - 2); + mask = 0b11; + a = a | mask; + printf("a = %x", a); + // a = ffffff03 + return 0; +} diff --git a/17_Seminar04/05_OR_AND.c b/17_Seminar04/05_OR_AND.c new file mode 100644 index 0000000..f820d7b --- /dev/null +++ b/17_Seminar04/05_OR_AND.c @@ -0,0 +1,11 @@ +#include +#include +#include + + +int main(void) +{ + int32_t x = 3, y = 8;//0b0011 = 3, 0b1000 = 8 + (x & y) ? printf("True ") : printf("False ");//0b0011 & 0b1000 = 0b0000 + (x && y) ? printf("True ") : printf("False ");//0b0011 && 0b1000 = 0b0001 +} diff --git a/18_Seminar05/00_switch_p1.c b/18_Seminar05/00_switch_p1.c new file mode 100644 index 0000000..f9a2772 --- /dev/null +++ b/18_Seminar05/00_switch_p1.c @@ -0,0 +1,30 @@ +#include + +int max(int a,int b) +{ + return a>b ? a : b; +} + +int main() +{ + int a,b; + scanf ("%d%d", &a,&b); + switch (max(a,b)) + { + case 2: + printf("case 2:\n"); + a *= 2 ; // Если expr == 2, то выполнится a += 5; из следующей ветки + //~ break; + case 3: + a += 5; + break; // А здесь произойдет выход + case 4: + a -= b; + break; + default: + break; + } + printf("%d", a); + return 0; //Завершить программу успешно +} + diff --git a/18_Seminar05/01_printf_p2.c b/18_Seminar05/01_printf_p2.c new file mode 100644 index 0000000..2e761ce --- /dev/null +++ b/18_Seminar05/01_printf_p2.c @@ -0,0 +1,14 @@ +#include + +int main() +{ + int n; + n=1; + while (n <= 5) { + printf("%d\n", n); + n = n+2; + } + return 0; +} + + diff --git a/18_Seminar05/02_printf_p4.c b/18_Seminar05/02_printf_p4.c new file mode 100644 index 0000000..6cadd10 --- /dev/null +++ b/18_Seminar05/02_printf_p4.c @@ -0,0 +1,15 @@ +#include + +int main() +{ + int n; + n=5; + while (n >= 1) { + printf("%d\n", n*n*n); + n--; + } + return 0; +} + + + diff --git a/18_Seminar05/03_digits_p7.c b/18_Seminar05/03_digits_p7.c new file mode 100644 index 0000000..cba4271 --- /dev/null +++ b/18_Seminar05/03_digits_p7.c @@ -0,0 +1,18 @@ +#include + +int main( ) +{ + int n, count; + printf("Input number: "); + scanf("%d", &n); + count = 0; + while (n != 0) + { + count++; + n = n / 10; // Отбросили одну цифру + } + //~ for(count = 0;n != 0;n /= 10,count++) //; Отбросили одну цифру + //~ {} + printf("In %d found %d digits", n, count); + return 0; +} diff --git a/18_Seminar05/04_for_1_p8.c b/18_Seminar05/04_for_1_p8.c new file mode 100644 index 0000000..984c6c2 --- /dev/null +++ b/18_Seminar05/04_for_1_p8.c @@ -0,0 +1,10 @@ +#include + +int main( ) +{ + int i, a=1; + for(i=1; i>=3; i--) + a++; + printf("%d", a); + return 0; +} diff --git a/18_Seminar05/05_for_2_p8.c b/18_Seminar05/05_for_2_p8.c new file mode 100644 index 0000000..9ade938 --- /dev/null +++ b/18_Seminar05/05_for_2_p8.c @@ -0,0 +1,10 @@ +#include + +int main( ) +{ + int i, a=1; + for(i=1; i<=4; i--) + a++; + printf("%d", a); + return 0; +} diff --git a/18_Seminar05/06_deviders_p11.c b/18_Seminar05/06_deviders_p11.c new file mode 100644 index 0000000..b990347 --- /dev/null +++ b/18_Seminar05/06_deviders_p11.c @@ -0,0 +1,22 @@ +#include +#include + +int main(void) +{ + uint32_t n, i=2; + scanf("%" PRIu32, &n); + while(i<=n) + { + if(n%i == 0) + { + printf("%" PRIu32 " ",i); + n/=i; + } + else + { + i++; + } + } + printf("\n"); + return 0; +} diff --git a/18_Seminar05/07_greater_number_p13.c b/18_Seminar05/07_greater_number_p13.c new file mode 100644 index 0000000..b0f080f --- /dev/null +++ b/18_Seminar05/07_greater_number_p13.c @@ -0,0 +1,12 @@ +#include +#include + +int main(void) +{ + unsigned int i,n, pow2=1; + scanf("%u",&n); + for(i=0; pow2 +#include + +int main() +{ + + + uint32_t count=0; + for(uint32_t i=11; i<=999; i++) + { + if ((i < 100) && (i%10 == i/10)) // 11, 22, ... + { + count++; + printf("%" PRIu32 " ",i); + } + else if(i>=100 && i%10 == i/100) // 111, 121, 131, ... + { + count++; + printf("%" PRIu32 " ",i); + } + } + printf("Total polindroms %" PRIu32 "\n",count); + return 0; +} + diff --git a/18_Seminar05/09_printf_p16.c b/18_Seminar05/09_printf_p16.c new file mode 100644 index 0000000..d4fc0b6 --- /dev/null +++ b/18_Seminar05/09_printf_p16.c @@ -0,0 +1,14 @@ +#include + +int a=5; + +int main(){ + int a=10; + if (1==1) { + int a=20; /*Локальная переменная видна только внутри if. Не стоит называть переменные также как и во внешней области видимости */ + printf("a = %d\n",a); + } + printf("a = %d\n",a); + return 0; +} + diff --git a/18_Seminar05/10_count_p19.c b/18_Seminar05/10_count_p19.c new file mode 100644 index 0000000..2038a7a --- /dev/null +++ b/18_Seminar05/10_count_p19.c @@ -0,0 +1,17 @@ +#include +int main() +{ + int i, count = 0; + for (i = 102; i <= 987; i++) + { + int d1 = i % 10; + int d2 = i % 100/10; + int d3 = i / 100; + if (d1!=d2 && d1!=d3 && d2!=d3) + //if (i / 100 != i % 10 && i / 100 != i % 100 / 10 && i % 100 / 10 != i % 10) + count++; + } + printf("%d", count); + return 0; +} + diff --git a/18_Seminar05/11_palindrom_p21.c b/18_Seminar05/11_palindrom_p21.c new file mode 100644 index 0000000..f4db376 --- /dev/null +++ b/18_Seminar05/11_palindrom_p21.c @@ -0,0 +1,19 @@ +#include +#include + +int main() +{ + uint32_t count=0; + for(uint32_t i=11;i<=999;i++) { + if ( i<100 && i%10 == i/10 ) { + count++; + printf("%" PRIu32 " ",i); + } else if(i>=100 && i%10 == i/100) { + count++; + printf("%" PRIu32 " ",i); + } + } + printf("Total polindroms %" PRIu32 "\n",count); + return 0; +} + diff --git a/19_Seminar06/00_float_p3.c b/19_Seminar06/00_float_p3.c new file mode 100644 index 0000000..e1c451c --- /dev/null +++ b/19_Seminar06/00_float_p3.c @@ -0,0 +1,19 @@ +#include + +int main() +{ + int X1, Y1, X2, Y2; //Объявить целочисленные переменные + float k, b; //Объявить вещественные переменные + scanf ("%d %d %d %d", &X1, &Y1, &X2, &Y2); //Считать числа и записать по адресам + //~ k=((Y1-Y2)/(float)(X1-X2)); + k=(Y1-Y2)/(X1-X2); + b=Y2-k*X2; + printf("%.2f %.2f\n", k, b); + float dX = X1-X2; + float dY = Y1-Y2; + k = dY/dX; + b=Y2-k*X2; + printf("%.2f %.2f\n", k, b); + return 0; //Завершить программу успешно +} + diff --git a/20_Seminar07/00_func_p2.c b/20_Seminar07/00_func_p2.c new file mode 100644 index 0000000..1941852 --- /dev/null +++ b/20_Seminar07/00_func_p2.c @@ -0,0 +1,15 @@ +#include + +void func(void) { + int a=5; //локальная переменная + a++; + printf("a = %d\n",a); +} + +int main(void) +{ + func(); + func(); + func(); + return 0; +} diff --git a/20_Seminar07/01_f_p4.c b/20_Seminar07/01_f_p4.c new file mode 100644 index 0000000..e0c5146 --- /dev/null +++ b/20_Seminar07/01_f_p4.c @@ -0,0 +1,17 @@ +#include + +int y=1; + +int f(int *pa) { + static int x=3; + x += y;//3+2=5//5+2=7 + return x + *pa + y;//5+10+2=17//7+10+2=19 +} + +int main(void){ + int n = 10; + y++;//2 + printf("Answer = %d\n", f(&n)); + printf("Answer = %d\n", f(&n)); +} + diff --git a/20_Seminar07/02_taylor_p9_10.c b/20_Seminar07/02_taylor_p9_10.c new file mode 100644 index 0000000..5aa3f08 --- /dev/null +++ b/20_Seminar07/02_taylor_p9_10.c @@ -0,0 +1,29 @@ +#include +#include +const float PI = 3.1415926535; + +double sinx(double x) +{ + double Xn = x; + double sum = 0.0; + int i = 1; + do + { + sum += Xn; + Xn *= -1.0 * x * x / ((2 * i) * (2 * i + 1)); + i++; + } + while (fabs(Xn) > 0.0000001); + return sum; +} + +int main() +{ + double x; + scanf("%lf",&x); + x *= PI/180.0; + printf("sinx =%lf\tlibSin =%f",sin(x),sinx(x)); + //~ printf("%.3f",sinx(x)); + return 0; +} + diff --git a/20_Seminar07/03_refact_1_p11.c b/20_Seminar07/03_refact_1_p11.c new file mode 100644 index 0000000..b0e0b37 --- /dev/null +++ b/20_Seminar07/03_refact_1_p11.c @@ -0,0 +1,32 @@ +#include +#include +#include + +float const END = 0.000001; + +double teylor(double Xn, int n, double x,double sum) +{ + if (fabs(Xn)>END) + { + n+=1; + Xn*=(-1)*x*x/(2*n*(2*n-1)); + sum+=Xn; + return teylor(Xn, n, x, sum); + } + return sum; +} + +int main() +{ +double x; + setlocale( LC_ALL,".utf-8" ); + printf("Рекурсия. Ряд Тейлора. Косинус.\n"); + printf("Введите икс в градусах:"); + scanf("%lf", &x); + const float PI = 3.1415926535; + x *= PI/180.0; + printf("Cos = %lf\n", teylor(1.0,0,x,1.0)); + printf("Cos = %lf\n", cos(x)); + return 0; +} + diff --git a/20_Seminar07/04_refact_2_p12.c b/20_Seminar07/04_refact_2_p12.c new file mode 100644 index 0000000..119dcdb --- /dev/null +++ b/20_Seminar07/04_refact_2_p12.c @@ -0,0 +1,36 @@ +#include "stdafx.h" +#include +#include +#include +#include +#include + +extern float END = 0.000001; +double teylor(double summ, double z, int n); +float x; +float VAL, LASTVAL; +long float z=1; +long float summ=1; +float d=1; +int n=0; + +int main(){ + setlocale( LC_ALL,"Russian" ); + printf("Рекурсия. Ряд Тейлора. Косинус.\n"); + printf("Введите икс:"); + scanf("%f", &x); + teylor(0,z,0); + printf("Cos = %lf\n", d); + getch(); +} + +double teylor(double summ, double z, int n){ + if (x==0) return 1; + else if (fabs(z)>END){ + n+=1; + z=(-1)*z*x*x/(2*n*(2*n-1)); + d=d+z; + return summ=teylor(summ+d, z, n); + } +} + diff --git a/20_Seminar07/05_prbs_p14.c b/20_Seminar07/05_prbs_p14.c new file mode 100644 index 0000000..4e432cd --- /dev/null +++ b/20_Seminar07/05_prbs_p14.c @@ -0,0 +1,23 @@ +#include +#include +#include + +int main(int argc, char* argv[]) +{ + uint8_t start = 0x08; + uint8_t a = start; + int i; + for (i = 1;; i++) + { + int newbit = (((a >> 6) ^ (a >> 5)) & 1); + a = ((a << 1) | newbit) & 0x7f; + printf("%x\n", a); + if (a == start) + { + printf("repetition period is %d\n", i); + break; + } + } + return 0; +} + diff --git a/21_Seminar08/00_random_p3.c b/21_Seminar08/00_random_p3.c new file mode 100644 index 0000000..f1dabd1 --- /dev/null +++ b/21_Seminar08/00_random_p3.c @@ -0,0 +1,18 @@ +#include +#include +#include + +int main() +{ + int x; + printf("MAX RANDOM = %d\n",RAND_MAX); //2147483647 + // Задать начальное значение последовательности + srand(123); // одни и те же числа, seed задан константно + //~ srand( time(NULL) ); // числа меняются при каждом запуске программы + //Случайное целое число в интервале [0, RAND_MAX] + x = rand(); + printf("x = %d\n",x); // первое случайное число + x = rand(); + printf("x = %d\n",x); // другое случайное число + return 0; +} diff --git a/21_Seminar08/01_random_func_p4.c b/21_Seminar08/01_random_func_p4.c new file mode 100644 index 0000000..1e7ba5f --- /dev/null +++ b/21_Seminar08/01_random_func_p4.c @@ -0,0 +1,22 @@ +#include +#include + +enum {SIZE = 10, SEED = 123}; + +int random_number(int n) +{ + return rand() % n; +} + +int main(void) +{ + int a[SIZE], i; + srand(SEED); + printf("Array:\n"); + for (i = 0; i < SIZE; i++ ) + { + a[i] = random_number(100) - 50; + printf("%4d", a[i]); + } + return 0; +} diff --git a/21_Seminar08/02_random_generator_p5.c b/21_Seminar08/02_random_generator_p5.c new file mode 100644 index 0000000..d8dca21 --- /dev/null +++ b/21_Seminar08/02_random_generator_p5.c @@ -0,0 +1,31 @@ +#include +#include + +enum {SIZE = 10, SEED = 123}; + +int random_number(int n) +{ + return rand() % n; +} + +void init_array(int size, int a[], int max_random) +{ + for (size_t i = 0;i +#include +#include + +#define SIZE 12 + +enum { SEED = 123}; + +int random_number(int n) +{ + return rand() % n; +} + +void init_array(int size, int a[], int max_random) +{ + for (size_t i = 0;i= i ; j-- ) + { + if ( a[j] > a[j+1] ) + { + swap(&a[j], &a[j+1]); + flag = 1; // поднять флаг если есть обмен + } + } + i++; + } + while ( flag ); // выход при flag = 0 +} + +// Сортировка выбором +void choose_sort_array(int a[], int size) +{ + int nMin=0; + for(int i = 0; i < size-1 ; i++ ) + { + nMin = i; + for (int j = i+1; j < size; j++) + { + //~ printf("i=%d,j=%d,a[j]=%d,a[nMin]=%d\n",i,j,a[j],a[nMin]); + if( a[j] < a[nMin] ) + { + nMin = j; + printf("i=%d,j=%d,a[j]=%d,a[nMin]=%d\n",i,j,a[j],a[nMin]); + } + } + if(nMin != i) + { + swap(&a[i], &a[nMin]); + printf("i=%d,nMin=%d,a[i]=%d,a[nMin]=%d\n",i,nMin,a[i],a[nMin]); + } + } +} + +int main(void) +{ + int arr[SIZE]; + //~ Input(arr,SIZE); + init_array(SIZE, arr, 100); + Print(arr,SIZE); + shift_array_left(arr,SIZE); + Print(arr,SIZE); + revers_array(arr,SIZE); + Print(arr,SIZE); + buble_sort_array(arr,SIZE); + Print(arr,SIZE); + choose_sort_array(arr,SIZE); + Print(arr,SIZE); + return 0; +} diff --git a/21_Seminar08/04_crc8_p19_20.c b/21_Seminar08/04_crc8_p19_20.c new file mode 100644 index 0000000..23b81fd --- /dev/null +++ b/21_Seminar08/04_crc8_p19_20.c @@ -0,0 +1,33 @@ +#include + +/* Name : CRC-8 +Poly : 0x31 x^8 + x^5 + x^4 + 1 +Init : 0xFF +Revert: false +XorOut: 0x00 +Check : 0xF7 ("123456789") +MaxLen: 15 байт(127 бит) - обнаружение одинарных, двойных, тройных +и всех нечетных ошибок */ +unsigned char Crc8(unsigned char*pcBlock, unsigned int len) +{ + unsigned char crc=0xFF; + unsigned int i; + while (len--) + { + crc^=*pcBlock++; + for (i =0; i <8; i++) + crc=crc&0x80? (crc<<1) ^0x31:crc<<1; + } + return crc; +} + +int main(int argc, char **argv) +{ + unsigned char arr[] = {'1','2','3','4','5','6','7','8','9',0}; + arr[9] = Crc8(arr,9); + printf("%x \n",Crc8(arr,9)); + arr[3]=7; + printf("%x \n",Crc8(arr,9)); + return 0; +} + diff --git a/21_Seminar08/05_isPrime_p23_24.c b/21_Seminar08/05_isPrime_p23_24.c new file mode 100644 index 0000000..959ae6d --- /dev/null +++ b/21_Seminar08/05_isPrime_p23_24.c @@ -0,0 +1,36 @@ +#include + +enum bool {false = 0,true = 1}; + +void Input(enum bool isPrime[],int n) +{ + for (int i = 0; i < n; i++) + isPrime[i] = true; +} + +void Print(enum bool isPrime[],int n) +{ + for (int i = 2; i < n; i++) + if (isPrime[i]) + printf("%d ",i); +} + +void Rot(enum bool isPrime[],int n) +{ + for (int i = 2; i*i < n; i++) + { + if (isPrime[i]) + for(int j=i*i;j<=n;j+=i) + isPrime[j] = false; + } +} + +int main() +{ + int n = 255; + enum bool isPrime[n]; + Input(isPrime,n); + Rot(isPrime,n); + Print(isPrime,n); + return 0; +} diff --git a/21_Seminar08/06_isPrime_refact_p25.c b/21_Seminar08/06_isPrime_refact_p25.c new file mode 100644 index 0000000..e8ec296 --- /dev/null +++ b/21_Seminar08/06_isPrime_refact_p25.c @@ -0,0 +1,23 @@ +#include + +int main(int argc, char **argv) +{ + int arr[255] = {2,3}; + int capacity = 2; + int i,j; + for(i=4;i<255;++i) + { + int isPrime=1; + for(j=0;j +#include + +int main() +{ + char s1[100]="", s2[100]=""; + //~ scanf("%s%s",s1,s2); + //~ fscanf("%d;%d;%d",a1,a2,a3); + int r = scanf("%[0-9]=%[a-z]", s1, s2); + printf("s1 = %s s2 = %s\n",s1,s2); + return 0; +} diff --git a/22_Seminar09/01_scanf_2_p5.c b/22_Seminar09/01_scanf_2_p5.c new file mode 100644 index 0000000..c0fee72 --- /dev/null +++ b/22_Seminar09/01_scanf_2_p5.c @@ -0,0 +1,12 @@ +#include +#include + +int main() +{ + char s1[100]="", s2[100]=""; + int r; + r=scanf("%[0-9]=%[a-z]",s1,s2); //prog.exe < test.txt + printf("r = %d\n",r); + printf("s1 = %s s2 = %s\n",s1,s2); + return 0; +} diff --git a/22_Seminar09/02_pointer_increase_p8.c b/22_Seminar09/02_pointer_increase_p8.c new file mode 100644 index 0000000..b9fc283 --- /dev/null +++ b/22_Seminar09/02_pointer_increase_p8.c @@ -0,0 +1,11 @@ +#include + +int main() +{ + int a[5] = {10,20,30,40,50}; + int *pa, n; + pa = a; + n = *pa+2; // a[0] + 2 + printf("n = %d\n", n); + return 0; +} diff --git a/22_Seminar09/03_pointer_difference_p10.c b/22_Seminar09/03_pointer_difference_p10.c new file mode 100644 index 0000000..843f5d1 --- /dev/null +++ b/22_Seminar09/03_pointer_difference_p10.c @@ -0,0 +1,11 @@ +#include + +int main() +{ + int a[5] = {10,20,30,40,50}; + int *pa, *qa; + pa = &a[1]; + qa = &a[3]; + printf("%p - %p = %lld\n",qa,pa, qa-pa); + return 0; +} diff --git a/22_Seminar09/04_pointer_printf_p12.c b/22_Seminar09/04_pointer_printf_p12.c new file mode 100644 index 0000000..ef1aa40 --- /dev/null +++ b/22_Seminar09/04_pointer_printf_p12.c @@ -0,0 +1,12 @@ +#include + +int main() +{ + int a[5] = {10,20,30,40,50}; + int *pa, n=10; + pa = a+2; + *pa++ = n+3; + printf("%d\n",*pa); + printf("%d %d %d %d %d\n", a[0],a[1],a[2],a[3],a[4]); + return 0; +} diff --git a/22_Seminar09/05_sum_p14.c b/22_Seminar09/05_sum_p14.c new file mode 100644 index 0000000..b103149 --- /dev/null +++ b/22_Seminar09/05_sum_p14.c @@ -0,0 +1,18 @@ +#include + +int sum(int a[], int size) +{ + int i, s=0; + for(i=0;i + +#define SIZE 5 + +int sum0(int* a0) +{ + int i, s=0; + for(i=1;i + +int main(int argc, char **argv) +{ + { + int matr[3][2]; + int *pm; + pm = (int*)matr; + //~ pm[1][1] = 123; //ТАК НЕЛЬЗЯ + *(pm+2*1+1)=123; + printf("%d\n",matr[1][1]); + } + { + + } + return 0; +} diff --git a/22_Seminar09/07_arr_printf_p22.c b/22_Seminar09/07_arr_printf_p22.c new file mode 100644 index 0000000..b3a43cd --- /dev/null +++ b/22_Seminar09/07_arr_printf_p22.c @@ -0,0 +1,12 @@ +#include + +int main(int argc, char **argv) +{ + int matr[3][2]; + //~ int *pm[2]; //массив из 2-ух указателей *int + int (*pm)[2]; //указатель на строку из 2-ух int + pm = matr; + pm[1][1] = 123; //теперь все ок + printf("%d\n",matr[1][1]); // 123 + return 0; +} diff --git a/22_Seminar09/07_arr_printf_p23.c b/22_Seminar09/07_arr_printf_p23.c new file mode 100644 index 0000000..7512500 --- /dev/null +++ b/22_Seminar09/07_arr_printf_p23.c @@ -0,0 +1,19 @@ +#include + +int main(int argc, char **argv) +{ + int m[3][3] = + { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9} + }; + int *p1; + int (*p2)[3]; + p1 = m[1]; + p2 = m + 1; + p1++; + p2++; + printf ("%d %d\n", *p1, **p2); + return 0; +} diff --git a/22_Seminar09/08_VLA_p26.c b/22_Seminar09/08_VLA_p26.c new file mode 100644 index 0000000..44cfa56 --- /dev/null +++ b/22_Seminar09/08_VLA_p26.c @@ -0,0 +1,11 @@ +#include +#include + + +int main(int argc, char **argv) +{ + uint32_t n; + scanf("%u",&n); //вводим количество элементов + int32_t ar[n]; //создаем VLA массив + return 0; +} diff --git a/22_Seminar09/08_qsort_p31.c b/22_Seminar09/08_qsort_p31.c new file mode 100644 index 0000000..6042457 --- /dev/null +++ b/22_Seminar09/08_qsort_p31.c @@ -0,0 +1,39 @@ +#include +#include +#include +#define SIZE 5 + +int comparator (const int *, const int *); + +/* сравнение двух целых V1 */ +int comparator (const int *a, const int *b) +{ + return *a - *b; + //~ return *b - *a;//обрасный порядок +} + +/* сравнение двух целых V1 */ +//int func_name(const void *arg1, const void *arg2) +//~ int comparator (const void *a, const void *b) { + //~ return *(int*)a - *(int*)b; + //~ // return *a - *b;//ОШИБКА +//~ } + +void print(int n,int a[]) +{ + for (int i = 0; i < n; i++) + { + printf("%d,",a[i]); + } + printf("\n"); +} + +int main(void) +{ + int a[SIZE]={3,5,3,2,1}; + print(SIZE,a); + + //~ qsort(a, SIZE, sizeof(int),comparator); + qsort(a, SIZE, sizeof (int), (int(*) (const void *, const void *)) comparator); + print(SIZE,a); +} diff --git a/22_Seminar09/08_str_p28.c b/22_Seminar09/08_str_p28.c new file mode 100644 index 0000000..1b04703 --- /dev/null +++ b/22_Seminar09/08_str_p28.c @@ -0,0 +1,11 @@ +#include +#include + + +int main(int argc, char **argv) +{ + char *ps[] = {"one", "two", "three", NULL}; // NULL признак конца + for(uint32_t i=0; ps[i] ; i++)//ps[i]!=NULL + printf("%s\n", ps[i]); + return 0; +} diff --git a/23_Seminar10/00_struct_size_p3.c b/23_Seminar10/00_struct_size_p3.c new file mode 100644 index 0000000..7df729e --- /dev/null +++ b/23_Seminar10/00_struct_size_p3.c @@ -0,0 +1,28 @@ +#include +#include + +//~ #pragma pack(push, 1) +struct str1 { + uint32_t u; + uint8_t c1; + int32_t i; + uint8_t c2; +} s1; +//~ #pragma pack(pop) + + +//~ #pragma pack(push, 1) +struct str2 { + uint32_t u; + int32_t i; + uint8_t c1; + uint8_t c2; +} s2; +//~ #pragma pack(pop) + +int main() +{ + printf("Sizeof s1 = %llu\n", sizeof(s1)); + printf("Sizeof s2 = %llu\n", sizeof(s2)); + return 0; +} diff --git a/23_Seminar10/01_union_p7.c b/23_Seminar10/01_union_p7.c new file mode 100644 index 0000000..bc4d89a --- /dev/null +++ b/23_Seminar10/01_union_p7.c @@ -0,0 +1,22 @@ +#include +#include + +union intbytes { + uint32_t number; + uint8_t bytes[4]; +} d; + +int main() +{ + d.number = 0x12345678; + printf ("Number %x",d.number); + printf(" in memory is: %x %x %x %x\n", d.bytes[0], + d.bytes[1], d.bytes[2], d.bytes[3]); + // Number 12345678 in memory is: 78 56 34 12 + uint32_t number = 0x12345678; + + printf(" in memory is: %x\n",(number >> 8)&0xff); + + + return 0; +} diff --git a/23_Seminar10/02_union_pi_p8.c b/23_Seminar10/02_union_pi_p8.c new file mode 100644 index 0000000..b504bfd --- /dev/null +++ b/23_Seminar10/02_union_pi_p8.c @@ -0,0 +1,18 @@ +#include +#include + +union intbytes { + uint32_t number; + float real; + uint8_t bytes[4]; +} d; + +int main() +{ + d.real = 3.14; + printf ("Real %f\n",d.real); + printf ("Number %x",d.number); + printf(" in memory is: %x %x %x %x\n", d.bytes[0], + d.bytes[1], d.bytes[2], d.bytes[3]); + return 0; +} diff --git a/23_Seminar10/03_bit_field_p11.c b/23_Seminar10/03_bit_field_p11.c new file mode 100644 index 0000000..d6f5a7c --- /dev/null +++ b/23_Seminar10/03_bit_field_p11.c @@ -0,0 +1,15 @@ +#include + +struct point +{ + unsigned char x:5; // 0-31 + unsigned char y:3; // 0-7 +}; + +int main(void) +{ + struct point center = {0, 5}; + center.x = 2; + printf("x=%d y=%d size=%llu \n", center.x, center.y,sizeof(center)); // x=2 y=5 + return 0; +} diff --git a/23_Seminar10/04_sensor_p14_16.c b/23_Seminar10/04_sensor_p14_16.c new file mode 100644 index 0000000..f0faf74 --- /dev/null +++ b/23_Seminar10/04_sensor_p14_16.c @@ -0,0 +1,120 @@ +#include +#include +#include + +#define SIZE 30 + +struct sensor { + uint8_t day; + uint8_t month; + uint16_t year; + int8_t t; +}; + +void cgangeIJ(struct sensor* info,int i, int j){ +struct sensor temp; + temp=info[i]; + info[i]=info[j]; + info[j]=temp; +} +//упорядочивающую его по неубыванию температуры +void SortByT(struct sensor* info,int n){ + for(int i=0; i=info[j].t) + cgangeIJ(info,i,j); +} + +unsigned int DateToInt(struct sensor* info){ + return info->year << 16 | info->month << 8 | info->day; +} +//упорядочивающую его по дате +void SortByDate(struct sensor* info,int n){ + for(int i=0; i= DateToInt(info+j)) + cgangeIJ(info,i,j); +} + +void AddRecord(struct sensor* info,int number, +uint16_t year,uint8_t month,uint8_t day,int8_t t){ + info[number].year = year; + info[number].month = month; + info[number].day = day; + info[number].t = t; +} + +struct data +{ + uint32_t number; + struct sensor info[SIZE]; +} d; + +void load_bin(struct sensor* info,int number) +{ + //Проверка на открытие файла + FILE* f = fopen("sensor.bin","rb"); + //Проверка на считанные данные + fread(info,number*sizeof(struct sensor),1,f); + fclose(f); +} + +void save_bin(struct sensor* info,int number) +{ + FILE* f = fopen("sensor.bin","wb"); + fwrite(info,number*sizeof(struct sensor),1,f); + fclose(f); +} + +void load_bin_d(struct data* d) +{ + //Проверка на открытие файла + FILE* f = fopen("sensor.bin","rb"); + //Проверка на считанные данные + fread(&d->number,sizeof(d->number),1,f); + fread(d->info,d->number*sizeof(struct sensor),1,f); + fclose(f); +} + +void save_bin_d(struct data* d) +{ + FILE* f = fopen("sensor.bin","wb"); + fwrite(&d->number,sizeof(d->number),1,f); + fwrite(d->info,d->number*sizeof(struct sensor),1,f); + fclose(f); +} + +int AddInfo(struct sensor* info){ +int counter=0; + AddRecord(info,counter++,2021,9,16,9); + AddRecord(info,counter++,2022,9,2,-9); + AddRecord(info,counter++,2021,1,7,8); + AddRecord(info,counter++,2021,9,5,1); + return counter; +} + +void print(struct sensor* info,int number){ + printf("===================================\n"); + for(int i=0;i +#include +#include + +#define SIZE 30 + +struct sensor { + uint8_t day; + uint8_t month; + uint16_t year; + int8_t t; +}; + +void cgangeIJ(struct sensor* info,int i, int j){ +struct sensor temp; + temp=info[i]; + info[i]=info[j]; + info[j]=temp; +} +//упорядочивающую его по неубыванию температуры +void SortByT(struct sensor* info,int n){ + for(int i=0; i=info[j].t) + cgangeIJ(info,i,j); +} + +unsigned int DateToInt(struct sensor* info){ + return info->year << 16 | info->month << 8 | info->day; +} +//упорядочивающую его по дате +void SortByDate(struct sensor* info,int n){ + for(int i=0; i= DateToInt(info+j)) + cgangeIJ(info,i,j); +} + +void AddRecord(struct sensor* info,int number, +uint16_t year,uint8_t month,uint8_t day,int8_t t){ + info[number].year = year; + info[number].month = month; + info[number].day = day; + info[number].t = t; +} + +struct data +{ + uint32_t number; + struct sensor info[SIZE]; +} d; + +void load_bin(struct sensor* info,int number) +{ + //Проверка на открытие файла + FILE* f = fopen("sensor.bin","rb"); + //Проверка на считанные данные + fread(info,number*sizeof(struct sensor),1,f); + fclose(f); +} + +void save_bin(struct sensor* info,int number) +{ + FILE* f = fopen("sensor.bin","wb"); + fwrite(info,number*sizeof(struct sensor),1,f); + fclose(f); +} + +void load_bin_d(struct data* d) +{ + //Проверка на открытие файла + FILE* f = fopen("sensor.bin","rb"); + //Проверка на считанные данные + fread(&d->number,sizeof(d->number),1,f); + fread(d->info,d->number*sizeof(struct sensor),1,f); + fclose(f); +} + +void save_bin_d(struct data* d) +{ + FILE* f = fopen("sensor.bin","wb"); + fwrite(&d->number,sizeof(d->number),1,f); + fwrite(d->info,d->number*sizeof(struct sensor),1,f); + fclose(f); +} + +int AddInfo(struct sensor* info){ +int counter=0; + AddRecord(info,counter++,2021,9,16,9); + AddRecord(info,counter++,2022,9,2,-9); + AddRecord(info,counter++,2021,1,7,8); + AddRecord(info,counter++,2021,9,5,1); + return counter; +} + +void print(struct sensor* info,int number){ + printf("===================================\n"); + for(int i=0;ibytes,sizeof(d->_data.number),1,f); + fread(d->bytes+sizeof(d->_data.number), + d->_data.number*sizeof(struct sensor),1,f); + fclose(f); +} + +void save_bin_ser(union sensor_serdes* d) +{ + FILE* f = fopen("sensor.bin","wb"); + fwrite(d->bytes,sizeof(d->_data.number),1,f); + fwrite(d->bytes+sizeof(d->_data.number), + d->_data.number*sizeof(struct sensor),1,f); + fclose(f); +} + +int main(int argc, char **argv) +{ + struct data d; + union sensor_serdes* ds = (union sensor_serdes*)&d; + d.number=AddInfo(d.info); + print(d.info,d.number); + save_bin_ser(ds); + printf("\nSort by t\n"); + SortByT(d.info,d.number); + print(d.info,d.number); + printf("\nSort by date\n"); + SortByDate(d.info,d.number); + print(d.info,d.number); + load_bin_des(ds); + print(d.info,d.number); + return 0; +} diff --git a/23_Seminar10/test.c b/23_Seminar10/test.c new file mode 100644 index 0000000..f2fc9e1 --- /dev/null +++ b/23_Seminar10/test.c @@ -0,0 +1,149 @@ +#include +#include +#include + +#define SIZE 30 + +//~ #pragma pack(push, 1) +struct sensor +{ + uint16_t year; + uint8_t month; + uint8_t day; + int8_t t; +}; +//~ #pragma pack(pop) + +void cgangeIJ(struct sensor* info,int i, int j){ +struct sensor temp; + temp=info[i]; + info[i]=info[j]; + info[j]=temp; +} + +//упорядочивающую его по неубыванию температуры +void SortByT(struct sensor* info,int n){ + for(int i=0; i=info[j].t) + cgangeIJ(info,i,j); +} + +unsigned int DateToInt(struct sensor* info){ + return info->year << 16 | info->month << 8 | info->day; +} +//упорядочивающую его по дате +void SortByDate(struct sensor* info,int n){ + for(int i=0; i= DateToInt(info+j)) + cgangeIJ(info,i,j); +} + +void AddRecord(struct sensor info[],int number,uint16_t year,uint8_t month,uint8_t day,int8_t t){ + info[number].year = year; + info[number].month = month; + info[number].day = day; + info[number].t = t; +} + + +int AddInfo(struct sensor info[]) +{ +int counter=0; + AddRecord(info,counter++,2021,9,16,9); + AddRecord(info,counter++,2022,9,2,-9); + AddRecord(info,counter++,2021,1,7,8); + AddRecord(info,counter++,2021,9,5,1); + return counter; +} + +void print(struct sensor* info,int number){ + printf("===================================\n"); + for(int i=0;inumber;i++) + printf("%04d-%02d-%02d t=%3d\n", + d->info[i].year, + d->info[i].month, + d->info[i].day, + d->info[i].t); +} + +union sensor_serdes +{ + struct data _data; + uint8_t bytes[sizeof(struct data)]; +}; + +void load_bin_des(union sensor_serdes * d) +{ + FILE* f = fopen("sensor.bin","rb"); + fread(d->bytes,sizeof(d->_data.number),1,f); + fread(d->bytes+sizeof(d->_data.number),d->_data.number*sizeof(struct sensor),1,f); + fclose(f); +} + +void save_bin_ser(union sensor_serdes* d) +{ + FILE* f = fopen("sensor.bin","wb"); + fwrite(d->bytes,sizeof(d->_data.number),1,f); + fwrite(d->bytes+sizeof(d->_data.number),d->_data.number*sizeof(struct sensor),1,f); + fclose(f); +} + +int main(void) +{ + struct data d; + union sensor_serdes* ds = (union sensor_serdes*)&d; + d.number=AddInfo(d.info); + print(d.info,d.number); + save_bin_ser(ds); + printf("\nSort by t\n"); + SortByT(d.info,d.number); + print(d.info,d.number); + printf("\nSort by date\n"); + SortByDate(d.info,d.number); + print(d.info,d.number); + load_bin_des(ds); + print(d.info,d.number); + return 0; +} + diff --git a/24_Seminar11/00_file_p3_8.c b/24_Seminar11/00_file_p3_8.c new file mode 100644 index 0000000..e803fde --- /dev/null +++ b/24_Seminar11/00_file_p3_8.c @@ -0,0 +1,113 @@ +#include +#include +#include +#include +#include +#include +enum {PATH_LENGTH=256}; + +#define STR255 "%255s" + +void convert_path_to_full(char *full_path, const char *dir) { + if(dir[0]=='/') { + strcpy(full_path, dir); + } else if (dir[0]=='.') { + getcwd(full_path,PATH_LENGTH); + } + else { + getcwd(full_path,PATH_LENGTH); + strcat(full_path, "/"); + strcat(full_path, dir); + } + if(full_path[strlen(full_path)-1] !='/') + strcat(full_path,"/");// добавляем / в конце +} +/* +void print_filetype(int type) { + switch (type) { + case DT_BLK: printf("b "); break; + case DT_CHR: printf("c "); break; + case DT_DIR: printf("d "); break; //directory + case DT_FIFO: printf("p "); break; //fifo + case DT_LNK: printf("l "); break; //Sym link + case DT_SOCK: printf("s "); break; //Filetype isn't identified + default: printf(" "); break; + } +} +*/ +/** + Расширить строку пробелами. + @print_lenth длина до которой надо расширить +*/ +void print_space(int print_lenth, int str_lenth) { + while( (print_lenth - str_lenth)>0 ) { + putchar(' '); + str_lenth++; + } +} +void print_tab(int tab_number) { + for(int t=1; td_name[0]=='.' )// пропускаем поддиректории + continue; + char full_filename[PATH_LENGTH]={0}; + files_number++; + print_tab(tab_count);//отступы при рекурсии + printf("%4d : ",files_number); + //print_filetype(entry->d_type); + //не работает для Windows + strcpy(full_filename, full_path); + strcat(full_filename, entry->d_name); + printf("%s", entry->d_name); + print_space(20, entry->d_namlen); + if (!stat(full_filename, &file_stats)){ + print_file_size(file_stats.st_size); + printf("\n"); + } + else { + printf("stat failed for this file\n"); + perror(0); + } + } + closedir(folder); + tab_count--; +} + +int main(void) +{ + char dir[PATH_LENGTH], buf[PATH_LENGTH]; + printf("Input dir: "); + scanf(STR255,dir); + convert_path_to_full(buf, dir); + printf("ls for folder %s\n",buf); + ls(dir); + return 0; +} diff --git a/25_Seminar12/00_file_p2_5.c b/25_Seminar12/00_file_p2_5.c new file mode 100644 index 0000000..e803fde --- /dev/null +++ b/25_Seminar12/00_file_p2_5.c @@ -0,0 +1,113 @@ +#include +#include +#include +#include +#include +#include +enum {PATH_LENGTH=256}; + +#define STR255 "%255s" + +void convert_path_to_full(char *full_path, const char *dir) { + if(dir[0]=='/') { + strcpy(full_path, dir); + } else if (dir[0]=='.') { + getcwd(full_path,PATH_LENGTH); + } + else { + getcwd(full_path,PATH_LENGTH); + strcat(full_path, "/"); + strcat(full_path, dir); + } + if(full_path[strlen(full_path)-1] !='/') + strcat(full_path,"/");// добавляем / в конце +} +/* +void print_filetype(int type) { + switch (type) { + case DT_BLK: printf("b "); break; + case DT_CHR: printf("c "); break; + case DT_DIR: printf("d "); break; //directory + case DT_FIFO: printf("p "); break; //fifo + case DT_LNK: printf("l "); break; //Sym link + case DT_SOCK: printf("s "); break; //Filetype isn't identified + default: printf(" "); break; + } +} +*/ +/** + Расширить строку пробелами. + @print_lenth длина до которой надо расширить +*/ +void print_space(int print_lenth, int str_lenth) { + while( (print_lenth - str_lenth)>0 ) { + putchar(' '); + str_lenth++; + } +} +void print_tab(int tab_number) { + for(int t=1; td_name[0]=='.' )// пропускаем поддиректории + continue; + char full_filename[PATH_LENGTH]={0}; + files_number++; + print_tab(tab_count);//отступы при рекурсии + printf("%4d : ",files_number); + //print_filetype(entry->d_type); + //не работает для Windows + strcpy(full_filename, full_path); + strcat(full_filename, entry->d_name); + printf("%s", entry->d_name); + print_space(20, entry->d_namlen); + if (!stat(full_filename, &file_stats)){ + print_file_size(file_stats.st_size); + printf("\n"); + } + else { + printf("stat failed for this file\n"); + perror(0); + } + } + closedir(folder); + tab_count--; +} + +int main(void) +{ + char dir[PATH_LENGTH], buf[PATH_LENGTH]; + printf("Input dir: "); + scanf(STR255,dir); + convert_path_to_full(buf, dir); + printf("ls for folder %s\n",buf); + ls(dir); + return 0; +} diff --git a/25_Seminar12/01_macros_sum_p7.c b/25_Seminar12/01_macros_sum_p7.c new file mode 100644 index 0000000..f871777 --- /dev/null +++ b/25_Seminar12/01_macros_sum_p7.c @@ -0,0 +1,11 @@ +#include + +#define SUM(a,b) (a) + (b) + +int main(void) +{ + int a=5, b; + b = SUM(a,3) * 10 ; + printf("a = %d b = %d\n",a, b); + return 0; +} diff --git a/25_Seminar12/02_macros_sum_2_p9.c b/25_Seminar12/02_macros_sum_2_p9.c new file mode 100644 index 0000000..cc0f62d --- /dev/null +++ b/25_Seminar12/02_macros_sum_2_p9.c @@ -0,0 +1,11 @@ +#include + +#define SUM(a,b) ((a) + (b)) + +int main(void) +{ + int a=5, b; + b = SUM(a,3) * 10 ; + printf("a = %d b = %d\n",a, b); + return 0; +} diff --git a/25_Seminar12/03_macros_max_p11.c b/25_Seminar12/03_macros_max_p11.c new file mode 100644 index 0000000..efc0042 --- /dev/null +++ b/25_Seminar12/03_macros_max_p11.c @@ -0,0 +1,11 @@ +#include + +#define MAX(a,b) ((a)>=(b)?(a):(b)) + +int main(void) +{ + int a=5, b=7, c; + c = MAX(a,++b) ; + printf("c = %d\n", c); + return 0; +} diff --git a/25_Seminar12/04_macros_swap_p13.c b/25_Seminar12/04_macros_swap_p13.c new file mode 100644 index 0000000..b8e17a6 --- /dev/null +++ b/25_Seminar12/04_macros_swap_p13.c @@ -0,0 +1,14 @@ +#include + +#define SWAP(type,a,b) {type t = a; a = b; b = t;} + +int main(void) +{ + int a=5, b=7; + if (a + +#define SWAP(type,a,b) do{type t = a; a = b; b = t;}while(0) + +int main(void) +{ + int a=5, b=7; + if(a + +#define ANAME(n) a##n +#define MERGE(a,b) a##b +#define PRINT_AN(n) printf("a" #n " = %d\n", a ## n); + +int main(void) +{ + int ANAME(1) = 10; + int ANAME(2) = 22; + int ANAME(3) = 35; + PRINT_AN(1); + PRINT_AN(2); + PRINT_AN(3); + int ai=3; + PRINT_AN(i);//ai + MERGE(a1+,+); + PRINT_AN(1); + return 0; +} diff --git a/25_Seminar12/07_varg_p18.c b/25_Seminar12/07_varg_p18.c new file mode 100644 index 0000000..445f4e1 --- /dev/null +++ b/25_Seminar12/07_varg_p18.c @@ -0,0 +1,16 @@ +#include +#include +#define PR(X, ...) printf("Message " #X ": " __VA_ARGS__) + +int main(void) +{ + double x = 48; + double y; + + y = sqrt(x); + PR(1, "x = %g\n", x); + PR(2, "x = %.2f, y = %.4f\n", x, y); + PR(test, "x = %.2f, y = %.4f\n", x, y); + + return 0; +} diff --git a/25_Seminar12/08_p19.c b/25_Seminar12/08_p19.c new file mode 100644 index 0000000..2f70108 --- /dev/null +++ b/25_Seminar12/08_p19.c @@ -0,0 +1,32 @@ +#include +#include +#define PR(X, ...) printf("Message " #X ": " __VA_ARGS__) +#define DEBUGPRINT fprintf (stderr, "debug in %d line func: %s\n", __LINE__, __func__) + +int main(void) +{ + DEBUGPRINT; + printf("File: %s\n", __FILE__); +#line 100 "help.c" + DEBUGPRINT; +#line 200 + DEBUGPRINT; + + printf("File: %s\n", __FILE__); +#ifndef __APPLE__ + //~ #error This code work only in MacOs +#endif +/* + * Единственное применение, которое я могу придумать, — это приведение номеров строк в порядок + * после длинной серии многострочных макросов. + * +В основном он используется для предоставления имен файлов и номеров строк исходного файла, +из которого был создан файл C (будь то заголовок или реализация). +Учитывая это, компилятор выдаст диагностику, которая намекает на исходный файл, а не на сгенерированный файл. + +Препроцессоры также используют это для указания на включенные заголовки в предварительно обработанном файле, +в котором они расширены. +*/ + + return 0; +} diff --git a/25_Seminar12/ls.c b/25_Seminar12/ls.c new file mode 100644 index 0000000..2520412 --- /dev/null +++ b/25_Seminar12/ls.c @@ -0,0 +1,70 @@ + +//https://iq.opengenus.org/ls-command-in-c/ + +//Used for basic input/output stream +#include +//Used for handling directory files +#include +//For EXIT codes and error handling +#include +#include + +void _ls(const char *dir,int op_a,int op_l) +{ + //Here we will list the directory + struct dirent *d; + DIR *dh = opendir(dir); + if (!dh) + { + if (errno = ENOENT) + { + //If the directory is not found + perror("Directory doesn't exist"); + } + else + { + //If the directory is not readable then throw error and exit + perror("Unable to read directory"); + } + exit(EXIT_FAILURE); + } + //While the next entry is not readable we will print directory files + while ((d = readdir(dh)) != NULL) + { + //If hidden files are found we continue + if (!op_a && d->d_name[0] == '.') + continue; + printf("%s ", d->d_name); + if(op_l) printf("\n"); + } + if(!op_l) + printf("\n"); +} +int main(int argc, const char *argv[]) +{ + if (argc == 1) + { + _ls(".",0,0); + } + else if (argc == 2) + { + if (argv[1][0] == '-') + { + //Checking if option is passed + //Options supporting: a, l + int op_a = 0, op_l = 0; + char *p = (char*)(argv[1] + 1); + while(*p){ + if(*p == 'a') op_a = 1; + else if(*p == 'l') op_l = 1; + else{ + perror("Option not available"); + exit(EXIT_FAILURE); + } + p++; + } + _ls(".",op_a,op_l); + } + } + return 0; +} diff --git a/26_Seminar13/.vscode/tasks.json b/26_Seminar13/.vscode/tasks.json new file mode 100644 index 0000000..d13c845 --- /dev/null +++ b/26_Seminar13/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: gcc.exe build active file", + "command": "D:\\msys64\\ucrt64\\bin\\gcc.exe", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}\\${fileBasenameNoExtension}.exe" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/26_Seminar13/00_case_p3.c b/26_Seminar13/00_case_p3.c new file mode 100644 index 0000000..870efd5 --- /dev/null +++ b/26_Seminar13/00_case_p3.c @@ -0,0 +1,30 @@ +#include +#include + +int main(int argc, char *argv[]) +{ + int rez=0; + // printf("%d\n",argc); + if(argc==1) + printf("-h help\n"); + //opterr=0;Можно отключить вывод сообщений об ошибках, для этого надо где-то в + //программе перед вызовом функции вставить opterr=0 + while ( (rez = getopt(argc,argv,"hf:m:")) != -1) + { + switch (rez) + { + case 'h': + printf("found argument \"h\".\n"); + break; + case 'f': + printf("found argument \"f = %s\".\n",optarg); + break; + case 'm': + printf("found argument \"m = %s\".\n",optarg); + break; + case '?': + printf("Error found !\n"); + break; + }; + }; +} \ No newline at end of file diff --git a/26_Seminar13/01_errors_p6.c b/26_Seminar13/01_errors_p6.c new file mode 100644 index 0000000..1b369d3 --- /dev/null +++ b/26_Seminar13/01_errors_p6.c @@ -0,0 +1,28 @@ +#include +#include +#define N 3 + +int main(int argc, char **argv) +{ + FILE *open; + char name[] = "temperature_small1.csv"; + open = fopen(name, "r"); + if(open==NULL) + return 1; +int Y,M,D; +int r; + while((r = fscanf(open, "%d;%d;%d",&Y,&M,&D))>0) + { + printf("."); + if(r +#include +#define N 3 + +int main(int argc, char **argv) +{ + FILE *open; + char name[] = "temperature_small1.csv"; + open = fopen(name, "r"); + if(open==NULL) + return 1; +int Y,M,D; +int r; +char ch=0; + while((r = fscanf(open, "%d;%d;%d",&Y,&M,&D))>0) + { + if(r +#include +#define N 3 + +int main(int argc, char **argv) +{ + FILE *open; + char name[] = "temperature_small1.csv"; + open = fopen(name, "r"); + if(open==NULL) + return 1; +int counter = 0; +int arr[3] = {0}; +char ch; + while((ch=fgetc(open))!=EOF) + { + if(ch==';') + { + counter++; + if(counter>N) + counter = 0; + } + else if(ch=='\n') + { + if(counter == 2) + printf("%d = %d;%d;%d\n", counter, arr[0],arr[1],arr[2]); + else + printf("ERROR %d = %d;%d;%d\n", counter,arr[0],arr[1],arr[2]); + counter = 0; + arr[0]=arr[1]=arr[2]=0; + } + else if(ch>='0' && ch<='9') + arr[counter] = arr[counter]*10 + ch - '0'; + else if(ch != 0xD) + counter = 0; + } + return 0; +} diff --git a/26_Seminar13/04_file_errors_p10.c b/26_Seminar13/04_file_errors_p10.c new file mode 100644 index 0000000..7c1aacc --- /dev/null +++ b/26_Seminar13/04_file_errors_p10.c @@ -0,0 +1,24 @@ +#include + +int main(int argc, char **argv) +{ + FILE *open; + char name[] = "temperature_small2.csv"; + open = fopen(name, "r"); + if(open==NULL) + return 1; +int Y,M,D,H,m,T,n_scan; + while((n_scan = fscanf(open, "%d;%d;%d;%d;%d;%d", &Y, &M, &D, &H, &m, &T)) > 0) + { + if (n_scan < 6) + { + char s[20], c; + n_scan = fscanf(open, "%[^\n]%c", s, &c); + printf("Wrong format in line %s\n", s); + } + else + printf("%d = %d;%d;%d;%d;%d;%d\n", n_scan, Y, M, D, H, m, T); + } + fclose(open); + return 0; +} diff --git a/26_Seminar13/05_Sensor_p12_13/main.c b/26_Seminar13/05_Sensor_p12_13/main.c new file mode 100644 index 0000000..1da4142 --- /dev/null +++ b/26_Seminar13/05_Sensor_p12_13/main.c @@ -0,0 +1,18 @@ +#include +#include +#include "sensor.h" + +int main(void) +{ +struct sensor info[SIZE]; +int number=AddInfo(info); + print(info,number); + printf("\nSort by t\n"); + SortByT(info,number); + print(info,number); + printf("\nSort by date\n"); + SortByDate(info,number); + // qsort(info,number,sizeof(info[0]),CompareDate); + print(info,number); + return 0; +} \ No newline at end of file diff --git a/26_Seminar13/05_Sensor_p12_13/makefile b/26_Seminar13/05_Sensor_p12_13/makefile new file mode 100644 index 0000000..06a8f50 --- /dev/null +++ b/26_Seminar13/05_Sensor_p12_13/makefile @@ -0,0 +1,14 @@ +all: sensor_demo + +sensor_demo: main.o sensor.o + gcc -o sensor_demo main.o sensor.o + +main.o: main.c sensor.h + gcc -c -std=c99 -o main.o main.c + +sensor.o: sensor.c + gcc -c -std=c99 -o sensor.o sensor.c + +clean: + rm *.o + rm sensor_demo.exe diff --git a/26_Seminar13/05_Sensor_p12_13/sensor.c b/26_Seminar13/05_Sensor_p12_13/sensor.c new file mode 100644 index 0000000..d07ad6a --- /dev/null +++ b/26_Seminar13/05_Sensor_p12_13/sensor.c @@ -0,0 +1,79 @@ +#include +#include "sensor.h" +#include + +void cgangeIJ(struct sensor *info, int i, int j) +{ + struct sensor temp; + temp = info[i]; + info[i] = info[j]; + info[j] = temp; +} +// упорядочивающую его по неубыванию температуры +void SortByT(struct sensor *info, int n) +{ + for (int i = 0; i < n; ++i) + for (int j = i; j < n; ++j) + if (info[i].t >= info[j].t) + cgangeIJ(info, i, j); +} + +int CompareDate(const void *x,const void *y) +{ + struct sensor *a = (struct sensor *)x; + struct sensor *b = (struct sensor *)y; + if ((a->year - b->year) != 0) + return a->year - b->year; + else if ((a->month - b->month) != 0) + return a->month - b->month; + else if ((a->day - b->day) != 0) + return a->day - b->day; + else if ((a->hour - b->hour) != 0) + return a->hour - b->hour; + else if ((a->t - b->t) != 0) + return a->t - b->t; + return 0; +} + +unsigned int DateToInt(struct sensor *info) +{ + return info->year << 16 | info->month << 8 | info->day; +} +// упорядочивающую его по дате +void SortByDate(struct sensor *info, int n) +{ + for (int i = 0; i < n; ++i) + for (int j = i; j < n; ++j) + if (CompareDate(info + i, info + j) > 0) + //~ if(DateToInt(info+i)>= DateToInt(info+j)) + cgangeIJ(info, i, j); +} +void AddRecord(struct sensor *info, int number, uint16_t year, uint8_t month, uint8_t day, uint8_t hour, int8_t t) +{ + info[number].year = year; + info[number].month = month; + info[number].day = day; + info[number].hour = hour; + info[number].t = t; +} + +int AddInfo(struct sensor *info) +{ + int counter = 0; + AddRecord(info, counter++, 2021, 9, 16, 10, 9); + AddRecord(info, counter++, 2022, 9, 2, 11, -9); + AddRecord(info, counter++, 2021, 1, 7, 23, 8); + AddRecord(info, counter++, 2021, 9, 5, 1, 1); + return counter; +} +void print(struct sensor *info, int number) +{ + printf("===================================\n"); + for (int i = 0; i < number; i++) + printf("%04d-%02d-%02d h=%02d t=%3d\n", + info[i].year, + info[i].month, + info[i].day, + info[i].hour, + info[i].t); +} \ No newline at end of file diff --git a/26_Seminar13/05_Sensor_p12_13/sensor.h b/26_Seminar13/05_Sensor_p12_13/sensor.h new file mode 100644 index 0000000..03a47a8 --- /dev/null +++ b/26_Seminar13/05_Sensor_p12_13/sensor.h @@ -0,0 +1,23 @@ +#include +#define SIZE 30 + +struct sensor +{ + uint8_t day; + uint8_t month; + uint16_t year; + uint8_t hour; + int8_t t; +}; + +void cgangeIJ(struct sensor* info,int i,int j); +//упорядочивающую его по неубыванию температуры +void SortByT(struct sensor* info,int n); +unsigned int DateToInt(struct sensor* info); +//упорядочивающую его по дате +void SortByDate(struct sensor* info,int n); +void AddRecord(struct sensor* info,int number, +uint16_t year,uint8_t month,uint8_t day,uint8_t hour,int8_t t); +int AddInfo(struct sensor* info); +void print(struct sensor* info,int number); +int CompareDate(const void *x,const void *y); \ No newline at end of file diff --git a/26_Seminar13/06_array_stack_p15.c b/26_Seminar13/06_array_stack_p15.c new file mode 100644 index 0000000..c5e328c --- /dev/null +++ b/26_Seminar13/06_array_stack_p15.c @@ -0,0 +1,24 @@ +#include +#include +#include + +#define SIZE 30000000 + +struct sensor +{ + uint8_t day; + uint8_t month; + uint16_t year; + uint8_t hour; + int8_t t; +}; + +int main(int argc, char **argv) +{ + static struct sensor info[SIZE]; + struct sensor* info1 = malloc(SIZE*sizeof(struct sensor)); + if(!info1) + return -1; + free(info1); + return 0; +} diff --git a/26_Seminar13/temperature_small1.csv b/26_Seminar13/temperature_small1.csv new file mode 100644 index 0000000..acb2d9f --- /dev/null +++ b/26_Seminar13/temperature_small1.csv @@ -0,0 +1,16 @@ +2021;01;16 +2021;01;16 +2021;01;16 +2021;01;16 +2021;02;xx +2021;02;17 +2021;aa;16 +2021;04;16 +2021;05;16 +20;06;16 +2021;07;16 +2021;08;16 +2021;09;16 +2021;10;16 +2021;11;16 +2021;12;16 diff --git a/26_Seminar13/temperature_small2.csv b/26_Seminar13/temperature_small2.csv new file mode 100644 index 0000000..4c38a2d --- /dev/null +++ b/26_Seminar13/temperature_small2.csv @@ -0,0 +1,16 @@ +2021;01;16;01;01;-47 +2021;01;16;01;03;-44 +2021;01;16;01;04;-43 +2021;01;16;01;05;-xx +2021;02;16;01;01;-25 +2021;02;17;01;01;-30 +2021;03;16;01;01;-10 +2021;04;16;01;01;0 +2021;05;16;01;01;10 +2021;06;16;01;01;25 +2021;07;16;01;01;30 +2021;08;16;01;01;20 +2021;09;16;01;01;18 +2021;10;16;01;01;2 +2021;11;16;01;01;-5 +2021;12;16;01;01;-20 diff --git a/CourseWork/temperature_data_examples (1).zip b/CourseWork/temperature_data_examples (1).zip new file mode 100644 index 0000000..2f9422a Binary files /dev/null and b/CourseWork/temperature_data_examples (1).zip differ diff --git a/CourseWork/Итоговое задание.pdf b/CourseWork/Итоговое задание.pdf new file mode 100644 index 0000000..eefeb07 Binary files /dev/null and b/CourseWork/Итоговое задание.pdf differ