首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

如果能帮小弟解决有关问题 分数大大的

2012-03-13 
如果能帮小弟解决问题 分数大大的小弟有个困难多时的问题:structstd{charname[20]intidintpost}struct

如果能帮小弟解决问题 分数大大的
小弟有个困难多时的问题:
struct   std
{
        char   name[20];
        int   id;
        int   post;
};
struct   student
{
int   xuehao;
char   xingming[21];
char   xingbie[6];
int   zongchengji;
int   youbian;
};
struct   student   asdf[3]={{123, "aa ",123,123},{321, "bb ",321,321},{456, "cc ",432,432}};


就简单这么说吧如何采用指针对他里面的元素进行排序呢?

[解决办法]
用冒泡法可以排序,这样可以吗?
int i,j;
struct student temp;
for(i=0;i <3;i++)//from big to small, ordered by xuehao;
{
for(j=i+1;j <3;j++)
{
if(asdf[i].xuehao <asdf[j].xuehao)
{
temp=asdf[i];
asdf[i]=asdf[j];
asdf[j]=temp;
}
}
}
for(i=0;i <3;i++)
{
printf( "%d\t%s\t%s\t%d\t\n ",asdf[i].xuehao,asdf[i].xingming,asdf[i].xingbie,asdf[i].youbian);
}
没有用指针,经常用不好
[解决办法]

//按学号排序
#include "stdio.h "
#include "stdlib.h "

typedef struct student
{
int xuehao;
char xingming[21];
char xingbie[6];
int zongchengji;
int youbian;
}Student;

main()
{
int i,j;
Student* p[3];
Student* temp;
struct student asdf[3]={{123, "aa ",123,123},{321, "bb ",321,321},{456, "cc ",432,432}};
for(i = 0; i < 3; i++)
p[i] = &asdf[i];

for(i = 0;i <3;i++)
for(j = i + 1; j <3;j++)
{
if(p[i]-> xuehao < p[j]-> xuehao)
{
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}

printf( "\n原序为:\n ");
for(i = 0;i < 3;i++)
{
printf( "xuehao=%d xingming =%s \n ", asdf[i].xuehao,asdf[i].xingming);

}


printf( "\n排序后为:\n ");
for(i = 0;i < 3;i++)
{
printf( "xuehao=%d xingming =%s \n ", p[i]-> xuehao,p[i]-> xingming);

}

getchar();

}
[解决办法]
struct student
{
int xuehao;
char xingming[21];
char xingbie[6];
int zongchengji;
int youbian;
bool operator < (const strudent &t)
{
return (this-> xuehao <t.xuehao);
}
};
struct student asdf[3]={{123, "aa ",123,123},{321, "bb ",321,321},{456, "cc ",432,432}};
vector <struct student> vss;
for (int i=0; i <3; i++)
{
vss.push_back(asdf[i]);
}
sort(vss.begin(), vss.end());

热点排行