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

实验7 文件应用编程

时间:2023-12-20 19:33:58浏览次数:24  
标签:printf 文件 int 编程 st STU 实验 pass t%

实验任务4:文件简单应用

程序源码

 1 #include<stdio.h>
 2 
 3 int main(){
 4     FILE *fp;
 5     long size;
 6     int c;
 7     int count = 0;
 8     fp = fopen("C:\\data\\data4.txt", "rb");
 9     while((c = fgetc(fp))!=EOF){
10         if(c == '\n' || c == ' '){
11             count++;
12         }
13     }
14     size = ftell(fp) - count - 1;
15     printf("data4.txt中总共包含字符数(不计空白符):%d", size);
16     fclose(fp);
17     return 0;
18 } 
View Code

运行结果截图

 实验任务5:文件综合应用

程序源码

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 
  5 #define N 10
  6 
  7 typedef struct {
  8     long int id;
  9     char name[20];
 10     float objective;    // 客观题得分
 11     float subjective;   // 操作题得分
 12     float sum;          // 总分
 13     char ans[10];       // 考试结果
 14 } STU;
 15 
 16 // 函数声明
 17 void finput(STU st[], int n);
 18 void foutput(STU st[], int n);
 19 void output(STU st[], int n);
 20 int process(STU st[], int n, STU st_pass[]);
 21 
 22 int main() {
 23     STU stu[N], stu_pass[N];
 24     int cnt;
 25     double pass_rate;
 26 
 27     printf("从文件读入%d个考生信息...\n", N);
 28     finput(stu, N);
 29 
 30     printf("\n对考生成绩进行统计...\n");
 31     cnt = process(stu, N, stu_pass);
 32 
 33     printf("\n通过考试的名单:\n");
 34     output(stu, N);      // 输出到屏幕
 35     foutput(stu, N);    // 输出到文件
 36 
 37     pass_rate = 1.0 * cnt / N;
 38     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
 39 
 40     return 0;
 41 }
 42 
 43 // 把通过考试的考生完整信息输出到屏幕上
 44 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 45 void output(STU st[], int n) {
 46     int i;
 47     
 48     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 49     for (i = 0; i < n; i++)
 50         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);
 51 }
 52 
 53 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 54 void finput(STU st[], int n) {
 55     int i;
 56     FILE *fin;
 57 
 58     fin = fopen("examinee.txt", "r");
 59     if (fin == NULL) {
 60         printf("fail to open file\n");
 61         exit(0);
 62     }
 63 
 64     while (!feof(fin)) {
 65         for (i = 0; i < n; i++)
 66             fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
 67     }
 68 
 69     fclose(fin);
 70 }
 71 
 72 // 把通过考试的考生完整信息写入文件list_pass.txt
 73 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 74 void foutput(STU s[], int n) {
 75     FILE *fout;
 76     int i;
 77     
 78     // 保存到文件 
 79     fout = fopen("list_pass.txt", "w");
 80     if (!fout) {
 81         printf("fail to open or create list_pass.txt\n");
 82         exit(0);
 83     }
 84     
 85     fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 86 
 87     for (i = 0; i < n; i++)
 88         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);
 89 
 90     fclose(fout);
 91 }
 92 
 93 
 94 
 95 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 96 int process(STU st[], int n, STU st_pass[]) {
 97    int i;
 98     int count = 0;
 99     for(i = 0; i < n; i++){
100         st[i].sum = st[i].subjective + st[i].objective;
101         if(st[i].sum >= 60){
102             strcpy(st[i].ans, "pass");
103             count++;
104         }
105         else{
106             strcpy(st[i].ans, "fail");
107         } 
108     }
109     return count;
110 }
View Code

运行结果截图

 实验任务6

程序源码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 
 5 typedef struct{
 6     int num;
 7     char name[10];
 8     char clas[50];
 9 }STU;
