首页 > 其他分享 >实验7

实验7

时间:2023-12-21 23:14:11浏览次数:22  
标签:int printf st stu 实验 sts id

task 4

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

int main(){
    FILE *fp;
    char ch;
    int cnt=0;

    fp = fopen("data4.txt","r");
    if(fp==NULL){
        printf("fail\n");
        exit(0);
    }

    while(!(feof(fp))){
        ch = fgetc(fp);
        if(ch==' '||ch=='\n') continue;
        if(ch==EOF) break;
        cnt++;
    }
    fclose(fp);

    printf("data4.txt中有字符%d个\n",cnt);

    return 0;
}

 

task 5

#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("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);

 

 task 6

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

typedef struct {
     int id;
     char name[10];
     char grade[100];
}stu;

void finput(stu st[],int n){
    int i;
    FILE *fp;

    fp=fopen("list.txt","r");
    while (!feof(fp)) {
        for (i=0;i<N;i++)
            fscanf(fp,"%d%s%s",st[i].id,st[i].name,st[i].grade);
    }
    fclose(fp);

}

void output(stu st[], int n) {
    int i;
    for(i=0;i<n;i++)
       printf("%d\t%s\t%s\n",st[i].id,st[i].name,st[i].grade);

}

void translate(stu st[],stu sts[],int n) {
    srand((unsigned)time(NULL));
    int judge[80] = { 0 };
    for (int i = 0; i < 5; i++) {
        int j = rand() % 80 + 1;
        if (judge[j] == 1) {
            i--;
            continue;
        }
        sts[i].id = st[j].id;
        strcpy(sts[i].name, st[j].name);
        strcpy(sts[i].grade,st[j].grade);
        judge[j] = 1;
    }
void sort(stu sts[], int n)
{
    for (int i=0;i<5;i++) {
        for(int j=i; j<5;j++) {
            if(sts[i].id>sts[j].id) {
                int l=sts[j].id;
                sts[i].id=sts[j].id;
                sts[j].id=l;
                char p[100];
                strcpy(p,sts[i].name);
                strcpy(sts[i].name,sts[j].name);
                strcpy(sts[j].name,p);
            }
        }
    }
}

int main ()
{
    stu st[N], sts[5];
    finout(stu,N);
    output(stu,N);
    printf("\n");
    translate(st,sts,N)
    sort(sts,5)
    OUTPUT(STS,5);

    return 0;
}

 

 

标签:int,printf,st,stu,实验,sts,id
From: https://www.cnblogs.com/p2kp2k/p/17920313.html

相关文章

  • 实验7
     ......
  • 实验七
    #include<stdio.h>#include<stdlib.h>#include<time.h>typedefstruct{longid;charname[20];charban[40];}xx;intmain(){xxa[5],x[80];inti,n;FILE*fp,*fps;fp=fopen("d:\\data\\......
  • 实验七
    #include<stdio.h>#defineN2intmain(){char*x[N]={"0123456789-0123456789","nuist2023-nuist2024"};inti;FILE*fp;fp=fopen("data4.txt","w");if(fp==......
  • 实验7
    task4#include<stdio.h>#include<ctype.h>intmain(){FILE*file;intcount=0;intch;file=fopen("d:\\data\\data4.txt","r");if(file==NULL){printf("Couldnotopenfile.......
  • 实验七
    实验四#include<stdio.h>#include<stdlib.h>intSizeLength(FILE*ptr){charch;intcnt=0;while(1){ch=fgetc(ptr);if(ch==EOF){break;}if(ch!='')cnt++;}retur......
  • jfinal实验体会
    这次实验我使用的是vue前端+jfinal后端,出现了非常多的问题,因此也花费了我不少时间。在一开始啃jfinal的文档的时候,我感觉jfinal是一个和springboot非常类似的框架,但是使用中我也发现了很多因为不熟悉带来的不便:比如jfinal的代码生成器,他和mysql数据库的关联那些步骤我只会模仿,没有......
  • 配置ospf通告缺省路由实验
    1、实验拓扑图2、实验目标R2与R3为企业边界出口设备,组成双出口冗余备份以实现负载分担,企业内部配置运行部署OSPF路由协议,令PC1与PC2可正常访问R1路由器的100.1.1.0网络;为减少路由表的容量,在R2与R3上配置缺省路由,提供连通性的同时保证网络的高可用性3、实验配置步骤......
  • 实验七
    task4#include<stdio.h>#include<ctype.h>intmain(){FILE*file;intcount=0;intch;file=fopen("d:\\data\\data4.txt","r");if(file==NULL){printf("Couldnotopenfile.......
  • 实验7
    //使用fputs()将字符串写入文本文件#include<stdio.h>#defineN5intmain(){//定义字符指针数组,每个元素存放字符串的起始地址char*ptr[N]={"Working\'sBlues","EverythingWillFlow","Streets......
  • 定时器PWM输出实验
    定时器PWM输出实验由于是用TIM定时器输出,必涉及到IO口的复用。原理:以上计数方式为例:计数器从零计数到arr(重装载值),此过程中,我们定义一个crr_x,则被分为两种情况:计时器值大于crr_x,计数器值小于crr_x。我们使第二种情况输出低电平,第一种情况输出高电平,就可以得到输出电平。我们调整a......