Ëåêöèÿ 3 (20.09.2021)
íàä ïîñëåäîâàòåëüíîñòÿìè ÷èñåë â ôàéëå.
Çàäà÷à 1. ßâëÿåòñÿ ëè ïîñëåäîâàòåëüíîñòü ïîñòîÿííîé?
Íå èñïîëüçîâàòü feof()!!!
#include <stdio.h>
int main(void) {
FILE *IN;
int first, num;
IN = fopen("tmp.dat", "r");
if (IN == NULL) {
printf("File not opened\n");
return -1;
}
if (fscanf(IN, "%d", &first) != 1) {
printf("File empty\n");
fclose(IN);
return -1;
}
while (fscanf(IN, "%d", &num) == 1) {
if (num != first) {
printf("No\n");
fclose(IN);
return 0;
}
}
printf("Yes\n");
fclose(IN);
return 0
}
Âîïðîñ. Çà÷åì çàêðûâàòü ôàéëû?
Îòâåò.
1. Â ôàéëàõ, îòêðûòûõ íà çàïèñü, íå ñîõðàíÿòñÿ èçìåíåíèÿ.
2. Âñå îòêðûòûå ôàéëû çàíîñÿòñÿ â òàáëèöó îòêðûòûõ ôàéëîâ
â îïåðàöèîííîé ñèñòåìå, îáùàÿ äëÿ âñåõ ïðîöåññîâ
(òàáëèöà äåñêðèïòîðîâ).
Ðàçìåð òàáëèöû îãðàíè÷åí, ïîýòîìó
íåíóæíûå ôàéëû íàäî çàêðûâàòü, ÷òîáû òàáëèöà íå
ïåðåïîëíèëàñü.
Ïðèìåð 1. Íàéòè ìàêñèìàëüíûé ðàçìåð òàáëèöû äåñêðèïòîðîâ
(â ôàéëàõ)
#include <stdio.h>
int main(void) {
FILE *IN;
int i = 0;
while (1) { /* Inifinite loop */
IN = fopen("tmp.dat", "r");
if (IN == NULL)
break;
++i;
}
printf("Files opened: %d\n", i);
return 0;
}
Âûïîëíåíèå ýòîé ïðîãðàììû:
Ðåçóëüòàò 3197 îòêðûòûõ ôàéëîâ.
Çàäà÷à 2. Íàéòè çíà÷åíèå ìíîãî÷ëåíà, êîýôôèöèåíòû
êîòîðîãî íàõîäÿòñÿ â ôàéëå "tmp.dat" â ïîðÿäêå âîçðàñòàíèÿ
ñòåïåíåé, â òî÷êå x, çàäàâàåìîé ñ êëàâèàòóðû.
Ïðèìåð 2.  ôàéëå - ÷èñëà:
1
0
-1
Çíà÷åíèå ýòèõ ÷èñåë:
1 - ýòî êîýôôèöèåíò ïðè íóëåâîé ñòåïåíè ìíîãî÷ëåíà
0 - ïðè ïåðâîé
-1 - ïðè âòîðîé ñòåïåíè
Ïîëó÷àåòñÿ, ÷òî ôàéëå çàïèñàí ìíîãî÷ëåí:
y = -x*x + 0 * x +1
#include <stdio.h>
int main(void) {
double res = 0;
double a;
double x;
double xn = 1; /* This is x in degree of current n */
FILE *IN;
IN = fopen("tmp.dat", "r");
if (IN == NULL) {
printf("File not opened\n");
return -1;
}
printf("Input x\n");
scanf("%lf", &x);
while (fscanf(IN, "%lf", &a) == 1) {
res += a * xn;
xn *= x;
}
fclose(IN);
printf("Res: %lf\n", res);
return 0;
}
Ñîìåðñåò Ìîýì: "Ãëàâíîå â ïðîãðàììå - ÷òîáû îíà áûëà âûïîëíåíà"
Âûïîëíåíèÿ ïðîãðàììû-ðåøåíèÿ çàäà÷è 2:
Ôîðìàòû (îòâåò íà âîïðîñ èç çàëà):
%lf – ôîðìàò ÷òåíèÿ/çàïèñè âåùåñòâåííîãî ÷èñëà äâîéíîé òî÷íîñòè (Long Float)
%d - ôîðìàò ÷òåíèÿ/çàïèñè äåñÿòè÷íîãî öåëîãî ÷èñëà (Decimal)
man printf – ìîæíî ïîñìîòðåòü âñå ôîðìàòû (êîìàíäà man – êîìàíäà Ëèíóêñà, êîòîðàÿ âûäàåò êîðîòêóþ äîêóìåíòàöèþ ïî ôóíêöèÿì). Âûõîä — êîìàíäîé «q”.
info printf – áîëåå ïîëíàÿ äîêóìåíòàöèÿ. Âûõîä — êîìàíäîé «:q”.
Âû÷èñëåíèå ïðîèçâîäíîé äëÿ çàäà÷è 2:
#include <stdio.h>
int main(void) {
double val = 0, proizv = 0;
double a;
double x;
double xn = 1; /* This is x in degree of current n */
int n = 0;
FILE *IN;
IN = fopen("tmp.dat", "r");
if (IN == NULL) {
printf("File not opened\n");
return -1;
}
printf("Input x\n");
scanf("%lf", &x);
if (fscanf(IN, "%lf", &a) != 1) {
printf(“File empty\n”);
fclose(IN);
return -1;
}
val +=a;
++n;
while (fscanf(IN, "%lf", &a) == 1) {
proizv += n * a * xn; /* xn == x^(n-1) */
xn *= x;
val += a * xn; /* xn == x^n because already multiplied by x in the line above */
++n;
}
fclose(IN);
printf("Val: %lf, proizv: %lf\n", val, proizv);
return 0;
}
Ðåçóëüòàò âûïîëíåíèÿ ýòîé ïðîãðàììû:
Çàäà÷à 3. Íàéòè çíà÷åíèå ìíîãî÷ëåíà, êîýôôèöèåíòû
êîòîðîãî íàõîäÿòñÿ â ôàéëå "tmp.dat" â ïîðÿäêå óáûâàíèÿ
ñòåïåíåé, â òî÷êå x, çàäàâàåìîé ñ êëàâèàòóðû
(çà îäíî ïðî÷òåíèå ôàéëà, íåëüçÿ ïîëüçîâàòüñÿ äîïîëíèòåëüíîé
ïàìÿòüþ)
Ïðèìåð 2.  ôàéëå - ÷èñëà:
1
0
-1
1 - ýòî êîýôôèöèåíò ïðè âòîðîé ñòåïåíè ìíîãî÷ëåíà
0 - ïðè ïåðâîé
-1 - ïðè íóëåâîé ñòåïåíè
Ïîëó÷àåòñÿ ìíîãî÷ëåí:
y = 1*x*x + 0 * x - 1
y = an * xn + a(n-1) * x(n-1) + ... + a1 * x1 + a0 * x0
y = ((...(((an)*x + a(n-1))*x + a(n-2))*x + ...)*x+a1)*x+a0
Ðåøåíèå çàäà÷è 3:
#include <stdio.h>
int main(void) {
double res = 0;
double a;
double x;
FILE *IN;
IN = fopen("tmp.dat", "r");
if (IN == NULL) {
printf("File not opened\n");
return -1;
}
printf("Input x\n");
scanf("%lf", &x);
while (fscanf(IN, "%lf", &a) == 1) {
res = res*x + a;
}
fclose(IN);
printf("Res: %lf\n", res);
return 0;
}
Âûïîëíåíèå ýòîé ïðîãðàììû:
Óïðàæíåíèå: íàéòè çíà÷åíèå ìíîãî÷ëåíà è åãî ïðîèçâîäíîé, åñëè ìíîãî÷ëåí çàäàí, êàê â çàäà÷å 2 (êîýôôèöèåíòàìè ïî óáûâàíèþ ñòåïåíåé)
Çàäà÷à 4. Ñ÷èòàòü öåëîå ÷èñëî ñ êëàâèàòóðû è íàéòè
÷èñëî ñ òåìè æå öèôðàìè, ÷òî è èñõîäíîãî, íî
èäóùèìè â îáðàòíîì ïîðÿäêå, è âûâåñòè åãî
Ïðèìåð 3. 1234 -> 4321
(((4)*10 + 3)*10 + 2)*10 + 1 == 4321 (êàæäàÿ öèôðà — êîýôôèöèåíò ïðè 10n)
Ïîäñêàçêà: íàéòè ïîñëåäíþþ öèôðó öåëîãî ÷èñëà ìîæíî ïðè ïîìîùè îïåðàöèè «%» - îñòàòîê îò äåëåíèÿ. Íàïðèìåð, 21%6 == 3
1234%10 → 4, 1234/10 ->123
123%10 → 3, 123/10 →12
12%10 → 2, 12/10 → 1
1%10 → 1, 1/10 → 0 - âûõîä
Ðåçóëüòàò - 4321
#include <stdio.h>
int main(void) {
int n, last_digit, res = 0;
printf("Input integer number\n");
scanf("%d", &n);
while(n > 0) {
last_digit = n % 10;
res = res * 10 + last_digit;
n /= 10;
}
printf("Res: %d\n", res);
return 0;
}
Ïðèâåäåíèå òèïîâ:
Íàïðèìåð, åñëè âñå îïåðàíäû â âûðàæåíèè öåëûå,
òî ðåçóëüòàò òîæå áóäåò öåëûé (íåÿâíîå ïðèâåäåíèå òèïîâ â ÿçûêå C).
while:
last_digit res n
1234
1. 4 4 123
2. 3 43 12
3. 2 432 1
4. 1 4321 0
while(n) - n==0 => óñëîâèå öèêëà íå âûïîëíåíî => âûõîä
èç öèêëà
Âûïîëíåíèå ýòîé ïðîãðàììû:
Ïðèìåð 4. Êàê â ÿçûêå C ïîäåëèòü öåëûå ÷èñëà, ÷òîáû ðåçóëüòàò áûë âåùåñòâåííûé?
Ðåçóëüòàò âûïîëíåíèÿ ïðîãðàììû ñ ÿâíûì ïðåîáðàçîâàíèåì òèïîâ:
Çàäà÷à 5.  ïîñëåäîâàòåëüíîñòè ÷èñåë, çàïèñàííûõ â ôàéëå
"tmp.dat", íàéòè êîëè÷åñòâî ëîêàëüíûõ ìàêñèìóìîâ
(çà îäíî ïðî÷òåíèå ôàéëà).
Îïðåäåëåíèå. Ëîêàëüíûé ìàêñèìóì â ïîñëåäîâàòåëüíîñòè
÷èñåë - ýòî ÷èñëî, êîòîðîå íå ìåíüøå ñâîèõ ñîñåäåé.
Ïðèìåð:
5
0
5
-1
2
5
5
Âîïðîñ: Ñêîëüêî ëîêàëüíûõ ìàêñèìóìîâ? Îòâåò: 4
#include <stdio.h>
int main(void) {
int res = 0;
int a, b, c;
FILE *IN;
IN = fopen("tmp.dat", "r");
if (IN == NULL) {
printf("File not opened\n");
return -1;
}
if (fscanf(IN, "%d", &a) != 1) {
printf("File empty\n");
fclose(IN);
return -1;
}
if (fscanf(IN, "%d", &b) == 1) {
if (a >= b)
++ res;
while (fscanf(IN, "%d", &c) == 1) {
if ((b >= a) && (b >= c))
++res;
a = b;
b = c;
}
if (b >= a)
++res;
} else {
res = 1;
}
fclose(IN);
printf("Res: %d\n", res);
return 0;
}
tmp.dat:
5
0
5
-1
2
5
5
Ïðîòîêîë âûïîëíåíèÿ ïðîãðàììû ïî øàãàì:
|
a |
b |
c |
res |
|
5 |
|
|
|
|
|
0 |
|
1 |
|
|
|
5 |
1 |
|
0 |
5 |
-1 |
2 |
|
5 |
-1 |
2 |
2 |
|
-1 |
2 |
5 |
2 |
|
2 |
5 |
5 |
3 |
Ïîñëå êîíöà öèêëà |
5 |
5 |
|
4 |
Ðåçóëüòàò âûïîëíåíèÿ ïðîãðàììû 5: