程序代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 10//根据实际教师人数更改(要大于实际教师人数);
struct date
{ int year;
int month;
int day;
};
struct teacher
{ char no[10];
char name[9];
char sex[3];
struct date birthday;
int score[2];
};
void writeti(struct teacher *teach, int *n)
{
//从键盘输入数据并写入文本中,并返回结构体实际长度;
FILE *fp1;
int i=0;
char ch;
if((fp1=fopen("text.txt","wb"))==NULL)
{
printf("can not open file");
return;
}
printf("请输入教师工号,姓名,性别,出生日期(**.**.**),教学分,科研分\n");
printf("例子:123456 小王 男 1987.02.23 87 96 \n");
do
{
printf("是否继续Y/N?\n");
fflush(stdin); //清空键盘缓冲区;
ch=getchar();
if(ch=='N')break;
else
{
printf("请输入教师工号,姓名,性别,出生日期(**.**.**),教学分,科研分\n");
scanf("%s %s %s %d.%d.%d %d %d",teach[i].no,teach[i].name,teach[i].sex,
&teach[i].birthday.year,&teach[i].birthday.month,&teach[i].birthday.day,&teach[i].score[0],&teach[i].score[1]);
fwrite(&teach[i],sizeof(struct teacher),1,fp1);
(*n)++; i++;
}
}while(1);
fclose(fp1);
}
void readti(struct teacher *teach,int n)
{
//从文本中读出数据;
FILE *fp1;
int i;
if((fp1=fopen("text.txt","rb"))==NULL)
{
printf("can not open file");
exit(0);
}
printf("output from file:\n");
fread(teach,sizeof(teach),n,fp1);
for(i=0; i<n; i++)
{
printf("%s%10s%10s %d.%d.%d%10d%10d\n",teach[i].no,teach[i].name,teach[i].sex,
teach[i].birthday.year,teach[i].birthday.month,teach[i].birthday.day,teach[i].score[0],teach[i].score[1]);
}
fclose(fp1);
}
void printsi(struct teacher *teach, int n)
{
//输出教师的信息;
int i;
for(i=0; i<n; i++)
{
printf("教师工号");
printf("%10s,%10s,%10s,%d.%d.%d,%10d\n",teach[i].no,teach[i].name,teach[i].sex,
teach[i].birthday.year,teach[i].birthday.month,teach[i].birthday.day,teach[i].score[0],teach[i].score[1]);
}
}
void sort_no(struct teacher *teach, int n)
{
//按照教师工号排序;
int i,j;
struct teacher k;
for(i=0; i<n-1; i++)
for(j=i; j<n-1; j++)
if(strcmp(teach[j].no,teach[j+1].no)>0)
{
k= teach[j];
teach[j]=teach[j+1];
teach[j+1]=k;
}
}
void sort_total(struct teacher *teach, int n)
{
//按照科研和教学总分排序;
int i, j;
struct teacher k;
for(i=0;i<n-1;i++)
{
teach[i].score[2]=teach[i].score[0]+teach[i].score[1];
}
for(i=0; i<n-1; i++)
for(j=i; j<n; j++)
if(teach[j].score[2]<teach[j+1].score[2])
{
k=teach[j];
teach[j]=teach[j+1];
teach[j+1]=k;
}
}
void srch_no(struct teacher *teach, int n)
{
//通过教师工号查询教师信息;
char k[10];
printf("请输入教师工号:");
scanf("%s",k);
for(int i=0; i<n; i++)
{
if(strcmp(teach[i].no,k)==0)
printf("%s, %d, %d, %d\n",teach[i].no,teach[i].score[0],teach[i].score[1],(teach[i].score[0]+teach[i].score[1])/2);
}
}
void srch_name(struct teacher *teach, int n)
{
//通过教师姓名查询教师信息;
char k[10];
printf("请输入教师姓名:");
scanf("%s",k);
for(int i=0; i<n; i++)
{
if(strcmp(teach[i].name,k)==0)
printf("%s, %d, %d, %d\n",teach[i].name,teach[i].score[0],teach[i].score[1],(teach[i].score[0]+teach[i].score[1])/2);
}
}
void insert_total(struct teacher *teach,int n)
{
//插入新的教师信息;
char a[10],b[9],c[3];
int d,e,f,g,h;
sort_total(teach,n);
printf("请输入新教师的工号,姓名,性别,出生日期,教学考评值,科研考评值中间用空格分隔");
scanf("%s %s %s %d %d %d %d %d %d",a,b,c,d,e,f,g,h);
if(n>=N)
{
printf("结构体数组长度不够,不能插入!");
return;
}
for(int j=n; j>=0; j--)
{
if(strcmp(teach[j].no,a)<0)
{
teach[j+1]=teach[j];
}
else
{
strcpy(teach[j].no,a);
strcpy(teach[j].name,b);
strcpy(teach[j].sex,c);
teach[j].birthday.year=d;
teach[j].birthday.month=e;
teach[j].birthday.day=f;
teach[j].score[0]=g;
teach[j].score[1]=h;
teach[j].score[2]=g+h;
}
}
}
void find_ave(struct teacher *teach, int n)
{
//求全校老师教学和科研平均分;
int sum;
for(int i=0; i<n; i++)
{
sum += teach[i].score[2];
}
printf("全校老师教学和科研平均分:%d",sum/N);
}
int main()
{
struct teacher teach[N];
int length=0;//结构体数组插入数据实际长度;
writeti(teach,&length);//从键盘读入数据写入文件,并通过指针返回实际长度;
readti(teach,length);//从文件中读出数据;
//下面的函数自己调用;
return 0;
}
[
本帖最后由 liao06550107 于 2011-11-3 20:35 编辑 ]