那就这样吧e 2022-11-22 23:01 采纳率: 98.1%
浏览 2
已结题

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

img

img

img


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

  • 写回答

2条回答 默认 最新

  • 关注

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

    #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;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月1日
  • 已采纳回答 11月23日
  • 创建了问题 11月22日

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?