首页 > 其他分享 >实验七

实验七

时间:2024-12-30 09:00:56浏览次数:1  
标签:fp %- int st STU 实验 printf

2.

//多了一行,且输出一个8。75-76行是判断文件指针是否已经指向文件结尾了,当number等于一时则已经结束,break。

3.

\'意义就是输出一个 ' 的符号。

4.

 

 1 #include<stdio.h>
 2 #define N 100
 3 
 4 int main(){
 5     char str[N];
 6     int i,j,line;
 7     FILE *fp;
 8     i=0;
 9     j=0,line=0;
10     fp = fopen("C:\\Users\\LEGION\\Desktop\\实验7数据文件及部分代码\\data4.txt","r");
11     if(!fp){
12         printf("fail to open file to read\n");
13         return 0;
14     }
15     while(!feof(fp))
16     {
17         fgets(str,N,fp);
18         line++;
19         
20         while(str[i]!='\0')
21         {
22             if(str[i]!=' '&&str[i]!='\n')
23             {
24                 j++;
25                 i++;
26                 
27             }
28         }
29     }
30     fclose(fp);
31 
32     printf("data的统计结果:\n行数:");
33     printf("-10.%f\n",line);
34     printf("字符数(不计空白符):-10.%f\n",j);
35     return 0;
36 }

5.

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

 6.

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #include<time.h>
 5 typedef struct student{
 6     long id;
 7     char name[20];
 8     char class[20];        
 9 }STU;
10 
11 
12 void read(STU st[])
13 {   
14     FILE *fp;
15     fp=fopen("C:\\Users\\LEGION\\Desktop\\实验7数据文件及部分代码\\list.txt","r");
16     if (!fp) {
17         printf("fail to open file\n");
18         return;
19     }
20     int i,number;
21     while (!feof(fp))
22     {
23         for (i = 0; i < 80; i++)
24         {
25            number=fscanf(fp, "%ld %s %s ", &st[i].id, st[i].name,st[i].class);
26            if(number!=3)
27                 break;
28         } 
29     }
30         fclose(fp);
31 }
32 void select(STU st[])
33  {
34     srand(time(NULL));
35     int i,temp,count=0;
36     int selected[5];
37     while(count<5)
38     {
39         int sign=1;
40         temp=rand()%80;
41         for(i=0;i<count;i++)
42         {
43         if(count==selected[i])
44             {
45                 sign=0;
46                 break;
47             }
48         }
49         if(sign!=0)
50         {
51             selected[i]=temp;
52             count++;
53         }
54     }    
55     char s[50];
56     printf("------------随机抽点名单------------\n");
57     for (i = 0; i < 5; i++) 
58     {
59         printf("%ld %-6s %s\n", st[selected[i]].id, st[selected[i]].name, st[selected[i]].class);
60     }
61     
62     printf("------------保存到文件------------\n输入文件名:");
63     scanf("%s", s);
64     char name[1000]="";
65     strcat(name,"D:\\");
66     strcat(name,s);
67     FILE *fout = fopen(name, "w");    
68     if (!fout)
69     {
70         printf("fail to open file \n");
71     }     
72     for (i = 0; i < 5; i++) 
73     {
74         fprintf(fout, "%ld %-6s %s\n", st[selected[i]].id, st[selected[i]].name, st[selected[i]].class);
75     }
76     fclose(fout);
77     printf("保存成功!\n");
78 }
79 
80 int main()
81 {
82     STU st[80];
83     read(st);
84     select(st);
85     return 0;    
86 }

 

 

标签:fp,%-,int,st,STU,实验,printf
From: https://www.cnblogs.com/Z20051022/p/18638637

相关文章

  • 实验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){......
  • 实验七
    task41#include<stdio.h>2intmain(){3intwords=0,lines=0;4charch;5FILE*fp;6fp=fopen("d:\\data4.txt","r");7if(!fp){8printf("failtoopenfiletoread\n");9......