首页 > 其他分享 >实验7

实验7

时间:2023-12-17 14:24:34浏览次数:37  
标签:fp stu int lucky STU 实验 include

4. 实验任务4

task4.c源码:

#define  _CRT_SECURE_NO_WARNINGS
#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 to open the file\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;
}

截图:



5. 实验任务5

task5.c


截图:




6. 实验任务6

源代码

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

#define N 80
#define M 5

typedef struct{
    long number;
    char name[20];
    char classname[20];
}STU;

void finput(STU stu[],int n);//从list.txt读入
void foutput(STU stu[],STU lucky[],int m);//取随机数,并写入lucky.txt
void output(STU lucky[],int n);//屏幕上输出

int main(){
    STU stu[N],lucky[5];

    finput(stu,N);

    foutput(stu,lucky,M);

    output(lucky,M);

    return 0;
}

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

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

    while(!feof(fp))
        for(i=0;i<N;++i)
            fscanf(fp,"%d%s%s",&stu[i].number,stu[i].name,stu[i].classname);

    fclose(fp);
}

void foutput(STU stu[],STU lucky[],int m){
    FILE *fp;
    int i;

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

    srand(time(NULL));
    for(i=0;i<m;++i){
        int number=rand()%N;//在0,N-1间取随机整数
        lucky[i]=stu[number];
    }

    for(i=0;i<m;++i)
        fprintf(fp,"%d\t%s\t%s\n",lucky[i].number,lucky[i].name,lucky[i].classname);

    fclose(fp);
}

void output(STU lucky[],int n){
    int i;

    for(i=0;i<n;++i)
        printf("%d\t%s\t%s\n",lucky[i].number,lucky[i].name,lucky[i].classname);

}

截图:



标签:fp,stu,int,lucky,STU,实验,include
From: https://www.cnblogs.com/PetroniusMo/p/17909014.html

相关文章

  • 实验6
    4.实验任务4task4.c源码#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册......
  • 鸿蒙小车之多任务调度实验
    说到鸿蒙我们都会想到华为mate60:遥遥领先!我们一直领先!我们这个小车也是采用的是鸿蒙操作系统,学习鸿蒙小车,让你遥遥领先于你的同学。@TOC前言本专栏将依次介绍鸿蒙小车的内核实验,硬件实验,wifi实验。一、什么是任务?为什么要有任务任务是操作系统(RTOS)中的基本组成单元,它们为嵌入式......
  • 实验七
    task4.c#include<stdio.h>intmain(){ charch; intcnt=0;FILE*fp;fp=fopen("data4.txt","r");if(fp==NULL){ printf("failtoopen\n"); return1; }while(1){ch=fgetc(fp);......
  • 实验6-模板类、文件I/O和异常处理
    Vector.hpp1#ifndefVECTOR_HPP2#defineVECTOR_HPP34#include<iostream>5#include<stdexcept>67template<classT>8classVector{9private:10T*arr;11intsize;1213public:14Vector():arr(nu......
  • 实验六
    task4.cpp:点击查看代码#include<iostream>#include"vector.hpp"voidtest(){usingnamespacestd;intn;cin>>n;Vector<double>x1(n);for(autoi=0;i<n;++i)x1.at(i)=i*0.7;output......
  • 实验7
    任务4#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>intmain(){   longi=0;   charch;   FILE*fp;   fp=fopen("c://data//data4.txt","r");   while(!feof(fp))   {       ch=fgetc(fp);       if(ch!=''&......
  • 实验七
    任务1:#include<stdio.h>#defineN80typedefstruct{charname[N];//书名charauthor[N];//作者}Book;intmain(){Bookx[]={{"《雕塑家》","斯科特.麦克劳德"},{"《灯塔》","克里斯多夫.夏布特&quo......
  • 实验7 文件应用编程
    1.实验任务1源代码1//将图书信息写入文本文件data1.txt23#include<stdio.h>45#defineN8067typedefstruct{8charname[N];//书名9charauthor[N];//作者10}Book;1112intmain(){13Bookx[]={{"《雕塑家》",......
  • 实验7
    task41#include<stdio.h>23intmain(){45inti=0;6chars;78FILE*fp;9fp=fopen("data4.txt","r");1011while(1){12s=fgetc(fp);13if(s==EOF){14......
  • 实验6
    任务41#include<iostream>2#include"Vector1.hpp"34voidtest(){5usingnamespacestd;67intn;8cin>>n;910Vector<double>x1(n);11for(autoi=0;i<n;++i)12x1......