1 #include <stdio.h> 2 int main() 3 { 4 FILE* fp; 5 int count=0; 6 char ch; 7 fp=fopen("date4.txt","r"); 8 if(fp==NULL) 9 { 10 printf("fail to open the file"); 11 return 1; 12 } 13 while((ch=fgetc(fp))!=EOF) 14 { 15 if(ch!=' '&&ch!='\n'&&ch!='\t') 16 count++; 17 } 18 fclose(fp); 19 printf("date4.txt中共包含字符数(不计空白符):%d", count); 20 return 0; 21 }
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <string.h> 4 #include <stdlib.h> 5 #define N 10 6 typedef struct { 7 long int id; 8 char name[20]; 9 float objective; // 客观题得分 10 float subjective; // 操作题得分 11 float sum; 12 char level[10]; 13 } STU; 14 // 函数声明 15 void input(STU s[], int n); 16 void output(STU s[], int n); 17 void process(STU s[], int n); 18 int main() { 19 STU stu[N]; 20 printf("从文件读入 % d个考生信息: 准考证号,姓名,客观题得分(<= 40),操作题得分(<= 60)\n", N); 21 input(stu, N); 22 23 printf("\n对考生信息进行处理: 计算总分,确定等级\n"); 24 process(stu, N); 25 26 printf("\n打印考生完整信息, 并保存到文件中"); 27 output(stu, N); 28 return 0; 29 } 30 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 31 void input(STU s[], int n) { 32 int i; 33 FILE* fin; 34 fin = fopen("examinee.txt", "r"); 35 if (fin == NULL) { 36 printf("fail to open file\n"); 37 exit(0); 38 } 39 while (!feof(fin)) { 40 for (i = 0; i < n; i++) 41 fscanf(fin, "%ld %s %f %f", &s[i].id, s[i].name, 42 &s[i].objective, &s[i].subjective); 43 } 44 fclose(fin); 45 } 46 // 输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级 47 // 不仅输出到屏幕上,还写到文本文件result.txt中 48 void output(STU s[], int n) { 49 FILE* fout; 50 int i; 51 // 输出到屏幕 52 printf("\n"); 53 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n"); 54 for (i = 0; i < n; i++) 55 printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, 56 s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].level); 57 // 保存到文件 58 fout = fopen("result.txt", "w"); 59 if (!fout) { 60 printf("fail to open or create result.txt\n"); 61 exit(0); 62 } 63 fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n"); 64 65 for (i = 0; i < n; i++) 66 fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, 67 s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].level); 68 fclose(fout); 69 } 70 // 对考生信息进行处理:计算总分,排序,确定等级 71 void process(STU s[], int n) { 72 int i, j; 73 STU t; 74 for (i = 0; i < n; i++) 75 s[i].sum = s[i].objective + s[i].subjective; 76 77 for (i = 0; i < n - 1; i++) 78 for (j = 0; j < n - 1 - i; j++) 79 if (s[j].sum < s[j + 1].sum) 80 { 81 t = s[j]; 82 s[j] = s[j + 1]; 83 s[j + 1] = t; 84 } 85 86 for (i = 0; i < 0.1 * n; i++) 87 strcpy(s[i].level, "优秀"); 88 for (; i < 0.5 * n; i++) 89 strcpy(s[i].level, "合格"); 90 for (; i < n; i++) 91 strcpy(s[i].level, "不合格"); 92 93 }
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <string.h> 4 #include <stdlib.h> 5 #include<time.h> 6 #define N 80 7 #define M 5 8 typedef struct { 9 long int id; 10 char name[20]; 11 char class[30]; 12 } STU; 13 // 函数声明 14 void input(STU s[], int n); 15 void output(STU t[], int m); 16 int main() { 17 STU stu[N]; 18 input( stu, N); 19 int number, i, j = 0; 20 STU lck[M]; 21 srand(time(0)); 22 for (i = 0; i < M; i++) { 23 number = rand() % 80; 24 lck[j] = stu[number]; 25 j++; 26 } 27 output(lck, M); 28 return 0; 29 } 30 31 void input(STU s[], int n) { 32 FILE* fp; 33 int i, j; 34 fp = fopen("list.txt", "r"); 35 if (fp == NULL) 36 { 37 printf("fail to open file\n"); 38 exit(0); 39 } 40 for (i = 0; i < n; i++) 41 fscanf(fp, "%ld %s %s", &s[i].id, s[i].name, s[i].class); 42 fclose(fp); 43 } 44 45 void output(STU t[], int m) 46 { 47 int i, j = 0; 48 for (i = 0; i < m; i++) 49 printf("%ld\t%s\t%s\n", t[i].id, t[i].name, t[i].class); 50 FILE* fout; 51 fout = fopen("lucky.txt", "w"); 52 if (fout == NULL) 53 { 54 printf("fail to open or create result.txt\n"); 55 exit(0); 56 } 57 for (i = 0; i < m; i++) 58 fprintf(fout,"%ld\t\t%s\t%s\n", t[i].id, t[i].name, t[i].class); 59 fclose(fout); 60 61 }
标签:fp,int,void,++,STU,实验,include From: https://www.cnblogs.com/wr236/p/17010352.html