首页 > 其他分享 >实验七

实验七

时间:2023-06-08 21:55:57浏览次数:24  
标签:10 20 int char STU 实验 include

1.实验任务4

 1 #include<stdio.h>
 2 int main()
 3 {
 4     char s[100];
 5     FILE *fp;
 6     int i = 0,j,k = 0;
 7     char ch;
 8     fp = fopen("data4.txt","rb");
 9     
10     if(fp == NULL)
11     {
12         printf("fail to open file\n");
13         return 1;
14     }
15     ch = fgetc(fp);
16     while(ch!=EOF)
17     {
18         s[i++] = ch;
19         ch = fgetc(fp);
20 
21         
22     }
23     
24     for(j = 0;j<i-1;j++)
25         if(s[j]!=' ')
26             k++;
27             
28     fclose(fp);
29     printf("data4.txt中共包含字符数(不计空白符):%d",k-1);
30     return 0;
31 }

 

2.实验任务5

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 
  5 #define N 10
  6 
  7 typedef struct {
  8     long int id;
  9     char name[20];
 10     float objective;    // 客观题得分
 11     float subjective;   // 操作题得分
 12     float sum;
 13     char level[10];
 14 } STU;
 15 
 16 // 函数声明
 17 void input(STU s[], int n);
 18 void output(STU s[], int n);
 19 void process(STU s[], int n);
 20 
 21 int main() {
 22     STU stu[N];
 23 
 24     printf("从文件读入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N);
 25     input(stu, N);
 26 
 27     printf("\n对考生信息进行处理: 计算总分,确定等级\n");
 28     process(stu, N);
 29 
 30     printf("\n打印考生完整信息, 并保存到文件中");
 31     output(stu, N);
 32 
 33     return 0;
 34 }
 35 
 36 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 37 void input(STU s[], int n) {
 38     int i;
 39     FILE *fin;
 40 
 41     fin = fopen("examinee.txt", "r");
 42     if (fin == NULL) {
 43         printf("fail to open file\n");
 44         exit(0);
 45     }
 46 
 47     while (!feof(fin)) {
 48         for (i = 0; i < n; i++)
 49             fscanf(fin, "%ld %s %f %f", &s[i].id, s[i].name, &s[i].objective, &s[i].subjective);
 50     }
 51 
 52     fclose(fin);
 53 }
 54 
 55 // 输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级
 56 // 不仅输出到屏幕上,还写到文本文件result.txt中
 57 void output(STU s[], int n) {
 58     FILE *fout;
 59     int i;
 60     
 61     // 输出到屏幕
 62     printf("\n");
 63     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n");
 64     for (i = 0; i < n; i++)
 65         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);
 66     
 67     // 保存到文件 
 68     fout = fopen("result.txt", "w");
 69     if (!fout) {
 70         printf("fail to open or create result.txt\n");
 71         exit(0);
 72     }
 73     
 74     fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n");
 75 
 76     for (i = 0; i < n; i++)
 77         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);
 78 
 79     fclose(fout);
 80 }
 81 
 82 // 对考生信息进行处理:计算总分,排序,确定等级
 83 void process(STU s[], int n) {
 84     // 补足代码
 85     // ×××
 86     int i,j;
 87     STU temp;
 88     for(i = 0;i < n;i++)
 89     s[i].sum = s[i].objective + s[i].subjective;
 90     
 91     for(i = 0;i < n-1;i++)
 92         for(j = 0;j < n-1;j++)
 93             if(s[j].sum < s[j+1].sum)
 94             {
 95                 temp = s[j];
 96                 s[j] = s[j+1];
 97                 s[j+1] = temp;
 98             }
 99             
100     
101     for(i = 0;i < n;i++)
102     {
103         if(i<(n/10))
104             strcpy(s[i].level,"优秀");
105         if(i>=(n/10)&&i<(n/2))
106             strcpy(s[i].level,"合格");
107         if(i>=(n/2))
108             strcpy(s[i].level,"不合格");
109         
110     }
111              
112              
113             
114 }

 

