最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言中結(jié)構(gòu)體和共用體實例教程

 更新時間:2021年06月30日 12:00:02   作者:Jasmine-Lily  
這篇文章主要給大家介紹了關(guān)于C語言中結(jié)構(gòu)體和共用體的相關(guān)資料,結(jié)構(gòu)體是一種自定義的復合數(shù)據(jù)類型,共用體也叫聯(lián)合體,使幾個不同類型的變量共占一段內(nèi)存(相互覆蓋),需要的朋友可以參考下

一、實驗?zāi)康?br />

  • 掌握結(jié)構(gòu)體類型變量的定義和使用;
  • 掌握結(jié)構(gòu)體類型數(shù)組的概念和應(yīng)用;
  • 掌握鏈表的概念,初步學會對鏈表進行操作;
  • 掌握共用體的概念與使用;
  • 掌握指向結(jié)構(gòu)體變量的指針。
  • 掌握指向結(jié)構(gòu)體數(shù)組的指針的應(yīng)用。

二、實驗內(nèi)容

編寫下列程序,然后上機調(diào)試運行。

  1. 對候選人得票的統(tǒng)計程序。設(shè)有3個候選人,每次輸入一個得票的候選人的名字,要求最后輸出各人得票結(jié)果。
  2. 編寫一個函數(shù)print,打印一個學生的成績數(shù)組,該數(shù)組中有5個學生的數(shù)據(jù)記錄,每個記錄包括num、name、score[3],用主函數(shù)輸入這些記錄,用print函數(shù)輸出這些記錄。
  3. 建立一個鏈表,每個結(jié)點包括:學號、姓名、性別、年齡。輸入一個年齡,如果鏈表中的結(jié)點所包含的年齡等于此年齡,則將此結(jié)點刪去。(選作)

三、實驗記錄

3.1 候選人選票統(tǒng)計

(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!='#')/*當輸入為#時,表示投票結(jié)束。*/
	{
		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)運行結(jié)果截圖

3.2 print函數(shù)

(一)源代碼

# 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)運行結(jié)果截圖

3.3 鏈表

(1)源代碼

# include <stdio.h>
# include <malloc.h>
//定義了一個鏈表節(jié)點的數(shù)據(jù)類型
struct student
{
	char num[10];
	char name[6];
	char sex[2];
	int age;//數(shù)據(jù)域
	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;
	}
	//刪除結(jié)點
	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)運行結(jié)果截圖

總結(jié)

到此這篇關(guān)于C語言中結(jié)構(gòu)體和共用體的文章就介紹到這了,更多相關(guān)C語言結(jié)構(gòu)體和共用體內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

榕江县| 吐鲁番市| 同仁县| 施秉县| 诏安县| 大悟县| 宜章县| 贡觉县| 库车县| 凌源市| 宜黄县| 丽水市| 鄂托克前旗| 弥勒县| 连南| 伊通| 黔西| 探索| 荃湾区| 桃园县| 乌什县| 莲花县| 鄂伦春自治旗| 衡阳市| 扶余县| 余庆县| 封丘县| 呼图壁县| 集贤县| 永川市| 乐平市| 平江县| 资溪县| 汽车| 英山县| 行唐县| 鱼台县| 苏尼特右旗| 普格县| 永清县| 清新县|