Polito Computer Science

大学的计算机课程作业

lab 11 ex3

Write a C program that reads the contents of a file whose name is acquired from the command line. The number of rows in the file is up to 80 and each row of the file contains the following fields: <% pass rate>

Each field of the row is composed of up to 20 characters and contains no spaces. The program must print on the screen the following information: a) the name of the subject that assigns the maximum credits; b) for each academic period (considering maximum 4 ), the name of the subject that is most difficult to pass. Afterwards, the program will ask the user to enter a professor’s last name (maximum 20 characters) and print the following information: c) the sum of the credits assigned with the courses taught by this professor; d) the average pass rate of the courses taught by this professor.
Further insight: for points a) and b), consider the case that two or more subjects are assigned with the maximum credits and that two or more subjects in the same period have the lowest pass rate. The program must output the list of all the identified subjects.
The following is a possible example of the input file: HorseRiding Donato Cavallo 1 5 50
Rowing Remo Controcorrente 2 4 70
Speed Tina Svelta 1 10 80

题目分析与代码

考的是简单struct的应用,题目要求如下:
1.从命令行打开文件,并将信息放入struct,FILE *,fopen,fscanf的应用。
a.找出所有注册的科目中credits最大的一项;
b.对每一段学术时期,找出最难通过的科目;
Further insight:有重复的最大最小科目全部输出;
接下来需要用户输入一位教授的姓名:
c.找出该教授课程学分总和;
d.该教授的课程平均通过率;
a,b以及Further insight直接循环,c,d通过strcmp对比确定教授编号,然后求和就行了。
代码如下:

#include<stdio.h>
#define SIZE 10
#define NAMESIZE 25
typedef struct exam{
 char subject[20];
 char profname[20];
 char profsurname[20];
 char period[20];
 int credits;
 int passrate;
}exam;
int main(int argc, char *argv[]){
    struct exam exam0[SIZE];
    FILE *fd;
    if((fd=fopen(argv[1],"r"))==NULL){
        printf("Fail to open!\n");
        return 0;
    }
    for(int i=0;i<SIZE;i++){
      fscanf(fd,"%s %s %s %s %d %d",&exam0[i].subject,&exam0[i].profname,&exam0[i].profsurname,&exam0[i].period,&exam0[i].credits,&exam0[i].passrate);
    }
     if(exam0[i].period==1){
            if(exam0[i].passrate<min1){
                min1=exam0[i].passrate;
                a=i;
            }
        }
        else if(exam0[i].period==2){
            if(exam0[i].passrate<min2){
                min1=exam0[i].passrate;
                b=i;
            }
        }
          else if(exam0[i].period==3){
            if(exam0[i].passrate<min3){
                min1=exam0[i].passrate;
                c=i;
            }
        }
        else if(exam0[i].period==4){
            if(exam0[i].passrate<min4){
                min1=exam0[i].passrate;
                d=i;
            }
        }
        }
    printf("%s %s %s %s %s \n",exam0[h].subject,exam0[a].subject,exam0[b].subject,exam0[c].subject,exam0[d].subject);
    printf("put your teacher's name");
    char name[20];
    scanf("%s",&name);
    int credits_sum=0,pass_rate_sum=0,n=0;
    for(int i=0;i<SIZE;i++){
        if(!strcmp(name,exam0[i].profname)){
            for(int j=0;j<SIZE;j++){
                if(!strcmp(exam0[i].profname,exam0[j].profname)){
                    credits_sum+=exam0[j].credits;
                    pass_rate_sum+=exam0[j].passrate;
                    n=n+1;
                }
            }
        }
    }
    printf("%s %d %d",name,credits_sum,pass_rate_sum/n);}
    

处理问题b时暂时没有更好想法,暂时就用if_else了,后面几题明天在更。

lab11 ex5

Exercise 5. Write a C program for the management of an athletic contest. The program shall: a. read from a text file named contest.txt a non-ordered sequence of names with associated score. The names does not contain spaces and the score is an integer; it is assumed that there are at most 100 athletes; b. display on the screen the sequence of names in alphabetic order, with associated score; c. simultaneously, write the output into a text file named contest_ord.txt, of the same ordered data; d. display on the screen the names of the best three positions, without reordering the sequence.
Remember that the first classified is the athlete with the highest score, the second is the one with the highest score lower than the first one, and so on (in general, each time it should be searched for the maximum value, this must be lower than the maximum value found previously).
Note: it is suggested to perform an ordered insertion into an array of struct, as seen in the previous laboratory exercises.

题目分析及代码

四个问题:
1.打开文件读取信。
2.将运动员信息以词典序进行排序。
3.写入ordered_contest.txt.
4.按成绩选取前三名,此时不能再次给信息排序。
代码如下:

#include<stdio.h>
#define SIZE 10
typedef struct contest{
    char athletic_name[20];
    int score;
}contest;
int main(int argc,char *argv[]){
struct contest contest0[SIZE];
FILE *fd;
if((fd=fopen(argv[1],"r"))==NULL){
    printf("Fail to open!\n");
    return 0;
}
for(int i=0;i<SIZE;i++){
        fscanf(fd,"%s %d",&contest0[i].athletic_name,&contest0[i].score);
}
fclose(fd);
struct contest temp0;
for(int i=0;i<SIZE;i++){
    for(int j=0;j<i;j++){
        if(strcmp(contest0[i].athletic_name,contest0[j].athletic_name)<0){
           temp0=contest0[i];
           contest0[i]=contest0[j];
           contest0[j]=temp0;
        }
    }
}
for(int i=0;i<SIZE;i++){
    printf("%s %d\n",contest0[i].athletic_name,contest0[i].score);
}
FILE *fpwrite=fopen("ordered_context.txt","w+");
setbuf(fpwrite,NULL);
if(fpwrite==NULL){
    printf("Fail to open file");
    return 0;
}for(int i=0;i<SIZE;i++){
    fprintf(fpwrite,"%s %d",&contest0[i].athletic_name,&contest0[i].score);
    fflush(fpwrite);
  }fclose(fpwrite);
int a[3],b[3];
int max=0;
for(int i=0;i<3;i++){
    for(int j=0;j<SIZE;j++){
        if(max<contest0[j].score){
            max=contest0[j].score;
            a[i]=j;
            b[i]=max;}}
    contest0[a[i]].score=0;
    max=0;}
for(int i=0;i<3;i++){
    printf("%s %d\n",contest0[a[i]].athletic_name,b[i]);}
}

fprintf无法写入文件,新建一个项目复制进去又是可以写入的。。。。无语
第六题明天更新,这样struct及文件读写就此完结。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值