结构体的应用2020-11-12

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define NUM 4

struct student
{
	char *name;
	int age;
	char author[10];
};
void initcls(struct student *p, char *s)
{//用字符串内容初始化结构体内存
	int i = 0;
	char *t = strtok(s, ",;");
	printf("%s\n", t);
	while(t != NULL)
	{
		p[i].name = malloc(strlen(t)+1);//+1存'\0'
		strcpy(p[i].name, t);//name是一个指针,分配内存把name储存在t

		t = strtok(NULL, ",;");
		p[i].age = atoi(t);//字符串转换成整形的一个函数 

		t = strtok(NULL, ",;");
		strcpy(p[i].author, t);

		t = strtok(NULL, ",;");

		i++;
	}
}
/*int cmpname(struct student *s1,struct student *s2)
{
	return s1->name - s2->name;
}
int cmpage(struct student *s1,struct student *s2)
{
	return s1->age - s2->age;
}
int cmpscore(struct student *s1,struct student *s2)
{
	return s1->author - s2->author;
}
void sortcls(struct student *p, int (*fp)(struct student *,struct student *))
{//排序
	int i, j;
	for(i = 0; i < NUM-1; i++)
	{
		for(j = 0; j < NUM-i-1; j++)
		{
			if(fp(p+j,p+j+1)>0)
			{
				struct student tmp = p[j];
				p[j] = p[j+1];
				p[j+1] = tmp;
			}
		}
	}
}
*/void sortcls(struct student *p)
{//排序
	int i, j;
	for(i = 0; i < NUM-1; i++)
	{
		for(j = 0; j < NUM-i-1; j++)
		{
			if(strcmp(p[j].author, p[j+1].author) > 0)
		//	if(p[j].age > p[j+1].age)
		//	if(strcmp(p[j].name, p[j+1].name)>0)
			{
				struct student tmp = p[j];
				p[j] = p[j+1];
				p[j+1] = tmp;
			}
		}
	}
}
void showcls(struct student *p)
{
 	int i;
	for(i = 0; i < NUM; i++)
	{
		printf("name %s age %d score %s\n", (*(p+i)).name, p[i].age,(p+i)->author);
	}
}
void destory(struct student *p)
{//free(name)
	int i;
	for(i = 0;i < NUM; i++)
	{
		free(p[i].name);
		p[i].name = NULL;
	}
}

int main()
{
	char data[] = {"zhangsan,20,AAA; lisi,21,FFF; wangtao,18,BBB; jack,22,CCC"};
	struct student cls[NUM];
	initcls(cls, data); //初始化结构内存
//	sortcls(cls, cmpname); //排序
//	sortcls(cls, cmpage); //排序
//	sortcls(cls, cmpsauthor); //排序

	sortcls(cls); //排序
	showcls(cls); //输出
	destory(cls); //释放name

}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define NUM 4

struct student
{
	char *name;
	int age;
	char author[10];
};
void initcls(struct student *p, char **s)
{//用字符串内容初始化结构体内存
	int i = 0, j = 0;
	char arr[100];
	while(j < NUM)
	{
		strcpy(arr, s[j]);
		char *t = strtok(arr, ";");
		while(t != NULL)
		{
			p[i].name = malloc(strlen(t)+1);//+1存'\0'
			strcpy(p[i].name, t);//name是一个指针,分配内存把name储存在t
			t = strtok(NULL, ";");
			p[i].age = atoi(t);//字符串转换成整形的一个函数 
			t = strtok(NULL, ";");
			strcpy(p[i].author, t);
			t = strtok(NULL, ";");
			i++;
		}
	j++;
	}
}

int cmpname(struct student *s1,struct student *s2)
{
	return s1->name - s2->name;
}
int cmpage(struct student *s1,struct student *s2)
{
	return s1->age - s2->age;
}
int cmpscore(struct student *s1,struct student *s2)
{
	return s1->author - s2->author;
}
void sortcls(struct student *p, int (*fp)(struct student *,struct student *))
{//排序
	int i, j;
	for(i = 0; i < NUM-1; i++)
	{
		for(j = 0; j < NUM-i-1; j++)
		{
			if(fp(p+j,p+j+1)>0)
			{
				struct student tmp = p[j];
				p[j] = p[j+1];
				p[j+1] = tmp;
			}
		}
	}
}

/*void sortcls(struct student *p)
{//排序
	int i, j;
	for(i = 0; i < NUM-1; i++)
	{
		for(j = 0; j < NUM-i-1; j++)
		{
			if(strcmp(p[j].author, p[j+1].author) > 0)
		//	if(p[j].age > p[j+1].age)
		//	if(strcmp(p[j].name, p[j+1].name)>0)
			{
				struct student tmp = p[j];
				p[j] = p[j+1];
				p[j+1] = tmp;
			}
		}
	}
}*/
void showcls(struct student *p)
{
 	int i;
	for(i = 0; i < NUM; i++)
	{
		printf("name=%s age=%d author=%s\n", p[i].name, p[i].age, p[i].author);
		printf("===========\n");
	}
}
void free1(struct student *p)
{//free(name)
	int i;
	for(i = 0;i < NUM; i++)
	{
		free(p[i].name);
		p[i].name = NULL;
	}
}

int main()
{
	char *data[] = {"zhangsan; 20; AAA", "lisi; 21; FFF" ,"wangtao; 18; BBB", "jack; 22; CCC"};
	struct student cls[NUM];
	initcls(cls, data); //初始化结构内存
//	sortcls(cls, cmpname); //排序
	sortcls(cls, cmpage); //排序
//	sortcls(cls, cmpsauthor); //排序

//	sortcls(cls); //排序
	showcls(cls); //输出
	free1(cls); //释放name

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值