江苏网友来相约

江苏的网友,不如来这里相约吧,我是江苏省海门市人,这里也许能找到你知心朋友。

103 成员713 主题4,022 回复0 张照片

program!

*
*龙翱九天*
2001-03-19 20:39
/* 应用指针和结构数组处理学生信息的函数。 stfun_1.c */
/* */
/* 学号:20015101 姓名:李为民 日期:2001.2 */

/* studf.txt 文件结构:
学号 姓名 性别 年 月 日 数学 英语 C 总分
******** -------- ** ---- ** -- *** --- *** ----
studf.txt 文件数据:
20015101 张明 男 1982 10 21 60 72 76 0
20015103 李红英 女 1983 8 25 100 96 89 0
20015102 成杰 男 1983 11 20 95 70 87 0
20015105 许民光 男 1984 1 26 78 87 74 0
20015104 陈富来 男 1983 12 15 86 74 92 0

使用 studf.txt 文件时,将其写入文件 */


/* 主控菜单 menu_1.c */
int menu_1()
{
int code;
printf(" 菜 单\n");
printf(" ┌──────────────────┐\n");
printf(" │ 0. 退出 │\n");
printf(" │ 1. 显示学生信息 │\n");
printf(" │ 2. 按姓名排序 │\n");
printf(" │ 3. 按总分排序 │\n");
printf(" │ 4. 按学号查学生分数和平均分数 │\n");
printf(" │ 5. 按姓名查学生分数和平均分数 │\n");
printf(" │ 6. 按总分查学生分数和平均分数 │\n");
printf(" │ 7. 查各课程平均分数 │\n");
printf(" │ 8. 查学生平均年令 │\n");
printf(" │ 9. 查学生人数 │\n");
printf(" │ 10. 显示学生信息表 │\n");
printf(" └──────────────────┘\n");
printf(" 选择代码(0,1,2,3,4,5,6,7,8,9):");
scanf("%d",&code);
return code;
}


/* 从正文文件 studf.txt 读入数据,建立学生信息结构数组 */
void readsi(struct student stud[],int *n)
{
FILE *fp;
int i;

if ((fp=fopen("studf.txt","r"))==NULL)
{
printf("不能打开 studf.txt 文件!\n");
exit(1);
}
for (i=0;!feof(fp);i++)
{
(*n)++;
if (*n>N)
{
printf("最多处理 35 位学生信息!\n");
return;
}
fscanf(fp,"%s %s %s %d %d %d %d %d %d %d",
stud[i].no,stud[i].name,stud[i].sex,
&stud[i].birthday.year,&stud[i].birthday.month,
&stud[i].birthday.day,
&stud[i].score[0],&stud[i].score[1],&stud[i].score[2],
&stud[i].score[3]);
stud[i].score[3]=stud[i].score[0]+stud[i].score[1]+
stud[i].score[2];
}
fclose(fp);
}


/* 按学号排序 --- 简单比较排序法 */
void sort_no(struct student stud[],int n)
{
struct student temp;
int i,j;

for (i=0;i<n-1;i++)
for (j=i+1;j<n;j++)
if (strcmp(stud[i].no,stud[j].no)>0)
{
temp=stud[i];
stud[i]=stud[j];
stud[j]=temp;
}
}


/* 按学号查学生分数和平均分数 */
void srch_no(struct student stud[],int n)
{
int lower=0,upper=n,mid,found=0,row=0,i;
char no[9];

printf("读入学号:");
scanf("%s",no);
printf("\n");
if (strcmp(stud[lower].no,no)==0)
{
found=1;
row=0;
}
else if (strcmp(stud[upper].no,no)==0)
{
found=1;
row=n;
}
else
while (lower<=upper && !found)
{
mid=(lower+upper)/2;
if (strcmp(stud[mid].no,no)==0)
{
row=mid;
found=1;
}
else if (strcmp(no,stud[mid].no)>0)
lower=mid+1;
else
upper=mid-1;
}

if (found==0)
{
printf("学号: %s 未找到!\n",no);
scanf("%*2c");
exit(1);
}


/* 显示已找到的学生分数 */
printf(" 学号 姓名 性别 年 月 日 数学 英语 C 总分 平均分数\n");
printf("******** -------- ** ---- ** -- *** --- *** ---- ***.*\n");
printf("===========================================================\n");
while (strcmp(stud[row].no,no)==0)
{
printf("%-8s %-8s %-2s %4d %2d %2d %4d %4d %4d %5d %6.1f\n",
stud[row].no,stud[row].name,stud[row].sex,
stud[row].birthday.year,stud[row].birthday.month,
stud[row].birthday.day,
stud[row].score[0],stud[row].score[1],
stud[row].score[2],stud[row].score[3],
stud[row].score[3]/3.0);
row++;
}
}


