回复 9楼 TonyDeng
好像是啊。。。
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <windows.h> const int MAX_NAME = 128; const int NAME = 32; const int LEVEL = 16; typedef struct teacher //教师类型结构体 { int num; char name[128]; char level[16]; char sex[10]; int age; double score; } TEACHER; void save(TEACHER *data,int len); void input(TEACHER *data, int &len); /*输入教师信息函数 */ void input(TEACHER *data, int &len) { int num; putchar('\n'); printf("%s\n", "请输入新的记录,如果需要退出当前菜单请输入-1."); printf("%s\n", "格式:\n职工号\n"); printf("%s\n", "姓名 职称 性别 年龄 教学效果综合评分"); putchar('\n'); printf("如:\n"); printf("1\n"); printf("李白 副教授 男 25 92\n"); scanf("%d", &num); while(num != -1) { data[len].num = len+1; scanf("%s %s %s %d %lf", data[len].name, &data[len].level, &data[len].sex, &data[len].age, &data[len].score); len++; scanf("%d", &num); } } /*输出教师信息函数*/ void output(TEACHER *data, int len) { int i; system("cls"); printf("%10s", "职工号"); printf("%10s", "姓名"); printf("%10s", "职称"); printf("%10s", "性别"); printf("%10s", "年龄"); printf("%12s", "教学效果综合评分"); putchar('\n'); for (i =0; i < 80; i++) putchar('='); putchar('\n'); for (i = 0; i< len; i++) { printf("%10d", data[i].num); printf("%10s", data[i].name); printf("%10s", data[i].level); printf("%10s", data[i].sex); printf("%10d", data[i].age); printf("%12lf", data[i].score); putchar('\n'); } for (i =0; i < 80; i++) putchar('='); putchar('\n'); printf("按回车键继续."); getchar(); getchar(); } /*排序函数*/ void sort(TEACHER *data, int len) { int i,j,k; TEACHER temp; for (i=0; i<len-1; i++) { for (j=i; j<len-i-1; j++) if (data[j].score > data[j+1].score) { temp = data[j]; data[j] = data[j+1]; data[j+1] = temp; } } } /*搜索函数*/ void find(TEACHER *data, int len) { int find_num, result ; int i; lab: result=0; printf("%s\n", "请选择输入需要查找的老师的职工号,退出当前菜单请输入 -1."); scanf("%d", &find_num); if (find_num == -1) return; while( data[result].num != find_num && 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("%10s", "职工号"); printf("%10s", "姓名"); printf("%10s", "职称"); printf("%10s", "性别"); printf("%10s", "年龄"); printf("%12s", "教学效果综合评分"); putchar('\n'); printf("%10d", data[result].num); printf("%10s", data[result].name); printf("%10s", data[result].level); printf("%10s", data[result].sex); printf("%10d", data[result].age); printf("%12.1lf", data[result].score); putchar('\n'); for (i =0; i < 80; i++) putchar('='); putchar('\n'); goto lab; } } /*显示信息函数*/ void paint() { int i; system("cls"); printf("%55s\n", "北方工业大学学生成绩管理系统\n"); printf(" 制作者: 何闻达 \n"); for (i = 0; i < 80; i++) putchar('='); putchar('\n'); putchar('\n'); putchar('\n'); printf(" 1 输入信息 2 输出信息\n"); printf(" 3 按教学效果综合评分排序 4 按学号查找\n"); printf(" 0 退出\n"); putchar('\n'); putchar('\n'); putchar('\n'); for (i = 0; i<= 79; i++) putchar('='); putchar('\n'); putchar('\n'); printf("%s\n", "请输入各操作对应的序号:"); } /*保存文件函数*/ void save(TEACHER *data, int len) { int i; FILE *fp; fp=fopen("C:\\Users\\Administrator\\Desktop\\教师信息统计.txt","w+"); fprintf(fp,"%10s", "职工号"); fprintf(fp,"%10s", "职称"); fprintf(fp,"%10s", "姓名"); fprintf(fp,"%10s", "性别"); fprintf(fp,"%10s", "年龄"); fprintf(fp,"%12s", "教学效果综合评分"); fprintf(fp,"========================================================================\n"); putchar('\n'); for (i = 0; i< len; i++) { fprintf(fp,"%10d", data[i].num); fprintf(fp,"%10s", data[i].name); fprintf(fp,"%10s", data[i].level); fprintf(fp,"%10s", data[i].sex); fprintf(fp,"%10d", data[i].age); fprintf(fp,"%12lf", data[i].score); } fprintf(fp,"========================================================================\n"); fclose(fp); printf("文件已保存到\"成绩统计表.txt\""); getchar(); getchar(); } /*主函数*/ int main() { TEACHER data[NAME]; 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': save(data, len); break; default: if (ctrl_ch != '\n') printf("%s\n", "输入错误!"); } if (ctrl_ch != '\n') paint(); scanf("%c", &ctrl_ch); } return 0; }