首页 > 其他分享 >实验7

实验7

时间:2024-12-29 13:09:08浏览次数:4  
标签:fp int st STU 实验 printf sel

任务4

源代码

#include<stdio.h>
#define N 30
int main(){
    char s[N];
    int i,n;
    int line=0,word=0;
    FILE *fin;
    fin = fopen("data4.txt","r");

    if(!fin){
        printf("fail to open the file");
        return 0;
    }
    while(!feof(fin)){
        fgets(s,N,fin);
        line++;
        i = 0;
        while(s[i]!='\0'){
            if(s[i]!=' '&&s[i]!='\n')
            word++;
            i++;
        }
        }
    fclose(fin);
    printf("data4.txt统计结果:\n");
    printf("行数:%d\n",line);
    printf("字符数(不计空白符):%d\n",word);
    
    return 0;
}

 

运行结果

 

 

任务5

源代码

#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_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].result);
    
}
    
void read(STU st[], int n) {
    int i;
    FILE *fin;
    
    fin = fopen("examine.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) {
    int i;
    FILE *fp;
    
    fp = fopen("data5.txt", "w");
    
    if(fp == NULL){
        printf("fail to open file to write");
        return;
    }

    for(i = 0;i < n; i++)
        fprintf(fp, "%-20ld%-8s%-15.2f%-15.2f%-15.2f%-10s\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 i, j = 0, cnt = 0;
    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[j++] = st[i];
            cnt++;
        }
         else {
             strcpy(st[i].result, "不通过");
         }  
    }
    return cnt;
}

 

运行结果

 

任务6

必做

源代码

 

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#define N 1000
typedef struct student{
        int id;
        char name[N];
        char cla[N];
    }STU;
void read (STU st[]);
void process (STU st[]); 
int main(){
    STU s[80];
    
    read(s);
    process(s);

    return 0;
}
void read (STU st[]){
    int i;
    FILE *fp;
    fp = fopen("list.txt","r");
    
    if(fp == NULL){
        printf("fail to open the file");
        return;
    }
    
    while(!feof(fp)){
        for (i = 0;i < 80; i++){
            fscanf(fp,"%ld %s %s",&st[i].id,st[i].name,st[i].cla);
        }
    }
    fclose(fp);
}

void process (STU st[]){
    srand(time(NULL));
    int i = 0,temp,count = 0;
    int sel[5];
    char time[1000];
    
    while(count < 5){
        int sign = 1;
        temp = rand() % 80;

        for(i = 0;i < count; i++){
            if(sel[i] == temp)
                count--;
        } 
        sel[i] = temp;
        count++;
    }    
    
    printf("-------------随机抽点名单-------------\n");
    
    for(i = 0;i < 5; i++){
        printf("%d\t%s\t\t%s\t\n",st[sel[i]].id,st[sel[i]].name,st[sel[i]].cla);
    }    
    
    printf("--------------保存到文件---------------\n");
    printf("输入文件名:");
    scanf("%s",time); 

    FILE *fp;
    fp = fopen(time,"w");
    if(fp == NULL){
        printf("fail to open the file");
        return;
    }
    for(i = 0;i < 5; i++){
        fprintf(fp,"%d\t\t%s\t\t%s\t\n",st[sel[i]].id,st[sel[i]].name,st[sel[i]].cla);
    }
    fclose(fp);
    printf("保存成功!");
    
}

 

运行结果

 

 

 

选做

源代码

 

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#define N 1000
typedef struct student{
        int id;
        char name[N];
        char cla[N];
    }STU;
void read (STU st[]);
void process (STU st[]); 
int main(){
    STU s[80];
    
    read(s);
    process(s);

    return 0;
}
void read (STU st[]){
    int i;
    FILE *fp;
    fp = fopen("list.txt","r");
    
    if(fp == NULL){
        printf("fail to open the file");
        return;
    }
    
    while(!feof(fp)){
        for (i = 0;i < 80; i++){
            fscanf(fp,"%ld %s %s",&st[i].id,st[i].name,st[i].cla);
        }
    }
    fclose(fp);
}

