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