下面代码是我曾经帮一位同学完成课程作业的,不是完全符合题意~不过要说的是那代码没有实现排序功能,可以在此代码上添加~代码仅供参考~
PS:
没有看下面那个程序用结构体数组处理而不是用链表,那个还是忽略吧,我汗~~~~~~
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<math.h>
#define N 101
struct Student
{
char xh[3];
char name[20];
char sex[2];
int score[3];
int sum;
float ave;
}stu[N]={0};//初始化数据
int count=-1;
void menu();//菜单函数
void csh();
void tj();
void add(int flag,int n);
void find(int flag);//根据学号查询
void edit(int n);//修改学生信息
void del(int n);//删除学生信息
int judge_input1();
void judge_input2(void *s,void *p);
int judge_input3(void *p);
int judge_sex(void *p);
void print(int i);
int judge_empty();//判断学生信息是否为空
void ex();//退出系统函数
int main()
{
char ch=0;
while (1)
{
system("cls");
menu();
switch(ch=getch())
{
printf("\n");
case '1':csh();break;
case '2':add(0,0);break;
case '3':find(0);break;
case '4':find(1);break;
case '5':find(2);break;
case '6':tj();break;
case '7':ex();break;
default:break;
}
}
}
void menu()
{
printf("**************<<学生管理系统>>****************\n");
printf("\t1-----初始化信息\n");
printf("\t2-----追加学生信息\n");
printf("\t3-----按学号查询学生信息\n");
printf("\t4-----按学号修改学生信息\n");
printf("\t5-----按学号删除学生信息\n");
printf("\t6-----统计并浏览学生信息\n");
printf("\t7-----退出系统\n");
printf("**********************************************\n");
printf("请输入:\n");
}
void csh()
{
char *p1[][3]=
{
{"01","aa","男"},
{"02","bb","女"},
{"03","cc","男"},
{"04","dd","女"},
{"05","ee","男"},
};
int p2[][3]=
{
{80,76,67},
{70,80,90},
{75,85,95},
{87,67,78},
{78,89,79},
};
int i=0;
int j=0;
count=-1;
for (i=0;i<sizeof(p2)/sizeof(*p2);i++)
{
stu[i].sum=0;
strcpy(stu[i].xh,p1[i][0]);
strcpy(stu[i].name,p1[i][1]);
strcpy(stu[i].sex,p1[i][2]);
for (j=0;j<3;j++)
stu[i].sum+=stu[i].score[j]=p2[i][j];
stu[i].ave=(float)stu[i].sum/3;
count++;
}
printf("已初始化信息\n");
system("pause");
}
void tj()
{
int i=0;
int j=0;
if (judge_empty())
return;
system("cls");
printf("学生人数有%d人\n",count+1);
printf("%-10s%-10s%-4s ","学号","姓名","性别");
for (i=0;i<3;i++)
printf("科目%d",i+1);
printf("%5s%10s","总分","平均分");
printf("\n");
for (i=0;i<count+1;i++)
{
printf("%-10s",stu[i].xh);
printf("%-10s",stu[i].name);
printf("%-5s",stu[i].sex);
for (j=0;j<3;j++)
printf("%5d",stu[i].score[j]);
printf("%8d",stu[i].sum);
printf("%10.2f",stu[i].ave);
printf("\n");
}
system("pause");
}
void add(int flag,int n)
{
if (flag==0)
printf("现在进行追加数据\n");
else
{
printf("现在进行修改数据");
stu[n]=stu[N-1];//先把修改学生的数据清空,避免学号重复影响
}
system("pause");
while (1)
{
int i=0;
count++;
printf("\n请输入学生的学号:");
do
{
judge_input2("%s",stu[count].xh);
}while (judge_input3(stu[count].xh));
printf("\n请输入学生姓名:");
judge_input2("%s",stu[count].name);
printf("\n请输入学生性别(男\\女):");
do
{
judge_input2("%s",stu[count].sex);
}while(judge_sex(stu[count].sex));
stu[count].sum=0;
for (i=0;i<3;)
{
static flag=0;
if (flag==0)
printf("\n请输入科目%d的成绩:",i+1);
judge_input2("%d%*c",&stu[count].score[i]);
if (stu[count].score[i]<0||stu[count].score[i]>100)
{
printf("学生输入成绩数据范围有误,请重新输入\n");
flag=1;
}
else
{
stu[count].sum+=stu[count].score[i];
flag=0;
i++;
}
stu[count].ave=(float)stu[count].sum/3;
}
system("cls");
if (flag)
break;
printf("添加成功!\n");
printf("要继续添加数据吗?(Y\\N)");
if (judge_input1())
break;
}
}
int judge_input1()
{
char ch=0;
ch=getch();
if (ch=='Y'||ch=='y')
return 0;
else if (ch=='N'||ch=='n')
return 1;
else
{
printf("\n输入有误,请重新输入:");
return judge_input1();
}
}
void judge_input2(void *s,void *p)
{
while (scanf(s,p)!=1)
{
printf("输入数据有误,请重新输入\n");
fflush(stdin);
}
fflush(stdin);
}
int judge_input3(void *p)
{
int i=0;
char s[10]="\0";
char *p2=s;
for (i=0;i<count;i++)
if (strcmp(p,stu[i].xh)==0)
{
printf("该学生学号已经存在,请重新输入\n");
print(i);
return 1;
}
strcpy(s,p);
if (strcmp(p,"0")==0)
{
printf("学号输入不能为0");
return 1;
}
return 0;
}
int judge_sex(void *p)
{
if (strcmp(p,"男")!=0&&strcmp(p,"女")!=0)
{
printf("性别输入有误,请重新输入\n");
return 1;
}
return 0;
}
void print(int i)
{
int j=0;
printf("%-10s%-10s%-4s","学号","姓名","性别");
for (j=0;j<3;j++)
printf(" 科目%d",i+1);
printf("%5s%10s","总分","平均分");
printf("\n");
printf("%-10s",stu[i].xh);
printf("%-10s",stu[i].name);
printf("%-5s",stu[i].sex);
for (j=0;j<3;j++)
printf("%5d",stu[i].score[j]);
printf("%8d",stu[i].sum);
printf("%10.2f",stu[i].ave);
printf("\n");
}
void find(int flag)
{
char num[3];
system("cls");
if (judge_empty())
return;
if (flag==0)
printf("\n请输入需要查询的学号:");
else if (flag==1)
printf("\n请输入需要修改的学号:");
else if (flag==2)
printf("\n请输入需要删除学生的学号");
judge_input2("%s",num);
while (1)
{
int i=0;
system("cls");
while (i<count+1)
{
system("cls");
if (strcmp(num,stu[i].xh)==0&&flag==0)
{
printf("您需要查询的学号对应的学生信息为:\n");
print(i);
break;
}
else if (strcmp(num,stu[i].xh)==0&&flag==1)
{
printf("您需要修改的学号对应的学生信息为:\n");
print(i);
add(1,i);
edit(i);
break;
}
else if (strcmp(num,stu[i].xh)==0)
{
printf("您需要删除的学号对应的学生信息为:\n");
print(i);
del(i);
break;
}
i++;
}
if (i==count+1)
printf("没有查询到该学生\n");
if (flag==0)
printf("要继续查询吗?(Y\\N)");
else if (flag==1)
printf("要继续修改吗?(Y//N)");
else if (flag==2&&count!=-1)
printf("要继续删除吗?(Y//N)");
else if (flag==2&&count==-1)
{
printf("现在学生记录为空,请初始化信息\n");
system("pause");
break;
}
if (judge_input1())
break;
if (flag!=2)
printf("\n请输入需要查询的学号:");
else
printf("\n请输入需要删除学生的学号:");
judge_input2("%s",num);
}
}
void edit(int n)
{
stu[n]=stu[count];
count--;//修改完毕学生人数减少1
printf("修改成功!\n");
printf("修改后的学生信息为:\n");
print(n);
}
void del(int n)
{
int i=n;
printf("要确认删除吗(Y//N)\n");
if (judge_input1())
return;
for (;i<count+1;i++)
stu[i]=stu[i+1];
count--;
}
int judge_empty()
{
if (count==-1)
{
printf("现在学生记录为空,请初始化信息\n");
system("pause");
return 1;
}
return 0;
}
void ex()
{
printf("\t\t欢迎您的使用,再见\n");
exit(0);
}
[此贴子已经被作者于2017-2-10 22:53编辑过]