首页 > 其他分享 >实验七

实验七

时间:2024-12-30 18:41:30浏览次数:5  
标签:fp int selected st 实验 printf pass

task4

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

int main() {
    FILE *fp;
    
    char ch;
    int lines = 0, characters = 0;
    fp = fopen("d:\\xia01\\Documents\\data4qwe.txt", "r");
    if (fp == NULL) {
        perror("Error opening file");
        return EXIT_FAILURE;
    }

    while ((ch = fgetc(fp))!= EOF) {
        if (ch!= '\n'&&ch!='\t' &&ch!='\0') {
            characters++;
        }
        if (ch == '\n') {
            lines++;
        }
    }

    
    if (characters > 0) {
        lines++;
    }

    fclose(fp);

    printf("data4.txt统计结果:\n");
    printf("行数: %d\n", lines);
    printf("字符数(不计空白符): %d\n", characters);

    return 0;} 

task4

#include <stdio.h>
#include <string.h>

#define N 10

typedef struct {
    long id;            // 准考证号
    char name[20];      // 姓名
    float objective;    // 客观题得分
    float subjective;   // 操作题得分
    float sum;          // 总分
    char result[10];    // 考试结果
} STU;

// 函数声明
void read(STU st[], int n);
void write(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);
    read(stu, N);

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

    printf("\n通过考试的名单:\n");
    output(stu, N);   // 输出所有考生完整信息到屏幕
    write(stu, N);    // 输出考试通过的考生信息到文件

    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].result);
}


void read(STU st[], int n) {
    int i;
    FILE *fin;

    fin = fopen("d:\\xia01\\Documents\\examinee.txt", "r");
    if (!fin) {
        printf("fail to open file\n");
        return;
    }

    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);
}


