大学的计算机课程作业
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及文件读写就此完结。