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

实验7 文件应用编程

时间:2023-12-18 22:47:05浏览次数:27  
标签:printf 文件 int 编程 st STU 实验 pass t%

1、实验4

源代码

 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5     FILE *fp;
 6     char ch;
 7     int i=0;
 8     fp=fopen("data4.txt","r");
 9     if(fp==NULL){
10         printf("fail to open file\n");
11         return 1;
12     }
13     while((ch=fgetc(fp))!=EOF){
14         if(ch != ' '&&ch !='\n')
15             i++;
16     }
17     fclose(fp);
18     printf("data4.txt中共包含字符数(不记空白符):%d\n",i);
19     return 0;
20  } 

运行结果

 

实验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     // 待补足
 98     // ×××
 99     int i,m=0;
100     for(i = 0;i<n;i++){
101         st[i].sum = st[i].objective+st[i].subjective;
102     }
103     for(i = 0;i < n;i++){
104         if(st[i].sum>=60){
105             strcpy(st[i].ans,"pass");
106             m++;
107         }
108         else{
109             strcpy(st[i].ans,"fail");
110         }
111     }
112     return m;
113     
114 }

 

运行结果

 

实验6

源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #include<string.h>
 5 #define N 80
 6 #define M 5
 7 
 8 typedef struct{
 9     long id;
10     char name[20];
11     char grade_class[20];
12 }STU;
13 
14 int main()
15 {
16     STU st[N],st_lucky[M];
17     int num[5];
18     int i;
19     FILE *fp;
20     fp = fopen("list.txt","r");
21     if(fp == NULL){
22         printf("fail to open file\n");
23         return 1;
24     }
25     while(!feof(fp)){
26         for(i=0;i<N;i++){
27             fscanf(fp,"%ld %s %s",&st[i].id,st[i].name,st[i].grade_class);
28         }
29     }
30     fclose(fp);
31     
32     srand((double)time(NULL));
33     for(i = 0;i<5;i++){
34         num[i] = rand()%80;
35     }
36     for(i =0;i < 5; i++){
37         st_lucky[i].id=st[num[i]].id;
38         strcpy(st_lucky[i].name,st[num[i]].name);
39         strcpy(st_lucky[i].grade_class,st[num[i]].grade_class);
40     }
41     
42     FILE *fnp;
43     fnp = fopen("lucky.txt","w");
44     if(fnp == NULL){
45         printf("fail to open file\n");
46         return 1;
47     }
48     
49     for(i = 0;i<5;i++){
50         printf("%-10ld%-10s%-10s\n",st_lucky[i].id,st_lucky[i].name,st_lucky[i].grade_class);
51         fprintf(fnp,"\n");
52     }
53     fclose(fnp);
54     for(i=0;i<5;i++){
55         printf("%-10ld%-10s%-10s\n",st_lucky[i].id,st_lucky[i].name,st_lucky[i].grade_class);
56         printf("\n");
57     }
58     return 0;
59 }

 

运行结果

 

 

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

相关文章

  • 文件系统和软硬链接(下)
    在这里我们首先学习两个新的知识点第一个:第二个:下面我们再来理解思考下面的四个问题:我们首先来解决第一个问题如果新建一个文件Linux系统会做什么呢?新建文件,系统会做什么当我们要新建一个文件的时候,我们肯定是在系统的一个文件路径下去新建的,这个路径能够帮助我们确定清楚这个文件......
  • 实验7
    task4#include<stdio.h>intmain(){FILE*fp;inti,k=0;chara[1000];fp=fopen("data4.txt","r");if(!fp){printf("找不到文件");return1;}for(i=0;!(feof(fp));i++){a[i]......
  • 自动化文件管理:使用Python创建匹配Excel数据的文本文件
    介绍在日常工作中,我们经常需要处理大量的数据和文件。尤其是在处理涉及多层嵌套目录和数据文件时,手动操作变得极其繁琐和耗时。为了提高效率,自动化这一过程显得尤为重要。本博客介绍了一个实用的Python脚本,它能够自动读取Excel表格中的数据,并在相应的文件夹中创建文本文件。这个......
  • 实验7 文件
    task41#include<stdio.h>2intmain()3{inti=0;charch;4FILE*fp;56fp=fopen("c:/aa/data4.txt","r");7if(fp==NULL){8printf("failtoopenfile\n");9return1;10}11while(1){12ch=fgetc(......
  • DataX配置文件生成脚本
    创建文件cd/opt/softwaremkdirgen_import_config.pyvimgen_import_config.pygen_import_config.py#coding=utf-8importjsonimportgetoptimportosimportsysimportMySQLdb#MySQL相关配置,需根据实际情况作出修改mysql_host="slave1"mysql_port="33......
  • 实验7 文件应用编程
    一、实验目的二、实验准备三、实验内容四、实验结论 1.实验任务4源代码:1#include<stdio.h>2#include<string.h>3#defineN100045intmain(){6FILE*fp;78fp=fopen("data4.txt","r");910if(fp==NULL){11......
  • 实验7
    任务4源码 1#define_CRT_SECURE_NO_WARNINGS2#include<stdio.h>3#include<string.h>4intmain()5{6FILE*fp;7fp=fopen("e:\\data4.txt","r");8if(fp==NULL)9{10printf("......
  • 自动化查找并记录含图片文件夹的Python脚本
    功能介绍此Python脚本用于遍历指定的父目录,自动识别并记录所有包含图片文件(如PNG、JPG、GIF等格式)的子文件夹。脚本运行后,将在父目录下生成一个名为“文件夹名统计”的Excel表格,其中列出了所有含有图片的文件夹名称。这对于整理大量分散在不同子文件夹中的图片文件特别有用,尤其是......
  • linux - 文件与目录权限的设置与修改
    1.文件权限说明[-][rwx][r-x][r--]文件权限分为四部分:1.第一部分表示文件类型,-代表普通文件,d代表目录,l代表链接文件2.第二部分表示文件所有者权限,r代表可读,w代表可写,x代表可执行3.第三部分表示所属用户组权限,r代表可读,x代表可执行4.第四部分表示其他用户权限,r......
  • C#文件上传和下载
    一.  在Form中一定要将encType设为"multipart/form-data": <formid="WebForm3"method="post"encType="multipart/form-data"runat="server"> 二.  判断是否有文件上传了: 当用户没有选择任何要上传的文件,即HtmlInputFile控件中的文本框为空时点击了上传按钮后,在服务......