C语言——结构体

一、实验目的

  1. 了解开发环境
  2. 学习如何编辑、编译、连接和运行c程序
  3. 了解并掌握结构体的定义、申明及调用规则
  4. 理解结构体的应用

二、实验内容

【项目1—读程序】阅读下面的程序,体会结构体的应用方法。

  1. 体会结构体类型变量的定义方法
#include <stdio.h>
struct Student
{
	int num;
	char name[20];
	char sex;
	int age;
	float score;
	char addr[30];
};
int main()
{
	struct Student student1,student2;
	printf("%d\n",sizeof(student1));
	return 0; 
}

实验结果截图:

在这里插入图片描述

  1. 结构体作函数参数
#include <stdio.h>
struct Student
{
	int num;
	char name[20];
	char sex;
	int age;
	float score;
	char addr[30];
};
void print(struct Student s)
{
	printf("%d %s %c\n",s.num,s.name,s.sex);
	//可再加。。。
	return; 
}
int main()
{
	struct Student student1,student2;
	struct Student *p_stu;
	student1.num = 10001;
	student2.age = 120;
	p_stu = &student2;
	print(student1);
	print(*p_stu);
	
	return 0;
}

实验结果截图:
代码:

#include <stdio.h>
#include <string.h>
struct Student
{
    int num;
    char name[20];
    char sex[20];
    int age;
    double score;
    char addr[30];
};
void print(struct Student s)
{
    printf("%d %s %s\n",s.num,s.name,s.sex);
    return;
}
int main()
{
    struct Student student1,student2;
    struct Student *p_stu;
    student1.num=10001;
    strcpy(student1.name,"张三");
    strcpy(student1.sex,"male");
    student1.age=15;
    student2.num=20002;
    strcpy(student2.name,"李四");
    strcpy(student2.sex,"female");
    student2.age=120;
    p_stu=&student2;
    print(student1);
    print(*p_stu);
    return 0;
}

在这里插入图片描述

  1. 当结构体成员是数组
#include <stdio.h>
#include <string.h>
struct Student
{
	int num;
	char name[10];
	double score[3];
};
void print(struct Student);
int main()
{
	struct Student stu;
	stu.num = 12345;
	strcpy(stu.name,"Li Fung");
	stu.score[0] = 67.5;
	stu.score[1] = 89;
	stu.score[2] = 78.5;
	print(stu);
	printf("%d %s ",stu.num , stu.name);
	printf("%.1f %.1f %.1f\n",stu.score[0],stu.score[1],stu.score[2]);
	return 0;
}

实验结果截图:

在这里插入图片描述

  1. 结构体数组的应用。
#include <stdio.h>
#include <string.h>
typedef struct
{
	char name[20];
	int count; 
}Person;
int main()
{
	Person person[3] = {{"Li",0},{"Zhang",0},{"Fun",0}};
	int i, j;
	char name[20];
	//输入10人投票情况,并确定给3人中哪个计票
	for(i = 0;i < 10;i++)
	{
		scanf("%s",name);
		for(j = 0,j < 3;j++)
			if(strcmp(name,person[j].name) == 0)
				person[j].count++;
	}
	printf("\nResult:\n");
	//输出计票结果
	for(i = 0;i < 3; i++)
	{
		printf("%s: %d\n",person[i].name,person[i].count);
	}
	return 0;
}

实验结果截图:

方便输入投票姓名结果,将投票数由10改成了5
在这里插入图片描述

  1. 指向结构体变量的指针的应用
#include <stdio.h>
#include <string.h>
typedef struct
{
	int num;
	char name[12];
	char sex;
	float score;
};
int main()
{
	struct Student stu;
	stu.num = 10301;
	strcpy(stu.name,"Wang Fun");
	stu.sex = 'f';
	stu.score = 89.5;
	struct Student *p=&stu;
	printf("%d %s %c %.1f\n",stu.num,stu.name,stu.sex,stu.score);
	printf("%d %s %c %.1f\n",(*p).num,(*p).name,(*p).sex,(*p).score);
	printf("%d %s %c %.1f\n",p->num,p->name,p->sex,p->score);
	
	return 0;
}

实验结果截图:
在这里插入图片描述

  • 用指向结构体变量的指针做实参
    在这里插入图片描述

在这里插入图片描述
实验结果截图:
在这里插入图片描述
【项目2–编程题】简单链表的实现

用链表来进行学生信息管理,要求定义出该学生信息的结点的结构体(结点信息包括学号No Name ,还有一个存放下一个结点的地址的指针next),能查询学生信息,添加、删除学生。
在这里插入图片描述
源代码:

#include <stdio.h>
#include <string.h>
struct Student
{
    int no;
    char name[20];
    struct Student *next;
};
int main()
{
    struct Student a,b,c,*head,*p;
    a.no=10001;
    strcpy(a.name,"张三");
    b.no=10002;
    strcpy(b.name,"李四");
    c.no=10003;
    strcpy(c.name,"王二");
    head=&a;
    a.next=&b;
    b.next=&c;
    c.next=NULL;
    p=head;
    do
    {
        printf("%d %s\n",p->no,p->name);
        p=p->next;
    }
    while(p!=NULL);
    //添加
    printf("添加学生李华后输出全部学生\n");
    struct Student d,*s;
    s=&d;
    d.no=10004;
    strcpy(d.name,"李华");
    p=&b;
    s->next=p->next;
    p->next=s;
    p=head;
    do
    {
        printf("%d %s\n",p->no,p->name);
        p=p->next;
    }
    while(p!=NULL);
    //删除
    printf("删除学生李华后输出全部学生\n");
    struct Student *q;
    p=&b;
    q=p->next;
    p->next=q->next;
    p=head;
    do
    {
        printf("%d %s\n",p->no,p->name);
        p=p->next;
    }
    while(p!=NULL);
    printf("查询学生李四信息\n");
    p=head;
    while(strcmp("李四",p->name)!=0)
        p=p->next;
    printf("%d\n",p->no);
    return 0;
}

运行截图:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值