用函数以及指针来解决问题

img

img

img


这个是题目所要求的内容,要求用指针和函数来解决问题,大家看看该怎么做呢,对我来说有点困难

按照题目要求定义一维数组和二维数组,然后用指针访问数组元素
二维数组访问用行指针,之前给你写过代码,跟那个是一样的
代码如下:

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
void display(long* num, char(*name)[10], char(*cn)[10], float(*score)[3], int n)
{
    int i, j;
    float* tmp;
    printf("%-10s %-10s %-10s %-10s %-10s\n", "No.", "Name", "Computer", "Math", "English");
    for (i = 0; i < n; i++)
    {
        printf("%-10d %-10s", *(num + i), *(name + i));
        tmp = *(score + i);
        for (j = 0; j < 3; j++)
            printf(" %-10.1f", *(tmp + j));
        printf("\n");
    }
}


void query(long* num, char(*name)[10], char(*cn)[10], float(*score)[3], int n)
{
    int i, j, flag = 0;
    long id;
    float* tmp;
    printf("input a student ID: ");
    scanf("%ld", &id);
    for (i = 0; i < n; i++)
    {
        if (id == *(num + i))
        {
            flag = 1; //表示找到,输出学生信息
            printf("%-10s %-10s %-10s %-10s %-10s\n", "No.", "Name", "Computer", "Math", "English");
            printf("%-10d %-10s", *(num + i), *(name + i));
            tmp = *(score + i);
            for (j = 0; j < 3; j++)
                printf(" %-10.1f", *(tmp + j));
            printf("\n");
            break;
        }
    }
    if (flag == 0)
        printf("No such student\n"); //提示学生不存在
}

void update(long* num, char(*name)[10], char(*cn)[10], float(*score)[3], int n)
{
    int i, j, flag = 0;
    long id;
    float* tmp, sc;
    char kc[10] = { 0 };
    printf("input a student ID: ");
    scanf("%ld", &id);
    printf("input course name: ");
    scanf("%s", kc);
    for (i = 0; i < n; i++)
    {
        if (id == *(num + i))
        {
            flag = 1; //表示找到,输出学生信息
            printf("please input a score: ");
            scanf("%f", &sc);
            tmp = *(score + i);

            for (j = 0; j < 3; j++)
            {
                if (strcmp(kc, *(cn + j)) == 0)
                {
                    flag = 2;
                    *(tmp + j) = sc;
                    break;
                }
            }
            break;
        }
    }
    if (flag == 0)
        printf("No such student\n"); //提示学生不存在
    else if (flag == 1)
        printf("No such course\n"); //提示没有这门课程
    else
        display(num, name, cn, score, n);
}

void remove(long* num, char(*name)[10], char(*cn)[10], float(*score)[3], int* n)
{
    int i, j, k, flag = 0;
    long id;
    float* tmp1, * tmp2;
    printf("input a student ID: ");
    scanf("%ld", &id);
    for (i = 0; i < *n; i++)
    {
        if (id == *(num + i))
        {
            flag = 1; //表示找到,输出学生信息
            //前移
            for (j = i; j < *n - 1; j++)
            {
                *(num + j) = *(num + j + 1);
                strcpy(*(name + j), *(name + j + 1));
                tmp1 = *(score + j);
                tmp2 = *(score + j + 1);
                for (k = 0; k < 3; k++)
                    *(tmp1 + k) = *(tmp2 + k);
            }
            *n = (*n) - 1;
            break;
        }
    }
    if (flag == 0)
        printf("No such student\n"); //提示学生不存在
    else
        display(num, name, cn, score, *n); //显示删除后的信息
}

void add(long* num, char(*name)[10], char(*cn)[10], float(*score)[3], int* n)
{
    int i, flag = 0;
    long id;
    float* tmp;
    int n2 = *n;
    while (1)
    {
        printf("input a student ID: ");
        scanf("%ld", &id);
        for (i = 0; i < n2; i++)
        {
            if (id == *(num + i))
            {
                flag = 1; //表示找到,输出学生信息
                break;
            }
        }
        if (flag == 0)
            break;
        else
            printf("the Id has exist, please ");
    }
    *(num + n2) = id;
    printf("input student name: ");
    scanf("%s", *(name + n2));
    tmp = *(score + n2);
    printf("input Computer score: ");
    scanf("%f", (tmp + 0));
    printf("input Math score: ");
    scanf("%f", (tmp + 1));
    printf("input English score: ");
    scanf("%f", (tmp + 2));
    *n = (*n) + 1; //学生数量+1
    display(num, name, cn, score, *n);
}

int main()
{
    char c;
    long num[20] = { 20210101,20210102,20210103,20210104 };
    char name[20][10] = { "Stu_01","Stu_02","Stu_03","Stu_04" };
    char courseName[3][10] = { "Computer","Math","English" };
    float score[20][3] = { {60.5,97.0,90.5},{88.5,67.5,88.1},{72.3,75.0,92.0} ,{66.8,86.0,45.5} };
    int stuNmb = 4; //学生数量 

    printf("d. show all information\n");
    printf("q. search students' scores according to student ID numbers\n");
    printf("u. modify the students' scores according to student ID numbers and course name\n");
    printf("r. delete the students' information according to student ID numbers\n");
    printf("a. add a student's information\n");
    printf("e. exit\n");

    while ((c = getchar()) != 'e') //input e to end the program
    {
        if (c == 'd')
        {
            display(num, name, courseName, score, stuNmb);
        }
        else if (c == 'q')
        {
            query(num, name, courseName, score, stuNmb);
        }
        else if (c == 'u')
        {
            update(num, name, courseName, score, stuNmb);
        }
        else if (c == 'r')
        {
            remove(num, name, courseName, score, &stuNmb);
        }
        else if (c == 'a')
        {
            add(num, name, courseName, score, &stuNmb);
        }
    }
    return 0;
}

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632