#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxsize 5
struct student {
int num;
char name[30];
int english,computer,math,chinese;
int rank;
};
float aen,aco,ama,ach;int maxch,maxma,maxen,maxco,minch,minma,minen,minco,sum[5];
//void menu (struct student students []);
void input_student (struct student students []);
void change_student (struct student *p);
void search_student (struct student *p);
//void cal (struct student s[]);
void max (struct student s[]);
void min (struct student s[]);
void sort (struct student students[]);
void output_student (struct student students []);
void input_student (struct student students [])
{
int i;
for (i=0;i<5;i++){
printf("********************************\n");
printf("請輸入:\n學號:");
scanf_s("%d",&students [i].num);getchar();
printf("姓名:");
scanf_s("%s",students [i].name);getchar();
printf("英語:");
scanf_s("%d",&students [i].english);getchar();
printf("計算機:");
scanf_s("%d",&students [i].computer);getchar();
printf("語文:");
scanf_s("%d",&students [i].math);getchar();
printf("數學:");
scanf_s("%d",&students [i].chinese);getchar();
}
}
void output_student (struct student students [ ])
{
int i;
printf ("---------------------------------------------------------------------------\n");
printf ("num\t name\t english\t computer\t math\t chinese\t rank\n");
for (i=0;i<5;i++){
printf("%d\t",students [i].num);
printf(" %s\t",students [i].name);
printf(" %d\t",students [i].english);
printf(" %d\t",students [i].computer);
printf(" %d\t",students [i].math);
printf(" %d\t",students [i].chinese);
printf(" %d\t\n",students [i].rank);}
max (students);
min (students);
printf("平均分 %.2f\t%.2f\t%.2f\t%.2f\n",aen,aco,ama,ach);
printf("最高分 %d\t%d\t%d\t%d\n",maxen,maxco,maxma,maxch); /*分數的調用*/
printf("最低分 %d\t%d\t%d\t%d\n",minen,minco,minma,minch);
printf ("---------------------------------------------------------------------------");
}
void search_student (struct student students [ ] )
{
int i,way,j;
char a[20],cour;
printf("choice the way:1.姓名2.課程3.總分名次");
scanf_s("%d",&way);getchar();
switch(way){
case 1:
printf("姓名:");
scanf_s("%s",a);getchar();
for (i=0;i<5;i++){
if (strcmp(students[i].name,a)==0) { /*逐個比較相等為0 ,不等返回不相等的差值 !strcmp(students[i].name, a)*/
printf(" num:%d",students [i].num);
printf(" english:%d",students [i].english);
printf(" computer:%d",students [i].computer);
printf(" math:%d",students [i].math);
printf("chinese:%d\n",students [i].chinese);}
}
case 2:printf("課程:");
printf("a-英語,b-計算機,c-數學,d-語文:");
scanf_s("%c",&cour);
if(cour=='a') {
printf(" 英語:");
for (i=0;i<5;i++) printf(" %d",students [i].english);
printf("平均分: %f",aen);
printf(" 最高分:%d",maxen);
printf("最低分: %d",minen);
}
if(cour=='b') {
printf(" 計算機:");
for (i=0;i<5;i++) printf(" %d",students [i].computer);
printf("平均分: %f",aco);
printf(" 最高分:%d",maxco);
printf("最低分: %d",minco);
}
if(cour=='c') {
printf(" 數學:");
for (i=0;i<5;i++) printf(" %d",students [i].math);
printf("平均分: %f",ama);
printf(" 最高分:%d",maxma);
printf("最低分: %d",minma);
}
if(cour=='d') {
printf("語文:");
for (i=0;i<5;i++) printf(" %d",students [i].chinese);
printf("平均分: %f",ach);
printf(" 最高分:%d",maxch);
printf("最低分: %d",minch);
}
case 3:
for (j=1;j<6;j++)
for (i=0;i<5;i++)
if(students [i].rank==j) printf(" name:%s",students [i].name);
return;
}
}
void sort (struct student students[]){
int i,j,x,k,maxsum;x=1;
for(i=0;i<5;i++)
students[i].rank=0;
for(i=0;i<4;i++)
{
if(students[i].rank==0)
{
maxsum=sum[i];
for(j=i+1;j<5;j++)
{
if(students[j].rank==0)
if(sum[j]<maxsum)
{
maxsum=sum[j]; k=j;
}
}
students[k].rank=x; x++;
}
}
}
void max (struct student students []){
int i;
maxch=students [0].chinese;
maxma=students [0].math;
maxen=students [0].english;
maxco=students [0].computer;
for (i=1;i<5;i++) {if (students [i].chinese>maxch){
maxch=students [i].chinese;}
if (students [i].math>maxma){
maxma=students [i].math;}
if (students [i].english>maxen){
maxen=students [i].english;}
if (students [i].computer>maxco){
maxco=students [i+1].computer;}
}
}
void min (struct student students []){
int i;
minch=students [0].chinese;
minma=students [0].math;
minen=students [0].english;
minco=students [0].computer;
/*各科最低*/
for (i=1;i<5;i++) {
if(students [i].chinese<minch)
maxch=students [i].chinese;
if (students [i].math<minma)
maxma=students [i].math;
if (students [i].english<minen)
maxen=students [i].english;
if (students [i].computer<minco)
maxco=students [i].computer;}
}
void change_student (struct student *p)
{
int num,course,score,i;
printf("輸入學號:");
scanf_s("%d",&num);
printf("choice the course:1.english2.computer3.math4.chinese:");
scanf_s("%d",&course);
printf("輸入新成績:");
scanf_s("%d",&score);
for(i=0;i<5;i++,p++){
if(p->num==num)
switch(course)
{
case 1:p->english=score;break;
case 2:p->computer=score;break;
case 3:p->math=score;break;
case 4:p->chinese=score;break;
}
}
}
int main (void){
system("pause");
struct student students[maxsize];
int c;
while (1){
printf("********************************\n");
printf(" 學生成績管理系統\n");
printf("********************************\n");
printf(" (1)輸入學生成績\n");
printf(" (2)查詢學生成績\n");
printf(" (3)修改學生成績\n");
printf(" (4)輸出學生成績\n");
printf(" (5) 退出系統\n");
printf("請選擇(1、2、3、4、5)______\n");
scanf_s("%d",&c);getchar();
if (c>5||c<1)
printf("error\n");
else
switch(c)
{
case 1:input_student (students);break;
case 2:search_student (students);break;
case 3:change_student (students);break;
case 4:output_student (students) ;break;
case 5:break;
}
if(c==5)
break;
}
return 0;
}
scanf_s函數的%c,%s格式必須多加一個參數,指明參數數組的長度。
如:
scanf_s("%s", strudent[i].name ,30);