Ëåêöèÿ 10


Çàäà÷à 1. Íàéòè ïëîùàäü è ïåðèìåòð ïåðåñå÷åíèÿ äâóõ ïðÿìîóãîëüíèêîâ, ñòîðîíû êîòîðûõ ïàðàëëåëüíû îñÿì êîîðäèíàò


Ïåðåñå÷åíèå — ìíîæåñòâî âñåõ òî÷åê, êîòîðûå ïðèíàäëåæàò îáîèì ïðÿìîóãîëüíèêàì

Òî÷êà x ïðèíàäëåæèò ïðÿìîóãîëüíèêó (x0, y0)*(x1,y1), åñëè x0<=x<=x1 è y0<=y<=y1.

Òî÷êà x ïðèíàäëåæèò ïåðåñå÷åíèþ ïðÿìîóãîëüíèêîâ (x0, y0)*(x1,y1) è (x2, y2)*(x3,y3), åñëè x0<=x<=x1, x2<=x<=x3, y0<=y<=y1 è y2<=y<=y3.


1) x >= x0 è x >= x2 <=> x >= max(x0,x2)

2) x <= x1 è x <= x3 <=> x <= min(x1,x3)

.



(max(x0,x2), max(y0,y2))*(min(x1,x3), min(y1,y3))







Åñëè êîîðäèíàòû âåðøèí ïðÿìîóãîëüíèêîâ çàäàþòñÿ â íåïðàâèëüíîì ïîðÿäêå, òî ýòî ìîæíî èñïðàâèòü â ïðîãðàììå:





Çàäà÷à 2.  ôàéëàõ ina.txt è inb.txt çàäàíû äâà ìàññèâà âåùåñòâåííûõ ÷èñåë.

Ìàññèâ A “ìåíüøå» ìàññèâà B, åñëè â îòñîðòèðîâàííûõ ïî âîçðàñòàíèþ ìàññèâàõ A è B âñå A[i]<B[i].

 ñëó÷àå, åñëè îäèí ìàññèâ êîðî÷å äðóãîãî, òî îí äîïîëíÿåòñÿ ïîñëåäíèì ÷èñëîì îòñîðòèðîâàííîãî ìàññèâà (òî åñòü ìàêñèìàëüíûì, òàê êàê â îòñîðòèðîâàííîì ìàññèâå ìàêñèìàëüíûé — ýòî ïîñëåäíèé).

Îòâåò âûâåñòè â ôàéë output.txt â âèäå ñëîâ YES èëè NO.

Ïðîãðàììà âîçâðàùàåò -1, åñëè îäèí èç ôàéëîâ îòñóòñòâóåò èëè ïóñò.

Îáðàáîòêà ìàññèâîâ — âûäåëÿåòñÿ â ôóíêöèè.


#include <stdio.h>

#include <stdlib.h>


int size(char *name);

void sort(double *array, int size);

int lt(double *ar1, int size1, double *ar2, int size2);


int main() {

FILE *INA = NULL, *INB = NULL, *OUT = NULL;

double *A, *B;

int sizeA, sizeB, i;

sizeA = size("ina.txt");

if (sizeA == -1) {

return -1;

}

sizeB = size("inb.txt");

if (sizeB == -1) {

return -1;

}

if ((sizeA <= 0) || (sizeB <= 0)) {

printf("File is empty\n");

return -1;

}

A = (double*)malloc(sizeA * sizeof(double));

if (A == NULL) {

printf("Memory not allocated\n");

return -1;

}

B = (double*)malloc(sizeB * sizeof(double));

if (B == NULL) {

free(A);

printf("Memory not allocated\n");

return -1;

}

INA = fopen("ina.txt", "r");

if (INA == NULL) {

free(A);

free(B);

printf("File ina.txt not opened\n");

return -1;

}

for (i = 0; i < sizeA; i++) {

if (fscanf(INA, "%lf", &A[i]) != 1) {

fclose(INA);

free(A);

free(B);

printf("Not enough numbers\n");

return -1;

}

}

fclose(INA);

INB = fopen("inb.txt", "r");

if (INB == NULL) {

free(A);

free(B);

printf("File inb.txt not opened\n");

return -1;

}

for (i = 0; i < sizeB; i++) {

if (fscanf(INB, "%lf", &B[i]) != 1) {

fclose(INB);

free(A);

free(B);

printf("Not enough numbers\n");

return -1;

}

}

fclose(INB);

sort(A, sizeA);

sort(B, sizeB);


OUT = fopen("output.txt", "w");

if (OUT == NULL) {

free(A);

free(B);

printf("File output.txt not opened\n");

return -1;

}

if (lt(A, sizeA, B, sizeB)) {

fprintf(OUT, "YES");

} else {

fprintf(OUT, "NO");

}

fclose(OUT);

free(A);

free(B);

return 0;

}


int size(char *name) {

double x;

FILE *IN = NULL;

int res = 0;

IN = fopen(name, "r");

if (IN == NULL) {

printf("File %s not opened\n", name);

return -1;

}

while (fscanf(IN, "%lf", &x) == 1) {

++res;

}

fclose(IN);

return res;

}


void sort(double *ar, int size) {

int i, j;

double buf;

for (i = 0; i < size; i++) {

for (j = 0; j < size - 1; j++) {

if(ar[j] > ar[j+1]) {

buf = ar[j];

ar[j] = ar[j+1];

ar[j+1] = buf;

}

}

}

}


#define MAX(x,y) (((x) >= (y)) ? (x) : (y))


int lt(double *A, int sizeA, double *B, int sizeB) {

double a, b;

int i;

int len;

/* len = sizeA;

if (sizeB > len)

len = sizeB; */

len = MAX(sizeA,sizeB)

for (i = 0; i < len; i++) {

if (i < sizeA)

a = A[i];

else

a = A[sizeA — 1];

if (i < sizeB)

b = B[i];

else

b = B[sizeB — 1];

if (a >= b)

return 0;

}


return 1;

}



EOF è feof()









Ïðîãðàììà çàöèêëèâàåòñÿ, ïîòîìó ÷òî fscanf() íå ïðî÷èòàë ÷èñëî, íî è íå äîøåë äî êîíöà ôàéëà. Ïîýòîìó îí íå âîçâðàùàåò -1 (êîíñòàíòà EOF).


Ïðàâèëüíûé êîä:

#include <stdio.h>

int main(void) {

FILE *IN;

int n, res;

IN = fopen("EOF_1.dat", "r");

if (IN == NULL) {

printf("File not opened\n");

return -1;

}

while ((res = fscanf(IN, "%d", &n)) == 1) {

printf("res: %d, n: %d\n", res, n);

}

return 0;

}


feof():








Ïðîãðàììà çàöèêëèëàñü, ïîòîìó ÷òî feof() ñ÷èòàåò, ÷òî åùå íå êîíåö ôàéëà (ñèìâîë ñ êîäîì 0x1A).


Ïðàâèëüíûé êîä:


#include <stdio.h>

int main(void) {

FILE *IN;

int n, res;

IN = fopen("feof.dat", "r");

if (IN == NULL) {

printf("File not opened\n");

return -1;

}

while (fscanf(IN, "%d", &n) == 1) {

printf("res: %d, n: %d\n", res, n);

}

return 0;

}