首页 > 其他分享 >实验七

实验七

时间:2024-06-21 15:36:11浏览次数:13  
标签:int pass st STU 实验 printf t%

task4

#include <stdio.h>
#include <ctype.h>

int main() {
    char filename[100];
    FILE *file;
    int ch;
    int count = 0;

    printf("请输入要统计字符数的文件名: ");
    scanf("%s", filename);

    file = fopen(filename, "r");
    if (file == NULL) {
        perror("无法打开文件");
        return 1;
    }

    while ((ch = fgetc(file)) != EOF) {
        if (!isspace(ch)) {
            count++;
        }
    }

    fclose(file);

    printf("文件 '%s' 中的非空白字符数为: %d\n", filename, count);

    return 0;
}

 

 

 

 

task5

#include <stdio.h>
#include <string.h>
#include <stdlib.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 count = 0;
    int i;
    for (i = 0; i < n; i++) {
        st[i].sum = st[i].objective + st[i].subjective;
        if (st[i].objective >= 30 && st[i].subjective >= 30) {
            strcpy(st[i].ans, "Pass");
            st_pass[count] = st[i];
            count++;
        } else {
            strcpy(st[i].ans, "Fail");
        }
    }
    return count;
}

 

 

 

task6

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define TOTAL_STUDENTS 80
#define SELECTED_STUDENTS 5

void shuffle(int *array, int n) {
    if (n > 1) {
        int i;
        for (i = 0; i < n - 1; i++) {
            int j = i + rand() / (RAND_MAX / (n - i) + 1);
            int t = array[j];
            array[j] = array[i];
            array[i] = t;
        }
    }
}

int main() {
    char studentList[TOTAL_STUDENTS][100]; // Assume each student's info is less than 100 characters
    int indices[TOTAL_STUDENTS];

    FILE *listFile = fopen("list.txt", "r");
    if (listFile == NULL) {
        perror("Error opening list.txt");
        return 1;
    }

    int i;
    for (i = 0; i < TOTAL_STUDENTS; i++) {
        fgets(studentList[i], sizeof(studentList[i]), listFile);
        indices[i] = i; // Initialize indices array
    }
    fclose(listFile);

    srand(time(NULL));
    shuffle(indices, TOTAL_STUDENTS);

    FILE *luckyFile = fopen("lucky.txt", "w");
    if (luckyFile == NULL) {
        perror("Error opening lucky.txt");
        return 1;
    }

    printf("Selected students:\n");
    
    for (i = 0; i < SELECTED_STUDENTS; i++) {
        printf("%s", studentList[indices[i]]);
        fprintf(luckyFile, "%s", studentList[indices[i]]);
    }

    fclose(luckyFile);

    return 0;
}

 

标签:int,pass,st,STU,实验,printf,t%
From: https://www.cnblogs.com/lmbjyx/p/18260597

相关文章

  • 实验7
    task4#include<stdio.h>intmain(){intcount=0;charc;FILE*fp;fp=fopen("data4.txt","r");if(fp==NULL){printf("failtoopenthefile\n");return1;}......
  • 实验七
    TASK4#include<stdio.h>intmain(){ intcnt=0; charc; FILE*fp; fp=fopen("data4.txt","r"); if(fp==NULL){ printf("failtoopenthefile\n"); return1; } while((c=fgetc(fp))!=EOF){ if(c!='......
  • Linux 阻塞和非阻塞 IO 实验学习
    Linux阻塞和非阻塞IO实验学习IO指的是Input/Output,也就是输入/输出,是应用程序对驱动设备的输入/输出操作。当应用程序对设备驱动进行操作的时候,如果不能获取到设备资源,那么阻塞式IO就会将应用程序对应的线程挂起,直到设备资源可以获取为止。对于非阻塞IO,应用程序对应的线......
  • 阿里云Apsara Clouder专项技能认证——弹性计算Clouder认证:ECS快速入门(实验、考试操
    前言证书特点:免费!免费!免费!多一个证书有啥不好呢!!!证书名称:阿里ECS专项认证证书有效期:2年培训需求:免费培训,阿里提供课程发证机构:阿里巴巴考试时间:随时,线上直接考考试语言:中文考试费用:0考试难度:★★☆☆☆社会认可度:★★☆☆☆性价比:★★★★★(因为免费,性价比拉满)技术......
  • 实验7
    task4#include<stdio.h>intmain(){intcount=0;charch;FILE*fp;//以读的方式打开文本文件data1.txtfp=fopen("data4.txt","r");//如果打开文件失败,输出提示信息并返回if(fp==NULL){printf("fai......
  • 人工智能模型组合学习的理论和实验实践
        组合学习,即掌握将基本概念结合起来构建更复杂概念的能力,对人类认知至关重要,特别是在人类语言理解和视觉感知方面。这一概念与在未观察到的情况下推广的能力紧密相关。尽管它在智能中扮演着核心角色,但缺乏系统化的理论及实验研究方法,使得分析计算模型的组合学习能力......
  • 实验七
    点击查看代码#include<stdio.h>intmain(){intcount=0;intch;FILE*fp;fp=fopen("data4.txt","r");if(fp==NULL){printf("failtoopenfile\n");return1;}while((......
  • PXE高效批量网络装机(补充) 实验部分
    然后把防火墙、安全机制全都给关闭掉,不要让它们干扰后续的实验:然后安装那几个需要用到的软件包:如果重启了系统vsftpd是不能自动启动起来的,如果想让该服务每次开机都自动的启动起来,可以执行下图中的命令:注:在dhcp的配置文件里面需要指定一些TFTP的相关数据然后保......
  • 矿大数据结构 实验三
    A.二叉链表存储的二叉树定义节点---建立二叉树的函数---前序遍历函数---中序遍历函数#include<bits/stdc++.h>#definelllonglongusingnamespacestd;structNode{//节点,用char存储本身的数据,指针指向左右节点 chardata; Node*lc; Node*rc; Node......
  • 5.24实验三 数据库完整性、安全性实现
    实验三 数据库完整性、安全性实现一、实验目的:使学生加深对数据库安全性和完整性的理解,并掌握SQLServer中有关用户、角色及操作权限的管理方法,学会创建和使用规则、缺省和触发器以及存储过程。二、实验要求:通过实验对数据进行完整性控制、安全性维护。三、实验步骤:......