首页 > 编程语言 >实验6 文件应用编程

实验6 文件应用编程

时间:2022-12-25 19:33:57浏览次数:37  
标签:文件 int 编程 list lucky STU 实验 include define

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 int main()
 4 {
 5     int num=0;
 6     FILE* fp;
 7     char ch;
 8     if ((fp = fopen("data4.txt", "r"))==NULL)
 9         printf("fail to open it");
10     ch = fgetc(fp);
11     while (!feof(fp))
12     {
13         if (ch != ' ' &&  ch!= '\n' )
14             num++;
15         ch = fgetc(fp);
16     }
17     fclose(fp);
18     printf("%d", num);
19 }

 

 

#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 level[10];
} STU;

// 函数声明
void input(STU s[], int n);
void output(STU s[], int n);
void process(STU s[], int n);

int main() {
    STU stu[N];

    printf("从文件读入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N);
    input(stu, N);

    printf("\n对考生信息进行处理: 计算总分,确定等级\n");
    process(stu, N);

    printf("\n打印考生完整信息, 并保存到文件中");
    output(stu, N);

    return 0;
}

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

    fclose(fin);
}

// 输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级
// 不仅输出到屏幕上,还写到文本文件result.txt中
void output(STU s[], int n) {
    FILE* fout;
    int i;

    // 输出到屏幕
    printf("\n");
    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", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].level);

    // 保存到文件 
    fout = fopen("result.txt", "w");
    if (!fout) {
        printf("fail to open or create result.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].level);

    fclose(fout);
}

// 对考生信息进行处理:计算总分,排序,确定等级
void process(STU s[], int n)
{
    STU temp;
    int i, j, k;

    for (i = 0; i < N; i++)
    {
        s[i].sum = s[i].objective  + s[i].subjective ;
    }
    for (i = 0; i < N - 1; i++)
    {
        for (j = 0; j < N - i - 1; j++)
        {
            if (s[j].sum < s[j + 1].sum)
            {
                temp = s[j + 1];
                s[j + 1] = s[j];
                s[j] = temp;
            }
        }
    }
    for (k = 0; k < N; k++)
    {
        switch (k)
        {
        case 0:strcpy(s[k].level, "优秀"); break;
        case 1:
        case 2:
        case 3:
        case 4:strcpy(s[k].level, "合格"); break;
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:strcpy(s[k].level, "不合格"); break;
        }
    }
}

  

 

 

 

 

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<time.h>
 5 #include<string.h>
 6 #define N 80
 7 #define M 5
 8 typedef struct 
 9 {
10     long no;
11     char name[10];
12     char class[20];
13 }STU;
14 int main()
15 {
16     STU list[N],lucky[M];
17     FILE* fp1,*fp2;
18     int i,j,random;
19     if ((fp1 = fopen("list.txt", "r")) == NULL)
20         printf("fail to open it");
21     for (i = 0; i < N; i++)
22         fscanf(fp1, "%ld %s %s", &list[i].no, list[i].name, list[i].class);
23     fclose(fp1);
24     srand((unsigned int)time(NULL));
25     for (j = 0; j < M; j++)
26     {
27         random = rand() % N + 1;
28         lucky[j] = list[random];
29     }
30     if ((fp2 = fopen("lucky.txt", "w")) == NULL)
31         printf("fail to open it");
32     for (j = 0; j < M; j++)
33         fprintf(fp2, "%ld  %s   %s\n", lucky[j].no, lucky[j].name, lucky[j].class);
34     fclose(fp2);
35 }

 


 

 

标签:文件,int,编程,list,lucky,STU,实验,include,define
From: https://www.cnblogs.com/libeiqundad/p/17004433.html

相关文章

  • 《并发编程入门》总结篇
    前言本文是《Java并发视频入门》视频课程的笔记总结,旨在帮助更多同学入门并发编程。本系列共五篇博客,此文为五篇博客的汇总篇。目录​​并发编程入门(一):多线程基础_......
  • C++中头文件由来及使用细节
    在刚学习C++的时候,无法一下子习惯头文件’.h’这个东西,因为在C#中,通常函数/变量的声明和实现都是写在一起的,跨.cs文件调用其他类成员时,编译器并不会冒出’未找到该......
  • 构建根文件系统的其他部分
    /etc/inittab格式id:runlevels:action:processid:ID意义对BusyBoxinit和SystemVinit是不同的,此处ID指明程序的控制tty,如果程序不需要交互,则ID为空runlevels:BusyBox......
  • 构建根文件系统的步骤
    采用Busybox创建基本命令创建基本的目录/lib/etc/var/tmp/dev/sys/proc等添加glibc基本动态库创建基本的设备节点添加启动配置和脚本程序/etc/inittab/etc/fsta......
  • Java编程思想11
    第十六章:数组Java中已经有了容器,为什么还需要数组呢,是因为数组可以持有基本类型吗?但是在泛型出来之后,通过自动包装机制,其实通过容器也能够持有基本类型。在Jav中,数组是一种......
  • Java编程思想12
    第十七章:容器深入研究完整的容器分类法:这张图是把工作中常用到的实现类和相关接口使用UML类图辨识出来  JavaSE5新添加了:Queue接口及其实现PriorityQueue和各种风......
  • 实验八
    部署过程配置华为云服务器  安装程序dnfinstallhttpdmysql-serverphpphp-mysqlndphp-fpm    启用Apachesystemctlstarthttpd.se......
  • 实验七-缓冲区溢出
    一、实验指导书内容1.实验准备系统用户名shiyanlou实验楼提供的是64位Ubuntulinux,而本次实验为了方便观察汇编语句,我们需要在32位环境下作操作,因此实验之前需要......
  • 使用不同的文件目录层次结构?
    使用不同的文件目录层次结构?FHS(文件系统层次化标准)规定了制定根文件系统的规则  通常大多少应用程序和发布依赖这些规则,但这些规则不是内核强制的在嵌入式Linux系统中......
  • 实验六
    #include<stdio.h>#include<stdlib.h>intmain(){FILE*fp;charal;intcount=0;fp=fopen("C:\\data4.txt","r");if(fp==NULL){prin......