1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 int main() 4 { 5 int num=0; 6 FILE* fp; 7 char ch; 8 if ((fp = fopen("data4.txt", "r"))==NULL) 9 printf("fail to open it"); 10 ch = fgetc(fp); 11 while (!feof(fp)) 12 { 13 if (ch != ' ' && ch!= '\n' ) 14 num++; 15 ch = fgetc(fp); 16 } 17 fclose(fp); 18 printf("%d", num); 19 }
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #define N 10 typedef struct { long int id; char name[20]; float objective; // 客观题得分 float subjective; // 操作题得分 float sum; char level[10]; } STU; // 函数声明 void input(STU s[], int n); void output(STU s[], int n); void process(STU s[], int n); int main() { STU stu[N]; printf("从文件读入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N); input(stu, N); printf("\n对考生信息进行处理: 计算总分,确定等级\n"); process(stu, N); printf("\n打印考生完整信息, 并保存到文件中"); output(stu, N); return 0; } // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 void input(STU s[], int n) { int i; FILE* fin; fin = fopen("examinee.txt", "r"); if (fin == NULL) { printf("fail to open file\n"); exit(0); } while (!feof(fin)) { for (i = 0; i < n; i++) fscanf(fin, "%ld %s %f %f", &s[i].id, s[i].name, &s[i].objective, &s[i].subjective); } fclose(fin); } // 输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级 // 不仅输出到屏幕上,还写到文本文件result.txt中 void output(STU s[], int n) { FILE* fout; int i; // 输出到屏幕 printf("\n"); 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", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].level); // 保存到文件 fout = fopen("result.txt", "w"); if (!fout) { printf("fail to open or create result.txt\n"); exit(0); } fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n"); for (i = 0; i < n; i++) fprintf(fout, "%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].level); fclose(fout); } // 对考生信息进行处理:计算总分,排序,确定等级 void process(STU s[], int n) { STU temp; int i, j, k; for (i = 0; i < N; i++) { s[i].sum = s[i].objective + s[i].subjective ; } for (i = 0; i < N - 1; i++) { for (j = 0; j < N - i - 1; j++) { if (s[j].sum < s[j + 1].sum) { temp = s[j + 1]; s[j + 1] = s[j]; s[j] = temp; } } } for (k = 0; k < N; k++) { switch (k) { case 0:strcpy(s[k].level, "优秀"); break; case 1: case 2: case 3: case 4:strcpy(s[k].level, "合格"); break; case 5: case 6: case 7: case 8: case 9:strcpy(s[k].level, "不合格"); break; } } }
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<time.h> 5 #include<string.h> 6 #define N 80 7 #define M 5 8 typedef struct 9 { 10 long no; 11 char name[10]; 12 char class[20]; 13 }STU; 14 int main() 15 { 16 STU list[N],lucky[M]; 17 FILE* fp1,*fp2; 18 int i,j,random; 19 if ((fp1 = fopen("list.txt", "r")) == NULL) 20 printf("fail to open it"); 21 for (i = 0; i < N; i++) 22 fscanf(fp1, "%ld %s %s", &list[i].no, list[i].name, list[i].class); 23 fclose(fp1); 24 srand((unsigned int)time(NULL)); 25 for (j = 0; j < M; j++) 26 { 27 random = rand() % N + 1; 28 lucky[j] = list[random]; 29 } 30 if ((fp2 = fopen("lucky.txt", "w")) == NULL) 31 printf("fail to open it"); 32 for (j = 0; j < M; j++) 33 fprintf(fp2, "%ld %s %s\n", lucky[j].no, lucky[j].name, lucky[j].class); 34 fclose(fp2); 35 }