level 1
程序:
#define N 60#
define M 20
#include "stdio.h"#
include "string.h"struct student{ int num; char name[20]; float sc[M]; float tsc;};typedef struct student STU;char kcname[M][12];int n,m;void inscore(STU * pstu) /* 输入每个学生的数据 */{ float score[M]; int i,j; char c; char * pin; FILE * fpin; printf("选择学生数据的来源(K— 键盘输入, F—文件读入);\n"); c=getch(); if(c=='K'||c=='k') /*键盘输入*/ { printf("输入学生的实际人数:");scanf(" %d",&n);printf("输入课程的实际门数:");scanf("%d", &m);if(n<=0||n>60||m<=0||m>20){ printf("超出本程序处理范围!"); exit(0);}printf("输入%d门课程名称:",m);for(i=0;i
num,&pstu->name);printf("输入%d门课程的成绩:",m);for(i=0;i
tsc=0;for(i=0;i
sc[i]=score[i]; pstu->tsc +=pstu->sc[i];}pstu ++;}pstu = pstu - n;pin ="SRCSCORE.DAT";if((fpin=fopen(pin,"wb "))== NULL){ printf(" 文件建立出错!"); exit(0);}fwrite(&n,2,1,fpin);fwrite(&m,2,1,fpin);fwrite(kcname,12,m,fpin);fwrite(pstu,sizeof(STU),n,fpin);fclose(fpin); } else if(c=='F'||c=='f') /* 从文件读取数据 */{ printf("输入文件名:"); scanf("%s",pin); if((fpin=fopen(pin,"rb"))==NULL) {printf(" 文件打开出错!"); exit(0); } fread(&n,2,1,fpin); fread(&m,2,1,fpin); fread(kcname,12,m,fpin); fread(pstu,sizeof(STU),n,fpin); fclose(fpin); } else { printf("选择学生数据出错!");exit(0);} } void numsearch(STU * ); void namesearch(STU * ); void countsc(STU * ); void aversc(STU * ); void sortsc(STU * ); main() { STU stu[N];char ch;clrscr();inscore(stu);do{ printf(" \n \t \t \t菜单");printf(" \n \t * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ");printf("\n \t \t1--------------------按学号查询"); printf(" \n \t \t2--------------------按姓名查询"); printf (" \n \t \t3--------------------统计每门课的分数段"); printf (" \n \t \t4--------------------计算每门课的平均分"); printf (" \n \t \t5--------------------按每个学生总分排名次"); printf (" \n \t \t6--------------------退出");printf(" \n \t * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * "); printf(" \n \t请选择:");ch = getch();switch (ch){ case '1':numsearch(stu);getch();break; case '2':namesearch(stu);getch();break; case '3':countsc(stu);getch();break; case '4':aversc(stu);getch();break; case '5':sortsc(stu);getch();break; } }while(ch!='6');}void numsearch(STU * pstu){ int i,nm,flag=0,k=0; printf("请输入要查询学生的学号:"); scanf(" %d",&nm); while(k
num==nm){ printf(" \n number \t name"); for(i=0;i
num,pstu->name); for(i=0;i
sc[i]);
2007年06月26日 13点06分
1
#define N 60#
define M 20
#include "stdio.h"#
include "string.h"struct student{ int num; char name[20]; float sc[M]; float tsc;};typedef struct student STU;char kcname[M][12];int n,m;void inscore(STU * pstu) /* 输入每个学生的数据 */{ float score[M]; int i,j; char c; char * pin; FILE * fpin; printf("选择学生数据的来源(K— 键盘输入, F—文件读入);\n"); c=getch(); if(c=='K'||c=='k') /*键盘输入*/ { printf("输入学生的实际人数:");scanf(" %d",&n);printf("输入课程的实际门数:");scanf("%d", &m);if(n<=0||n>60||m<=0||m>20){ printf("超出本程序处理范围!"); exit(0);}printf("输入%d门课程名称:",m);for(i=0;i
num,&pstu->name);printf("输入%d门课程的成绩:",m);for(i=0;i
tsc=0;for(i=0;i
sc[i]=score[i]; pstu->tsc +=pstu->sc[i];}pstu ++;}pstu = pstu - n;pin ="SRCSCORE.DAT";if((fpin=fopen(pin,"wb "))== NULL){ printf(" 文件建立出错!"); exit(0);}fwrite(&n,2,1,fpin);fwrite(&m,2,1,fpin);fwrite(kcname,12,m,fpin);fwrite(pstu,sizeof(STU),n,fpin);fclose(fpin); } else if(c=='F'||c=='f') /* 从文件读取数据 */{ printf("输入文件名:"); scanf("%s",pin); if((fpin=fopen(pin,"rb"))==NULL) {printf(" 文件打开出错!"); exit(0); } fread(&n,2,1,fpin); fread(&m,2,1,fpin); fread(kcname,12,m,fpin); fread(pstu,sizeof(STU),n,fpin); fclose(fpin); } else { printf("选择学生数据出错!");exit(0);} } void numsearch(STU * ); void namesearch(STU * ); void countsc(STU * ); void aversc(STU * ); void sortsc(STU * ); main() { STU stu[N];char ch;clrscr();inscore(stu);do{ printf(" \n \t \t \t菜单");printf(" \n \t * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ");printf("\n \t \t1--------------------按学号查询"); printf(" \n \t \t2--------------------按姓名查询"); printf (" \n \t \t3--------------------统计每门课的分数段"); printf (" \n \t \t4--------------------计算每门课的平均分"); printf (" \n \t \t5--------------------按每个学生总分排名次"); printf (" \n \t \t6--------------------退出");printf(" \n \t * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * "); printf(" \n \t请选择:");ch = getch();switch (ch){ case '1':numsearch(stu);getch();break; case '2':namesearch(stu);getch();break; case '3':countsc(stu);getch();break; case '4':aversc(stu);getch();break; case '5':sortsc(stu);getch();break; } }while(ch!='6');}void numsearch(STU * pstu){ int i,nm,flag=0,k=0; printf("请输入要查询学生的学号:"); scanf(" %d",&nm); while(k
num==nm){ printf(" \n number \t name"); for(i=0;i
num,pstu->name); for(i=0;i
sc[i]);