/* 显示学生信息 */
void printsi(struct student stud[],int n)
{
int i,k=0;

printf(" 学号 姓名 性别 年 月 日 数学 英语 C 总分\n");
printf("******** -------- ** ---- ** -- *** --- *** ----\n");
printf("====================================================\n");
for (i=0;i<n;i++)
{
printf("%-8s %-8s %-2s %4d %2d %2d %4d %4d %4d %5d\n",
stud[i].no,stud[i].name,stud[i].sex,
stud[i].birthday.year,stud[i].birthday.month,
stud[i].birthday.day,
stud[i].score[0],stud[i].score[1],stud[i].score[2],
stud[i].score[3]);
k++;
if (k%20==0)
scanf("%*c");
}
}


/* 按总分排序 -- 选择排序 */
void sort_tscore(struct student *ptscore[],int n)
{
int i,j,max;
struct student *ptemp;

for (i=0;i<n-1;i++)
{
max=i; /* 找最高分的下标值 */
for (j=i+1;j<n;j++)
if (ptscore[j]->score[3]>ptscore[max]->score[3])
max=j;

if (i!=max) /* 交换指针 */
{
ptemp=ptscore[i];
ptscore[i]=ptscore[max];
ptscore[max]=ptemp;
}
}
}




/* 按总分排序显示学生信息 */
void printsi_tscore(struct student *ptscore[],int n)
{
int i,k=0;
printf(" 学号 姓名 性别 年 月 日 数学 英语 C 总分\n");
printf("******** -------- ** ---- ** -- *** --- *** ----\n");
printf("====================================================\n");
for (i=0;i<n;i++)
{
printf("%-8s %-8s %-2s %4d %2d %2d %4d %4d %4d %5d\n",
ptscore[i]->no,ptscore[i]->name,ptscore[i]->sex,
ptscore[i]->birthday.year,ptscore[i]->birthday.month,
ptscore[i]->birthday.day,
ptscore[i]->score[0],ptscore[i]->score[1],
ptscore[i]->score[2],ptscore[i]->score[3]);
k++;
if (k%20==0)
scanf("%*c");
}
}


/* 按总分查学生分数和平均分数 */
void srch_tscore(struct student *ptscore[],int n)
{
int lower=0,upper=n,mid,found=0,row=0,mark,i;

printf("读入分数:");
scanf("%d",&mark);
printf("\n");
if (ptscore[lower]->score[3]==mark)
{
found=1;
row=0;
}
else if (ptscore[upper]->score[3]==mark)
{
found=1;
row=n;
}
else
while (lower<=upper && !found)
{
mid=(lower+upper)/2;
if (ptscore[mid]->score[3]==mark)
{
row=mid;
found=1;
}
else if (ptscore[mid]->score[3]>mark)
lower=mid+1;
else
upper=mid-1;
}

if (found==0)
{
printf("分数: %d 未找到!\n",mark);
scanf("%*2c");
exit(1);
}

/* 找第一个相同的分数 mark */
if (row!=0)
{
row--;
while (ptscore[row]->score[3]==mark)
row--;
row++;
}

/* 显示已找到的学生分数 */
printf(" 学号 姓名 性别 年 月 日 数学 英语 C 总分 平均分数\n");
printf("******** -------- ** ---- ** -- *** --- *** ---- ***.*\n");
printf("===========================================================\n");
while (ptscore[row]->score[3]==mark)
{
printf("%-8s %-8s %-2s %4d %2d %2d %4d %4d %4d %5d %6.1f\n",
ptscore[row]->no,ptscore[row]->name,ptscore[row]->sex,
ptscore[row]->birthday.year,ptscore[row]->birthday.month,
ptscore[row]->birthday.day,
ptscore[row]->score[0],ptscore[row]->score[1],
ptscore[row]->score[2],ptscore[row]->score[3],
ptscore[row]->score[3]/3.0);
row++;
}
}


/* 按姓名排序 -- 选择排序 */
void sort_name(struct student *pname[],int n)
{
int i,j,min;
struct student *ptemp;

for (i=0;i<n-1;i++)
{
min=i; /* 找最小姓名的下标值 */
for (j=i+1;j<n;j++)
if (strcmp(pname[j]->name,pname[min]->name)<0)
min=j;

if (i!=min) /* 交换指针 */
{
ptemp=pname[i];
pname[i]=pname[min];
pname[min]=ptemp;
}
}
}


/* 按姓名排序显示学生信息 */
void printsi_name(struct student *pname[],int n)
{
int i,k=0;
printf(" 学号 姓名 性别 年 月 日 数学 英语 C 总分\n");
printf("******** -------- ** ---- ** -- *** --- *** ----\n");
printf("====================================================\n");
for (i=0;i<n;i++)
{
printf("%-8s %-8s %-2s %4d %2d %2d %4d %4d %4d %5d\n",
pname[i]->no,pname[i]->name,pname[i]->sex,
pname[i]->birthday.year,pname[i]->birthday.month,
pname[i]->birthday.day,
pname[i]->score[0],pname[i]->score[1],
pname[i]->score[2],pname[i]->score[3]);
k++;
if (k%20==0)
scanf("%*c");
}
}


/* 按姓名查学生分数和平均分数 */
void srch_name(struct student *pname[],int n)
{
int lower=0,upper=n,mid,found=0,row=0,i;
char name[9];

printf("读入姓名:");
scanf("%s",name);
printf("\n");
if (strcmp(pname[lower]->name,name)==0)
{
found=1;
row=0;
}
else if (strcmp(pname[upper]->name,name)==0)
{
found=1;
row=n;
}
else
while (lower<=upper && !found)
{
mid=(lower+upper)/2;
if (strcmp(pname[mid]->name,name)==0)
{
row=mid;
found=1;
}
else if (strcmp(name,pname[mid]->name)>0)
lower=mid+1;
else
upper=mid-1;
}

if (found==0)
{
printf("姓名: %s 未找到!\n",name);
scanf("%*2c");
exit(1);
}

/* 找第一个相同的姓名 name */
if (row!=0)
{
row--;
while (strcmp(pname[row]->name,name)==0)
row--;
row++;
}

/* 显示已找到的学生分数 */
printf(" 学号 姓名 性别 年 月 日 数学 英语 C 总分 平均分数\n");
printf("******** -------- ** ---- ** -- *** --- *** ---- ***.*\n");
printf("===========================================================\n");
while (strcmp(pname[row]->name,name)==0)
{
printf("%-8s %-8s %-2s %4d %2d %2d %4d %4d %4d %5d %6.1f\n",
pname[row]->no,pname[row]->name,pname[row]->sex,
pname[row]->birthday.year,pname[row]->birthday.month,
pname[row]->birthday.day,
pname[row]->score[0],pname[row]->score[1],
pname[row]->score[2],pname[row]->score[3],
pname[row]->score[3]/3.0);
row++;
}
}


/* 查各课程平均分数 */
void find_ave(struct student stud[],int n)
{
int i;
float amath=0.0,aengl=0.0,ac=0.0;

for (i=0;i<n;i++)
{
amath+=stud[i].score[0];
aengl+=stud[i].score[1];
ac+=stud[i].score[2];
}
printf("数学平均分数:%5.1f\n",amath/(float)n);
printf("英语平均分数:%5.1f\n",aengl/(float)n);
printf(" C 平均分数:%5.1f\n",ac/(float)n);
}


/* 查学生平均年令 */
void find_age(struct student stud[],int n)
{
int i,cyear;
float age=0.0;

printf("读入年份:");
scanf("%d",&cyear);
printf("\n");
for (i=0;i<n;i++)
age+=cyear-stud[i].birthday.year;
printf("学生平均年令:%4.1f\n",age/(float)n);
}


/* 查学生人数 */
void find_pnum(struct student stud[],int n)
{
int i,male=0,female=0;

for (i=0;i<n;i++)
if (strcmp(stud[i].sex,"男")==0)
male++;
else
female++;
printf("学生总人数:%d\n",n);
printf("男学生人数:%d\n",male);
printf("女学生人数:%d\n",female);
}


/* 显示学生信息表 */
void printtable(struct student stud[],int n)
{
int i,k=0;

printf("\n\n");
printf(" 学生信息表\n");
printf(" ==========\n\n");
printf("┌────┬────┬──┬─────┬──┬──┬──┬──┐\n");
printf("│ 学号 │ 姓名 │性别│ 年 月 日│数学│英语│ C │总分│\n");
printf("├────┼────┼──┼─────┼──┼──┼──┼──┤\n");
printf("│********│--------│ ** │---- ** --│ ***│ ---│ ***│----│\n");
printf("├────┼────┼──┼─────┼──┼──┼──┼──┤\n");
for (i=0;i<n;i++)
{
printf("│%-8s│%-8s│ %-2s │%4d %2d %2d│%4d│%4d│%4d│%4d│\n",
stud[i].no,stud[i].name,stud[i].sex,
stud[i].birthday.year,stud[i].birthday.month,
stud[i].birthday.day,
stud[i].score[0],stud[i].score[1],stud[i].score[2],
stud[i].score[3]);
if (i==n-1)
printf("└────┴────┴──┴─────┴──┴──┴──┴──┘\n");
else
printf("├────┼────┼──┼─────┼──┼──┼──┼──┤\n");
k++;
if (k%20==0)
scanf("%*c");
}
}

1 / 1

登录后回复。