第1个回答 2014-07-03
参考代码如下,不过还是建议自己写一写比较好:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student //结构体
{
char name[20]; //姓名
char number[20]; //学号
double math; //数学
double english; //英语
double chinese; //语文
double program; //程序
}s[50];
void head() //界面
{
printf(
"**********************************************************************\n"
"** **\n"
"** **\n"
"** 学生成绩管理系统 **\n"
"** **\n"
"** 1.信息录入 **\n"
"** 2.信息统计 **\n"
"** 3.信息浏览 **\n"
"** 4.信息查询 **\n"
"** 5.信息排序 **\n"
"** 6.信息删除 **\n"
"** 0.退出系统 **\n"
"** **\n"
"**********************************************************************\n"
);
}
void daoru(struct student s[], int* n) //文件导入函数
{
FILE *p;
int i=*n;
if((p=fopen("数据.txt", "r"))==NULL)
{
n=n;
}
else
{
while(!feof(p))
{
fscanf(p, "%s%s%lf%lf%lf%lf\n", s[i].name, s[i].number, &s[i].math, &s[i].english, &s[i].chinese, &s[i].program);
i++;
*n=*n+1;
}
}
fclose(p);
}
void daochu(struct student s[], int n)
{
FILE *p;
int i=0;
if((p=fopen("数据.txt", "w"))==NULL)
{
printf("无法打开此文件!");
}
else
{
while(i<n-1)
{
fprintf(p, "%s %s %lf %lf %lf %lf\n", s[i].name, s[i].number, s[i].math, s[i].english, s[i].chinese, s[i].program);
i++;
}
fprintf(p, "%s %s %lf %lf %lf %lf", s[i].name, s[i].number, s[i].math, s[i].english, s[i].chinese, s[i].program);
}
fclose(p);
}
void dayin(struct student s[], int n) //显示所有信息
{
int i;
double all=0.0;
printf("\n姓名\t\t学号\t\t数学\t英语\t语文\t程序\t总分\n");
for (i=0; i<n; i++)
{
all=s[i].math+s[i].english+s[i].chinese+s[i].program;
printf("%s\t\t%s\t\t%.1lf\t%.1lf\t%.1lf\t%.1lf\t%.1lf\n", s[i].name, s[i].number, s[i].math, s[i].english, s[i].chinese, s[i].program, all);
}
}
void shuru(struct student s[], int* n) //信息输入函数
{
int i=*n, j, k, m;
printf("请输入学生姓名:");
scanf("%s", s[i].name);
printf("请输入学生学号:");
for (j=0; ; j++)
{
m=0;
scanf("%s", s[i].number);
for (k=0; k<i; k++)
{
if (strcmp(s[i].number, s[k].number)==0)
{
m=m+1;
printf("学号重复,请重新输入学号:");
break;
}
}
if (m==0)
{
break;
}
}
printf("请输入数学成绩:");
scanf("%lf", &s[i].math);
printf("请输入英语成绩:");
scanf("%lf", &s[i].english);
printf("请输入语文成绩:");
scanf("%lf", &s[i].chinese);
printf("请输入程序成绩:") ;
scanf("%lf", &s[i].program);
printf("添加信息成功!\n");
*n=*n+1;
daochu(s, *n);
}
void paixu(struct student s[], int n) //排序函数
{
int i, j;
double all1, all2;
struct student stu;
for (i=0; i<n-1; i++)
{
for (j=i+1; j<n; j++)
{
all1=s[i].math+s[i].english+s[i].chinese+s[i].program;
all2=s[j].math+s[j].english+s[j].chinese+s[j].program;
if (all1<all2)
{
stu=s[i];
s[i]=s[j];
s[j]=stu;
}
}
}
printf("排序后的数据:\n");
dayin(s, n);
}
void chazhao(struct student s[], int n) //查找函数
{
char name[20], num[20];
int m1, m2=0, i, j;
printf("1.按姓名查找\n2.按学号查找\n选择查询方式(1或2):");
scanf("%d", &m1);
if (m1==1)
{
printf("请输入您要查找的学生姓名:");
scanf("%s", name);
for (i=0; i<n; i++)
{
if (strcmp(s[i].name, name)==0)
{
m2=m2+1;
if (m2==1)
{
printf("\n姓名\t\t学号\t数学\t英语\t语文\t程序\n");
}
printf("%s\t\t%s\t\t%s\t\t%s\t\t%s\n", s[i].name, s[i].number, s[i].math, s[i].english, s[i].chinese, s[i].program);
}
}
if (m2==0)
{
printf("没有此学生信息!\n");
}
}
else if (m1==2)
{
printf("请输入您要查找的学生学号:");
scanf("%s", num);
j=0;
for (i=0; i<n; i++)
{
if (strcmp(s[i].number, num)==0)
{
m2=m2+1;
if (m2==1)
{
printf("\n姓名\t\t学号\t数学\t英语\t语文\t程序\n");
}
printf("%s\t\t%s\t\t%s\t\t%s\t\t%s\n", s[i].name, s[i].number, s[i].math, s[i].english, s[i].chinese, s[i].program);
}
}
if (m2==0)
{
printf("没有此学生信息!\n");
}
}
}
void shanchu(struct student s[], int* n) //删除函数
{
char num[20];
int m=0, i, j;
printf("请输入您要删除的学生学号:");
scanf("%s", num);
for (i=0; i<=*n; i++)
{
if (strcmp(s[i].number, num)==0)
{
m=m+1;
for (j=i; j<*n; j++)
{
s[j]=s[j+1];
}
*n=*n-1;
}
}
if (m==0)
{
printf("没有此学生信息!\n");
}
else
{
daochu(s, *n);
printf("删除完毕!\n");
}
}
void allAndAver(struct student s[], int n)
{
int i ;
double all=0.0, aver=0.0;
for (i=0; i<n; i++)
{
all=s[i].math+s[i].english+s[i].chinese+s[i].program;
aver=all/4;
printf("%s\t\t%s\t\t%.1lf\t%.1lf\n", s[i].name, s[i].number, all, aver);
}
}
void Fail(struct student s[], int n) //统计单科不及格人数
{
int i, fail[4]={0};
for (i=0; i<n; i++)
{
if (s[i].math<60)
{
fail[0]++;
}
if (s[i].english<60)
{
fail[1]++;
}
if (s[i].chinese<60)
{
fail[2]++;
}
if (s[i].program<60)
{
fail[3]++;
}
}
printf("\n不及格信息:\n");
printf("数学不及格的人数为:%d人\n", fail[0]);
printf("英语不及格的人数为:%d人\n", fail[1]);
printf("语文不及格的人数为:%d人\n", fail[2]);
printf("程序不及格的人数为:%d人\n", fail[3]);
}
void Max(struct student s[], int n) //统计单科最高分人数
{
int i, counter[4]={0};
double max[4]={s[0].math, s[0].english, s[0].chinese, s[0].program};
for (i=0; i<n; i++)
{
if (s[i].math>max[0])
{
max[0]=s[i].math;
}
if (s[i].math>max[1])
{
max[1]=s[i].math;
}
if (s[i].math>max[2])
{
max[2]=s[i].math;
}
if (s[i].math>max[3])
{
max[3]=s[i].math;
}
}
for (i=0; i<n; i++)
{
if (s[i].math==max[0])
{
counter[0]++;
}
if (s[i].math==max[1])
{
counter[1]++;
}
if (s[i].math==max[2])
{
counter[2]++;
}
if (s[i].math==max[3])
{
counter[3]++;
}
}
printf("\n最高分信息:\n");
printf("数学最高分为:%.1lf, 人数为:%d人\n", max[0], counter[0]);
printf("英语最高分为:%.1lf, 人数为:%d人\n", max[1], counter[1]);
printf("语文最高分为:%.1lf, 人数为:%d人\n", max[2], counter[2]);
printf("程序最高分为:%.1lf, 人数为:%d人\n", max[3], counter[3]);
}
void Min(struct student s[], int n) //统计单科最低分人数
{
int i, counter[4]={0};
double min[4]={s[0].math, s[0].english, s[0].chinese, s[0].program};
for (i=0; i<n; i++)
{
if (s[i].math<min[0])
{
min[0]=s[i].math;
}
if (s[i].math<min[1])
{
min[1]=s[i].math;
}
if (s[i].math<min[2])
{
min[2]=s[i].math;
}
if (s[i].math<min[3])
{
min[3]=s[i].math;
}
}
for (i=0; i<n; i++)
{
if (s[i].math==min[0])
{
counter[0]++;
}
if (s[i].math==min[1])
{
counter[1]++;
}
if (s[i].math==min[2])
{
counter[2]++;
}
if (s[i].math==min[3])
{
counter[3]++;
}
}
printf("\n最低分信息:\n");
printf("数学最低分为:%.1lf, 人数为:%d人\n", min[0], counter[0]);
printf("英语最低分为:%.1lf, 人数为:%d人\n", min[1], counter[1]);
printf("语文最低分为:%.1lf, 人数为:%d人\n", min[2], counter[2]);
printf("程序最低分为:%.1lf, 人数为:%d人\n", min[3], counter[3]);
}
void tongji(struct student s[], int n) //统计函数
{
printf("统计信息如下:\n");
printf("\n姓名\t\t学号\t\t总分\t平均分\n");
allAndAver(s, n);
Max(s, n);
Min(s, n) ;
Fail(s, n);
}
int main() //主函数
{
int k, n=0;
daoru(s, &n);
daochu(s, n);
while (1)
{
head();
printf("\n请按对应的键选择相应的功能:");
scanf("%d",&k);
switch (k)
{
case 1:
shuru(s, &n);
break;
case 2:
tongji(s, n);
break;
case 3:
dayin(s, n);
break;
case 4:
chazhao(s, n);
break;
case 5:
paixu(s, n);
break;
case 6:
shanchu(s, &n);
break;
case 0:
exit(1);
break;
default : printf("请输入正确的命令!\n");
}
system("pause");
system("cls");
}
return 0;
}
第2个回答 推荐于2017-11-24
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 1000
/*定义学生成绩信息结构*/
struct stu
{
char id[8];
char name[8];
double Chinese;
double Math;
double English;
double average;
double total;
};
/*学生结构数组,用于存储学生成绩信息*/
struct stu students[MAX];
/*当前学生人数*/
int current;
void input()
{
int i;
printf("请输入学生人数:");
scanf("%d", & current);
for (i = 0; i < current; i++)
{
printf("\n请输入学生学号,最多为7位数: ");
scanf("%s", students[i].id);
printf("请输入学生姓名:");
scanf("%s", students[i].name);
printf("请输入语文成绩:");
scanf("%lf", &students[i].Chinese);
printf("请输入数学成绩:");
scanf("%lf", &students[i].Math);
printf("请输入英语成绩:");
scanf("%lf", &students[i].English);
students[i].total = students[i].Chinese + students[i].Math + students[i].English;
students[i].average = students[i].total / 3;
}
}
/*排名次,即对学生结构数组排序*/
void sort(struct stu array[], int n)
{
int i, j;
struct stu temp;
for (i = 0; i < n; i++)
{
for (j = i+1; j < n; j++)
{
if (array[i].average - array[j].average < 1e-16)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
/*统计平均成绩,并输出各分数段的人数*/
void print_score(struct stu array[], int n)
{
int i;
double ave = 0;
int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
for (i = 0; i < n; i++)
{
ave += array[i].average;
switch ((int)(array[i].average / 10))
{
case 10:
case 9: sum1++; break;
case 8: sum2++; break;
case 7:
case 6: sum3++; break;
default: sum4++;
}
}
printf("全班的平均分为:%.1f\n", ave/n);
printf("平均分在90分以上的人数为:%d\n", sum1);
printf("平均分在80~89的人数为:%d\n", sum2);
printf("平均分在60~79的人数为:%d\n", sum3);
printf("平均分在60分以下的人数为:%d\n", sum4);
}
/*输出不及格科目及学生名单*/
void print_unreach(struct stu array[], int n)
{
int i;
printf("科目\t学号\t名字\n");
for (i = 0; i < n; i++)
{
if (array[i].Chinese < 60)
{
printf("语文\t%s\t%s\n", array[i].id, array[i].name);
}
if (array[i].Math < 60)
{
printf("数学\t%s\t%s\n", array[i].id, array[i].name);
}
if (array[i].English < 60)
{
printf("英语\t%s\t%s\n", array[i].id, array[i].name);
}
}
}
/*打印优等生名单*/
void print_good(struct stu array[], int n)
{
int i;
char mark = 0;
if (n > 3)
{
n = 3;
}
printf("学号\t姓名\t语文\t数学\t英语\t平均分\t名次\n");
for (i = 0; i < n; i++)
{
if ((array[i].Chinese > 60) && (array[i].Math > 60) && (array[i].English > 60))
{
if (array[i].average > 90) mark = 1;
else if (array[i].average > 85)
{
if (array[i].Chinese == 100) mark = 1;
if (array[i].Math == 100) mark = 1;
if (array[i].English == 100) mark = 1;
if (array[i].Chinese > 95 && array[i].English > 95) mark = 1;
if (array[i].Chinese > 95 && array[i].Math > 95) mark = 1;
if (array[i].Math > 95 && array[i].English > 95) mark = 1;
}
if (mark == 1)
{
printf("%s\t%s\t", array[i].id, array[i].name);
printf("%.f\t%.f\t%.f\t%.1f\t", array[i].Chinese, array[i].Math, array[i].English, array[i].average);
printf("%d\n", i+1);
}
}
}
}
/*按学生姓名查询成绩*/
void search_name(struct stu array[], int n, char* name)
{
int i;
char mark = 1;
for (i = 0; i < n; i++)
{
if (strcmp(name, array[i].name) == 0)
{
printf("学生姓名:%s\n", name);
printf("学生学号:%s\n", array[i].id);
printf("语文:%.f\n", array[i].Chinese);
printf("数学:%.f\n", array[i].Math);
printf("英语:%.f\n", array[i].English);
printf("平均分:%.1f\n", array[i].average);
mark = 0;
}
}
if (mark == 1) puts("不存在该学生记录");
}
/*按学生学号查询成绩*/
void search_id(struct stu array[], int n, char* id)
{
int i;
char mark = 1;
for (i = 0; i < n; i++)
{
if (strcmp(id, array[i].id) == 0)
{
printf("学生姓名:%s\n", array[i].name);
printf("学生学号:%s\n", array[i].id);
printf("语文:%.f\n", array[i].Chinese);
printf("数学:%.f\n", array[i].Math);
printf("英语:%.f\n", array[i].English);
printf("平均分:%.1f\n", array[i].average);
mark = 0;
break;
}
}
if (mark == 1) puts("不存在该学生记录");
}
void print_menu()
{
system("cls");
printf("1.输入学生信息\n");
puts("2.统计全班学生成绩");
puts("3.按学号查询学生成绩");
puts("4.按姓名查询学生成绩");
puts("5.输出不及格情况");
puts("6.输出优等生名单");
puts("0.退出本程序");
printf("\n\n 请输入你的选择:");
}
int main(int argc, char **argv)
{
char ch = 0;
char id[8], name[8];
while (ch != '0')
{
print_menu();
ch = getchar();
switch (ch)
{
case '1':
{
system("cls");
input();
sort(students, current);
break;
}
case '2':
{
system("cls");
print_score(students, current);
break;
}
case '3':
{
system("cls");
printf("请输入学号:");
scanf("%s", id);
search_id(students, current, id);
break;
}
case '4':
{
system("cls");
printf("请输入姓名:");
scanf("%s", name);
search_name(students, current, name);
break;
}
case '5':
{
system("cls");
print_unreach(students, current);
break;
}
case '6':
{
system("cls");
print_good(students, current);
break;
}
case '0': exit(0);
}
printf("\n\n按任意键返回主菜单……");
getchar();
getchar();
}
return 0;
}本回答被提问者采纳
第3个回答 2014-06-25
去年写的,你稍微改下加个成绩就行
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
FILE *fp;
struct student{
char name[10];
char number[12];
char age[3];
struct student *next;
};
struct student *creat(); //手动创建链表
struct student *build_list(); //从文件读取信息创建链表
void read_list(struct student *head); //打印链表
void search(struct student *head); //查找相应信息
struct student *delete_(struct student *head); //删除相应信息
void ad_list(struct student *head); //增加信息
int control(struct student *head); //控制函数
void head_print(); //打印界面函数
void change_list(struct student *head); //更改链表信息函数
void save(struct student *head); //保存文件
void remove(struct student *head); //递归释放链表空间
char name[10],number[12],age[3]; //学生姓名、学号、年龄全局变量
int main(){
struct student *head;
head=(struct student*)malloc(sizeof(struct student));
control(head);
free(head);
fclose(fp);
return 0;
}
struct student *creat(){
struct student *head;
char name[10],number[12],age[3];
struct student *p,*r,*p1;
head=(struct student *)malloc(sizeof(struct student));
p=head->next;
r=head;
printf("依次输入姓名学号年龄(按q退出):\n");
scanf("%s",name);
while(name[0]!='q'){
p1=(struct student*)malloc(sizeof(struct student));
scanf("%s",number);
scanf("%s",age);
strcpy(p1->name,name);
strcpy(p1->number,number);
strcpy(p1->age,age);
p1->next=NULL;
r->next=p1;
r=p1;
scanf("%s",name);
}
printf("建立成功\n");
return head;
}
/*若无文件或文件为空,用链表创建函数,为防止占用多余内存在循环入口处进行判断,函数返回头指针地址*/
struct student *build_list(){
char ch;
struct student *head;
struct student *p1,*r;
rewind(fp);
head=(struct student*)malloc(sizeof(struct student));
r=head;
while(!feof(fp)){
p1=(struct student*)malloc(sizeof(struct student));
fscanf(fp,"%s%s%s",p1->name,p1->number,p1->age);
p1->next=NULL;
r->next=p1;
r=p1;
fseek(fp,2L,SEEK_CUR);
if((ch=getc(fp))==EOF)
break;
else
fseek(fp,-3L,SEEK_CUR);
}
return head;
}
/*文件内有信息时按行读取文件信息并创建链表,为防止误读结尾换行符,用fseek函数在每次循环后移两单位判断是否为空
,若为空结束循环,若不为空则用fseek函数前移三个单位*/
void read_list(struct student *head){
struct student *p;
p=head->next;
if(p==NULL){
head=creat();
}
printf("*******************************************\n");
while(p!=NULL){
printf("%-20s%-12s%-3s\n",p->name,p->number,p->age);
p=p->next;
}
printf("*******************************************\n");
}
/*输出链表信息,如果进行删除操作后链表为空则转到链表创建函数;按行读取文件信息并输出。*/
void search(struct student *head){
char s[20];
struct student *p;
int k=0;
printf("请输入要查询的字符串:\n");
p=head->next;
scanf("%s",s);
printf("查询结果:\n");
while(p!=NULL){
if((strcmp(s,p->age)&&strcmp(s,p->name)&&strcmp(s,p->number))==0){
printf("%-20s%-12s%-3s\n",p->name,p->number,p->age);
k=1;
}
p=p->next;
}
if(k==0)
printf("无记录\n");
}
/*查找函数,*/
struct student *delete_(struct student *head){
char s[20];
char ch;
int v=0;
struct student *p,*q;
printf("请输入要删除的学生的学号:\n");
scanf("%s",s);
p=head->next;
q=head;
while(p!=NULL){
if(strcmp(s,p->number)==0){
q->next=p->next;
v=1;
free(p);
break;
}
q=p;
p=p->next;
}
if(v==1)
printf("删除成功\n");
else{
printf("未删除成功\n");
return q->next;
}
if(head->next==NULL){
printf("已无信息,是否输入新的(Y/N):\n");
scanf("%c",&ch);
ch=getchar();
if(ch=='y')
head=creat();
return head;
}
return q->next;
}
void ad_list(struct student *head){
char s[20];
int v=0;
struct student *p,*q;
q=(struct student *)malloc(sizeof(struct student));
p=head->next;
printf("插在学号为多少的学生后?\n");
scanf("%s",s);
while(p!=NULL){
if(strcmp(s,p->number)==0){
printf("请输入学生信息(姓名 学号 年龄):\n");
scanf("%s%s%s",q->name,q->number,q->age);
q->next=p->next;
p->next=q;
v=1;
break;
}
p=p->next;
}
if(v==0)
printf("插入失败\n");
}
void change_list(struct student *head){
char s1[12];
char ch;
struct student *p;
printf("请输入要更改的学生的学号:\n");
scanf("%s",s1);
p=head->next;
while(p!=NULL){
if(strcmp(s1,p->number)==0)
break;
p=p->next;
}
printf("请输入要更改的内容:\na.姓名\nb.学号\nc.年龄\n");
scanf("%c",&ch);
ch=getchar();
printf("请输入更改后结果:\n");
scanf("%s",s1);
switch(ch){
case'a':strcpy(p->name,s1);break;
case'b':strcpy(p->number,s1);break;
case'c':strcpy(p->age,s1);break;
}
}
void save(struct student *head){
struct student *p;
p=head->next;
fp=fopen("stu.out","w+");
rewind(fp);
while(p!=NULL){
fprintf(fp,"%-20s%-12s%3s\n",p->name,p->number,p->age);
p=p->next;
}
printf("保存成功\n");
}
void remove(struct student *head){
if(head==NULL)
return;
remove(head->next);
free(head);
}
int control(struct student *head){
int i=0;
char ch;
head_print();
if((fp=fopen("stu.out","r"))==NULL){
printf("无文件,请先建立:\n");
head=creat();
}
else if((ch=getc(fp))==EOF){
printf("无信息,请先建立:\n");
head=creat();
}
else
head=build_list();
while((ch=getchar())!='q'){
switch(ch){
case'a':search(head);break;
case'b':delete_(head);break;
case'c':ad_list(head);break;
case'd':change_list(head);break;
case'e':read_list(head);break;
case'f':save(head);break;
case'h':head_print();
}
}
if(ch=='q')
remove(head);
return 0;
}
void head_print(){
printf("%13.0s********************************************%13.0s\n");
printf("%13.0s**%40.0s**\n");
printf("%13.0s**%9.0sa.查找%9.0sb.删除%10.0s**\n");
printf("%13.0s**%40.0s**\n");
printf("%13.0s**%9.0sc.增加%9.0sd.更改%10.0s**\n");
printf("%13.0s**%40.0s**\n");
printf("%13.0s**%9.0se.查看%9.0sf.存档%10.0s**\n");
printf("%13.0s**%40.0s**\n");
printf("%13.0s**%9.0s按h显示提示信息,q退出%10.0s**\n");
printf("%13.0s********************************************%13.0s\n");
}