void write(STU s[], int n) { 
FILE *fp;
    fp = fopen("d:\\新建文件夹\\shiyan7.txt", "w");
    if (fp == NULL) {
        
        return ;
    }
    int i;
    for ( i = 0; i < n; i++) {
        if (s[i].sum >= 60) {
            fprintf(fp, "%ld %s %.2f %.2f %.2f %s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result);
        }
    }
    fclose(fp);
    }


int process(STU st[], int n, STU st_pass[]) {
     int pass_count = 0;
     int i;
    for ( i = 0; i < n; i++) {
        st[i].sum = st[i].objective + st[i].subjective;
        if (st[i].sum >= 60) {
            strcpy(st[i].result, "通过");
            st_pass[pass_count] = st[i];
            pass_count++;
        } else {
            strcpy(st[i].result, "不通过");
        }
    }
    return pass_count;
}

task6

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

#define STUDENT_NUM 80


typedef struct {
    int id;
    char name[20];
    char class1[20];
} Student;


void readStudents(Student students[]) {
    FILE *fp;
    fp = fopen("d:\\xia01\\Documents\\list.txt", "r");
    if (fp == NULL) {
        perror("Error opening file");
        exit(1);
    }
    for (int i = 0; i < STUDENT_NUM; i++) {
        fscanf(fp, "%d %s %s", &students[i].id, students[i].name, students[i].class1);
    }
    fclose(fp);
}


void randomSelect(Student students[], Student selected[], int num) {
    int selected_index[STUDENT_NUM] = {0};
    srand((unsigned int)time(NULL));
    for (int i = 0; i < num; i++) {
        int index;
        do {
            index = rand() % STUDENT_NUM;
        } while (selected_index[index] == 1);
        selected_index[index] = 1;
        selected[i] = students[index];
    }
}


void displayStudents(Student selected[], int num) {
    printf("------随机抽点名单------\n");
    for (int i = 0; i < num; i++) {
        printf("%d %s %s\n", selected[i].id, selected[i].name, selected[i].class1);
    }
}


void writeStudents(Student selected[], int num, char *filename) {
    FILE *fp;
    fp = fopen("d:\\新建文件夹\\shiyan7x.txt", "w");
    if (fp == NULL) {
        perror("Error opening file");
        return ;
    }
    for (int i = 0; i < num; i++) {
        fprintf(fp, "%d %s %s\n", selected[i].id, selected[i].name, selected[i].class1);
    }
    fclose(fp);
    printf("保存成功!\n");
}

int main() {
    Student students[STUDENT_NUM];
    Student selected[5];
    char filename[20];

 
    readStudents(students);

  
    randomSelect(students, selected, 5);

 
    displayStudents(selected, 5);

  
    
    writeStudents(selected, 5, filename);

    return 0;
}

 

标签:fp,int,selected,st,实验,printf,pass
From: https://www.cnblogs.com/xia2005/p/18642153

相关文章

  • 导电布与铜箔在EMC实验中的屏蔽效果对比
    在电磁兼容(EMC)实验中,屏蔽材料的选择对于减少电磁干扰(EMI)至关重要。铜箔和导电布是两种常见的屏蔽材料,它们各自有着不同的物理特性和应用优势。尽管铜箔在传统的EMC屏蔽中常被采用,但在某些高频实验中,导电布却表现出更优的屏蔽效果。一、高频电磁波的吸收与反射铜箔因其卓越......
  • 担心IP实验效果?先免费打个质谱评估一下!
    IP实验是研究蛋白质相互作用、揭示蛋白质作用机理中最常用最重要研究方法。作为一个专注于蛋白质研究的质谱公司,我们给全国大量课题组提供了IP产物的质谱检测,但是50%以上的课题组在首次IP实验中均存在较为明显的问题,严重影响了有效数据的获得以及数据的准确性。所以我们长期呼......
  • 实验7
    Task.4程序:#include<stdio.h>#include<string.h>intmain(){FILE*fp;fp=fopen("d:\\data4.txt","r");if(fp==NULL){printf("failtoopenfiletowrite\n");return;}......
  • 实验7
    实验任务4:源代码:1#include<stdio.h>2#include<string.h>3#defineN345voidwrite();67intmain(){8printf("date4.txt统计结果:");9printf("\n");10write();11return0;12}1314voidwrite()......
  • 实验七
    4.1#include<stdio.h>2intmain(){3charch;4intline=0,num=0;5FILE*fp;6fp=fopen("data4.txt","r");7if(!fp){8printf("1");9return0;10}11while(!feof(fp)){12ch=fgetc(fp);13......
  • 实验7
    任务4源代码1#include<stdio.h>2intread1(){3intcount=0;45FILE*fp;6fp=fopen("datal4.txt","r");78if(fp==NULL){9printf("failtoopenfiletoread\n");10r......
  • 实验7
    #include<stdio.h>intmain(){inta=1,b=0;FILE*fp;fp=fopen("D:\\实验7数据文件及部分代码\\实验7数据文件及部分代码\\data4.txt","r");charf;if(fp==NULL){printf("failtoopenfiletowrite\n");......
  • 实验七
    任务4:源代码:#include<stdio.h>intmain(){FILE*p;charch;intcnt_zf=0,cnt_hs=1;p=fopen("D:/democ.1/data4.txt","r");if(!p)printf("failtoopen");while((ch=fgetc(p))!=EOF)......
  • 实验7
    实验7task.4源代码:1#define_CRT_SECURE_NO_WARNINGS2#include<stdio.h>3#defineN100004intmain(){5charch[N];6intline=1;7intn=0;8intj=0;9FILE*fp;10fp=fopen("data4.txt","r......
  • 实验七
    2.//多了一行,且输出一个8。75-76行是判断文件指针是否已经指向文件结尾了,当number等于一时则已经结束,break。3.\'意义就是输出一个 ' 的符号。4. 1#include<stdio.h>2#defineN10034intmain(){5charstr[N];6inti,j,line;7FILE*fp;......