8-1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
char ch;
FILE *i, *fp;
i = fopen("d:\\data\\sy8-1-out.txt", "w");
ch = getchar();
while (ch != '#') {
fputc(ch, i);
ch = getchar();
}
fclose(i);
i = fopen("d:\\data\\sy8-1-out.txt", "r");
fp = fopen("d:\\data\\sy8-1-out1.txt", "w");
while (1) {
ch = fgetc(i);
if (feof(i))
break;
if (ch <= 'z' && ch >= 'a') {
ch -= 32;
printf("%c", ch);
fputc(ch, fp);
}
}
fclose(fp);
fclose(i);
return 0;
}

8-2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdlib.h>

int main() {
char str[100];
int i = 0, n = 10, t, j, s = 0, arr[10];
float f;
FILE *fp1, *fp2;
fp1 = fopen("d:\\data\\sy8-2-in.txt", "r");
fp2 = fopen("d:\\data\\sy8-2-out.txt", "w");
for (i = 0; i < n; i++) {
fscanf(fp1, "%d", &arr[i]);
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}

}
for (i = 1; i < n - 1; i++) {
s += arr[i];
printf("%d ", arr[i]);
fprintf(fp2, "%d", arr[i]);

}
f = (float)s / 8;
fprintf(fp2, "\n%.2f", f);
printf("\n%.2f", f);
fclose(fp1);
fclose(fp2);
return 0;
}

8-3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

#include <stdio.h>
#include <stdlib.h>

struct student {
char num[11];
char name[11];
float c[3];
float j;
};

void input_data(struct student stu[], int n) {
int i = 0;
float s = 0;
for (; i < n; i++) {
s = 0;
scanf("%s%s%f%f%f", &stu[i].num, &stu[i].name, &stu[i].c[0], &stu[i].c[1], &stu[i].c[2]);
s += stu[i].c[0] + stu[i].c[1] + stu[i].c[2];
stu[i].j = s / 3;
}
}

void sort(struct student stu[], int n) {
struct student t;
int i, j;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (stu[i].j > stu[j].j) {
t = stu[j];
stu[j] = stu[i];
stu[i] = t;
}
}
}
}

void save(struct student stu[], int n, char filename[]) {
int i = 0;
FILE *fp;
fp = fopen(filename, "wb");
for (i = 0; i < n; i++)
fwrite(&stu[i], sizeof(struct student), 1, fp);
fclose(fp);
}

int read(struct student stu[], char filename[]) {
int n = 0, i = 0;
FILE *fp = fopen(filename, "rb");
while (fread(&stu[n], sizeof(struct student), 1, fp) == 1) {
n++;
}
fclose(fp);
return n ;
}

int insert(struct student stu[], int n, struct student x) {
int i = n - 1;
x.j = (x.c[0] + x.c[1] + x.c[2]) / 3;

while (i >= 0 && x.j < stu[i].j) {
stu[i + 1] = stu[i];
i--;
}

stu[i + 1] = x;
return n + 1;
}

void print_data(struct student stu[], int n) {
int i = 0;
for (; i < n; i++) {
printf("%s %s %.2f %.2f %.2f %.2f\n", stu[i].num, stu[i].name, stu[i].c[0], stu[i].c[1], stu[i].c[2], stu[i].j);
}

}

int main() {
int n;
scanf("%d", &n);
struct student stu[100];
input_data(stu, n);
sort(stu, n);
save(stu, n, "d:\\data\\sy8-3-in.txt");
print_data(stu, n);
struct student x;
scanf("%s%s%f%f%f", &x.num, &x.name, &x.c[0], &x.c[1], &x.c[2]);
n = insert(stu, n, x);
save(stu, n, "d:\\data\\sy8-3-out.txt");
n = read(stu, "d:\\data\\sy8-3-out.txt");
print_data(stu, n);
printf("%d", n);
return 0;
}

8-4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student {
char num[11];
char name[11];
float c[3];
float j;
};

struct node {
char num[11];
char name[11];
float c[3];
float j;
struct node *next;
};

struct node *create(char filename[]) {
int i = 0, n = 3;
struct node *h, *p, *q;
h = (struct node *)malloc(sizeof(struct node));
h->next = NULL;
struct student t;
FILE *fp = fopen(filename, "rb");
for (; fread(&t, sizeof(struct student), 1, fp) == 1;) {
p = (struct node *)malloc(sizeof(struct node));
strcpy(p->num, t.num);
strcpy(p->name, t.name);
for (i = 0; i < n; i++)
p->c[i] = t.c[i];
p->j = t.j;
p->next = h->next;
h->next = p;
}
return h;
}

void print(struct node *h) {
struct node *p;
p = h->next;
while (p != NULL) {
printf("%s %s %f %f %f %f\n", p->num, p->name, p->c[0], p->c[1], p->c[2], p->j);
p = p->next;
}

}

int main() {
struct node *h;
h = create("d:\\data\\sy8-3-out.txt");
print(h);
return 0;
}