设为首页 加入收藏

TOP

问题一百三十八:指针操作学生结构体
2014-11-23 22:13:06 来源: 作者: 【 】 浏览:0
Tags:问题 一百三十八 指针 操作 学生 结构

[plain] #include
#include

void print(struct students *p); //打印函数print的定义
void max_score(struct students *p); //函数max的定义

struct students //结构体students的定义
{
char number[15];
float score;
};

int main(int argc, char *argv[])
{
struct students num[5]={{"121", 88.8}, {"122", 77.7}, {"123", 48.4}, {"124", 99.1}, {"125", 78.3}};

print(num);
max_score(num);

system("PAUSE");
return 0;
}

// Definition of function

//**print
void print(struct students *p)
{
int i;

i=0;

while(i<5)
{
printf("%d student scored is %0.2f\n", i+1, p[i]);
i+=2;
}
}

// ** max_score
void max_score(struct students *p)
{
struct students t; // 用于保存最大数
int i;
int j;

for(i=0; i<4; i++) //按分数排序
{
for(j=0; j<5; j++)
{
if( p[i].score> p[j].score)
{
t= p[i]; //学号和分数也交换
p[i]=p[j];
p[j]=t;
}
}
}

printf("The highest score is %f, and he number is %s\n", p[0].score, p[0].number);
}

#include
#include

void print(struct students *p); //打印函数print的定义
void max_score(struct students *p); //函数max的定义

struct students //结构体students的定义
{
char number[15];
float score;
};

int main(int argc, char *argv[])
{
struct students num[5]={{"121", 88.8}, {"122", 77.7}, {"123", 48.4}, {"124", 99.1}, {"125", 78.3}};

print(num);
max_score(num);

system("PAUSE");
return 0;
}

// Definition of function

//**print
void print(struct students *p)
{
int i;

i=0;

while(i<5)
{
printf("%d student scored is %0.2f\n", i+1, p[i]);
i+=2;
}
}

// ** max_score
void max_score(struct students *p)
{
struct students t; // 用于保存最大数
int i;
int j;

for(i=0; i<4; i++) //按分数排序
{
for(j=0; j<5; j++)
{
if( p[i].score> p[j].score)
{
t= p[i]; //学号和分数也交换
p[i]=p[j];
p[j]=t;
}
}
}

printf("The highest score is %f, and he number is %s\n", p[0].score, p[0].number);
}

\

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇问题一百三十六:三角形类问题 下一篇问题一百三十七:计算坐标上两点..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: