欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C基础_复合类型

程序员文章站 2024-03-07 15:49:33
...

结构体

struct stul
{
	char name[21];
	float scores[3];
};
int main03()
{
	struct stul s[3];
	for (int i = 0; i < 3; i++)
	{
		printf("请输入学生姓名: 成绩: \n");
		scanf("%s%f%f%f", s[i].name, &s[i].scores[0], &s[i].scores[1], &s[i].scores[2]);
	}

	for (int i = 0; i < 3; i++)
	{
		printf("姓名:%s\n",s[i].name);
		printf("成绩:%.1f %.1f %.1f\n", s[i].scores[0], s[i].scores[1], s[i].scores[2]);
	}

}

堆空间开辟结构体

struct tec
{
	char *name;//4
	int age;//4
}t;

int main04()
{
	struct tec * p = (struct tec *)malloc(sizeof(t));
	p->name = (char*)malloc(sizeof(char) * 21);

	strcpy(p->name, "张三");
	p->age = 18;

	printf("%s  %d\n", p->name, p->age);

	if (p->name != NULL)
	{
		free(p->name);
		p->name = NULL;
	}
	if(p)
	{
		free(p);
		p = NULL;
	}

	printf("%d\n", sizeof(struct tec*));
		
}

结构体和函数

struct info
{
	char name[21];
	int age;
};

void fun01(struct info s)//结构体作为形参
{
	strcpy(s.name, "lisi");
	s.age = 20;

	printf("%s  %d\n", s.name, s.age);

}//形参

void fun02(struct info *s)//结构体指针作为形参
{
	strcpy(s->name, "lisi");
	s->age = 20;

	//printf("%s  %d\n", s.name, s.age);
}

struct info fun03()//结构体作为返回值
{
	struct info s;
	strcpy(s.name, "lisi");
	s.age = 20;

	return s;
};

struct info * fun04()//结构体指针作为返回值
{
	struct info s;
	strcpy(s.name, "lisi");
	s.age = 20;

	return &s;
};

int main05()
{
	struct info s = { "zhangsan",18 };

	fun02(&s);//实参

	printf("%s %d\n", s.name, s.age);

}

结构体嵌套结构体

struct info
{
	char name[21];
	int age;
};

void fun01(struct info s)//结构体作为形参
{
	strcpy(s.name, "lisi");
	s.age = 20;

	printf("%s  %d\n", s.name, s.age);

}//形参

void fun02(struct info *s)//结构体指针作为形参
{
	strcpy(s->name, "lisi");
	s->age = 20;

	//printf("%s  %d\n", s.name, s.age);
}

struct info fun03()//结构体作为返回值
{
	struct info s;
	strcpy(s.name, "lisi");
	s.age = 20;

	return s;
};

struct info * fun04()//结构体指针作为返回值
{
	struct info s;
	strcpy(s.name, "lisi");
	s.age = 20;

	return &s;
};

int main05()
{
	struct info s = { "zhangsan",18 };

	fun02(&s);//实参

	printf("%s %d\n", s.name, s.age);

}

共用体

//共用体 union:共用体名称 成员列表 共用体变量名
//少用 会很乱 优点是省内存
union vars
{
	double a;
	float b;
	int c;//如果是c[10]的话,大小就是40(8的倍数);如果是c[11]的话(44),大小就是48(加到8的倍数)
	short d;
	char f;
	//char arr[12];
}var;

int main08()
{
	printf("%d\n", sizeof(var));
	var.a = 100;
	var.b = 3.14;
	printf("%f\n", var.a);
	printf("%f\n", var.b);
	//最新读数准,之前的不准
}

枚举

/*
	用途(工作中常用):
	例 银行取钱

	插卡      比如 clo=0 代表插上
	输入密码  clo=1
	锁定      clo=2
	取款      clo=3
	查询      clo=4
	退卡      clo=5
	锁定解除

	例 游戏中
	移动      clo=10
	攻击	  clo=11  (自增长1)
	技能	  clo=20
	死亡	  clo=21

*/
enum colors
{
	red,blue,yellow,black,white,green
}clo;

