#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
}