| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 462 人关注过本帖
标题:问各位一个问题。
只看楼主 加入收藏
Evaimale
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2006-10-6
收藏
 问题点数:0 回复次数:1 
问各位一个问题。

#include<stdio.h>
#include<stdlib.h>

struct student
{
int id;
char name[20];
int score[3];
float avg;
};

struct student input(); //单个学员信息录入的函数,前向声明
void display(struct student stu[],int count); //显示函数,前向声明
void mysort(struct student stu[],int count); //排序函数,前向声明
void insert(struct student stu[],struct student in , int count); //插入函数,前向声明
void delect(struct student stu[],struct student del,int count); //删除函数,前向声明
void main()
{
struct student arr[50];
int count=0;
struct student in,del;
char ans;
printf("\n\t请输入学员信息\n");

//使用循环接受用户的输入
do
{
arr[count]=input(); //调用担搁学员信息录入函数来获得学员的信息
count++; //计数器增加
printf("\ncontinue?(y/n)\n");
scanf(" %c",&ans);
if(count==50)
{
exit(1);
}
}while(ans=='y' ||ans=='Y' );

printf("\n排序前的学员信息如下:\n");
display(arr,count); //调用显示函数显示学员信息

mysort(arr,count); //调用排序函数将学员信息按照平均成绩进行排序
printf("\n排序后的学员信息如下:\n");
display(arr,count); //调用显示函数显示学员信息

printf("\n是否要插入新学员?(y/n)");
scanf(" %c",&ans);

if(ans=='y' ||ans=='Y')
{
printf("\n请输入要插入的学员信息:\n");
in=input();
count++;
insert(arr,in,count);//调用插入函数将学员信息按照平均成绩进行插入
printf("\n插入新学员后的信息如下:\n");
display(arr,count);//调用显示函数显示学员信息
}
printf("\n是否要删除某个学员?(y/n)");
scanf(" %c",&ans);
if(ans=='y' ||ans=='Y')
{
printf("\n请输入要删除的学员的学号:\n");
scanf("%d",&del.id);
delect(arr,del,count);//调用删除函数将学员信息按照学号进行删除
printf("\n删除后的学员的信息如下:\n");
display(arr,count-1); //调用显示函数显示学员信息
}
}
/*单个录入学员信息的函数,函数定义
无参数
返回值,接受的输入的学员结构变量*/
student input()
{
int i; //循环变量
float sum = 0; //由于要求出3门课程的平均值,所以定义一个保存成绩总和的变量
struct student temp; //学员结构变量,用来接受用户的输入

printf("\n学号:");
scanf("%d", &(temp.id)); //接受学号
printf("姓名:");
fflush(stdin);
gets(temp.name); //接受姓名
//循环接受3门课程的成绩
for (i = 0; i < 3; i++)
{
printf("成绩%d:", i + 1);
scanf("%d", &temp.score[i]);
}

//使用循环求出3门课程成绩的总和
for (i = 0; i < 3; i++)
{
sum += temp.score[i];
}
temp.avg = sum / 3; //求出平均成绩

return (temp); //将保存有信息的结构变量返回出去
}
void display(struct student stu[],int count)
{
int i;
printf("\n学号\t姓名\t平均成绩\n");
for(i=0;i<count;i++)
{
printf("\n%d\t%s\t%f\t\n",stu[i].id,stu[i].name,stu[i].avg);
}
}
void mysort(struct student stu[],int count)
{
int i, j; //循环变量
struct student temp; //用于交换的中间变量,结构变量

//使用冒泡排序法进行排序,降序排列
for (i = count - 1; i > 0; i--)
{
for (j = count - 1; j > count - 1 - i; j--)
{
if (stu[j].avg > stu[j - 1].avg)
{
temp = stu[j];
stu[j] = stu[j-1];
stu[j - 1] = temp;
}
}
}
}
void insert(struct student stu[50],struct student in ,int count)
{
int i,j;

for(i=0;i<count;i++)
{
if(in.avg>stu[i].avg) //找到第一个小于要插入的数的位置
{

break;
}
}
for(j=count;j>i;j--) //为要插入的数留出空位
{
stu[j]=stu[j-1];
}
stu[i]=in;//将要插入的数保存到改位置
printf("\n");
}
void delect(struct student stu[],struct student del ,int count)
{
int i,j;
for(i=0;i<count;i++)
{
if(del.id==stu[i].id)
{
break;
}

}

for(j=i;j<count-1;j++)
{
stu[j]=stu[j+1]; //从找到删除的数起往前整体移动一位
}
}



我想问一这个程序怎么不用自定义函数做

搜索更多相关主题的帖子: void 录入 display include 信息 
2006-11-26 22:35
Evaimale
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2006-10-6
收藏
得分:0 

自己顶一下先。。


希望和大家一起成长为优秀的程序员!
2006-11-27 20:07
快速回复:问各位一个问题。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024455 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved