首页 > 其他分享 >实验7

实验7

时间:2024-12-30 09:01:16浏览次数:1  
标签:printf int time st STU 实验 pass

实验7

task.4

源代码:

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #define N 10000
 4 int main() {
 5     char ch[N];
 6     int line = 1;
 7     int n = 0;
 8     int j = 0;
 9     FILE* fp;
10     fp = fopen("data4.txt", "r");
11     if (!fp) {
12         printf("fail to open the file to read\n");
13         return 0;
14     }
15     int i = 0;
16     while (!feof(fp)) {
17         ch[i] = fgetc(fp);
18         if (ch[i] == '\n') {
19             line++;
20         }
21         if (ch[i] == EOF) {
22             break;
23         }
24         if (ch[i] == '\n' || ch[i] == ' ' || ch[i] == '    ') {
25             j++;
26         }
27         i++;
28     }
29     n = i - j;
30     fclose(fp);
31     printf("data4.txt统计结果:\n");
32     printf("%-20s%-20d\n", "行数", line);
33     printf("%-20s%-20d", "字符数(不计空白符)", n);
34     return 0;
35 }

输出结果:

 task.5

源代码:

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include <stdio.h>
  3 #include <string.h>
  4 
  5 #define N 10
  6 
  7 typedef struct {
  8     long id;            // 准考证号
  9     char name[20];      // 姓名
 10     float objective;    // 客观题得分
 11     float subjective;   // 操作题得分
 12     float sum;          // 总分
 13     char result[10];    // 考试结果
 14 } STU;
 15 
 16 // 函数声明
 17 void read(STU st[], int n);
 18 void write(STU st[], int n);
 19 void output(STU st[], int n);
 20 int process(STU st[], int n, STU st_pass[]);
 21 
 22 int main() {
 23     STU stu[N], stu_pass[N];
 24     int cnt;
 25     double pass_rate;
 26 
 27     printf("从文件读入%d个考生信息...\n", N);
 28     read(stu, N);
 29 
 30     printf("\n对考生成绩进行统计...\n");
 31     cnt = process(stu, N, stu_pass);
 32 
 33     printf("\n通过考试的名单:\n");
 34     output(stu, N);   // 输出所有考生完整信息到屏幕
 35     write(stu, N);    // 输出考试通过的考生信息到文件
 36 
 37     pass_rate = 1.0 * cnt / N;
 38     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100);
 39 
 40     return 0;
 41 }
 42 
 43 // 把所有考生完整信息输出到屏幕上
 44 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 45 void output(STU st[], int n) {
 46     int i;
 47 
 48     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 49     for (i = 0; i < n; i++)
 50         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);
 51 }
 52 
 53 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 54 void read(STU st[], int n) {
 55     int i;
 56     FILE* fin;
 57 
 58     fin = fopen("examinee.txt", "r");
 59     if (!fin) {
 60         printf("fail to open file\n");
 61         return;
 62     }
 63 
 64     while (!feof(fin)) {
 65         for (i = 0; i < n; i++)
 66             fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
 67     }
 68 
 69     fclose(fin);
 70 }
 71 
 72 // 把通过考试的考生完整信息写入文件list_pass.txt
 73 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 74 void write(STU s[], int n) {
 75     FILE* fout;
 76     fout = fopen("list_pass.txt", "w");
 77     if (!fout) {
 78         printf("fail to open the file to write\n");
 79         return;
 80     }
 81     fprintf(fout, "%-20s %-20s %-20s %-20s %-20s %-20s\n", "准考证号", "姓名", "客观题得分", "操作题得分", "总分", "结果");
 82     for (int i = 0; i < n; i++) {
 83         if (s[i].sum >= 60) {
 84             fprintf(fout, "%-20ld %-20s %-20.2f %-20.2f %-20.2f %-20s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result);
 85         }
 86     }
 87     fclose(fout);
 88 }
 89 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 90 int process(STU st[], int n, STU st_pass[]) {
 91     int cnt = 0;
 92     for (int 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[cnt] = st[i];
 97             cnt++;
 98         }
 99         else {
100             strcpy(st[i].result, "不通过");
101         }
102     }
103     return cnt;
104 }

输出结果:

 

 task.6

源代码:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 time_t current_time;
 5 struct tm* local_time;
 6 typedef struct STU
 7 {
 8     long long int id;
 9     char* name;
10     char* class;
11 }STU;
12 void read(STU st[], int n) {
13     int i;
14     FILE* fin;
15 
16     fin = fopen("E:\\Downloads\\somecode\\somecode\\list.txt", "r");
17     if (!fin) {
18         printf("fail to open file\n");
19         return;
20     }
21 
22     while (!feof(fin)) {
23         for (i = 0; i < n; i++)
24         {
25             st[i].name = (char*)malloc(20 * sizeof(char));
26             st[i].class = (char*)malloc(20 * sizeof(char));
27             fscanf(fin, "%ld %s %s", &st[i].id, st[i].name, st[i].class);
28         }
29     }
30 
31     fclose(fin);
32 }
33 void write(STU s[], int n, char* pass) {
34     int i;
35     FILE* fout;
36 
37     fout = fopen(pass, "w");
38     if (!fout) {
39         printf("fail to open file\n");
40         return;
41     }
42     for (i = 0; i < n; i++)
43         fprintf(fout, "%ld\t%s\t%s\n", s[i].id, s[i].name, s[i].class);
44     fclose(fout);
45 }
46 int main()
47 {
48     STU st[81];
49     read(st, 81);
50     char* pass;
51     pass = (char*)malloc(100 * sizeof(char));
52     current_time = time(NULL);
53     local_time = localtime(&current_time);
54     srand((unsigned int)time(NULL));
55     printf("--------%d-%d-%d %d:%d:%d点名-----------\n", local_time->tm_year + 1900, local_time->tm_mon + 1, local_time->tm_mday, local_time->tm_hour, local_time->tm_min, local_time->tm_sec);
56     sprintf(pass, "E:\\Downloads\\somecode\\somecode\\%d-%d-%d.txt", local_time->tm_year + 1900, local_time->tm_mon + 1, local_time->tm_mday);
57     STU strandon[5];
58     for (int i = 0; i < 5; i++)
59     {
60         int rand_num = rand() % 81;
61         strandon[i] = st[rand_num];
62         printf("%ld\t%s\t%s\n", strandon[i].id, strandon[i].name, strandon[i].class);
63     }
64     write(strandon, 5, pass);
65     for (int i = 0; i < 81; i++)
66     {
67         free(st[i].name);
68         free(st[i].class);
69     }
70     free(pass);
71 }

输出结果:

 

 

标签:printf,int,time,st,STU,实验,pass
From: https://www.cnblogs.com/wcy-0464/p/18639966

相关文章

  • 实验七
    2.//多了一行,且输出一个8。75-76行是判断文件指针是否已经指向文件结尾了,当number等于一时则已经结束,break。3.\'意义就是输出一个 ' 的符号。4. 1#include<stdio.h>2#defineN10034intmain(){5charstr[N];6inti,j,line;7FILE*fp;......
  • 实验7
    任务4#include<stdio.h>#include<stdlib.h>intmain(){FILE*fp;charfilename[]="data4.txt";charch;intlines=0;intcharacters=0;fp=fopen(filename,"r");if(fp==NULL){......
  • 实验7 文件应用编程
    4.实验任务4:文件简单应用#include<stdio.h>#include<stdlib.h>voidcountLinesAndChars(constchar*fileName){FILE*fp;intlines=0,chars=0;intch;intinWord=0;fp=fopen(fileName,"r");if(fp==NULL)......
  • 实验7
    任务4代码1#include<stdio.h>234intmain(){5inthang=1,zi=0;6charch;7FILE*fp;8fp=fopen("data4.txt","r");9if(fp==NULL){10printf("failtoopenfiletoread\n");11......
  • 实验7
    task1.c1#include<stdio.h>23#defineN804#defineM10056typedefstruct{7charname[N];8charauthor[N];9}Book;1011voidwrite();12voidread();1314intmain(){15printf("测试1:把图书信息写入文本文件......
  • 实验7
    实验4:1#include<stdio.h>2#include<string.h>34intmain(){5FILE*fp;6intsum=0;7charlines[100][1000];8inti=0,n;910fp=fopen("C:\\Users\\legion\\Desktop\\实验7数据文件及部分代码\\dat......
  • 实验7
    实验4:源码:#include<stdio.h>#include<stdlib.h>#defineN3#defineM100voidwrite();intfun1();intfun2();intmain(){write();printf("data4.txt统计结果:\n");printf("行数:%-20d\n",fun1());printf("字......
  • 实验7
    任务4:#include<stdio.h>#defineN100intmain(){inti;FILE*fp;intlines=0;intchars=0;fp=fopen("d:\\data4.txt","r");if(!fp){printf("failtoopenfiletoread\n");......
  • 实验7
    实验任务4#include<stdio.h>intmain(){intx=0,y=1;charc;FILE*fp;fp=fopen("data4.txt","r");if(fp==NULL){printf("failtoopenfiletoread\n");return1;}while(!feof(fp)){......
  • 实验7
    #include<stdio.h>#include<ctype.h>voidfun(int*line_Count,int*char_Count){FILE*fp;fp=fopen("C:\\Users\\Ciallo~\\Downloads\\实验7数据文件及部分代码\\  data4.txt","r");if(fp==NULL){......