+1
208行,拖进去直接运行。注意那三个文件放到同名文件夹下,还有三个txt的格式。
程序代码:
// Tonydeng.cpp : Defines the entry point for the console application. // #include <stdio.h> #include "stdafx.h" #include <malloc.h> #include <string.h> #include <process.h> struct stuInfo { long num; char name[6]; int grade; int sex; int score[10]; stuInfo*pNext; }*s_pHead=0; struct sexInfo { int sex_num; char sex[2]; sexInfo*e_pNext; }*sex_Head=0; struct CodeInfo { int code_num; char college[10]; CodeInfo*c_pNext; }*code_Head=0; int readtxt(char path[20]) { FILE *fp=fopen(path,"r"); if(!fp) return -1; int i=1,k; while(strcmp(path,"StudentInfo.txt")==0 && i!=-1 && k!=-1) { struct stuInfo*stu1=(stuInfo*)malloc(sizeof(stuInfo)); /* i=fscanf(fp,"%ld %s %d %d %d %d %d %d %d %d %d %d %d %d", &stu1->num,stu1->name,&stu1->sex,&stu1->grade,&stu1->score[0],&stu1->score[1],&stu1->score[2],&stu1->score[3], &stu1->score[4],&stu1->score[5],&stu1->score[6],&stu1->score[7],&stu1->score[8],&stu1->score[9]);*/ i=fscanf(fp,"%ld %s %d %d",&stu1->num,stu1->name,&stu1->sex,&stu1->grade); for(int m=0;m<10;m++) k=fscanf(fp,"%d",&stu1->score[m]); if(k==-1 || i==-1) //if(i==-1) free(stu1); else { stu1->pNext=s_pHead; s_pHead=stu1; } } while(strcmp(path,"SexInfo.txt")==0 && i!=-1) { struct sexInfo*sex1=(sexInfo*)malloc(sizeof(sexInfo)); i=fscanf(fp,"%d %s",&sex1->sex_num,sex1->sex); if(i==-1) free(sex1); else { sex1->e_pNext=sex_Head; sex_Head=sex1; } } while(strcmp(path,"CodeInfo.txt")==0 && i!=-1) { struct CodeInfo*code1=(CodeInfo*)malloc(sizeof(CodeInfo)); i=fscanf(fp,"%d %s",&code1->code_num,code1->college); if(i==-1) free(code1); else { code1->c_pNext=code_Head; code_Head=code1; } } return 0; } void read() { readtxt("StudentInfo.txt"); readtxt("SexInfo.txt"); readtxt("CodeInfo.txt"); } void prf() { struct stuInfo* stu1=s_pHead; while(stu1) { printf("num:%ld\tname:%s",stu1->num,stu1->name); int i=0; float sum=0.0; while(i<10) sum+=stu1->score[i++]; sum/=10; printf("\tscore:%f",sum); struct sexInfo*sex1=sex_Head; while(sex1 && sex1->sex_num!=stu1->sex) sex1=sex1->e_pNext; printf("\tsex:%s",sex1->sex); struct CodeInfo*code=code_Head; while(code && code->code_num!=(stu1->grade+1)) code=code->c_pNext; printf("\tgrade:%s\n",code->college); stu1=stu1->pNext; } } void sort_num()//大的数往0方向推 { struct stuInfo* stu1=s_pHead,*p2,*anchor,*now; int i=0,j; while(stu1) { i++; anchor=stu1; stu1=stu1->pNext; } while(i>=0) { j=0; stu1=s_pHead->pNext; if(s_pHead->num>stu1->num) { s_pHead->pNext=stu1->pNext; stu1->pNext=s_pHead; s_pHead=stu1; } stu1=s_pHead; while(j<i-2) { now=stu1->pNext; p2=now->pNext; //交换 if(now->num>p2->num) { stu1->pNext=p2; now->pNext=p2->pNext; p2->pNext=now; } stu1=stu1->pNext; j++; } i--; } } const char*menu[]= { "0.退出", "1.打印数据", "2.按学生学号排序", NULL }; int menu_dis() { int i=0; do { printf("\t\t%s\n",menu[i++]); }while(menu[i]!=NULL ||puts("输入选择")==1); return i; } void dis() { int i,j; do { i=menu_dis(); fflush(stdin); scanf("%d",&j); switch(j) { case 1: prf(); break; case 2: sort_num(); break; default: break; } }while( j<0 || j>i || j!=0 ); } int main(int argc, char* argv[]) { read(); dis(); return 0; }