10 
11 int main(){
12     STU st[80];
13     int i, n;
14     FILE *fp;
15     FILE *fp1;
16     fp = fopen("C:\\data\\list.txt", "r");
17     if (fp == NULL) {
18         printf("fail to open file\n");
19         exit(0);
20     }
21     for(i = 0; i < 80; i++){
22         fscanf(fp, "%d %s %s", &st[i].num, &st[i].name, &st[i].clas);
23     }
24     fclose(fp);
25     fp1 = fopen("lucky.txt", "w");
26     srand((unsigned)time(NULL));
27     for(i = 0; i < 5; i++){
28         n = rand() % 80 + 1;
29         fprintf(fp1, "%d%s%s", st[n].num, st[n].name, st[n].clas);
30         printf("%-20d%-20s%-20s\n", st[n].num, st[n].name, st[n].clas);
31     }
32     fclose(fp1); 
33     return 0; 
34 } 
View Code

运行结果截图

 

标签:printf,文件,int,编程,st,STU,实验,pass,t%
From: https://www.cnblogs.com/Eternity91/p/17917312.html

相关文章

  • 软件测试/测试开发|Ubuntu系统常用文件管理命令详解
    前言Ubuntu是一种广泛使用的Linux操作系统,提供了丰富而强大的文件管理命令,使用户能够通过命令行轻松管理文件和目录。本文将介绍一些常用的Ubuntu文件管理命令,帮助用户更好地理解和利用系统资源。ls-列出目录内容ls[选项][目录]ls命令用于列出目录中的文件和子目录。一些常用选......
  • 泛型编程
    泛型编程:使用模板,编写跟类型无关的代码。一个函数和类的时候,针对不同类型需要写很多重复的代码。函数:比如我们想实现交换int、double、char等等各种类型对象的函数swap类:比如想实现一个数据结构栈Stack,Stack的多个对象,st1存intst2存double,等等。在没有模板之前,我们得针对......
  • vue3 静态配置文件
    前言有时候我们负责做的只是一个大平台系统下的某个子系统,有时候我们需要调用这个大平台系统下别的子系统的接口。如果直接把别的接口地址,直接写在代码里,那么如果别的子系统的接口调整了,会影响到我们的系统,我们还要重新打包,部署,会很繁琐。解决思路我们的想法是这样:敲代码的......
  • 关于C#文件的上传和下载,文件流相关
    文件的上传和下载控制器:///<summary>///上传web文件///</summary>///<paramname="files"></param>///<paramname="wellName">井名</param>///<paramname="userName&quo......
  • beanshell导入java文件
    beanshell导入java文件beanshell可以读取class格式的文件步骤:    a、添加BeanShell预处理程序    b、请求调用   beanshell可以读取java格式的文件步骤:    a、添加BeanShell预处理程序    b、请求调用  ......
  • uniapp app安卓、ios文件选择 (上传pdf word video img )等
    1、hybrid 必须放在项目根目录下,不然会调用失效:如图 2、建立nvue 子窗体  代码:1<template>2<viewclass="nvue">3<textclass="popup-item"@click="clickfun">选择文件</text>4<textclass="ddddd......
  • 实验7 文件应用编程
    task4源代码1#include<stdio.h>2#include<stdlib.h>34intmain(){5FILE*fp;6charch;7intcnt=0;89fp=fopen("d:\\date\\data4.txt","r");10if(fp==NULL){11printf("......
  • 实验7
    #define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<stdio.h>#include<string.h>#defineN100#defineM100intmain(){FILE*fp;fp=fopen("data4.txt","r");if(fp==......
  • 实验7 文件应用编程
    一,实验目的二,实验准备三,实验内容1,实验任务1task1_1.c1//将图书信息写入文本文件data1.txt23#include<stdio.h>45#defineN8067typedefstruct{8charname[N];//书名9charauthor[N];//作者10}Book;1112intmain(){......
  • 实验七
    4.任务4#include<stdio.h>intmain(){inti=0;chars;FILE*fp;fp=fopen("D:\\hhh\\data4.txt","r");while(1){s=fgetc(fp);if(s==EOF){break;}elseif(s==......