0802(结构体)

关于结构体的声明

#include <stdio.h>
#include <string.h>
void stu_test(void);

int main()
{
	stu_test();
	return 0;
} 

struct stu_info//只是声明,没有分配空间  不赋值 
{
	int id;
	char name[20];
	int age;
	char add[20];
}stu01,stu02={1002,"大坏坏他弟小坏坏",4,"M78星云天马座星系"},\
stu03,stu05;//定义变量stu02+初始化 定义结束后不可初始化 

void stu_test(void)
{//struct是关键字  stu_info是类型名   如没有类型名,为无名结构体或匿名结构体\
								只能声明类型时定义结构体变量 无法二次定义变量 
	struct stu_info//只是声明,没有分配空间  不赋值 
	{
		int id;
		char name[20];
		int age;
		char add[20];
	};
	struct stu_info stu01;//stu01是结构体变量名
	//初始化结构体变量
	stu01.id=1001;
	strcpy(stu01.name,"超级无敌大坏坏");
	stu01.age=3;
	strcpy(stu01.add,"凌霄宝殿隔壁的茅草屋");
	
	printf("stu01.id=%d\n",stu01.id); 
	printf("stu01.name=%s\n",stu01.name); 
	printf("stu01.age=%d\n",stu01.age); 
	printf("stu01.add=%s\n",stu01.add);
	printf("\n"); 
	
	printf("stu02.id=%d\n",stu02.id); 
	printf("stu02.name=%s\n",stu02.name); 
	printf("stu02.age=%d\n",stu02.age); 
	printf("stu02.add=%s\n",stu02.add); 
	printf("\n"); 
	
	printf("stu03.id=%d\n",stu03.id); 
	printf("stu03.name=%s\n",stu03.name); 
	printf("stu03.age=%d\n",stu03.age); 
	printf("stu03.add=%s\n",stu03.add);
	printf("\n");  
	
	struct stu_info stu04={2009,"大坏坏他姐翠花"};
	printf("stu04.id=%d\n",stu04.id); 
	printf("stu04.name=%s\n",stu04.name); 
	printf("stu04.age=%d\n",stu04.age); 
	printf("stu04.add=%s\n",stu04.add);
	printf("\n"); 
	
	stu05=stu02;//如定义之外重新定义了  不可复制 
	stu05.age+=1;//如果是指针变量,可以直接让指针变量指向字符串 \
					 如果用数组保存,必须使用strcpy进行拷贝 
	printf("一年后...\n"); 
	printf("stu05.id=%d\n",stu05.id); 
	printf("stu05.name=%s\n",stu05.name); 
	printf("stu05.age=%d\n",stu05.age); 
	printf("stu05.add=%s\n",stu05.add); 
	printf("\n"); 
}

结果

stu01.id=1001
stu01.name=超级无敌大坏坏
stu01.age=3
stu01.add=凌霄宝殿隔壁的茅草屋

stu02.id=1002
stu02.name=大坏坏他弟小坏坏
stu02.age=4
stu02.add=M78星云天马座星系

stu03.id=0
stu03.name=
stu03.age=0
stu03.add=

stu04.id=2009
stu04.name=大坏坏他姐翠花
stu04.age=0
stu04.add=

一年后...
stu05.id=1002
stu05.name=大坏坏他弟小坏坏
stu05.age=5
stu05.add=M78星云天马座星系


--------------------------------
Process exited after 0.09088 seconds with return value 0
请按任意键继续. . .

关于挑选赋值

#include <stdio.h>
#include <string.h>
void test(void); 

int main()
{
	test();
	return 0;
}

void test(void)
{
	struct stu_in
	{
		int id;
		char *name;
		int age;
		char add[20];
	}stu01={101,"小狗嘚",4,"狗窝"},stu02={.name="大黄",.add="首尔"};
	
	printf("stu01.id=%d\n",stu01.id); 
	printf("stu01.name=%s\n",stu01.name); 
	printf("stu01.age=%d\n",stu01.age); 
	printf("stu01.add=%s\n",stu01.add);
	printf("\n"); 
	
//	strcpy(stu01.name,"赛虎"); //编译五警告,程序崩溃  指针变量只能保存地址 
	stu01.name="赛虎"; 
	strcpy(stu01.add,"白宫"); 
	printf("stu01.id=%d\n",stu01.id); 
	printf("stu01.name=%s\n",stu01.name); 
	printf("stu01.age=%d\n",stu01.age); 
	printf("stu01.add=%s\n",stu01.add);
	printf("\n"); 
	
	printf("stu02.id=%d\n",stu02.id); 
	printf("stu02.name=%s\n",stu02.name); 
	printf("stu02.age=%d\n",stu02.age); 
	printf("stu02.add=%s\n",stu02.add); 
	printf("\n"); 
}

