C语言中结构体和共用体的示例分析

发布时间:2021-06-30 15:23:18 作者:小新
来源:亿速云 阅读:186

这篇文章给大家分享的是有关C语言中结构体和共用体的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

一、实验目的

二、实验内容

编写下列程序,然后上机调试运行。

  1. 对候选人得票的统计程序。设有3个候选人,每次输入一个得票的候选人的名字,要求最后输出各人得票结果。

  2. 编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据记录,每个记录包括num、name、score[3],用主函数输入这些记录,用print函数输出这些记录。

  3. 建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去。(选作)

三、实验记录

3.1 候选人选票统计

(1)源代码

# include <stdio.h>

typedef struct node
{
	char name;
	int cnt;
}candt;

int main(void)
{
	candt A,B,C;
	char vote;
	A.name='A',A.cnt=0;
	B.name='B',B.cnt=0;
	C.name='C',C.cnt=0;
	while(vote!='#')/*当输入为#时,表示投票结束。*/
	{
		printf("Please enter the candidate:\n");
		scanf("%c",&vote);
		getchar();
		switch(vote)
		{
		case 'A':A.cnt++;break;
		case 'B':B.cnt++;break;
		case 'C':C.cnt++;break;
		default:printf("Input error!\n");
		}
	}
	printf("A'note:%d\n",A.cnt);
	printf("B'note:%d\n",B.cnt);
	printf("C'note:%d\n",C.cnt);
	return 0;
}

(2)运行结果截图

C语言中结构体和共用体的示例分析

3.2 print函数

(一)源代码

# include <stdio.h>
# define N 5
struct student
{
	char num[6];
	char name[10];
	int score[4];
}stu[N];
void print(struct student stu[6]);
int main(void)
{
	int i,j;
	for(i=0;i<N;i++)
	{
		printf("\nInput data of student:\n");
		printf("NO.: ");
		scanf("%s",stu[i].num);
		printf("name: ");
		scanf("%s",stu[i].name);
		for(j=0;j<3;j++)
		{
			printf("score %d:",j+1);
			scanf("%d",&stu[i].score[j]);
		}
	}
	print(stu);
	return 0;
}
void print(struct student stu[6])
{
	int i,j;
	printf(" NO.      name    score1    score2    score3\n");
	for(i=0;i<N;i++)
	{
		printf("%5s%10s",stu[i].num,stu[i].name);
		for(j=0;j<3;j++)
			printf("%9d",stu[i].score[j]);
		printf("\n");
	}
}

(2)运行结果截图

C语言中结构体和共用体的示例分析

3.3 链表

(1)源代码

# include <stdio.h>
# include <malloc.h>
//定义了一个链表节点的数据类型
struct student
{
	char num[10];
	char name[6];
	char sex[2];
	int age;//数据域
	struct student *next; //指针域
}stu[10];
int main(void)
{
	struct student *p,*pt,*head;
	int i,length,iage,flag=1;
	int find=0;
	while(flag==1)
	{
		printf("Please enter the length of the list(<10):");
		scanf("%d",&length);
		if(length<10)
			flag=0;
	}
	//建立链表
	for(i=0;i<length;i++)
	{
		p=(struct student *)malloc(sizeof(struct student));
		if(i==0)
			head=pt=p;
		else
			pt->next=p;
		pt=p;
		printf("NO.:");
		scanf("%s",&p->num);
		printf("name:");
		scanf("%s",&p->name);
		printf("sex:");
		scanf("%s",&p->sex);
		printf("age:");
		scanf("%d",&p->age);
	}
	p->next=NULL;
	p=head;
	printf("\nNO.     name    sex   age\n");
	while(p!=NULL)
	{
		printf("%4s%8s%6s%6d\n",p->num,p->name,p->sex,p->age);
		p=p->next;
	}
	//删除结点
	printf("Input age:");
	scanf("%d",&iage);
	pt=head;
	p=pt;
	if(pt->age==iage)/*链头是待删元素*/
	{
		p=pt->next;
		head=pt=p;
		find=1;
	}
	else/*链头不是待删元素*/
		pt=pt->next;
	while(pt!=NULL)
	{
		if(pt->age==iage)
		{
			p->next=pt->next;
			find=1;
		}
		else
			p=pt;
		pt=pt->next;
	}
	if(!find)
		printf("Not found%d.\n",iage);
	p=head;
	printf("\nNO.     name    sex    age\n");
	while(p!=NULL)
	{
		printf("%4s%8s",p->num,p->name);
		printf("%6s%6d\n",p->sex,p->age);
		p=p->next;
	}
	return 0;
}

(2)运行结果截图

C语言中结构体和共用体的示例分析

感谢各位的阅读!关于“C语言中结构体和共用体的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. 字节对齐-------结构体、共用体
  2. 结构体和共用体

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c语言

上一篇:PHP中如何使用eval()函数

下一篇:PHP中怎么获取checkbox值

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》