学习笔记1:结构指针变量作函数参数

题目:有一个结构体变量stu,内涵学生学号、姓名和三门课的成绩。通过调用函数print中将他们输出

答案1:用结构体变量的成员作参数

#include <stdio.h>
#include <string.h>  
//犹豫下方用到字符串的拷贝,所以要定义在头文件里
struct student  //定义结构体student
{
    int num;
    char name[20];
    float score[3];
};
void print(struct student);  
//定义print函数,结构体作为他的形参
void main()
{
    struct student stu;  
//定义结构体变量的名称为stu
    /*下方赋值*/
    stu.num = 8;
    strcpy(stu.name, "King");  
//stu.name= "I'm King!";   strcopy表示字符串拷贝,把I'm King!拷贝到stu.name里面
    stu.score[0] = 98.5;
    stu.score[1] = 99.0;
    stu.score[2] = 99.5;

    print(stu);  //调用print函数打印出结构,把实参传递给下方
}
void print(struct student stu)
{
    printf("\tnum     : %d\n", stu.num);
    printf("\tname    : %s\n", stu.name);
    printf("\tscore_1 : %5.2f\n", stu.score[0]);
    printf("\tscore_2 : %5.2f\n", stu.score[1]);
    printf("\tscore_3 : %5.2f\n", stu.score[2]);
    printf("\n");
}

答案2:用结构体变量作实参

#include <stdio.h>
#include <string.h>  
//犹豫下方用到字符串的拷贝,所以要定义在头文件里
struct student  //定义结构体student
{
    int num;
    char name[20];
    float score[3];
};
void print(struct student *);  
//定义print函数,结构体指针作为他的形参
void main()
{
    struct student stu;  
//定义结构体变量的名称为stu
    /*下方赋值*/
    stu.num = 8;
    strcpy(stu.name, "King");  
//stu.name= "I'm King!";   strcopy表示字符串拷贝,把I'm King!拷贝到stu.name里面
    stu.score[0] = 98.5;
    stu.score[1] = 99.0;
    stu.score[2] = 99.5;

    print(&stu);  //调用print函数打印出结构,把结构体的地址传递给指针
}
void print(struct student *p)
//指针在这里接收结构体的地址
{
    printf("\tnum     : %d\n", p->num);
    printf("\tname    : %s\n", p->name);
    printf("\tscore_1 : %5.2f\n", p->score[0]);
    printf("\tscore_2 : %5.2f\n", p->score[1]);
    printf("\tscore_3 : %5.2f\n", p->score[2]);
    printf("\n");
}

运行结果:

进度日志31 (结构指针变量作函数参数,链表)

学习笔记2:链表

编程1:根据下图建立链表:
进度日志31 (结构指针变量作函数参数,链表)

(横看)有三个节点,(竖看)每个节点有三个元素

编程:

#include <stdio.h>
struct student
{
    long num;
    float score;
    struct student *next;
 //因为下一个指向的是跟这个结构体相同结构的东西,所以定义一个指向一个指向结构的指针
};
void main()
{
    struct student a, b, c, *head;
    a.num = 10101;
    a.score = 89.5;
    b.num = 10103;
    b.score = 90;
    c.num = 10107;
    c.score = 85;

    head = &a; //head是头指针,指向链表的第一个元素
    a.next = &b;
    b.next = &c;
    c.next = NULL;

    do
    {
        printf("%ld %5.1f\n", head->num, head->score);
        head = head->next;
    } while (head != NULL);
}

运行结果:

进度日志31 (结构指针变量作函数参数,链表)

编程2:建立(含有学生学号、成绩数据的)单向动态链表

分析图 

进度日志31 (结构指针变量作函数参数,链表)   进度日志31 (结构指针变量作函数参数,链表)

#include <stdio.h>
#include<malloc.h>
#include <stdlib.h>

#define LEN sizeof(struct student) //student结构的大小

struct student *creat(); //创建链表函数
void print(struct student *head);   //打印链表函数

struct student
{
    int num;
    float score;
    struct student *next;
};

int n; //全局变量,用来记录存放了多少数据

void main()
{
    struct student *stu;

    stu = creat();
    print(stu);
    printf("\n\n");
    system("pause");
}
struct student *creat()
{
    struct student *head;
    struct student *p1, *p2;

    p1 = p2 = (struct student *)malloc(LEN);  //LEN是student结构的大小

    printf("Please enter the num :");
    scanf("%d", &p1->num);
    printf("Please enter the score :");
    scanf("%f", &p1->score);

    head = NULL;
    n = 0;

    while (p1->num)
    {
        n++;
        if (n == 1)
        {
            head = p1;
        }
        else
        {
            p2->next = p1;
        }
        p2 = p1;
        p1 = (struct student *)malloc(LEN);

        printf("\nPlease enter the num :");
        scanf("%d", &p1->num);
        printf("Please enter the score :");
        scanf("%f", &p1->score);
    }
    p2->next = NULL;

    return head;
}
void print(struct student *head)
{
    struct student *p;
    printf("\nThere are %d records!\n\n", n);

    p = head;
    if (NULL != head)
    {
        do
        {
            printf("学号为 %d 的成绩是: %f\n", p->num, p->score);
            p = p->next;
        } while (NULL != p);
    }
}

运行结果:

进度日志31 (结构指针变量作函数参数,链表)

相关文章:

  • 2021-12-17
  • 2021-11-29
  • 2022-12-23
  • 2021-07-01
  • 2021-09-16
  • 2021-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
  • 2021-05-28
  • 2022-12-23
  • 2021-04-26
  • 2022-12-23
相关资源
相似解决方案