首页 > 编程语言 >2022-2023-1学期 20221417 《计算机基础与程序设计》第15周学习总结

2022-2023-1学期 20221417 《计算机基础与程序设计》第15周学习总结

时间:2022-12-11 20:57:01浏览次数:87  
标签:15 student int 20221417 stu score every 2022 90

作业信息

这个作业属于哪个课程 <班级的链接>(如2022-2023-1-计算机基础与程序设计
这个作业要求在哪里 <作业要求的链接>(如2022-2023-1计算机基础与程序设计第十五周作业)
这个作业的目标 <>
作业正文 ... 本博客链接
要求
·课程总结:这个总结写的好的会被选出来出成一本博客书,大家认真写一下,被选中的会送你一本。
·文中的链接都要配上二维码,可以使用QQ中的工具或博客园的功能,点击自己每篇文章的最下面的微信图标就有二维码:

教材学习内容总结

梳理一下第十三章文件操作的一些函数
就当作复习一下吧

函数名 用法/原型
函数fopen() 用来打开文件 FILE *fopen(const char *filename,const char *mode)
函数fclose() 用来关闭有fopen打开的文件 int fclose(FILE *fp)
函数fgetc() 用于一个只读或读写方式打开的文件上读字符 int fgetc(FILE *fp)
函数 fputc() 用于将一个字符写到一个文件上
函数 feof() 检查是否到达文件末尾
函数 isprint() 判断是否为可打印字符
其余见图

其他常用函数
pow

字符串处理函数

calloc等

其他(感悟、思考等,可选)

上次小测验遇到一个将字符“1”“2”“3”转成数字123并且改用八进制的题没做对,其中涉及了《C语言程序设计》教材后附录的一些函数的使用
比如pow用来转化进制
可见附录的函数虽然没有强调但是也很重要。因此要认真对待。

实验六

点击查看代码
#include<string.h>
#define N 40
typedef struct student
{
    long studentID;
    char studentName[10];
    int score[3];
    float ave;
} STUDENT;
int menu();
void input(STUDENT stu[],int n,int m);
void calc(STUDENT stu[],int n,int m);
void sortascending(STUDENT stu[],int n);
void sortname(STUDENT stu[],int n);
int search(STUDENT stu[],int n);
void prints(STUDENT stu[],int i);
void writetofile(STUDENT stu[],int n);
int readfrom(STUDENT stu[]);
void printScore(STUDENT stu[],int n,int m);
int main()
{
    int i;
    char select;
    STUDENT stu[N];
    int n;
    printf("How many students?\n");
    scanf("%d",&n);
    while(1)
    {
        select=menu();
        switch(select)
        {
            case 1:
                input(stu,n,3);
                break;
            case 2:
                calc(stu,n,3);
                printScore(stu,n,3);
                break;
            case 3:
                sortascending(stu,n);
                printScore(stu,n,3);
                break;
            case 4:
                sortname(stu,n);
                printScore(stu,n,3);
                break;
            case 5:
                calc(stu,n,3);
                i=search(stu,n);
                prints(stu,i);
                break;
            case 6:
                writetofile(stu,n);
                break;
            case 7:
                n=readfrom(stu);
                printScore(stu,n,3);
                break;
            case 0:
                printf("End");
                exit(0);
            default:
                printf("Error!");

        }
    }

}
int menu()
{
    int select;
    printf("\n1.Append record\n");
    printf("2.Caculate total and average score of every student\n");
    printf("3.Sort in ascending order by total score of every student\n");
    printf("4.Sort in dictionary order by name\n");
    printf("5.Search by name\n");
    printf("6.Write to a file\n");
    printf("7.Read from a file\n");
    printf("0.Exit\n");
    printf("Please Input your choice:\n");
    scanf("%d",&select);
    return select;
}
void input(STUDENT stu[],int n,int m)
{
    int a,b;
    for(a=0; a<n; a++)
    {
        printf("Input record %d:\n",a+1);
        scanf("%ld",&stu[a].studentID);
        scanf("%s",stu[a].studentName);
        for(b=0; b<m; b++)
        {
            scanf("%d",&stu[a].score[b]);
        }
    }
}
void calc(STUDENT stu[],int n,int m)
{
    int i,j,sum;
    for(i=0;i<n;i++)
    {
        sum=0;
        for(j=0;j<m;j++)
        {
            sum+=stu[i].score[j];
        }
        stu[i].ave=(float)sum/m;
    }

}
void sortascending(STUDENT stu[],int n)
{
    int i,j,k,z,y;
    long x;
    float m;
    char temp[10];
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(stu[i].ave>stu[j].ave)
            {
                strcpy(temp,stu[i].studentName);
                strcpy(stu[i].studentName,stu[j].studentName);
                strcpy(stu[j].studentName,temp);

                x=stu[i].studentID;
                stu[i].studentID=stu[j].studentID;
                stu[j].studentID=x;

                m=stu[i].ave;
                stu[i].ave=stu[j].ave;
                stu[j].ave=m;
                for(k=0;k<3;k++)
                {
                    y=stu[i].score[k];
                    stu[i].score[k]=stu[j].score[k];
                    stu[j].score[k]=y;
                }
            }
        }
    }

}
void sortname(STUDENT stu[],int n)
{
    int i,j,k,y;
    long x;

    float m;
    char temp[10];
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(strcmp(stu[j].studentName,stu[i].studentName)<0)
            {
                strcpy(temp,stu[i].studentName);
                strcpy(stu[i].studentName,stu[j].studentName);
                strcpy(stu[j].studentName,temp);

                x=stu[i].studentID;
                stu[i].studentID=stu[j].studentID;
                stu[j].studentID=x;

                for(k=0;k<3;k++)
                {
                    y=stu[i].score[k];
                    stu[i].score[k]=stu[j].score[k];
                    stu[j].score[k]=y;
                }
                m=stu[i].ave;
                stu[i].ave=stu[j].ave;
                stu[j].ave=m;
            }
        }
    }

}
int search(STUDENT stu[],int n)
{
    int i;
    char x[10];
    scanf("%s",&x);
    for(i=0;i<n;i++)
    {
        if(stu[i].studentName==x) return i+1;
    }
}

