学生成绩处理
#include<stdio.h>
#include<string.h>
typedef struct {
char sno[10];
char name[16];
int score[4];
int total;
int mc;
}student;
void sr(student stud[], int n)
{
int i;
for (i = 1; i <= n; i++)
{
printf("输入学号 姓名(中间留一个空格):\n");
scanf("%s %s", stud[i].sno, stud[i].name);
printf("成绩1,成绩2,成绩3,成绩4\n");
scanf("%d,%d,%d,%d", &stud[i].score[0], &stud[i].score[1], &stud[i].score[2], &stud[i].score[3]);
stud[i].total = stud[i].score[0] + stud[i].score[1] + stud[i].score[2] + stud[i].score[3];
stud[i].mc = 1;
}
}
void cx(student stud[], int n)
{
int xz, i;
char no[10], nm[16];
printf(" 1.按学号查询 2.按姓名查询\n");
printf(" 请输入 1 或 2 并按回车键:");
scanf("%d", &xz);
getchar();
if (xz == 1) {
printf("请输入学号:");
gets(no);
}
else
if (xz == 2) {
printf("请输入姓名:");
gets(nm);
}
for (i = 1; i <= n; i++)
{
switch(xz){
case 1:
if (strcmp(stud[i].sno, no) == 0) {
printf("学号 姓名 成绩一 成绩二 成绩三 成绩四 总分 名次\n");
printf("%12s,%18s,%6d,%6d,%6d,%6d,%6d,%4d\n",
stud[i].sno, stud[i].name, stud[i].score[0], stud[i].score[1],
stud[i].score[2], stud[i].score[3], stud[i].total, stud[i].mc);
return;
}
case 2:
if (strcmp(stud[i].name, nm) == 0) {
printf("%12s,%18s,%6d,%6d,%6d,%6d,%6d,%4d\n",
stud[i].sno, stud[i].name, stud[i].score[0], stud[i].score[1],
stud[i].score[2], stud[i].score[3], stud[i].total, stud[i].mc);
return;
}
}
}
if (i>n) printf("你要查找的学生没查到!\n");
}
void xg(student stud[], int n)
{
char no[10];
int i;
printf("请输入要修改成绩的学生学号:");
getchar();
gets(no);
for (i = 1; i <= n; i++) {
if (strcmp(stud[i].sno, no) == 0) {
printf("%12s,%18s,%6d,%6d,%6d,%6d,%6d,%4d\n",
stud[i].sno, stud[i].name, stud[i].score[0], stud[i].score[1],
stud[i].score[2], stud[i].score[3], stud[i].total, stud[i].mc);
printf("输入最新的成绩:成绩一,成绩二,成绩三,成绩四\n");
scanf("%d,%d,%d,%d",&stud[i].score[0], &stud[i].score[1], &stud[i].score[2], &stud[i].score[3]);
stud[i].total = stud[i].score[0] + stud[i].score[1] + stud[i].score[2] + stud[i].score[3];
break;
}
}
if (i>n) printf("你要查找的学生没查到!\n");
}
void qdmc(student stud[], int n)
{
int i;
for (i = 1; i <= n; i++)
stud[i].mc = i;
for (i = 2; i <= n; i++)
if (stud[i].total == stud[i - 1].total)
stud[i].mc = stud[i - 1].mc;
}
void sc(student stud[], int n)
{
int i;
printf("学号 姓名 成绩一 成绩二 成绩三 成绩四 总分 名次\n");
for (i = 1; i<-n; i++)
printf("%12s,%18s,%6d,%6d,%6d,%6d,%6d,%4d\n",
stud[i].sno, stud[i].name, stud[i].score[0], stud[i].score[1],
stud[i].score[2], stud[i].score[3], stud[i].total, stud[i].mc);
}
void px(student stud[], int n)
{
int xz, i;
int d[5], t;
void DbubbleSort(student stud[], int n), ShellSort(student stud[], int d[], int n, int t),
QuickSort(student stud[], int, int), HeapSort(student
stud[], int), qdmc(student stud[], int);
printf("***排序方法选择***\n");
printf("==================\n");
printf(" 1.双向冒泡排序 \n");
printf(" 2.希 尔 排 序 \n");
printf(" 3.块 速 排 序 \n");
printf(" 4.堆 排 序 \n");
printf("==================\n");
printf(" 请选择:1,2,3,4:\n");
scanf("%d", &xz);
switch (xz) {
case 1:
DbubbleSort(stud, n);
break;
case 2:
printf("输入增量的个数:");
scanf("%d", &t);
printf("输入增量");
for (i = 0; i<t; i++)
scanf("%d", &d[i]);
ShellSort(stud,d, n, t);
break;
case 3:
QuickSort(stud,1,n);
break;
case 4:
HeapSort(stud, n);
break;
}
qdmc(stud, n);
}
void DbubbleSort(student R[], int n)
{
int i, j;
student t;
int NoSwap;
NoSwap = 1;
i = 1;
while (NoSwap)
{
NoSwap = 0;
for (j = n - i + 1; j >= i + 1; j--)
if (R[j].total>R[j - 1].total)
{
t = R[j];
R[j] = R[j + 1];
R[j + 1] = t;
NoSwap = 1;
}
i = i + 1;
}
}
void ShellInsert(student R[], int n, int dk)
{
int i, j;
for (i = dk + 1; i <= n; i++)
if (R[i].total > R[i-dk].total)
{
R[0] = R[i];
j = i - dk;
while (j>0 && R[0].total>R[j].total)
{
R[j + dk] = R[j];
j = j - dk;
}
R[j + dk] = R[0];
}
}
void ShellSort(student R[], int d[], int n, int t)
{
int k;
for (k = 0; k<t; k++)
ShellInsert(R,n, d[k]);
}
int Partition(student R[], int i, int j)
{
student x = R[i];
while (i<j) {
while (i<j && R[j].total <= x.total)
i++;
if (i<j) {
R[j] = R[i];
j--;
}
}
R[i] = x;
return i;
}
void QuickSort(student R[], int low, int high)
{
int p;
if (low<high) {
p = Partition(R, low, high);
QuickSort(R, low, p - 1);
QuickSort(R, p + 1, high);
}
}
void Sift(student R[], int i, int h)
{
int j;
student x = R[i];
j = 2 * i;
while (j <= h) {
if (j<h && R[j].total>R[j + 1].total)
j++;
if (x.total<R[j + 1].total) break;
R[i] = R[j];
i = j;
j = 2 * i;
}
R[i] = x;
}
void HeapSort(student R[], int n)
{
int i;
for (i = n / 2; i>0; i--)
Sift(R, i, n);
for (i = n; i>1; i--)
{
R[0] = R[1];
R[1] = R[i];
R[i] = R[i] = R[0];
Sift(R, 1, i - 1);
}
}
int main(void)
{
student stud[41];
int xz = 1, n;
printf("请输入学生数:");
scanf("%d", &n);
while (xz) {
printf("***学生成绩管理***\n");
printf("==================\n");
printf(" 1.学生信息输入 \n");
printf(" 2.学生信息查询 \n");
printf(" 3.学生信息修改 \n");
printf(" 4.学生成绩排序 \n");
printf(" 5.学生成绩输出 \n");
printf(" 0.结 束 程 序 \n");
printf("==================\n");
printf("请选择:1,2,3,4,5,0:");
scanf("%d", &xz);
switch (xz) {
case 1:
sr(stud, n);
break;
case 2:
cx(stud, n);
break;
case 3:
xg(stud, n);
break;
case 4:
px(stud, n);
break;
case 5:
sc(stud, n);
}
}
}
推荐大家一个小巧轻便的C语言编译工具
点击连接