3.实验任务6

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #define N 80
 5 int main()
 6 {
 7     typedef struct {
 8     long int id;
 9     char name[20];
10     char class[40];
11 } STU;
12     STU s[N];
13     FILE *fin;
14     int i,n;
15     
16     fin = fopen("list.txt","r");
17     if(fin == NULL)
18         {
19             printf("fail to open\n");
20             exit(0);
21         }
22         
23     while (!feof(fin)) 
24     {
25         for(i = 0;i < N;i++)
26             fscanf(fin, "%ld %s %s", &s[i].id, s[i].name, s[i].class);
27     }
28         
29     srand(time(NULL));
30     int x[5];
31     for(i = 0;i < 5;i++)
32         x[i] = rand()% 80 + 1;
33     
34     for(i =0;i < 5;i++)
35     {    
36         printf("%ld\t%s\t%s\n",s[x[i]].id,s[x[i]].name,s[x[i]].class);
37     }
38     
39     FILE *fout;
40     fout = fopen("lucky.txt", "w");
41     if (!fout) {
42         printf("fail to open or create result.txt\n");
43         exit(0);
44     }
45     
46     for (i = 0; i < 5; i++)
47         fprintf(fout,"%ld\t%s\t%s\n",s[x[i]].id,s[x[i]].name,s[x[i]].class);
48 
49     fclose(fout);
50     
51     return 0;
52 }

 

标签:10,20,int,char,STU,实验,include
From: https://www.cnblogs.com/cxj114-514/p/17467784.html

相关文章

  • 实验7
    task1源代码:1'''2银行账户3数据:持卡人姓名、账户、当前余额4操作:取款、存款、打印账户信息5返回账户余额6'''78classAccount:9'''一个模拟银行账户的简单类'''1011def__init__(self,name,account_number,initial_amount......
  • 实验7 面向对象编程与内置模块
    实验任务1:模拟银行账户,理解类的封装特性'''银行账户数据:持卡人姓名、账号、当前余额操作:取款、存款、打印账户信息、返回账户余额'''classAccount:'''一个模拟银行账户的简单类'''def__init__(self,name,account_number,initial_amount=10):'&......
  • 实验七
    实验七实验任务4实验代码#include<stdio.h>intmain(){FILE*file;charfilename[100];charch;intcount=0;file=fopen("data4.txt","r");if(file==NULL){printf("无法打开文件\n");r......
  • 实验7
    #include<stdio.h>#include<stdlib.h>intmain(){FILE*fp1,*fp2,*fp3;charch;fp1=fopen("data4.txt","r");fp2=fp3=fp1;while((ch=getc(fp2))!=EOF){if(ch!=''&&ch!='\......
  • 实验7
     task4.c1#include<stdio.h>2#include<stdlib.h>3#include<string.h>4#defineN1005intmain()6{7chars;8inti=0;9FILE*fp;1011fp=fopen("data4.txt","r");1213......
  • 五月二十五日实验
    实验一略实验二略实验三略实验四#include<stdio.h>#include<string.h>#defineN100typedefstruct{charnum[10];//学号ints1;//期末成绩ints2;//平时成绩doublesum;//总评charlevel[10];//等级}STU;intfun(STUa[],intn,STUh[]);//......
  • 实验6
    task1_1源代码:1fromturtleimport*23defmove(x,y):4'''画笔移动到坐标(x,y)处'''5penup()6goto(x,y)7pendown()89defdraw(n,size=100):10'''绘制边长为size的正n边形'''......
  • 实验七
    任务三#include<stdio.h>#include<stdlib.h>intmain(){FILE*fp;charch;intchar_count=0;fp=fopen("data4.txt","r");if(fp==NULL){printf("Couldnotopenfiledata4.txt.\n&q......
  • 实验7
    任务4源代码#include<stdio.h>#include<string.h>intmain(){FILE*fp;inti,num=0;chars[7][80];fp=fopen("data4.txt","r");if(fp==NULL){printf("failtoopenfp\n");......
  • 实验6 turtle绘图与python库应用编程体验
    task1-1fromturtleimport*defmove(x,y):penup()goto(x,y)pendown()defdraw(n,size=100):foriinrange(n):fd(size)left(360/n)defmain():pensize(2)pencolor('red')move(-200,0)draw......