学生成绩排序
程序代码:
/* 自己写的一个不成样子的东西,还望大家不要见笑。 */ #include<stdio.h> #define N 40 #define CLEAR { char c; while( ( c=getchar() ) != '\n' && c != EOF ) ; } char caidan[]={"*********************************************************\n"}; int xuanzhe() { int x; do { system("cls"); printf("%s\n",caidan); printf(" 您好,这是个成绩管理系统,它可以帮您整理出\n" "您的学生成绩,您可以有四种选择:\n" " 1.成绩从大到小排序,打印最高的学习成绩和学号;\n" " 2.成绩从小到大排序,打印最低的学习成绩和学号;\n" " 3.从低到高排序学号;\n" " 4.从高到低排序学号;\n" " 5.退出。\n\n%s\n",caidan); printf("请输入您的选择:"); scanf(" %d",&x); CLEAR; if(x<1||x>5) { system("cls"); printf("您的输入有误,请重新输入\n"); sleep(1); } } while(x<1||x>5); if(x==5) exit(0); system("cls"); return x; } void shuru(float *chengji,int *xuehao,int n) { int i,j; for(i=0;i<n;i++) { if(i==0) system("cls"); printf("\n\n\n%s\n\n",caidan); printf("请输入第%d个学生的学号:\n",i+1); scanf(" %d",xuehao++); CLEAR; puts(" "); printf("请输入该生成绩:\n"); scanf("%f",chengji++); CLEAR; } } void zuigao(float *chengji,int *xuehao,int n) { int i,j,zh; float jh; for(i=0;i<n;i++) for(j=0;j<n-i-1;j++) if(*(chengji+j)<*(chengji+j+1)) { jh=*(chengji+j); *(chengji+j)=*(chengji+j+1); *(chengji+j+1)=jh; zh=*(xuehao+j); *(xuehao+j)=*(xuehao+j+1); *(xuehao+j+1)=zh; } } void zuidi(float *chengji,int *xuehao,int n) { int i,j,zh; float jh; for(i=0;i<n;i++) for(j=0;j<n-i-1;j++) if(*(chengji+j)>*(chengji+j+1)) { jh=*(chengji+j); *(chengji+j)=*(chengji+j+1); *(chengji+j+1)=jh; zh=*(xuehao+j); *(xuehao+j)=*(xuehao+j+1); *(xuehao+j+1)=jh; } } void xuehao_gao(int *xuehao,float *chengji,int n) { int i,j,zh; float jh; for(i=0;i<n;i++) for(j=0;j<n-i-1;j++) if(*(xuehao+j)<*(xuehao+j+1)) { zh=*(xuehao+j); *(xuehao+j)=*(xuehao+j+1); *(xuehao+j+1)=zh; jh=*(chengji+j); *(chengji+j)=*(chengji+j+1); *(chengji+j+1)=jh; } } void xuehao_di(int *xuehao,float *chengji,int n) { int i,j,zh; float jh; for(i=0;i<n;i++) for(j=0;j<n-i-1;j++) if(*(xuehao+j)>*(xuehao+j+1)) { zh=*(xuehao+j); *(xuehao+j)=*(xuehao+j+1); *(xuehao+j+1)=zh; jh=*(chengji+j); *(chengji+j)=*(chengji+j+1); *(chengji+j+1)=jh; } } int main(void) { float chengji[N]; int xuehao[N],n,i,j,x; while(1) { x=xuanzhe(); printf("\n%s\n\n您好,请输入您的学生人数:\n",caidan); do { scanf(" %d",&n); CLEAR; puts(" "); if(n<1||n>40) printf("对不起,您的输入有误,请重新输入:\n"); } while(n<1||n>40); system("cls"); switch(x) { case 1:shuru(chengji,xuehao,n); zuigao(chengji,xuehao,n); printf("\n\n%s\n\n",caidan); system("cls"); printf("\n\n\n%s\n",caidan); j=1; for(i=0;i<n;i++,j++) { printf(" %6d %8.2f ",xuehao[i],chengji[i]); if(j%2==0) puts(" "); } printf("\n%s\n",caidan); printf("最高成绩和学号是:\n%8.2f %6d",chengji[0],xuehao[0]); puts(" "); break; case 2: shuru(chengji,xuehao,n); zuidi(chengji,xuehao,n); printf("\n\n%s\n\n",caidan); system("cls"); j=1; for(i=0;i<n;i++,j++) { printf(" %6d %8.2f ",xuehao[i],chengji[i]); if(j%2==0) puts(" "); } printf("\n%s\n",caidan); printf("最低成绩和学号是:\n%8.2f %6d",chengji[0],xuehao[0]); puts(" "); break; case 3: shuru(chengji,xuehao,n); xuehao_gao(&xuehao,&chengji,n); system("cls"); printf("\n%s\n\n",caidan); j=1; for(i=0;i<n;i++,j++) { printf(" 学号:%6d成绩:%8.2f ",xuehao[i],chengji[i]); if(j%2==0) puts(" "); } puts(" "); break; case 4: shuru(chengji,xuehao,n); xuehao_di(&xuehao,&chengji,n); system("cls"); printf("\n%s\n\n",caidan); j=1; for(i=0;i<n;i++,j++) { printf(" 学号:%6d 成绩:%8.2f ",xuehao[i],chengji[i]); if(j%2==0) puts(" "); } puts(" "); } printf("请按任意键:\n"); getch(); } getch(); }