14
1. .
:
if ((x > (a-b)) && !((a < c) || (c < d))) x = y;
0 ( 1 ( 2 ( 3 ) 2 ) 1 ( 2 ( 3 ) 2 ( 3 ) 2 ) 1 ) 0
if ((x > (a-b) && !((a < c) || (c < d)))
0 ( 1 ( 2 ( 3 ) 2 ( 2 ( 4 ) 3 ( 4) 3 ) 2) 1
if ((x > (a-b))) && !((a < c) || ((c < d)))
0 ( 1 ( 2 ( 3 ) 2 ) 1 ) 0 ( 1 ( 2 ) 1 ( 2 ( 3 ) 2 ) 1 ) 0
1.
(()) -
2. ())( -
++i
i++
. 0 ( 1 , , )
int balanced(char *str) {
int count = 0;
while (*str) {
switch(*(str++)) {
case '(':
++count;
break;
case ')':
if (count <= 0) /* , ! */
return 0;
--count;
break;
default:
break;
}
}
return count == 0;
}
. — , ’)’
1:
#include <stdio.h>
int balanced(char *str);
int main(void) {
char str[1000];
while (1) {
printf("Input expression:\n");
gets(str);
if (!*str)
break;
printf("%s\n", balanced(str) ? "Balanced" : "Non-balanced");
}
return 0;
}
int balanced(char *str) {
int count = 0;
char c;
while (c = *(str++)) {
switch(c) {
case '(':
++count;
break;
case ')':
if (count <= 0)
return 0;
--count;
break;
default:
break;
}
}
return count == 0;
}
2. . tmp.dat. — .
0
( 1 ( 2 ( 3 )
2 ( 3 ) 2 )
1 ) 0
2 1: — , — . .
struct ():
struct point {
double x;
int type; /* 1 – , -1 — */
};
double x;
int count = 0;
struct point p = { x, -1 };
x = p.x;
count += p.type;
2:
#include <stdio.h>
struct point {
double x;
int type;
};
void sort(struct point *ar, int n);
int main(void) {
FILE *IN;
int i, N, count = 0, maxCount = 0;
struct *point array = 0;
IN = fopen("input.txt", "r");
if (IN == NULL) {
printf("File not opened\n");
return -1;
}
if (fscanf(IN, "%d", &N) != 1) {
printf("File empty\n");
fclose(IN);
return -1;
}
if (N <= 0) {
printf(“Wrong number of points\n”);
fclose(IN);
return -1;
}
/* array «struct point”
0- , 2 : */
/* array = (struct point*)malloc(2 * N * sizeof(struct point)); */
array = (struct point*)malloc(2 * N * sizeof(array[0]));
if (array == NULL) {
printf("Memory not allocated\n");
fclose(IN);
return -1;
}
for (i = 0; i < N; i++) {
if (fscanf(IN, "%lf%lf", &array[i * 2].x, &array[i * 2 + 1].x) != 2) {
printf("Not enough numbers\n");
free(array);
fclose(IN);
return -1;
}
array[i * 2].type = 1; /* */
array[i * 2 + 1].type = -1; /* */
}
fclose(IN);
sort(array, 2 * N);
for (i = 0; i < 2 * N; i++) {
count += array[i].type;
if (count > maxCount) {
maxCount = count;
}
}
free(array);
printf("Max intersected: %d\n", maxCount);
return 0;
}
void sort(struct point *ar, int n) {
int i, j;
struct point buf;
for (i = 0; i < n; i++) {
for (j = 0; j < n - 1; j++) {
if ((ar[j].x > ar[j+1].x) ||
((ar[j].x == ar[j+1].x) &&
(ar[j].type == -1) && (ar[j+1].type == 1))) {
buf = ar[j];
ar[j] = ar[j+1];
ar[j+1] = buf;
}
}
}
}
3. , 2 [0,1].
1. , , , 0 . 0, , 1.
2. 0 1, , :