void process (STU st[]){
    srand(time(NULL));
    int i,j,t;
    int temp,count = 0;
    int sel[5];

    while(count < 5){
        int sign = 1;
        temp = rand() % 80;

        for(i = 0;i < count; i++){
            if(sel[i] == temp)
                count--;
        } 
        sel[i] = temp;
        count++;
    }    
    for(i = 0; i < 5; i++)
        for(j = 0; j < 4-i; j++){
            if(sel[i] > sel[i+1]){
            t = sel[i];
            sel[i] = sel[i+1];
            sel[i+1] = t;
        }
    }
    printf("-------------随机抽点名单-------------\n");
    
    for(i = 0;i < 5; i++){
        printf("%d\t%s\t\t%s\t\n",st[sel[i]].id,st[sel[i]].name,st[sel[i]].cla);
    }    
    
    time_t rawtime;
    struct tm *timein;
    char name[100];
    
    time(&rawtime);
    timein = localtime(&rawtime);
    strftime(name,sizeof(name),"%Y%m%d.txt",timein);
    FILE *fp;
    fp = fopen(name,"w");
    if(fp == NULL){
        printf("fail to open the file");
        return;
    }
    for(i = 0;i < 5; i++){
        fprintf(fp,"%d\t\t%s\t\t%s\t\n",st[sel[i]].id,st[sel[i]].name,st[sel[i]].cla);
    }
    fclose(fp);
    printf("文件保存成功!");
    
}

 

运行结果

 

 

标签:fp,int,st,STU,实验,printf,sel
From: https://www.cnblogs.com/syqsyqsyqsyq/p/18638571

相关文章

  • 实验7
    1#include<stdio.h>2intmain(){3FILE*fp;4fp=fopen("data4.txt","r");5charch;6inta=1,b=0;7while((ch=fgetc(fp))!=EOF){8if(ch=='\n'){9a......
  • 实验七
    任务四源代码1#include<stdio.h>2#include<string.h>3intmain(){4FILE*fp;5fp=fopen("d:\\data4.txt","r");6if(fp==NULL){7printf("failtoopenfiletowrite\n");8return;9......
  • 实验7
    实验任务4:1#include<stdio.h>2#include<stdlib.h>3intis_word(charx);4intmain(){5FILE*fp;6charch;7inti,line=1,count=0;89fp=fopen("d:\\data4.txt","r");10if(!fp)11......
  • 实验7_文件应用编程
    任务4#include<stdio.h>#include<stdlib.h>#include<stdbool.h>//判断字符是否为空白字符(空格、回车、tab)boolis_blank(charc){returnc==''||c=='\t'||c=='\n';}intmain(){charfilename[100];......
  • 实验7
    1.实验任务4程序task4.c源码1#include<stdio.h>2#defineN103#defineM100456intmain(){7charx[N][M];8inti,j,cnt=0,count=0;9FILE*fp;1011fp=fopen("data4.txt","r");12if(!fp){13......
  • 正在测试和完善的CH552(CH549)USB下载之单按键带入电路实验
    一、设计理由CH552或CH549进入USB下载,通常需要两个按键,一个控制电源的通断,一个通过串联电阻(一头接VCC或V33)冷启动时抬高UDP电平。时序上是这样的:断电--按下接UDP的轻触开关--通电--松开接UDP的轻触开关。这样操作上一般需要双手并用,比较麻烦。二、电子电路本人设计的电路是想通......
  • 实验七
    task4源代码:#include<stdio.h>#defineN10#defineM80voidcount_line(int*ptr1);voidcount_num(int*ptr2);intmain(){inta,b;count_line(&a);count_num(&b);printf("行数为:%d\n",a);printf("字符数......
  • 实验7
    #include<stdio.h>voidx(constchar*filePath){FILE*file=fopen(filePath,"r");inta=0;intb=0;if(file==NULL){printf("无法打开文件\n");return;}charline[300];while(......
  • py期末 实验1-6
    选择判断特别是对于范围在-5到256之间的整数。对于这些整数,Python会使用缓存机制,即它们在程序的生命周期内是单例的,所有引用该值的地方都会指向同一个内存地址。while和for可以有elseforiinrange(5):print(i)ifi==3:breakelse:print("循......
  • ssm实验室排课系统+jsp(10796)
     有需要的同学,源代码和配套文档领取,加文章最下方的名片哦一、项目演示项目演示视频二、资料介绍完整源代码(前后端源代码+SQL脚本)配套文档(LW+PPT+开题报告)远程调试控屏包运行三、技术介绍Java语言SSM框架SpringBoot框架Vue框架JSP页面Mysql数据库IDEA/Eclipse开发四、项......