task4
#include <stdio.h> #include <stdlib.h> int main() { FILE *fp; char ch; int lines = 0, characters = 0; fp = fopen("d:\\xia01\\Documents\\data4qwe.txt", "r"); if (fp == NULL) { perror("Error opening file"); return EXIT_FAILURE; } while ((ch = fgetc(fp))!= EOF) { if (ch!= '\n'&&ch!='\t' &&ch!='\0') { characters++; } if (ch == '\n') { lines++; } } if (characters > 0) { lines++; } fclose(fp); printf("data4.txt统计结果:\n"); printf("行数: %d\n", lines); printf("字符数(不计空白符): %d\n", characters); return 0;}
task4
#include <stdio.h> #include <string.h> #define N 10 typedef struct { long id; // 准考证号 char name[20]; // 姓名 float objective; // 客观题得分 float subjective; // 操作题得分 float sum; // 总分 char result[10]; // 考试结果 } STU; // 函数声明 void read(STU st[], int n); void write(STU st[], int n); void output(STU st[], int n); int process(STU st[], int n, STU st_pass[]); int main() { STU stu[N], stu_pass[N]; int cnt; double pass_rate; printf("从文件读入%d个考生信息...\n", N); read(stu, N); printf("\n对考生成绩进行统计...\n"); cnt = process(stu, N, stu_pass); printf("\n通过考试的名单:\n"); output(stu, N); // 输出所有考生完整信息到屏幕 write(stu, N); // 输出考试通过的考生信息到文件 pass_rate = 1.0 * cnt / N; printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); return 0; } void output(STU st[], int n) { int i; printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); for (i = 0; i < n; i++) 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); } void read(STU st[], int n) { int i; FILE *fin; fin = fopen("d:\\xia01\\Documents\\examinee.txt", "r"); if (!fin) { printf("fail to open file\n"); return; } while (!feof(fin)) { for (i = 0; i < n; i++) fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); } fclose(fin); } void write(STU s[], int n) { FILE *fp; fp = fopen("d:\\新建文件夹\\shiyan7.txt", "w"); if (fp == NULL) { return ; } int i; for ( i = 0; i < n; i++) { if (s[i].sum >= 60) { fprintf(fp, "%ld %s %.2f %.2f %.2f %s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result); } } fclose(fp); } int process(STU st[], int n, STU st_pass[]) { int pass_count = 0; int i; for ( i = 0; i < n; i++) { st[i].sum = st[i].objective + st[i].subjective; if (st[i].sum >= 60) { strcpy(st[i].result, "通过"); st_pass[pass_count] = st[i]; pass_count++; } else { strcpy(st[i].result, "不通过"); } } return pass_count; }
task6
#include <stdio.h> #include <stdlib.h> #include <time.h> #define STUDENT_NUM 80 typedef struct { int id; char name[20]; char class1[20]; } Student; void readStudents(Student students[]) { FILE *fp; fp = fopen("d:\\xia01\\Documents\\list.txt", "r"); if (fp == NULL) { perror("Error opening file"); exit(1); } for (int i = 0; i < STUDENT_NUM; i++) { fscanf(fp, "%d %s %s", &students[i].id, students[i].name, students[i].class1); } fclose(fp); } void randomSelect(Student students[], Student selected[], int num) { int selected_index[STUDENT_NUM] = {0}; srand((unsigned int)time(NULL)); for (int i = 0; i < num; i++) { int index; do { index = rand() % STUDENT_NUM; } while (selected_index[index] == 1); selected_index[index] = 1; selected[i] = students[index]; } } void displayStudents(Student selected[], int num) { printf("------随机抽点名单------\n"); for (int i = 0; i < num; i++) { printf("%d %s %s\n", selected[i].id, selected[i].name, selected[i].class1); } } void writeStudents(Student selected[], int num, char *filename) { FILE *fp; fp = fopen("d:\\新建文件夹\\shiyan7x.txt", "w"); if (fp == NULL) { perror("Error opening file"); return ; } for (int i = 0; i < num; i++) { fprintf(fp, "%d %s %s\n", selected[i].id, selected[i].name, selected[i].class1); } fclose(fp); printf("保存成功!\n"); } int main() { Student students[STUDENT_NUM]; Student selected[5]; char filename[20]; readStudents(students); randomSelect(students, selected, 5); displayStudents(selected, 5); writeStudents(selected, 5, filename); return 0; }
标签:fp,int,selected,st,实验,printf,pass From: https://www.cnblogs.com/xia2005/p/18642153