结果

stu01.id=101
stu01.name=小狗嘚
stu01.age=4
stu01.add=狗窝

stu01.id=101
stu01.name=赛虎
stu01.age=4
stu01.add=白宫

stu02.id=0
stu02.name=大黄
stu02.age=0
stu02.add=首尔


--------------------------------
Process exited after 0.3292 seconds with return value 0
请按任意键继续. . .

延伸数组挑选赋值

#include <stdio.h>

int main()
{
	int a[10]={1,2,0};
	int b[10]={1,2,[5]=6};
	int i;
	for(i=0;i<10;i++)
	{
		printf("a[%d]=%d  b[%d]=%d\n",i,a[i],i,b[i]);
	}
	
	return 0;
}

结果

a[0]=1  b[0]=1
a[1]=2  b[1]=2
a[2]=0  b[2]=0
a[3]=0  b[3]=0
a[4]=0  b[4]=0
a[5]=0  b[5]=6
a[6]=0  b[6]=0
a[7]=0  b[7]=0
a[8]=0  b[8]=0
a[9]=0  b[9]=0

--------------------------------
Process exited after 0.04913 seconds with return value 0
请按任意键继续. . .

结构体嵌套

#include <stdio.h>

int main()
{
	struct birth
	{
		int year;
		int month;
		int day;
	}; 
	
	struct stu
	{
		int id;
		char *name;
		char *sex;
		int age;
		char *add;
		long long int phone;
		long long int qq;
		struct birth bir;
	};
	
	struct stu stu01;
	stu01.id=1999;
//	strcpy(stu01.name,"恐怖大暴龙");
	stu01.name="恐怖大暴龙"; 
	stu01.age=501;
//	strcpy(stu01.sex,"丧尸");
	stu01.sex="丧尸"; 
//	strcpy(stu01.add,"白垩纪");
	stu01.add="白垩纪";
	stu01.phone=999999;
	stu01.qq=999999;
	stu01.bir.year=-500000;
	stu01.bir.month=3;
	stu01.bir.day=15; 
	
	printf("stu01.id=%d\n",stu01.id);
	printf("stu01.name=%s\n",stu01.name);
	printf("stu01.sex=%s\n",stu01.sex);
	printf("stu01.age=%d\n",stu01.age);
	printf("stu01.add=%s\n",stu01.add);
	printf("stu01.phone=%lld\n",stu01.phone);
	printf("stu01.qq=%lld\n",stu01.qq);
	printf("stu01.bir=%d-%d-%d\n",stu01.bir.year,stu01.bir.month,stu01.bir.day);
	
	return 0;
} 

结果

stu01.id=1999
stu01.name=恐怖大暴龙
stu01.sex=丧尸
stu01.age=501
stu01.add=白垩纪
stu01.phone=999999
stu01.qq=999999
stu01.bir=-500000-3-15

--------------------------------
Process exited after 0.09845 seconds with return value 0
请按任意键继续. . .

循环

#include <stdio.h>

int main()
{
	struct birth
	{
		int year;
		int month;
		int day;
	}; 
	
	struct stu
	{
		int id;
	//	char *name;//指针无空间  不可用gets 
		char name[20];
	//	char *sex;
		char sex[20];
		int age;
	//	char *add;
		char add[20];
		long long int phone;
		long long int qq;
		struct birth bir;
	};
	
	struct stu stu01;
	stu01.id=1999;
	strcpy(stu01.name,"恐怖大暴龙");
//	stu01.name="恐怖大暴龙"; 
	stu01.age=501;
	strcpy(stu01.sex,"丧尸");
//	stu01.sex="丧尸"; 
	strcpy(stu01.add,"白垩纪");
//	stu01.add="白垩纪";
	stu01.phone=999999;
	stu01.qq=999999;
	stu01.bir.year=-500000;
	stu01.bir.month=3;
	stu01.bir.day=15; 
	
	printf("stu01.id=%d\n",stu01.id);
	printf("stu01.name=%s\n",stu01.name);
	printf("stu01.sex=%s\n",stu01.sex);
	printf("stu01.age=%d\n",stu01.age);
	printf("stu01.add=%s\n",stu01.add);
	printf("stu01.phone=%lld\n",stu01.phone);
	printf("stu01.qq=%lld\n",stu01.qq);
	printf("stu01.bir=%d-%d-%d\n",stu01.bir.year,stu01.bir.month,stu01.bir.day);
	
	struct stu stu02[5];
	int i;
	for(i=0;i<5;i++)
	{
		printf("请输入第%d个学生的 id add age name\n",i+1);
		scanf("%d",&stu02[i].id);
		getchar();
		gets(stu02[i].add);
		scanf("%d",&stu02[i].age);
		getchar();
		gets(stu02[i].name);
	} 
	for(i=0;i<5;i++)
	{ 
		printf("第%d个学生的信息:",i+1);
		printf("%d-",stu02[i].id);
		printf("%s-",stu02[i].add);
		printf("%d-",stu02[i].age);
		printf("%s-",stu02[i].name);
		printf("\b \n");
		 
	}
	return 0;
} 