int main09()
{
	int val;
	scanf("%d\n",&val);
	switch (val)
	{
	case red:
		printf("red");
		break;
	case blue:
		break;
	case yellow:
		break;
	case black:
		break;
	case white:
		break;
	case green:
		break;
	default:
		break;
	}
}

typedef

//用途:给长变量起名字  
//或用在头文件起名 直接在.c文件中#$include"studentsInfoList.h"引用
typedef unsigned long long ull;

struct studentsInfoList
{
	char name[20];
	char sex;

};
typedef struct studentsInfoList sf;

int main10()
{
	sf s1;
	s1.name;
	s1.sex;
}

学生成绩结构体函数实现

struct stu
{
	char* name;
	float* scores;

};

void bubble_sort(struct stu *p,int len)
{
	//冒泡排序
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j < len - i - 1; j++)
		{
			float sum1 = p[j].scores[0] + p[j].scores[1] + p[j].scores[2];
			float sum2 = p[j + 1].scores[0] + p[j + 1].scores[1] + p[j + 1].scores[2];
			if (sum1 > sum2)
			{
				struct stu temp = p[j];
				p[j] = p[j + 1];
				p[j + 1] = temp;
			}
		}
	}
}

int main06()
{
	struct stu* p = (struct stu*)malloc(sizeof(struct stu) * 3);
	for (int i = 0; i < 3; i++)
	{
		p[i].name = (char*)malloc(sizeof(char) * 21);
		p[i].scores = (float*)malloc(sizeof(float) * 3);

		printf("请输入学生姓名 成绩:\n");
		scanf("%s%f%f%f", p[i].name, &p[i].scores[0], &p[i].scores[1], &p[i].scores[2] );
	}

	bubble_sort(p, 3);

	for (int i = 0; i < 3; i++)
	{
		printf("姓名:%s\n", p[i].name);
		printf("成绩:%.1f %.1f %.1f\n", p[i].scores[0], p[i].scores[1], p[i].scores[2]);
	}

	//释放
	for (int i = 0; i < 3; i++)
	{
		free(p[i].name);
		free(p[i].scores);

	}
	free(p);
}

打字游戏

void tips()
{
	printf("======打字游戏=========\n");
	printf("======按任意键继续=====\n");
	printf("======按ESC退出========\n");
	char ch = _getch();
	if (ch == 27)
	{
		exit(0);
	}
}

void rand_ch(char* arr)
{
	srand((unsigned int)time(NULL));//随机
	for (int i = 0; i < 50; i++)
	{
		arr[i] = rand() % 26 + 'a';
	}
}

void printf_ch(char* arr)
{
	//变量 计时器 开始 结束   计数器 val
	unsigned int start_time;
	unsigned int end_time;
	int val = 0;
	for (int i = 0; i < 50; i++)
	{
		char ch = _getch();
		if (i == 0)
		{
			start_time = time(NULL);
		}
		if (ch == arr[i])
		{
			printf("%c", ch);
			val++;
		}
		else
		{
			printf("_");
		}
	}
	end_time = time(NULL);

	printf("用时:%d\n", end_time - start_time);
	printf("正确率:%.1f%%\n", val * 1.0 / 50 * 100);
	if (val * 1.0 / 50 * 100 > 80)
	{
		printf("厉害了我的哥!");
	}
}

int main11()
{
	//字库
	char arr[51];
	memset(arr, 0, 51);

	while (1)
	{
		//1.提示
		tips();
		//2.随机字符
		rand_ch(arr);
		printf("%s\n", arr);
		//3.输入字符
		printf_ch(arr);

	}
}
相关标签: c语言

上一篇: SpringBoot缓存应用实践

下一篇: 1121