void prints(STUDENT stu[],int i)
{
    int j;
    printf("%d\t%10ld",i,stu[i-1].studentID);
    for(j=0;j<3;j++)
    {
        printf("%4d",stu[i-1].score[j]);
    }
    float x=3*stu[i-1].ave;
    printf("%6.1f\t%6.1f\n",x,stu[i-1].ave);
}

void writetofile(STUDENT stu[],int n)
{
    FILE *fp;
    if((fp=fopen("score.txt","w"))==NULL)
    {
        printf("Failure to open score.txt!\n");
        exit(0);
    }
    fwrite(stu,sizeof(STUDENT),n,fp);
    fclose(fp);
}
int readfrom(STUDENT stu[])
{
    FILE *fp;
    int i;
    if((fp=fopen("score.txt","r"))==NULL)
    {
        printf("Failure to open score.txt!\n");
        exit(0);
    }
    for(i=0;!feof(fp);i++)
    {
        fread(&stu[i],sizeof(STUDENT),1,fp);
    }
    fclose(fp);
    printf("Total students is %d.\n",i-1);
    return i-1;
}
void printScore(STUDENT stu[],int n,int m)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        printf("%d%10ld%8s",i+1,stu[i].studentID,
               stu[i].studentName);
        for(j=0;j<m;j++)
        {
            printf("%4d",stu[i].score[j]);
        }
        printf("%6.1f\t%6.1f\n",3*stu[i].ave,stu[i].ave);
    }

}

运行结果

点击查看代码
[root@ecs-20221417 ~]# ls
a.out  expersix.c  main.c  project.two  score.txt  test
[root@ecs-20221417 ~]# gcc -mabi=lp64 -march=armv8-a   -o  exper expersix.c -g
[root@ecs-20221417 ~]# ls
a.out  exper  expersix.c  main.c  project.two  score.txt  test
[root@ecs-20221417 ~]# ./test
-bash: ./test: Is a directory
[root@ecs-20221417 ~]# ./exper
How many students?
4

1.Append record
2.Caculate total and average score of every student
3.Sort in ascending order by total score of every student
4.Sort in dictionary order by name
5.Search by name
6.Write to a file
7.Read from a file
0.Exit
Please Input your choice:
1
Input record 1:
20221401 汪汪 85 90 95
Input record 2:
20221403 卷卷 90 95 86
Input record 3:
20221407 瑶瑶 97 98 90
Input record 4:
20221414 露露 90 95 83

