first commit
This commit is contained in:
commit
cd6a6b2682
12
01_Lecture01/00_hello_world_p36.c
Normal file
12
01_Lecture01/00_hello_world_p36.c
Normal file
@ -0,0 +1,12 @@
|
||||
/* Это комментарий.
|
||||
Он не влияет на работу программы */
|
||||
|
||||
// Это тоже комментарий только из одной строки
|
||||
|
||||
#include <stdio.h> // Подключаем библиотеку ввода-вывода
|
||||
|
||||
int main (int argc, char **argv) // Программа начинается тут. Это точка выполнения программы
|
||||
{
|
||||
printf ("Hello world!\n"); // Напечатать текст Hello World!
|
||||
return 0; // Завершает программу с кодом 0, что все хорошо
|
||||
} // Конец блока main
|
19
01_Lecture01/01_sqrt_equation_p37.c
Normal file
19
01_Lecture01/01_sqrt_equation_p37.c
Normal file
@ -0,0 +1,19 @@
|
||||
/*
|
||||
Комментарий
|
||||
*/
|
||||
//Комментарий
|
||||
#include <stdio.h>
|
||||
#include <locale.h>
|
||||
|
||||
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;
|
||||
}
|
14
01_Lecture01/02_ASCII_art_p38.c
Normal file
14
01_Lecture01/02_ASCII_art_p38.c
Normal file
@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
/*
|
||||
(\___/)
|
||||
(='.'=)
|
||||
(")_(")
|
||||
*/
|
||||
printf("(\\___/)\n");
|
||||
printf("(='.'=)\n");
|
||||
printf("(\")_(\")\n");
|
||||
|
||||
return 0;
|
||||
}
|
18
01_Lecture01/03_ASCII_art_hard_p39.c
Normal file
18
01_Lecture01/03_ASCII_art_hard_p39.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
/*
|
||||
___ _____ ______________ ___ ____ ______
|
||||
/ | / ___// ____/ _/ _/ / | / __ \/_ __/
|
||||
/ /| | \__ \/ / / / / / / /| | / /_/ / / /
|
||||
/ ___ |___/ / /____/ /_/ / / ___ |/ _, _/ / /
|
||||
/_/ |_/____/\____/___/___/ /_/ |_/_/ |_| /_/
|
||||
*/
|
||||
|
||||
printf(" ___ _____ ______________ ___ ____ ______\n");
|
||||
printf(" / | / ___// ____/ _/ _/ / | / __ \\/_ __/\n");
|
||||
printf(" / /| | \\__ \\/ / / / / / / /| | / /_/ / / / \n");
|
||||
printf(" / ___ |___/ / /____/ /_/ / / ___ |/ _, _/ / / \n");
|
||||
printf("/_/ |_/____/\\____/___/___/ /_/ |_/_/ |_| /_/\n");
|
||||
return 0;
|
||||
}
|
10
03_Lecture03/00_example_p4.c
Normal file
10
03_Lecture03/00_example_p4.c
Normal file
@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, sum=0;
|
||||
for (i=1; i<10; i++)
|
||||
sum*=i;
|
||||
printf("%d",sum);
|
||||
return 0;
|
||||
}
|
13
03_Lecture03/01_announcement_p10.c
Normal file
13
03_Lecture03/01_announcement_p10.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
13
03_Lecture03/02_announcement_spec_p13.c
Normal file
13
03_Lecture03/02_announcement_spec_p13.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
16
03_Lecture03/03_const_p15.c
Normal file
16
03_Lecture03/03_const_p15.c
Normal file
@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
|
||||
extern int Voltage;
|
||||
int main()
|
||||
{
|
||||
{ // Операторные скобки обознаячают составной оператор
|
||||
float Power = 0.5f;
|
||||
}
|
||||
{
|
||||
static int Current;
|
||||
}
|
||||
Power = 0;// Ошибка! области видимости
|
||||
const int SIZE=1000; // Объявление целочисленной константы.
|
||||
//Обратите внимание, имена констант
|
||||
// обычно пишут заглавными буквами.
|
||||
}
|
10
03_Lecture03/04_side_effect_p17.c
Normal file
10
03_Lecture03/04_side_effect_p17.c
Normal file
@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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
|
||||
}
|
10
03_Lecture03/05_calc_p19.c
Normal file
10
03_Lecture03/05_calc_p19.c
Normal file
@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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
|
||||
}
|
11
03_Lecture03/06_explicit_p21.c
Normal file
11
03_Lecture03/06_explicit_p21.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int X;
|
||||
int Y = 200;
|
||||
char C = 255; //Чтобы избежать неоднозначностей, рекомендуется явно
|
||||
//указывать знаковость для типа char -1 или 255?
|
||||
X = C * 10 + Y; // переменная С приводится к типу int
|
||||
printf("%d\n",X);
|
||||
}
|
20
03_Lecture03/07_implicit_p23.c
Normal file
20
03_Lecture03/07_implicit_p23.c
Normal file
@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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);
|
||||
}
|
10
03_Lecture03/08_implicit_2_p24.c
Normal file
10
03_Lecture03/08_implicit_2_p24.c
Normal file
@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
short i, x=5,y=-2;
|
||||
i = x + y; //переменные x и y сначала расширяются до типа int,
|
||||
// выполняется сложение а затем результат
|
||||
//помещается в i
|
||||
printf("%d",i);
|
||||
}
|
12
03_Lecture03/09_implicit_3_p25.c
Normal file
12
03_Lecture03/09_implicit_3_p25.c
Normal file
@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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
|
||||
}
|
8
03_Lecture03/10_short_p27.c
Normal file
8
03_Lecture03/10_short_p27.c
Normal file
@ -0,0 +1,8 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int a = 50, b=7;
|
||||
a = a + b; // a=57
|
||||
a += b; // a=57 можно так
|
||||
}
|
8
03_Lecture03/11_postfix_p28.c
Normal file
8
03_Lecture03/11_postfix_p28.c
Normal file
@ -0,0 +1,8 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// Постфиксная форма
|
||||
int a, b=7;
|
||||
a = b++; // a=7 b=8
|
||||
}
|
8
03_Lecture03/12_prefix_p28.c
Normal file
8
03_Lecture03/12_prefix_p28.c
Normal file
@ -0,0 +1,8 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// Префиксная форма
|
||||
int a, b=7;
|
||||
a = ++b; // a=8 b=8
|
||||
}
|
13
03_Lecture03/13_sequence_point_p30.c
Normal file
13
03_Lecture03/13_sequence_point_p30.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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);
|
||||
}
|
11
03_Lecture03/14_numbers_sum_p32.c
Normal file
11
03_Lecture03/14_numbers_sum_p32.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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);
|
||||
}
|
11
03_Lecture03/15_sum_p35.c
Normal file
11
03_Lecture03/15_sum_p35.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h> //Объявить библиотеки ввода-вывода
|
||||
|
||||
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; //Завершить программу успешно
|
||||
}
|
10
03_Lecture03/16_16rich_p38.c
Normal file
10
03_Lecture03/16_16rich_p38.c
Normal file
@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int a;
|
||||
printf("Input number:");
|
||||
scanf ("%d", &a);
|
||||
printf("%#5x", a);
|
||||
return 0;
|
||||
}
|
11
03_Lecture03/17_mistakes_p40.c
Normal file
11
03_Lecture03/17_mistakes_p40.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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
|
||||
}
|
17
03_Lecture03/18_output_p41.c
Normal file
17
03_Lecture03/18_output_p41.c
Normal file
@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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
|
||||
}
|
12
03_Lecture03/19_output_2_p42.c
Normal file
12
03_Lecture03/19_output_2_p42.c
Normal file
@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int x = 1234;
|
||||
printf ("%d\n", x); //минимальное число позиций под вывод числа
|
||||
//1234
|
||||
|
||||
printf ("%9d\n", x);//под вывод числа выделено 9 позиций
|
||||
printf ("%09d\n", x);//под вывод числа выделено 9 позиций
|
||||
// 1234
|
||||
}
|
17
03_Lecture03/20_output_3_p43.c
Normal file
17
03_Lecture03/20_output_3_p43.c
Normal file
@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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
|
||||
}
|
12
03_Lecture03/21_mistake_p45.c
Normal file
12
03_Lecture03/21_mistake_p45.c
Normal file
@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int a, b;
|
||||
float x, y;
|
||||
a = 5;
|
||||
10 = x;//константа
|
||||
y = 7,8; //точка вместо запятой
|
||||
b = 2.5; //неверный тип
|
||||
a = b + x; //пропадет дробная часть
|
||||
}
|
11
03_Lecture03/22_print_p46.c
Normal file
11
03_Lecture03/22_print_p46.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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);
|
||||
}
|
26
03_Lecture03/23_sqrt_equation_p49.c
Normal file
26
03_Lecture03/23_sqrt_equation_p49.c
Normal file
@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <locale.h>
|
||||
#include <math.h>
|
||||
|
||||
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;
|
||||
}
|
12
04_Lecture04/00_inttypes_p5.c
Normal file
12
04_Lecture04/00_inttypes_p5.c
Normal file
@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
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;
|
||||
}
|
11
04_Lecture04/01_compare_p7.c
Normal file
11
04_Lecture04/01_compare_p7.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int a, b;
|
||||
a = 5 == 3; // a=0 - ëîæü
|
||||
b = 100 >= 1; // b=1
|
||||
printf("a=%d b=%d",a,b);
|
||||
return 0;
|
||||
}
|
10
04_Lecture04/02_operations_p9.c
Normal file
10
04_Lecture04/02_operations_p9.c
Normal file
@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int a, b=5, n=0;
|
||||
a = ( n!=0 && b/n ); // Нет ошибки - деления на ноль
|
||||
printf("%d %d",a,b);
|
||||
return 0;
|
||||
}
|
16
04_Lecture04/03_solution_p14.c
Normal file
16
04_Lecture04/03_solution_p14.c
Normal file
@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
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;
|
||||
}
|
13
04_Lecture04/04_max_p18.c
Normal file
13
04_Lecture04/04_max_p18.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int a,b;
|
||||
scanf("%d%d",&a,&b);
|
||||
if (a>b)
|
||||
printf("%d",a);
|
||||
else
|
||||
printf("%d",b);
|
||||
return 0;
|
||||
}
|
17
04_Lecture04/05_max_tern_p21.c
Normal file
17
04_Lecture04/05_max_tern_p21.c
Normal file
@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
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;
|
||||
}
|
16
04_Lecture04/06_example_tv_p22.c
Normal file
16
04_Lecture04/06_example_tv_p22.c
Normal file
@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
11
04_Lecture04/07_right_shift_p25.c
Normal file
11
04_Lecture04/07_right_shift_p25.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
uint32_t u=0xaabbccdd;
|
||||
printf("0x%X\n",u); // 0aabbccdd
|
||||
u = u>>4; // логический сдвиг
|
||||
printf("0x%08X\n",u); // 0aabbccd
|
||||
return 0;
|
||||
}
|
10
04_Lecture04/08_left_shift_p26.c
Normal file
10
04_Lecture04/08_left_shift_p26.c
Normal file
@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int32_t a=0xaabbccdd;
|
||||
printf("%x\n",a); // 0aabbccdd
|
||||
a >>= 4; // арифметический сдвиг
|
||||
printf("%x\n",a); // faabbccd
|
||||
}
|
9
04_Lecture04/09_shift_features_p27.c
Normal file
9
04_Lecture04/09_shift_features_p27.c
Normal file
@ -0,0 +1,9 @@
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int a=1,b=-1;
|
||||
printf("a = %d b = %d", a>>32, b>>-1);
|
||||
// a = 0 b = -1
|
||||
}
|
10
04_Lecture04/10_bit operations_p29.c
Normal file
10
04_Lecture04/10_bit operations_p29.c
Normal file
@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
unsigned int a=0xAABBCCFF;
|
||||
a = a & ~7;//1111 1000
|
||||
printf("a = %x", a);
|
||||
// a = aabbccf8
|
||||
}
|
10
04_Lecture04/11_bit operations_2_p30.c
Normal file
10
04_Lecture04/11_bit operations_2_p30.c
Normal file
@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
unsigned int a=0xFFFFFF00;
|
||||
a = a | 3;
|
||||
printf("a = %x", a);
|
||||
// a = ffffff03
|
||||
}
|
14
04_Lecture04/12_bit operations_3_p31.c
Normal file
14
04_Lecture04/12_bit operations_3_p31.c
Normal file
@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
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
|
||||
}
|
14
04_Lecture04/13_bit operations_4_p32.c
Normal file
14
04_Lecture04/13_bit operations_4_p32.c
Normal file
@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
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
|
||||
}
|
13
04_Lecture04/14_bit operations_5_p33.c
Normal file
13
04_Lecture04/14_bit operations_5_p33.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
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
|
||||
}
|
13
04_Lecture04/15_bit operations_6_p33.c
Normal file
13
04_Lecture04/15_bit operations_6_p33.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
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
|
||||
}
|
14
04_Lecture04/16_invert_p34.c
Normal file
14
04_Lecture04/16_invert_p34.c
Normal file
@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
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
|
||||
}
|
49
04_Lecture04/17_sqrt_equation_p40_41.c
Normal file
49
04_Lecture04/17_sqrt_equation_p40_41.c
Normal file
@ -0,0 +1,49 @@
|
||||
#include <stdio.h>
|
||||
#include <locale.h>
|
||||
#include <math.h>
|
||||
|
||||
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;
|
||||
}
|
25
05_Lecture05/00_switch_p6.c
Normal file
25
05_Lecture05/00_switch_p6.c
Normal file
@ -0,0 +1,25 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
12
05_Lecture05/01_while_p11.c
Normal file
12
05_Lecture05/01_while_p11.c
Normal file
@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n = 0;
|
||||
while (n!=5)// лучше n<5
|
||||
{
|
||||
printf("Hello\n");
|
||||
n++; // n = n+1
|
||||
}
|
||||
return 0;
|
||||
}
|
13
05_Lecture05/02_while_2_p11.c
Normal file
13
05_Lecture05/02_while_2_p11.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
n=5;
|
||||
while (n!=0)//n>0
|
||||
{
|
||||
printf("Hello %d\n",n);
|
||||
n--;
|
||||
}
|
||||
return 0;
|
||||
}
|
13
05_Lecture05/03_while_example_p13.c
Normal file
13
05_Lecture05/03_while_example_p13.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
n=1;
|
||||
while (n <= 5)
|
||||
{
|
||||
printf("%d\n", n);
|
||||
n++;
|
||||
}
|
||||
return 0;
|
||||
}
|
13
05_Lecture05/04_while_example_2_p15.c
Normal file
13
05_Lecture05/04_while_example_2_p15.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
n=2;
|
||||
while (n != 5)
|
||||
{
|
||||
printf("%d\n", n);
|
||||
n = n+2;
|
||||
}
|
||||
return 0;
|
||||
}
|
17
05_Lecture05/05_digits_p19.c
Normal file
17
05_Lecture05/05_digits_p19.c
Normal file
@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
16
05_Lecture05/06_deviders_p21.c
Normal file
16
05_Lecture05/06_deviders_p21.c
Normal file
@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
11
05_Lecture05/07_do_while_p24.c
Normal file
11
05_Lecture05/07_do_while_p24.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int a = 4, b = 6;
|
||||
do
|
||||
{
|
||||
a++;
|
||||
} while (a < b); // Не забываем точку с запятой в конце
|
||||
return 0;
|
||||
}
|
19
05_Lecture05/08_number_p29.c
Normal file
19
05_Lecture05/08_number_p29.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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);
|
||||
}
|
12
05_Lecture05/09_comtinue_p31.c
Normal file
12
05_Lecture05/09_comtinue_p31.c
Normal file
@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
for(i=1; i<5; i++)
|
||||
{
|
||||
if (i==3)
|
||||
continue;
|
||||
printf("i = %d\n",i);
|
||||
}
|
||||
}
|
15
05_Lecture05/10_break_p32.c
Normal file
15
05_Lecture05/10_break_p32.c
Normal file
@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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
|
18
05_Lecture05/11_sqrt_p36.c
Normal file
18
05_Lecture05/11_sqrt_p36.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
19
05_Lecture05/12_count_p38.c
Normal file
19
05_Lecture05/12_count_p38.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
23
05_Lecture05/13_fibonacci_p40.c
Normal file
23
05_Lecture05/13_fibonacci_p40.c
Normal file
@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
10
05_Lecture05/14_goto_p42.c
Normal file
10
05_Lecture05/14_goto_p42.c
Normal file
@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("Hello");
|
||||
goto skip;
|
||||
printf("World");
|
||||
skip:
|
||||
return 0;
|
||||
}
|
11
05_Lecture05/15_global_p45.c
Normal file
11
05_Lecture05/15_global_p45.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
|
||||
/*Глобальная переменная. Видна внутри всего файла.*/
|
||||
int a=5;
|
||||
int main()
|
||||
{
|
||||
/*Локальная переменная видна только внутри main. */
|
||||
int a=10;
|
||||
printf("a = %d",a);
|
||||
return 0;
|
||||
}
|
11
05_Lecture05/16_example_1_p47.c
Normal file
11
05_Lecture05/16_example_1_p47.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, sum=0;
|
||||
for(i=0; i<5; i++) {
|
||||
sum+=i;
|
||||
}
|
||||
printf("%d\n",i); // i = 5
|
||||
return 0;
|
||||
}
|
12
05_Lecture05/16_example_2_p47.c
Normal file
12
05_Lecture05/16_example_2_p47.c
Normal file
@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int sum=0;
|
||||
// переменная i видна только внутри for
|
||||
for(int i=0; i<5; i++) {
|
||||
sum+=i;
|
||||
}
|
||||
printf("%d\n",i); //ошибка
|
||||
return 0;
|
||||
}
|
11
05_Lecture05/17_define_p52.c
Normal file
11
05_Lecture05/17_define_p52.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
|
||||
// Плохо
|
||||
unsigned int a, h;
|
||||
char ln[25], f[25];
|
||||
|
||||
// Хорошо
|
||||
#define NAME_LENGTH 100
|
||||
unsigned int age, height;
|
||||
char lastName[NAME_LENGTH],
|
||||
firstName[NAME_LENGTH];
|
35
05_Lecture05/18_sqrt_equation_p54.c
Normal file
35
05_Lecture05/18_sqrt_equation_p54.c
Normal file
@ -0,0 +1,35 @@
|
||||
#include <locale.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
9
06_Lecture06/00_getchar_putchar_p5.c
Normal file
9
06_Lecture06/00_getchar_putchar_p5.c
Normal file
@ -0,0 +1,9 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char c;
|
||||
while( (c=getchar())!='.')
|
||||
putchar(c);
|
||||
return 0;
|
||||
}
|
13
06_Lecture06/01_getchar_putchar_2_p6.c
Normal file
13
06_Lecture06/01_getchar_putchar_2_p6.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main ()
|
||||
{
|
||||
char character;
|
||||
puts("Введите символ, символ точки - выход('.'):");
|
||||
do
|
||||
{
|
||||
character = getchar(); // считать введённый со стандартного потока ввода символ
|
||||
putchar (character); // вывести этот символ
|
||||
} while (character != '.'); // пока введенный символ не точка
|
||||
return 0;
|
||||
}
|
13
06_Lecture06/02_letter_change_p12.c
Normal file
13
06_Lecture06/02_letter_change_p12.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
18
06_Lecture06/03_getch_p13.c
Normal file
18
06_Lecture06/03_getch_p13.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
#include <conio.h>
|
||||
//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/
|
21
06_Lecture06/03_getch_p13_ncurses.c
Normal file
21
06_Lecture06/03_getch_p13_ncurses.c
Normal file
@ -0,0 +1,21 @@
|
||||
#include <stdio.h>
|
||||
//#include <conio.h>
|
||||
#include <ncurses/ncurses.h>
|
||||
|
||||
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 <ncurses.h>
|
16
06_Lecture06/03_letter_add_p15.c
Normal file
16
06_Lecture06/03_letter_add_p15.c
Normal file
@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
14
06_Lecture06/04_numbers_combine_p16.c
Normal file
14
06_Lecture06/04_numbers_combine_p16.c
Normal file
@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
15
06_Lecture06/05_function_1_p19.c
Normal file
15
06_Lecture06/05_function_1_p19.c
Normal file
@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
18
06_Lecture06/06_function_2_p19.c
Normal file
18
06_Lecture06/06_function_2_p19.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
18
06_Lecture06/07_function_void_1_p21.c
Normal file
18
06_Lecture06/07_function_void_1_p21.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
16
06_Lecture06/08_function_void_p22.c
Normal file
16
06_Lecture06/08_function_void_p22.c
Normal file
@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
20
06_Lecture06/09_function_void_1_p23.c
Normal file
20
06_Lecture06/09_function_void_1_p23.c
Normal file
@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
18
06_Lecture06/10_function_void_2_p23.c
Normal file
18
06_Lecture06/10_function_void_2_p23.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
14
06_Lecture06/11_module_p29.c
Normal file
14
06_Lecture06/11_module_p29.c
Normal file
@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int abs(int num)
|
||||
{
|
||||
return (num<0)?-num:num;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int num;
|
||||
scanf("%d",&num);
|
||||
printf("%d",abs(num));
|
||||
return 0;
|
||||
}
|
24
06_Lecture06/12_prime_check_p30.c
Normal file
24
06_Lecture06/12_prime_check_p30.c
Normal file
@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
87
06_Lecture06/13_sqrt_final_34_37.c
Normal file
87
06_Lecture06/13_sqrt_final_34_37.c
Normal file
@ -0,0 +1,87 @@
|
||||
#include <stdio.h>
|
||||
#include <conio.h>
|
||||
#include <locale.h>
|
||||
#include <math.h>
|
||||
|
||||
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;
|
||||
}
|
13
06_Lecture06/14_fsm_p40.c
Normal file
13
06_Lecture06/14_fsm_p40.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
31
06_Lecture06/15_fsm_2_p41.c
Normal file
31
06_Lecture06/15_fsm_2_p41.c
Normal file
@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
68
06_Lecture06/16_coffee_p43.c
Normal file
68
06_Lecture06/16_coffee_p43.c
Normal file
@ -0,0 +1,68 @@
|
||||
#include <stdio.h>
|
||||
#include <conio.h>
|
||||
|
||||
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;
|
||||
}
|
78
06_Lecture06/16_coffee_p43refact.c
Normal file
78
06_Lecture06/16_coffee_p43refact.c
Normal file
@ -0,0 +1,78 @@
|
||||
#include <stdio.h>
|
||||
#include <conio.h>
|
||||
|
||||
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;
|
||||
}
|
15
07_Lecture07/00_func_p5.c
Normal file
15
07_Lecture07/00_func_p5.c
Normal file
@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void func(void) {
|
||||
static int a=5; //статическая память
|
||||
a++;
|
||||
printf("a = %d\n",a);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
func();
|
||||
func();
|
||||
func();
|
||||
return 0;
|
||||
}
|
11
07_Lecture07/01_pointer_p9.c
Normal file
11
07_Lecture07/01_pointer_p9.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
15
07_Lecture07/02_pointer_use_p10.c
Normal file
15
07_Lecture07/02_pointer_use_p10.c
Normal file
@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
14
07_Lecture07/03_pointer_to_pointer_p12.c
Normal file
14
07_Lecture07/03_pointer_to_pointer_p12.c
Normal file
@ -0,0 +1,14 @@
|
||||
//Можно объявлять указатели на указатели:
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
16
07_Lecture07/04_swap_p13.c
Normal file
16
07_Lecture07/04_swap_p13.c
Normal file
@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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);
|
||||
}
|
16
07_Lecture07/05_swap_2_p15.c
Normal file
16
07_Lecture07/05_swap_2_p15.c
Normal file
@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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);
|
||||
}
|
15
07_Lecture07/06_rec_p19.c
Normal file
15
07_Lecture07/06_rec_p19.c
Normal file
@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void rec(int n)
|
||||
{
|
||||
//~ printf("%5d",n);
|
||||
if(n>0)
|
||||
rec(n-1);
|
||||
printf("%5d",n);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
rec(3);
|
||||
return 0;
|
||||
}
|
15
07_Lecture07/07_rec_fact_p21.c
Normal file
15
07_Lecture07/07_rec_fact_p21.c
Normal file
@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
21
07_Lecture07/08_sum_p24.c
Normal file
21
07_Lecture07/08_sum_p24.c
Normal file
@ -0,0 +1,21 @@
|
||||
#include <stdio.h>
|
||||
|
||||
//Нерекурсивный способ:
|
||||
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;
|
||||
}
|
18
07_Lecture07/09_sum_rec_p25.c
Normal file
18
07_Lecture07/09_sum_rec_p25.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
|
||||
//Рекурсивный способ:
|
||||
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;
|
||||
}
|
15
07_Lecture07/10_fibonachi_25.c
Normal file
15
07_Lecture07/10_fibonachi_25.c
Normal file
@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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));
|
||||
}
|
38
07_Lecture07/11_2_for_rec_p33.c
Normal file
38
07_Lecture07/11_2_for_rec_p33.c
Normal file
@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void iterFor(int start,int n)
|
||||
{
|
||||
for(int i=start; i<n; i++)
|
||||
printf("%d\n",i);
|
||||
}
|
||||
|
||||
//Рекурсивный способ:
|
||||
void recursionFor(int i, int n)
|
||||
{
|
||||
if(i<n)
|
||||
{
|
||||
printf("%d ", i);
|
||||
recursionFor(i+1, n);
|
||||
}
|
||||
}
|
||||
//Рекурсивный способ:
|
||||
void recursionForStaic(int n)
|
||||
{
|
||||
static int i=0;
|
||||
if(i<n)
|
||||
{
|
||||
printf("%d\n", i);
|
||||
i++;
|
||||
recursionForStaic(n);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
iterFor(0,5);
|
||||
recursionFor(0,5);
|
||||
//~ recursionForStaic(5);
|
||||
//~ recursionForStaic(5);
|
||||
return 0;
|
||||
}
|
28
07_Lecture07/11_fibonachi_diag_p28.c
Normal file
28
07_Lecture07/11_fibonachi_diag_p28.c
Normal file
@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
18
07_Lecture07/12_print_rev_p36.c
Normal file
18
07_Lecture07/12_print_rev_p36.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user