首页 > 其他分享 >实验七

实验七

时间:2023-12-16 21:33:23浏览次数:26  
标签:STU int pass st stu 实验 printf

#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 100
typedef struct {
    long number;
    char name[N];
    char class[N];
}student;
int main()
{
    FILE* fp;
    FILE* a;
    int i = 0;
    int j = 0;
    int k = 0;
    student stu[N];



    srand((unsigned int)time(NULL));
    long ret[5];
    for (i = 0; i < 5; i++)
    {
        ret[i] = rand() % 80 + 1;
    }

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

    while (!feof(fp))
    {
        fscanf(fp, "%ld %s %s", &stu[i].number, &stu[i].name, &stu[i].class);
        i++;
    }

    for (j = 0; j < 5; j++)
    {
        printf("%-20ld %-20s %-20s\n", stu[ret[j]].number, stu[ret[j]].name, stu[ret[j]].class);
        fprintf(a, "%-20ld %-20s %-20s\n", stu[ret[j]].number, stu[ret[j]].name, stu[ret[j]].class);
    }
    fclose(fp);
    fclose(a);
    return 0;
}

 

task4

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
    long i = 0;
    char ch;
    FILE* fp;
    fp = fopen("c://data//data4.txt", "r");
    while (!feof(fp))
    {
        ch = fgetc(fp);
        if (ch != ' ' && ch != '\n' && ch != '\t'&&ch!=EOF)
        {
            i++;
        }
    }
    printf("data4.txt中共包含字符(不计空字符):%ld",i);
    fclose(fp);

    return 0;
}

task5

#define _CRT_SECURE_NO_WARNINGS
#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, N);      // 输出到屏幕
    foutput(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].ans);
}

// 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
void finput(STU st[], int n) {
    int i;
    FILE *fin;

    fin = fopen("c:\\data\\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;
    int j = 0;
    for (i = 0; i < n; i++)
    {
        st[i].sum = st[i].objective + st[i].subjective;
    }
    for (i = 0; i < n; i++)
    {
        if (st[i].sum >= 60)
        {
            st_pass[j++] = st[i];
            strcpy(st[i].ans, "合格");   
        }
        else
        {
            strcpy(st[i].ans, "不合格");
        }
    }
    return j;
}

 

标签:STU,int,pass,st,stu,实验,printf
From: https://www.cnblogs.com/suning17/p/17902045.html

相关文章

  • 实验七
    task4code1#include<stdio.h>23intmain(){4FILE*fp;5longcnt=0,c=0;6charch;78fp=fopen("data4.txt","r");9if(fp==NULL){10printf("failedtoopen");1......
  • 实验三-电子公文传输系统1-个人贡献
    实验三-电子公文传输系统1-个人贡献1简述你完成的工作与组内成员相互配合协作,高效率完成任务参与组内文档的撰写工作负责了前端设计与数据库的建立2你们小组总共的代码行数,你贡献的代码行数?相关代码链接?总共代码行数为55352行,其中大部分是gitee上的代码,我们组总共贡献......
  • 实验6 模板类、文件I/O和异常处理
    实验任务4Vector.hpp1#pragmaonce23#include<iostream>4#include<stdexcept>56usingnamespacestd;78template<typenameT>910classVector{11private:12T*data;13size_tsize;1415public:16......
  • 实验7
    //将图书信息写入文本文件data1.txt#include<stdio.h>#defineN80typedefstruct{charname[N];//书名charauthor[N];//作者}Book;intmain(){Bookx[]={{"《雕塑家》","斯科特.麦克劳德"},{"《灯塔》",......
  • 实验六
    task4源代码:1#pragmaonce23#include<iostream>4#include<stdexcept>5#include<cassert>6#include<iomanip>78usingnamespacestd;910template<typenameT>11classVector{12public:13Vecto......
  • 实验7
    //将图书信息写入文本文件data1.txt#include<stdio.h>#defineN80typedefstruct{charname[N];//书名charauthor[N];//作者}Book;intmain(){Bookx[]={{"《雕塑家》","斯科特.麦克劳德"},{"《灯塔》",......
  • 实验7_文件应用编程
    task4#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>intmain(){FILE*fp;charch;intcnt=0;fp=fopen("data4.txt","r");if(fp==NULL){printf("failtoop......
  • 实验七
    task41#include<stdio.h>2intmain(){3FILE*fp;4longcount=0;5chart;6if((fp=fopen("data4.txt","r"))==NULL){7printf("error\n");8return1;9}10while(!f......
  • 【腾讯云云上实验室】用向量数据库在金融信用数据库分析中的实战运用
    一、前言这篇文章将带领读者探索数据库的多样化解决方案及其演进历程,特别关注向量数据库的重要性和在实际项目中的应用。通过深入剖析腾讯云向量数据库及其在金融信用数据库分析中的实战运用,为读者提供全面而实用的指南,帮助他们理解、应用和掌握这一技术领域的关键要点。二、数......
  • 实验6 c语言结构体,枚举应用编程
    task4源代码1#include<stdio.h>2#include<string.h>3#defineN1045typedefstruct{6charisbn[20];//isbn号7charname[80];//书名8charauthor[80];//作者9doublesales_price;//......