c语言结构体怎么用函数实现?
这个程序输入输出互换都用函数怎么实现?实参形参该怎么定义,头都大了帮忙改下谢谢啦#include<stdio.h>#include<malloc.h>structstu/...
这个程序输入输出 互换 都用函数怎么实现?实参形参该怎么定义,头都大了 帮忙改下 谢谢啦
# include<stdio.h>
# include<malloc.h>
struct stu //结构体
{
int age;
float score;
char name[100];
};
void shuru (int len)
{
int i;
for(i = 0; i<len; i++)
{
printf("请输入第%d个学生的信息\n",i+1);
printf("name = ");
scanf("%s",pArr[i].name);
printf("age = ");
scanf("%d", &pArr[i].age);
printf("score = ");
scanf("%f", &pArr[i].score);
}
return;
}
int main(void)
{
int len ,i;
struct stu * pArr,t;
printf("请输入学生的个数:\n");
printf("人数= ");
scanf("%d",&len);
pArr = (struct stu *)malloc(len * sizeof(struct stu));
shuru(len);
for(i=0; i<len; i++)
{
if(pArr[i].score < pArr[i+1].score)
{
t = pArr[i+1];
pArr[i+1] = pArr[i];
pArr[i] = t;
}
}
i =0;
printf("\n\n排序后的学生信息为\n\n");
for(i=0; i<len; i++)
{
printf("name = %s\n",pArr[i].name);
printf("age = %d\n",pArr[i].age);
printf("score = %f\n",pArr[i].score);
printf("\n\n");
}
return 0;
} 展开
# include<stdio.h>
# include<malloc.h>
struct stu //结构体
{
int age;
float score;
char name[100];
};
void shuru (int len)
{
int i;
for(i = 0; i<len; i++)
{
printf("请输入第%d个学生的信息\n",i+1);
printf("name = ");
scanf("%s",pArr[i].name);
printf("age = ");
scanf("%d", &pArr[i].age);
printf("score = ");
scanf("%f", &pArr[i].score);
}
return;
}
int main(void)
{
int len ,i;
struct stu * pArr,t;
printf("请输入学生的个数:\n");
printf("人数= ");
scanf("%d",&len);
pArr = (struct stu *)malloc(len * sizeof(struct stu));
shuru(len);
for(i=0; i<len; i++)
{
if(pArr[i].score < pArr[i+1].score)
{
t = pArr[i+1];
pArr[i+1] = pArr[i];
pArr[i] = t;
}
}
i =0;
printf("\n\n排序后的学生信息为\n\n");
for(i=0; i<len; i++)
{
printf("name = %s\n",pArr[i].name);
printf("age = %d\n",pArr[i].age);
printf("score = %f\n",pArr[i].score);
printf("\n\n");
}
return 0;
} 展开
1个回答
展开全部
# include<stdio.h>
# include<malloc.h>
struct stu //结构体
{
int age;
float score;
char name[100];
};
void shuru (int len, stu * pArr)
{
int i;
for(i = 0; i<len; i++)
{
printf("请输入第%d个学生的信息\n",i+1);
printf("name = ");
scanf("%s",pArr[i].name);
printf("age = ");
scanf("%d", &pArr[i].age);
printf("score = ");
scanf("%f", &pArr[i].score);
}
return;
}
int main(void)
{
int len ,i;
struct stu * pArr,t;
printf("请输入学生的个数:\n");
printf("人数= ");
scanf("%d",&len);
pArr = (struct stu *)malloc(len * sizeof(struct stu));
shuru(len, pArr);
for(i=0; i<len; i++)
{
if(pArr[i].score < pArr[i+1].score)
{
t = pArr[i+1];
pArr[i+1] = pArr[i];
pArr[i] = t;
}
}
i =0;
printf("\n\n排序后的学生信息为\n\n");
for(i=0; i<len; i++)
{
printf("name = %s\n",pArr[i].name);
printf("age = %d\n",pArr[i].age);
printf("score = %f\n",pArr[i].score);
printf("\n\n");
}
return 0;
}
这样就行了,你的排序还没实现, 仿照输入的函数实现就OK
追问
嗯,谢谢 以前的知识全忘了 函数也要定义 结构行参就正确了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询