1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > c语言指针交换字符串组 c语言 指针交换两个字符串

c语言指针交换字符串组 c语言 指针交换两个字符串

时间:2021-03-27 11:06:22

相关推荐

c语言指针交换字符串组 c语言 指针交换两个字符串

其实这个题是要用结构把学生成绩输进去再根据程序排序再输出学生信息,我不知道怎么根据成绩大小把st[].name输出啊,我用了一个交换函数,change想交换st[j].name和st[j-1].name但是都不行,因为st[j].name是个常量?怎么交换?

#include

#define N 3

struct student

{

int number;

char name[20];

int result[3];

}st[3]={{1,"zhang"},{2,"li"},{3,"wang"}};

void getscore() //输成绩的函数

{

int i;

for(i=0; i

{

printf("请分别输入第%d个学生的数学,语文,英语成绩\n",i+1);

scanf("%d", &st[i].result[0]);

scanf("%d", &st[i].result[1]);

scanf("%d", &st[i].result[2]);

}

}

void change(char *str1,char *str2)

{

char *temp;

temp=str1;

str1=str2;

str2=temp;

}

void order(int index)

{

int i,j,temp1,temp2;

char *title[]={"数学","语文","英语"};

for(i=0; i

for(j=N-1;j>i;j--)

if(st[j].result[index]>st[j-1].result[index])

{

temp1=st[j].result[index];

st[j].result[index]=st[j-1].result[index];

st[j-1].result[index]=temp1;

temp2=st[j].number;

st[j].number=st[j-1].number;

st[j-1].number=temp2;

change(st[j].name,st[j-1].name);

}

for(i=0;i<3;i++)

printf("%s%d\n",st[i].name,st[i].number);

}

void main()

{

getscore();

printf("数学成绩从大到小学生姓名和学号依次是\n");

order(0);

printf("语文成绩从大到小学生姓名和学号依次是\n");

order(1);

printf("英语成绩从大到小学生姓名和学号依次是\n");

order(2);

}

嗯,这个对了,还有个问题,数学和英语成绩交换对了,但是第二个语文成绩是不随分数大小交换的,我明明按冒泡法遍历过数组了呀。。。为什么?

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。