大一C语言二维数组的运用

img

输入四名学生三门课程的成绩
计算每位学生的平均分
计算没门课的平均分
打印出成绩表
要求:成绩表上要有表头,例如姓名,学号,数学,语文,英语

代码如下:

#include<stdio.h>
int main()
{
    int stu[4][3];
    int i, j;
    float ave[4] = { 0 }, sum[4] = { 0 }, sum1[3] = { 0 }, ave1[3] = { 0 };
    char class[3][10] = { "数学","语文","英语" };
    char name[4][20] = { "张三","李四","王五","赵六" };
    for (i = 0; i < 4; i++)          //输入成绩
    {
        printf("请输入第%d名学生的成绩:\n", i + 1);
        for (j = 0; j < 3; j++)
        {
            printf("%s成绩:", class[j]);
            scanf_s("%d", &stu[i][j]);
            sum[i] += (double)stu[i][j];            //每人总分
            sum1[j] += (double)stu[i][j];            //每门课总分
        }
    }
    //计算每人平均分
    for (i = 0; i < 4; i++)
    {
        ave[i] = (double)sum[i] / 3.0;
    }
    //计算没门课平均分
    for (i = 0; i < 3; i++)
    {
        ave1[i] = (double)sum1[i] / 4.0;
    }
    printf("姓名\t\t学号\t\t数学\t\t语文\t\t英语\t\t平均分\n");
    for (i = 0; i < 4; i++)
    {
        printf("%s\t\t%d\t\t", name[i], i);
        for (j = 0; j < 3; j++)
            printf("%d\t\t", stu[i][j]);
        printf("%f\n", ave[i]);
    }
    printf("每门课平均成绩:\t\t");
    for (i = 0; i < 3; i++)
        printf("%f\t", ave1[i]);
}


输入输出:

img


//定义一个结构体
struct Node 
{
    struct student data;
    struct Node* next;
};
//创建链表头
struct Node* creat_Head_list()
{
    struct Node* Head_list = (struct Node*)malloc(sizeof(struct Node));
    Head_list->next = NULL;
    return Head_list;
}
//创建结点
struct Node* creat_node(struct student data)
{
    struct Node* next_node = (struct Node*)malloc(sizeof(struct Node));
    next_node->data = data;
    next_node->next = NULL;
    return next_node;
}
//打印链表
void print_list(struct Node* Head_list)
{
    struct Node* Head_list_next = Head_list->next;
    printf("姓名\t学号\t数学成绩\t语文成绩\t英语成绩\n");
    while (Head_list_next != NULL)
    {
        printf("%s\t%d\t%d\t\t%d\t\t%d\n", Head_list_next->data.name,Head_list_next->data.num,Head_list_next->data.math,Head_list_next->data.chinese,Head_list_next->data.english);
        Head_list_next = Head_list_next->next;
    }
}
//从头部添加结点
void inset_by_Head_list(struct Node* Head_list, struct student data)
{
    struct Node* inset_node = creat_node(data);
    inset_node->next = Head_list->next;
    Head_list->next = inset_node;
}
//删除指定的结点
void delet_node(struct Node* Head_list, int num)
{
    struct Node* Pmove_front = Head_list;
    struct Node* Pmove = Head_list->next;
    if (Pmove == NULL)
        return;
    else
        while (Pmove->data.num != num)
        {
            Pmove_front=Pmove;
            Pmove = Pmove_front->next;
            if (Pmove == NULL)
                return;
        }
        Pmove_front->next=Pmove->next;
        free(Pmove);
}
//修改指定位置的结点
void exchang_node(struct Node*Head_list,int num)
{
    struct Node*ex_node=Head_list->next;
    if(ex_node==NULL)
        return;
    else
        while(ex_node->data.num!=num)
        {
            ex_node=ex_node->next;
            if(ex_node==NULL)
                return;
        }
        printf("请进行修改数据,输入学生姓名,学号,数学,语文,英语成绩:\n");
        setbuf(stdin,NULL);
        scanf("%s%d%d%d%d",ex_node->data.name,&ex_node->data.num,&ex_node->data.math,&ex_node->data.chinese,&ex_node->data.english);
}
//保存链表信息到文件夹
void save_node(struct Node*Head_list)
{
    if (Head_list==NULL)
    {
        printf("节点为空,保存失败\n");
        return;
    }
    FILE*fp=fopen(Path,"w");
    if(fp==NULL)
    {
        printf("文件打开失败\n");
        return;
    }
    struct Node*p=Head_list->next;
    while(p!=NULL)
    {
        fwrite(&p->data,sizeof(struct student),1,fp);
        p=p->next;
    }
    fclose(fp);
    fp=NULL;

}