学习目标:
掌握结构体的创立和使用
自由构建一个可以建立多种类型的结构体
例如:记录一个学生的基本信息(姓名,学号,年龄,分数)
struct Stu
{
char name[20];
int num;
int age;
float score;
}s1;
学习内容:
1.定义结构体类型变量
2.结构体变量的初始化和引用
3.定义结构体数组
4.结构体指针
5.用指针处理链表
//静态链表:
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
struct Stu
{
char name[20];
float score;
struct Stu* next;
};
int main()
{
struct Stu* head = NULL;
struct Stu* cur = NULL;
struct Stu s1 = { "张三",95.5 };
struct Stu s2 = { "李四",99.2 };
struct Stu s3 = { "王五",100.5 };
s1.next = &s2;
s2.next = &s3;
s3.next = NULL;
head = &s1;
cur = head;
while (cur != NULL)
{
printf("%s %.2f\n", cur->name, cur->score);
cur = cur->next;
}
return 0;
}
学习时间:2022年10月25日
学习产出:
1.一篇博客
2.一些例子(输入名字进行投票)
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
struct Stu
{
char name[20];
int count;
};
int main()
{
char name[20];
int j;
struct Stu a[3] = { { "zhangsan",0 }, { "lisi",0 },{"wangwu",0} };
printf("请输入你投票的人的名字:\n");
for (int i = 0; i < 5; i++)
{
scanf("%s", name);
for (j = 0; j < 3; j++)
{
if (0 == strcmp(a[j].name, name))
a[j].count++;
}
}
for (j = 0; j < 3; j++)
{
printf("%s: %d票\n", a[j].name, a[j].count);
}
return 0;
}