#include<string.h>
using
namespace
std
struct
student
{
char
name[50]
int
score
}
void
teacherC(student
*stu)//老师录入成绩
{
int
i,n
cin>>n
cout<<"请依次输入学生的姓名和成绩\n"
for(i=0i<ni++)
{
cin>>stu[i].name>>stu[i].score
}
stu[i].score=-1
}
void
show(student
*stu)
{
int
i=0
for(i<100i++)
{
if(stu[i].score==-1)
break
cout<<stu[i].name<<"
"<<stu[i].score<<endl
}
}
int
search(student
*stu,char
*name)
{
int
i=0,k=-1
for(i<100i++)
{
if(stu[i].score==-1)
break
if(strcmp(stu[i].name,name)==0)
{
k=i
break
}
}
return
k
}
void
edit(student
*stu)
{
int
i=0,j=0
char
name[30],an
while(1)
{
cout<<"请输入需要修改的学生名字\n"
cin>>name
i=search(stu,name)
if(i>=0)
{
cout<<"该学生的信息为:\n"
cout<<stu[i].name<<"
"<<stu[i].score<<endl
cout<<"确定要修改该学生的信息是的输入Y不是输入N:\n"
cin>>an
if(an=='Y'||an=='y')
{
cout<<"请输入修改后的姓名成绩:\n"
cin>>stu[i].name>>stu[i].score
cout<<"修改成功\n"
}
cout<<"继续修改请输入1退出输入0:\n"
cin>>j
if(j==0)
break
}
else
cout<<"没有该学生的信息\n"
}
}
void
dele(student
*stu)
{
int
i=0,j=0
char
name[30],an
while(1)
{
cout<<"请输入需要删除的学生名字\n"
cin>>name
i=search(stu,name)
if(i>0)
{
cout<<"该学生的信息为:\n"
cout<<stu[i].name<<"
"<<stu[i].score<<endl
cout<<"确定要删除该学生的信息是的输入Y不是输入N:\n"
cin>>an
if(an=='Y'||an=='y')
{
*stu[i].name=0
cout<<"删除成功\n"
}
cout<<"继续删除请输入1退出输入0:\n"
cin>>j
if(j==0)
break
}
else
cout<<"没有该学生的信息\n"
}
}
int
main()
{
int
i,n
char
name[30]
student
stu[100]//学生总数上限为100个
while(1)
{
cout<<"请做出以下的选择:"<<endl
cout<<"输入
0
退出系统\n"
cout<<"输入
1
教书录入成绩\n"
cout<<"输入
2
学生查询成绩\n"
cout<<"输入
3
修改学生成绩\n"
cout<<"输入
4
删除学生成绩\n"
cin>>i
switch(i)
{
case
1:
teacherC(stu)break
case
2:
cout<<"请输入查询的姓名\n"
cin>>namen=search(stu,name)cout<<"该学生成绩是:"<<stu[n].score<<endlbreak
case
3:
edit(stu)break
case
4:
dele(stu)break
default:if(i!=0)cout<<"请输入有效数字\n"
}
if(i==0)
break
}
return
0
}
同学你好!这是我自己写的,肯定不是最好的,但是已经能够满足你的要求了,后面附有注释,若有不懂的话请留言。
#include<stdio.h>
#include<stdlib.h>
#define
N
1024
typedef
struct
node
{
int
sno/*这里分别是学号(便于最后列表时统计),四门课的分数,总分,平均分*/
int
course1
int
course2
int
course3
int
course4
int
aver
}student
void
main()
{
int
i,j,number
int
total1,total2,total3,total4
total1=total2=total3=total4=0/*total指的是四门课程的平均分*/
student
st[N],temp
printf("Please
input
the
student's
number:")/*确定学生人数*/
scanf("%d",&number)
if(number>1024)
{
printf("The
number
is
too
large!\n")
exit(0)
}
for(i=0i<numberi++)/*进行成绩输入*/
{
printf("Please
input
the
%dth
student's
sno:",i+1)
scanf("%d",&st[i].sno)
printf("Please
input
the
%d's
student's
course1:",st[i].sno)
scanf("%d",&st[i].course1)
printf("Please
input
the
%d's
student's
course2:",st[i].sno)
scanf("%d",&st[i].course2)
printf("Please
input
the
%d's
student's
course3:",st[i].sno)
scanf("%d",&st[i].course3)
printf("Please
input
the
%d's
student's
course4:",st[i].sno)
scanf("%d",&st[i].course4)
st[i].aver=(st[i].course1+st[i].course2+st[i].course3+st[i].course4)/4
}
for(i=0i<numberi++)/*输出每个学生的平均分*/
printf("The
%d's
student's
aver-score
is:%d\n",st[i].sno,st[i].aver)
for(i=0i<numberi++)/*输出每科目平均分*/
{
total1+=st[i].course1
total2+=st[i].course2
total3+=st[i].course3
total4+=st[i].course4
}
total1/=number
total2/=number
total3/=number
total4/=number
printf("The
averscore
of
course1
is:%d\n",total1)
printf("The
averscore
of
course2
is:%d\n",total2)
printf("The
averscore
of
course3
is:%d\n",total3)
printf("The
averscore
of
course4
is:%d\n",total4)
for(i=0i<number-1i++)/*进行冒泡排序*/
for(j=i+1j<numberj++)
if(st[i].aver<st[j].aver)
{
temp=st[i]
st[i]=st[j]
st[j]=temp
}
printf("The
list
of
the
student's
course:\n")
printf("==========\n")/*列表公布成绩*/
for(i=0i<numberi++)
{
printf("%d.",i+1)
printf("%dth
student:%d\n",st[i].sno,st[i].aver)
}
}
上级运行成功,希望对你有所帮助!
祝你的成绩步步高升!
#include<stdio.h>#include<stdlib.h>
#include<string.h>
typedef struct
{
char id[15]
char name[10]
int score
} StudentInfo
typedef struct
{
StudentInfo arr[100]
int length
} SeqList
FILE *fp
char *filePath = "C:/StudentInfo.txt"
void initSeqList(SeqList *L)
{
L->length = 0
}
void insertSeqList(SeqList *L, int i, StudentInfo info)å
{
int j
for(j = L->lengthj >ij--)
L->arr[j] = L->arr[j-1]
L->arr[j] = info
L->length++
}
void printStudentInfo(StudentInfo info)
{
printf("%s\t%s\t%d\n", info.id, info.name, info.score)
}
void printSeqListWithSeqNo(SeqList L)
{
for(int i = 0i <L.lengthi++)
{
printf("%d\t", i+1)
printStudentInfo(L.arr[i])
}
}
void printSeqList(SeqList L)
{
for(int i = 0i <L.lengthi++)
printStudentInfo(L.arr[i])
}
int queryInfoById(SeqList L, char *id)
{
for(int i = 0i <L.lengthi++)
if(strcmp(id, L.arr[i].id)==0)
return i
return -1
}
int queryInfoByName(SeqList L, char *name)
{
for(int i = 0i <L.lengthi++)
if(strcmp(name, L.arr[i].name)==0)
return i
return -1
}
void save2File(SeqList L)
{
fp = fopen(filePath, "w")
for (int i = 0i <L.lengthi++)
fprintf(fp, "%s\t%s\t%d\n", L.arr[i].id, L.arr[i].name, L.arr[i].score)
}
void showMenu(SeqList L)
void addInfo(SeqList L)
{
StudentInfo info
int index
do {
printf("请输入学号:")
scanf("%s", info.id)
index = queryInfoById(L, info.id)
if (index != -1)
{
printf("输入的学号已存在。\n")
printStudentInfo(L.arr[index])
}
} while(index != -1)
do {
printf("请输入姓名:")
scanf("%s", info.name)
index = queryInfoByName(L, info.name)
if(index != -1)
{
printf("输入的姓名已存在。\n")
printStudentInfo(L.arr[index])
}
} while(index != -1)
printf("请输入成绩:")
scanf("%d", &info.score)
L.arr[L.length] = info
L.length++
save2File(L)
}
void editInfo(SeqList L)
{
StudentInfo info
char choice
printf("1. 按学号修改\n2. 按姓名修改\n3. 返回上一层\n")
while((choice=getchar())=='\n')
int index
if(choice == '1' || choice == '2')
{
if(choice == '1')
{
printf("请输入学号:")
scanf("%s", info.id)
index = queryInfoById(L, info.id)
} else {
printf("请输入姓名:")
scanf("%s", info.name)
index = queryInfoByName(L, info.name)
}
if(index != -1)
printStudentInfo(L.arr[index])
else {
printf("%s 为 %s 的学生不存在\n", choice==1?"学号":"姓名", choice==1?info.id:info.name)
editInfo(L)
return
}
printf("成绩改为:")
scanf("%d", &L.arr[index].score)
save2File(L)
}else if(choice == '3')
showMenu(L)
else {
printf("选择有误,请重新选择\n")
editInfo(L)
return
}
}
void insertSort(SeqList *L)
{
for(int j = 1j <L->lengthj++)
{
StudentInfo key = L->arr[j]
int i = j - 1
while(i >= 0 &&L->arr[i].score <key.score)
{
L->arr[i+1] = L->arr[i]
i--
}
L->arr[i+1] = key
}
}
void statisticInfo(SeqList L)
{
// 显示 60 分以下、60~79、80~89、90 分以上各分数段的学生信息
SeqList sectionListArr[4]
for (int i = 0i <4i++)
initSeqList(&sectionListArr[i])
float sum = 0, average, passedRate
for (int i = 0i <L.lengthi++)
{
int score = L.arr[i].score
sum += score
if(score <60)
insertSeqList(&sectionListArr[0], sectionListArr[0].length, L.arr[i])
else if(score >= 60 &&score <80)
insertSeqList(&sectionListArr[1], sectionListArr[1].length, L.arr[i])
else if(score >= 80 &&score <90)
insertSeqList(&sectionListArr[2], sectionListArr[2].length, L.arr[i])
else insertSeqList(&sectionListArr[3], sectionListArr[3].length, L.arr[i])
}
average = sum/L.length
passedRate = (L.length-sectionListArr[0].length)*100.0/L.length
insertSort(&L)
printf("60 分以下的学生:\n")
printSeqList(sectionListArr[0])
printf("\n60 ~ 79 分的学生:\n")
printSeqList(sectionListArr[1])
printf("\n80 ~ 89 分的学生:\n")
printSeqList(sectionListArr[2])
printf("\n90 分以上的学生:\n")
printSeqList(sectionListArr[3])
printf("\n及格率:%.1f %%\n平均分:%.1f\n", passedRate, average)
printf("按顺序从高到低排序:\n")
insertSort(&L)
printSeqListWithSeqNo(L)
printf("\n最高分:\n")
printStudentInfo(L.arr[0])
printf("\n最低分:\n")
printStudentInfo(L.arr[L.length-1])
}
void queryInfo(SeqList L)
{
char choice
printf("1. 按学号查询\n2. 按姓名查询\n3. 返回上层\n")
while((choice=getchar())=='\n')
int index
if (choice == '1')
{
char id[15]
printf("请输入学号:")
scanf("%s", id)
index = queryInfoById(L, id)
} else if(choice == '2') {
char name[10]
printf("请输入姓名:")
scanf("%s", name)
index = queryInfoByName(L,name)
} else if(choice == '3') {
showMenu(L)
return
} else {
printf("选择有误,请重新选择\n")
queryInfo(L)
return
}
if(index == -1)
printf("没有查询到结果\n")
else
printStudentInfo(L.arr[index])
}
void showMenu(SeqList L)
{
char choice
printf("\n1. 查询\n2. 添加成绩\n3. 修改成绩\n4. 统计分析\n5. 退出\n")
while((choice = getchar()) == '\n')
switch(choice)
{
case '1': queryInfo(L)break
case '2': addInfo(L)break
case '3': editInfo(L)break
case '4': statisticInfo(L)break
case '5': exit(0)
}
}
int main(void)
{
StudentInfo temp
SeqList *L = (SeqList*)malloc(sizeof(SeqList))
do {
initSeqList(L)
fp = fopen(filePath, "r")
int i = 0
while(fscanf(fp, "%s%s%d", temp.id, temp.name, &temp.score) != EOF)
insertSeqList(L, i++, temp)
showMenu(*L)
fclose(fp)
}while(1)
return 0
}