结果

stu01.id=1999
stu01.name=恐怖大暴龙
stu01.sex=丧尸
stu01.age=501
stu01.add=白垩纪
stu01.phone=999999
stu01.qq=999999
stu01.bir=-500000-3-15
请输入第1个学生的 id add age name
1001
纽约
82
漂亮国
请输入第2个学生的 id add age name
1002
首尔
73
小偷国
请输入第3个学生的 id add age name
1003
东京
65
本子
请输入第4个学生的 id add age name
1004
巴黎
95
辱法
请输入第5个学生的 id add age name
1005
新德里
66
阿三
第1个学生的信息:1001-纽约-82-漂亮国
第2个学生的信息:1002-首尔-73-小偷国
第3个学生的信息:1003-东京-65-本子
第4个学生的信息:1004-巴黎-95-辱法
第5个学生的信息:1005-新德里-66-阿三

--------------------------------
Process exited after 91.78 seconds with return value 0
请按任意键继续. . .

结构体排序

#include <stdio.h>

struct stu_score
	{
		int id;
		char name[20];
		float score;
	};
	
void gett(struct stu_score stu[],int n)
{
	int i;
	for(i=0;i<n;i++)
	{
		printf("请输入第%d个学生的学号 姓名 成绩,回车隔开:\n",i+1);
		scanf("%d",&stu[i].id);
		getchar();
		gets(stu[i].name);
		scanf("%f",&stu[i].score);
	} 
	
}

void print(struct stu_score stu[],int n)
{
	int i;
	for(i=0;i<n;i++)
	{
		printf("第%d位学生的信息为:%d %s %.2f\n",i+1,stu[i].id,stu[i].name,stu[i].score);
	}
	printf("\n");
}

void cmp(struct stu_score stu[],int n)
{
	struct stu_score sd;
	int i,j;
	for(i=0;i<n-1;i++)
	{
		for(j=0;j<n-1-i;j++)
		{
			if(stu[j].score<stu[j+1].score)
			{
				sd=stu[j];
				stu[j]=stu[j+1];
				stu[j+1]=sd;
			}
		}
	}
}
	
int main()
{
	int n;
	printf("请输入学生人数:");
	scanf("%d",&n);
	struct stu_score stu[n];
	gett(stu,n);
	print(stu,n);
	cmp(stu,n);
	print(stu,n);
	
	return 0;
}

结果

请输入学生人数:5
请输入第1个学生的学号 姓名 成绩,回车隔开:
101
普京
999
请输入第2个学生的学号 姓名 成绩,回车隔开:
102
奥巴马
54.5
请输入第3个学生的学号 姓名 成绩,回车隔开:
103
白等
65.2
请输入第4个学生的学号 姓名 成绩,回车隔开:
104
特离谱
131.45
请输入第5个学生的学号 姓名 成绩,回车隔开:
105
安倍
77.3
第1位学生的信息为:101 普京 999.00
第2位学生的信息为:102 奥巴马 54.50
第3位学生的信息为:103 白等 65.20
第4位学生的信息为:104 特离谱 131.45
第5位学生的信息为:105 安倍 77.30

第1位学生的信息为:101 普京 999.00
第2位学生的信息为:104 特离谱 131.45
第3位学生的信息为:105 安倍 77.30
第4位学生的信息为:103 白等 65.20
第5位学生的信息为:102 奥巴马 54.50


--------------------------------
Process exited after 72.66 seconds with return value 0
请按任意键继续. . .

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值