大部分代码照着书上打的,自己修改写一些布局和数据后的成果如下
程序代码如下
#include<stdio.h>
#include<string.h>
#include <windows.h>
#define N 4
void gotoxy(int x, int y) //定义gotoxy函数 (使光标移动到指定位置)
{
int xx=0x0b;
HANDLE hOutput;
COORD loc;
loc.X = x;
loc.Y=y;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput, loc);
return;
}
int main()
{
char name[N][20],name_t[20];
int score[N][3],sum[N];
int i,j,t;
//clrscr();
printf("*********************************************************\n*\t\t\t\t\t\t\t*\n*\t\t\t\t\t\t\t*\n");
printf("*\t\t请输入学生名字和分数,回车结束\t\t*\n*\t\t\t\t\t\t\t*\n*\t\t\t\t\t\t\t*\n*********************************************************\n\n\n");
printf("%-12s%-12s%-12s%-12s\n\n","名字","语文","数学","C语言");
for(i=0;i<N;i++)
{
sum[i]=0;
gotoxy(0,10+i);
scanf("%s",&name[i]);
for(j=0;j<3;j++)
{
gotoxy((j+1)*12,10+i);
scanf("%d",&score[i][j]);
sum[i]=sum[i]+score[i][j];
}
}
//——————————————————数据排列————————————————————
for(i=0;i<N-1;i++)
for(j=i+1;j<N;j++)
if(sum[j]>sum[i])
{
t=sum[i];
sum[i]=sum[j];
sum[j]=t;
strcpy(name_t,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],name_t);
}
printf("\n\n************************成绩排名****************************\n\n");
printf("%-12s%-15s%-15s%-15s\n","名次","姓名","总分","平均分");
for(i=0;i<N;i++)
{
printf("%-12d%-15s%-15d%-15.2f\n",i+1,name[i],sum[i],sum[i]/3.0);
}
printf("\n\n************************************************************\n\n");
return 0;
}