1.源代码
#include
#include
#include
#define MAXN 35
#define MAX_NAME 256
typedef struct student //学生信息
{
int no;
char name[MAX_NAME];
double course;
double mark;
double mark_ave;
double mark_tot;
} STUDENT;
/*-----------输入信息子程序---------*/
void input(STUDENT *data, int *len)
{
int no;
putchar('\n');
printf("%s\n", "请输入新的记录,如果需要退出当前菜单请输入-1.");
printf("%s\n", "格式:\n准考证号\n");
printf("%s\n", "姓名 考场号 成绩");
putchar('\n');
printf("如:\n");
printf("5\n");
printf("李达最 001 425\n");
scanf("%d", &no);
while(no != -1) {
data[*len].no = no;
scanf("%s %lf %lf %lf", data[*len].name, &data[*len].course, &data[*len].mark);
(*len)++;
scanf("%d", &no);
}
}
/*---------------发布信息子程序---------------*/
void output(STUDENT *data, int len)
{
int i;
system("cls");
printf("%8s", "准考证号");
printf("%8s", "姓名");
printf("%8s", "考场号");
printf("%8s", "成绩");
printf("%12s", "平均分");
printf("%10s", "总分");
putchar('\n');
for (i =0; i < 80; i++)
putchar('=');
putchar('\n');
for (i = 0; i< len; i++)
{
printf("%8d", data[i].no);
printf("%8s", data[i].name);
printf("%8.1lf", data[i].course);
printf("%8.1lf", data[i].mark);
printf("%12.1lf", data[i].mark_ave);
printf("%10.1lf", data[i].mark_tot);
putchar('\n');
}
for (i =0; i < 80; i++)
putchar('=');
putchar('\n');
printf("按回车键继续.");
getchar();
getchar();
}
/*--------------排序子程序------------*/
void sort(STUDENT *data, int len)
{
int i,j,k;
STUDENT temp;
for (i=0; i
if (k != i) {
temp = data[i];
data[i] = data[k];
data[k] = temp;
}
}
}
/*--------------------检索子程序---------------------*/
void find(STUDENT *data, int len)
{
int find_no, result ;
int i;
lab: result=0;
printf("%s\n", "请输入需要查找的学生的学号,退出当前菜单请输入 -1.");
scanf("%d", &find_no);
if (find_no == -1) return; /*exit the fine sub program*/
while( data[result].no != find_no && result < len) result ++;
if (result >= len ) {
printf("%s\n", "未查询到相关信息");
goto lab;
}
else {
system("cls");
printf("%s\n", "查询信息如下:");
for (i =0; i < 80; i++)
putchar('=');
putchar('\n');
printf("%8s", "准考证号);
printf("%8s", "姓名");
printf("%8s", "考场号");
printf("%8s", "成绩");
printf("%12s", "平均分");
printf("%10s", "总分");
putchar('\n');
printf("%8d", data[result].no);
printf("%8s", data[result].name);
printf("%8.1lf", data[result].course
printf("%8.1lf", data[result].mark
printf("%12.1lf", data[result].mark_ave);
printf("%10.1lf", data[result].mark_tot);
putchar('\n');
for (i =0; i < 80; i++)
putchar('=');
putchar('\n');
goto lab;
}
}
/*---------------六级参考资格功能-----------*/
void identity(STUDENT *data, int len)
{
int no_424=0, no_425=0;
int i;
for (i = 0;i < len; i++) {
if (data[i].mark <=425) no_424++;
else no_425++;
}
system("cls");
for (i =0; i < 80; i++)
putchar('=');
putchar('\n');
printf("%10s", "分数");
printf("%10s", "0--424");
printf("%10s", "425--710");
putchar('\n');
putchar('\n');
printf("%10s", "学生");
printf("%10d", no_424);
printf("%10d", no_425);
putchar('\n');
for (i =0; i < 80; i++)
putchar('=');
putchar('\n');
printf("按回车键返回");
getchar();
getchar();
}
/*---------------显示信息子程序------------*/
void paint()
{
int i;
system("cls");
printf("%55s\n", "扬州大学信息工程学院\n");
printf(" 制作者:扬大计科0901 *** \n");
for (i = 0; i < 80; i++)
putchar('=');
putchar('\n');
printf(" 1 输入信息 2 发布信息\n");
printf(" 3排序 4 检索\n");
printf(" 5 六级参考资格 6保存到文件\n");
printf(" 0 退出\n");
for (i = 0; i<= 79; i++)
putchar('=');
putchar('\n');
printf("%s\n", "请输入各操作对应的序号:");
}
void save(STUDENT *data, int len)//将数据保存到文件
{
int i;
FILE *fp;
fp=fopen("成绩统计表.txt","w");
fprintf(fp,"%8s", "准考证号");
fprintf(fp,"%8s", "姓名");
fprintf(fp,"%8s", "考场号");
fprintf(fp,"%8s", "成绩");
fprintf(fp,"%12s", "平均分");
fprintf(fp,"%10s\n", "总分");
fprintf(fp,"========================================================================\n");
putchar('\n');
for (i = 0; i< len; i++)
{
fprintf(fp,"%8d", data[i].no);
fprintf(fp,"%8s", data[i].name);
fprintf(fp,"%8.1lf", data[i].course);
fprintf(fp,"%8.1lf", data[i].mark);
fprintf(fp,"%12.1lf", data[i].mark_ave);
fprintf(fp,"%10.1lf\n", data[i].mark_tot);
}
fprintf(fp,"========================================================================\n");
fclose(fp);
printf("文件已保存到\"成绩统计表.txt\"");
getchar();getchar();
}
/*-----------主程序--------------*/
void main()
{
STUDENT data[MAXN];
int len = 0;
char ctrl_ch;
paint();
scanf("%c", &ctrl_ch);
while (ctrl_ch != '0') {
switch(ctrl_ch) {
case '1':
input(data, &len);
break;
case '2':
output(data, len);
break;
case '3':
sort(data, len);
break;
case '4':
find(data, len);
break;
case '5':
identity(data, len);
break;
case '6':
save(data, len);
break;
default:
if (ctrl_ch != '\n') printf("%s\n", "输入错误!");
break;
}
if (ctrl_ch != '\n') paint();
scanf("%c", &ctrl_ch);
}
}