1.Append record
2.Caculate total and average score of every student
3.Sort in ascending order by total score of every student
4.Sort in dictionary order by name
5.Search by name
6.Write to a file
7.Read from a file
0.Exit
Please Input your choice:
2
1  20221401  汪汪  85  90  95 270.0       90.0
2  20221403  卷卷  90  95  86 271.0       90.3
3  20221407  瑶瑶  97  98  90 285.0       95.0
4  20221414  露露  90  95  83 268.0       89.3

1.Append record
2.Caculate total and average score of every student
3.Sort in ascending order by total score of every student
4.Sort in dictionary order by name
5.Search by name
6.Write to a file
7.Read from a file
0.Exit
Please Input your choice:
3
1  20221414  露露  90  95  83 268.0       89.3
2  20221401  汪汪  85  90  95 270.0       90.0
3  20221403  卷卷  90  95  86 271.0       90.3
4  20221407  瑶瑶  97  98  90 285.0       95.0

1.Append record
2.Caculate total and average score of every student
3.Sort in ascending order by total score of every student
4.Sort in dictionary order by name
5.Search by name
6.Write to a file
7.Read from a file
0.Exit
Please Input your choice:
4
1  20221403  卷卷  90  95  86 271.0       90.3
2  20221401  汪汪  85  90  95 270.0       90.0
3  20221407  瑶瑶  97  98  90 285.0       95.0
4  20221414  露露  90  95  83 268.0       89.3

1.Append record
2.Caculate total and average score of every student
3.Sort in ascending order by total score of every student
4.Sort in dictionary order by name
5.Search by name
6.Write to a file
7.Read from a file
0.Exit
Please Input your choice:
5
露露
4         20221414  90  95  83 268.0      89.3

1.Append record
2.Caculate total and average score of every student
3.Sort in ascending order by total score of every student
4.Sort in dictionary order by name
5.Search by name
6.Write to a file
7.Read from a file
0.Exit
Please Input your choice:
6

1.Append record
2.Caculate total and average score of every student
3.Sort in ascending order by total score of every student
4.Sort in dictionary order by name
5.Search by name
6.Write to a file
7.Read from a file
0.Exit
Please Input your choice:
7
Total students is 4.
1  20221403  卷卷  90  95  86 271.0       90.3
2  20221401  汪汪  85  90  95 270.0       90.0
3  20221407  瑶瑶  97  98  90 285.0       95.0
4  20221414  露露  90  95  83 268.0       89.3

1.Append record
2.Caculate total and average score of every student
3.Sort in ascending order by total score of every student
4.Sort in dictionary order by name
5.Search by name
6.Write to a file
7.Read from a file
0.Exit
Please Input your choice:
0
##实验过程的问题及解决方案 问题一:在用名字查询成绩时出现乱码 ![](/i/l/?n=23&i=blog/2967912/202212/2967912-20221211204015119-1025200040.png) 后来发现时prints函数输出时的下标越界访问。 要将返回值-1作为下标。 改正后输出无误。 问题二:输出时为何用"%d\t%10ld","%4d","%6.1f\t%6.1f\n" 当然书上有提过“%02d”的前导符0表示输出时如果左边有多余位则补零。 我突然想起了在C语言数据的格式化屏幕输出中讲过的格式修饰符的概念。L后加d,o,x,u时,用于输出long型数据。m为整数表示输出域宽指定输出行所占的列数。而.n(大于或等于零的整数)为显示精度,位于最小域宽修饰符之后。对于浮点数,它用于指定浮点数的小数位数;对于字符串,用于指定从字符串左侧开始截取的子串字符个数。 在此,我发现了我在课程学习最前面的章节的遗漏。这也让我更加体会到这次实验,以及平常写代码积累的重要。

学习进度条

代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长
目标 5000行 30篇 400小时
第十四周 250/3550 2/30 24/295
第十五周周 3500 2/4 18/38

参考资料

标签:15,student,int,20221417,stu,score,every,2022,90
From: https://www.cnblogs.com/20031004wzy/p/16974311.html

相关文章