实验4:
1 #include <stdio.h> 2 #include <string.h> 3 4 int main() { 5 FILE *fp; 6 int sum = 0; 7 char lines[100][1000]; 8 int i = 0, n; 9 10 fp = fopen("C:\\Users\\legion\\Desktop\\实验7数据文件及部分代码\\data4.txt", "r"); 11 if (fp == NULL) { 12 printf("fail to open file to read\n"); 13 return 0; 14 } 15 16 while (fgets(lines[i], sizeof(lines[i]), fp)!= NULL) 17 i++; 18 19 n = i; 20 21 for (int j = 0; j < n; j++) { 22 int w = strlen(lines[j]); 23 for (int m = 0; m < w; m++) { 24 if (lines[j][m] ==' '||lines[j][m]=='\t'||lines[j][m]=='\n') 25 w--; 26 27 } 28 sum += w; 29 } 30 printf("data4.txt统计结果:\n"); 31 printf("行数: %d\n", n); 32 printf("字符数(不计空白符):%d\n", sum); 33 34 fclose(fp); 35 36 return 0; 37 }
实验5:
1 #include <stdio.h> 2 #include <string.h> 3 4 #define N 10 5 6 typedef struct { 7 long id; // 准考证号 8 char name[20]; // 姓名 9 float objective; // 客观题得分 10 float subjective; // 操作题得分 11 float sum; // 总分 12 char result[10]; // 考试结果 13 } STU; 14 15 // 函数声明 16 void read(STU st[], int n); 17 void write(STU st[], int n); 18 void output(STU st[], int n); 19 int process(STU st[], int n, STU st_pass[]); 20 21 int main() { 22 STU stu[N], stu_pass[N]; 23 int cnt; 24 double pass_rate; 25 26 printf("从文件读入%d个考生信息...\n", N); 27 read(stu, N); 28 29 printf("\n对考生成绩进行统计...\n"); 30 cnt = process(stu, N, stu_pass); 31 32 printf("\n通过考试的名单:\n"); 33 output(stu, N); // 输出所有考生完整信息到屏幕 34 write(stu, N); // 输出考试通过的考生信息到文件 35 36 pass_rate = 1.0 * cnt / N; 37 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); 38 39 return 0; 40 } 41 42 // 把所有考生完整信息输出到屏幕上 43 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 44 void output(STU st[], int n) { 45 int i; 46 47 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 48 for (i = 0; i < n; i++) 49 printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); 50 } 51 52 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 53 void read(STU st[], int n) { 54 int i; 55 FILE *fin; 56 57 fin = fopen("C:\\Users\\legion\\Desktop\\实验7数据文件及部分代码\\examinee.txt", "r"); 58 if (!fin) { 59 printf("fail to open file\n"); 60 return; 61 } 62 63 while (!feof(fin)) { 64 for (i = 0; i < n; i++) 65 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 66 } 67 68 fclose(fin); 69 } 70 71 // 把通过考试的考生完整信息写入文件list_pass.txt 72 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 73 void write(STU s[], int n) { 74 int i; 75 FILE *fp; 76 77 fp = fopen("C:\\Users\\legion\\Desktop\\实验7数据文件及部分代码\\list_pass.txt","w"); 78 if(fp == NULL){ 79 printf("fail to open file to write\n"); 80 return ; 81 } 82 for(i=0;i<n;i++){ 83 fprintf(fp,"%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n",s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result); 84 } 85 fclose(fp); 86 } 87 88 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 89 int process(STU st[], int n, STU st_pass[]) { 90 int i; 91 int j=0; 92 for(i=0;i<n;i++){ 93 st[i].sum=st[i].objective+st[i].subjective; 94 if(st[i].sum>=60){ 95 strcpy(st[i].result,"通过"); 96 st_pass[j++]=st[i]; 97 } 98 else 99 strcpy(st[i].result,"不通过"); 100 } 101 return j; 102 }
实验6:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 #define STUDENT_NUM 80 6 7 typedef struct { 8 int id; 9 char name[20]; 10 char class[20]; 11 } Student; 12 13 void swap(Student *a, Student *b) { 14 Student temp = *a; 15 *a = *b; 16 *b = temp; 17 } 18 19 void random(Student students[], int n, Student selected[], int m) { 20 srand((unsigned int)time(NULL)); 21 int i, j; 22 for (i = 0; i < n; i++) { 23 j = rand() % (n - i) + i; 24 swap(&students[i], &students[j]); 25 } 26 for (i = 0; i < m; i++) 27 selected[i] = students[i]; 28 29 } 30 31 void output(Student students[], int n) { 32 int i; 33 for (i = 0; i < n; i++) 34 printf("%d %s %s\n", students[i].id, students[i].name, students[i].class); 35 } 36 37 38 void write(Student students[], int n, char *filename) { 39 FILE *fp; 40 int i; 41 fp = fopen(filename, "w"); 42 if (fp == NULL) { 43 printf("无法打开文件\n"); 44 return; 45 } 46 for (i = 0; i < n; i++) 47 fprintf(fp, "%d %s %s\n", students[i].id, students[i].name, students[i].class); 48 printf("保存成功!\n"); 49 fclose(fp); 50 } 51 52 int main() { 53 Student students[STUDENT_NUM]; 54 Student selected[5]; 55 char filename[100]; 56 int i; 57 58 for (i = 0; i < STUDENT_NUM; i++) { 59 students[i].id = 204942000 + i; 60 sprintf(students[i].name, "学生%d", i + 1); 61 sprintf(students[i].class, "星际联盟2049(1)班"); 62 } 63 random(students, STUDENT_NUM, selected, 5); 64 printf("---------随机抽取名单-----------:\n"); 65 output(selected, 5); 66 printf("---------保存到文件-----------:\n"); 67 printf("请输入文件名:"); 68 scanf("%s", filename); 69 write(selected, 5, filename); 70 return 0; 71 }
标签:fp,int,++,st,students,实验,printf From: https://www.cnblogs.com/88888888Z/p/18639802