首页 > 其他分享 >实验7

实验7

时间:2024-06-18 17:33:00浏览次数:14  
标签:int printf st STU 实验 pass fin

task4

#include <stdio.h>

int main() {
    int count = 0;
    char c;
    FILE *fp ;
    fp = fopen("data4.txt", "r");
    
    
    if (fp ==NULL) {
        printf("fail to open the file\n");
        return 1;
    }
    
    while ((c = fgetc(fp)) != EOF) {
        if (c != ' ' && c != '\n' && c != '\t') {
            count++;
        }
    }

    fclose(fp);

    printf("data4.txt中共包含字符数(不计空白符):%d\n", count);

    return 0;
}

task5

#include<stdio.h>
#include<string.h>
#define N 10
typedef struct{
    long int id;
    char name[20];
    float objective;
    float subjective;
    float sum;
    char ans[10];
}STU;

void finput(STU st[],int n);
void foutput(STU st[],int n);
void output(STU st[],int n);
int process(STU st[],int n,STU st_pass[]);

int main(){
    STU stu[N],stu_pass[N];
    int cnt;
    double pass_rate;

     printf("从文件读入%d个考生信息...\n",N);
     finput(stu,N);

     printf("\n对考生成绩进行统计...\n");
     cnt=process(stu,N,stu_pass);

     printf("\n通过考试的考生完整信息:\n");
     output(stu_pass,cnt);
     foutput(stu_pass,cnt);

     pass_rate=1.0*cnt/N;
     printf("\n本次等级考试通过率:%.2f%%\n",pass_rate*100);
     return 0;}

void output(STU st[], int n) {
    int i;
    
    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", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].ans);
}

// 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
void finput(STU st[], 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", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
    }

    fclose(fin);
}

// 把通过考试的考生完整信息写入文件list_pass.txt
// 准考证号,姓名,客观题得分,操作题得分,总分,结果
void foutput(STU s[], int n) {
    FILE *fout;
    int i;
    
    // 保存到文件 
    fout = fopen("list_pass.txt", "w");
    if (!fout) {
        printf("fail to open or create list_pass.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].ans);

    fclose(fout);
}

// 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
int process(STU st[], int n, STU st_pass[]) {
   int i,cnt=0;
   for(i=0;i<n;i++){
   st[i].sum=st[i].objective + st[i].subjective;
       if(st[i].sum>=60){
           strcpy(st[i].ans,"pass");
           st_pass[cnt++] = st[i];
       }
       else
       strcpy(st[i].ans,"fail");

   }
    return cnt;
   
   
}

 

task6

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define M 100
#define N 80

typedef struct {
    long int id;
    char name[10];
    char c[20];
}STU;


int main(){
    STU st[M];
    int i,n;
    FILE *fin;
    FILE *fp;

    fin = fopen("list.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 %s",&st[i].id,st[i].name,st[i].c);
    }

    fp = fopen("lucky.txt", "wb");
    if(fp == NULL) {
        printf("fail to open file\n");
        return 1;
    }

    srand(time(0));
    for(i=0;i<5;i++){
    n=rand()%N;
    fprintf(fp,"%ld %-10s %-20s\n",st[n].id,st[n].name,st[n].c);
    printf("%ld %-10s %-20s\n",st[n].id,st[n].name,st[n].c);
    }

    fclose(fin);
    fclose(fp);
    return 0;
}

 

标签:int,printf,st,STU,实验,pass,fin
From: https://www.cnblogs.com/sincere-/p/18254785

相关文章

  • 实验7
    task4点击查看代码#include<stdio.h>intmain(){ FILE*fp; intsum=0; charch; fp=fopen("data4.txt","r"); if(fp==NULL){ printf("不能打开文件\n"); return1; } ch=fgetc(fp); while((ch=fgetc(fp))!=EOF){ if(ch!=�......
  • 实验七
    Task41#include<stdio.h>23intmain(){4intnum=0;5charch;67FILE*fp;89fp=fopen("data4.txt","r");1011if(fp==NULL){12printf("failtoopenfile\n&......
  • 实验7 文件应用编程
    //task4.c#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(){ inti,n=0; FILE*fp; fp=fopen("data4.txt","r"); if(fp==NULL){ printf("failtoopenthefile"); return1; } while((i=fgetc(......
  • 生物实验室如何规范安全地使用加密U盘?
    生物实验室在进行科研工作时,通常涉及大量的敏感数据和核心研发成果。为保护这些数据和成果不被非法获取或泄露,许多生物实验室会考虑进行网络隔离。一般来说,大多数会采用物理隔离的方式,将生物实验室的内部网络与外部网络完全断开。这种方式可以确保内部网络不受外部网络的干扰和威......
  • 3DS MAX课程结业实验报告书
    一、实验内容选题13题目:安静人生模型:表、沙发、抱枕二、实验说明实验工具:3dsmax文具+3DSMAX布料模拟动画插件CGTrader–PolyClothv2.06for3dsMax2016-2024实验课时:两周实验方法:自主制作任务:完成3dsmax源文件的制作,提交实验报告三、实验过程3.1安装......
  • ROS实验课(三)
    writeinadvance此次实验课给我的生活来了沉重的一击,不单单是因为没有做出来,还因为我卡在了插件缺失而无法解决。之前对待实验课,能在操作流程之外有暇思考具体的实现,此次只能记录简单的操作流程部分。老规矩,基于同样的《ROS机器人开发技术基础》教材。以下是实验内容,要......
  • 实验七
    TASK1点击查看代码//将图书信息写入文本文件data1.txt//再从文件中读取图书信息,打印输出到屏幕上,并显示行号#include<stdio.h>#defineN80#defineM100typedefstruct{ charname[N];//书名 charauthor[N];//作者}Book;//函数声明voidfunc1()......
  • 矿大数据结构 实验二
     目录 A:子串个数B.模式串C.主对角线上的数据和D.顺时针排螺旋阵E:汉诺塔游戏中的移动F.树的先根遍历G.树的后根遍历A:子串个数本题未考虑重复的情况,直接使用公式既可考虑重复的情况:不同子串个数-洛谷#include<bits/stdc++.h>usingnamespacestd;i......
  • 实验7 文件应用编程
    task4.c1#include<stdio.h>23intmain(){4intcount=0;5charch;6FILE*fp;78fp=fopen("data4.txt","r");9if(fp==NULL){10printf("failtoopenfile\n");11......
  • (11.3)iic串口读写EEPROM实验:程序设计
    一、实验任务二、架构框图其中:i2c驱动模块: bit_ctrl:0代表发送8位字节地址;1代表发送16位字节地址(本实验采用)i2c_addr[15:0]:16位字节地址,当bit_ctrl为0时只有低8位是有效的i2c_data_w[7:0]:向EEPROM写入的8位数据i2c_exec:拉高代表当前进行......