#include
#include
#define maxsize 5
struct student {
int num;
char name[30];
int english,computer,math,chinese;
int rank;
};
static float aen,aco,ama,ach;static 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;char ch;
for (i=0;i<5;i++){
printf("********************************\n");
printf("請輸入:\n學號:");
scanf("%d",&students [i].num);
printf("姓名:");
scanf("%s",students [i].name);
printf("英語:");
scanf("%d",&students [i].english);
printf("計算機:");
scanf("%d",&students [i].computer);
printf("語文:");
scanf("%d",&students [i].math);
printf("數學:");
scanf("%d",&students [i].chinese);
printf("是否繼續?(Y/N)________________:\n");
scanf("%c",&ch);
if (ch==('N'||'n')) break;}
return;
}
void output_student (struct student students [ ])
{
int i,a,b,j;int sum[5];
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);}
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 ("---------------------------------------------------------------------------");
return ;
}
void search_student (struct student students [ ] )
{
int i,cour,way,j;
char a[20];
printf("choice the way:1.姓名2.課程3.總分名次");
scanf("%d",&way);
switch(way){
case 1:
printf("姓名:");
scanf("%s",&a);
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("%c",&cour);
if(cour=='a') {
printf(" 英語:");
for (i=0;i<5;i++) printf(" %d",students [i].english);
if(i==4) {
printf("平均分: %f",&aen);
printf(" 最高分:%d",&maxen);
printf("最低分: %d",&minen);}
}
if(cour=='b') {
printf(" 計算機:");
for (i=0;i<5;i++) printf(" %d",students [i].computer);
if(i==4) {
printf("平均分: %f",&aco);
printf(" 最高分:%d",&maxco);
printf("最低分: %d",&minco);}
}
if(cour=='c') {
printf(" 數學:");
for (i=0;i<5;i++) printf(" %d",students [i].math);
if(i==4) {
printf("平均分: %f",&ama);
printf(" 最高分:%d",&maxma);
printf("最低分: %d",&minma);}
}
if(cour=='d') {
printf("語文:");
for (i=0;i<5;i++) printf(" %d",students [i].chinese);
if(i==4) {
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;x=1;
for(i=0;i<5;i++)
for(j=1;i<6;i++)
if(sum[i]<sum[i+1])
{x=x+1;
students[i].rank=x;
}
return;
}
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=0;i if (students [i].chinese>students [i+1].chinese){
maxch=students [i+1].chinese;}
if (students [i].math>students [i+1].math){
maxma=students [i+1].math;}
if (students [i].english>students [i+1].english){
maxen=students [i+1].english;}
if (students [i].computer>students [i+1].computer){
maxco=students [i+1].computer;}
}
return;
}
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=0;i<5;i++) {
if(students [i].chinese<students [i+1].chinese)
maxch=students [i+1].chinese;
if (students [i].math<students [i+1].math)
maxma=students [i+1].math;
if (students [i].english<students [i+1].english)
maxen=students [i+1].english;
if (students [i].computer<students [i+1].computer)
maxco=students [i+1].computer;}
return;
}
void change_student (struct student *p)
{
int num,course,score,i;
printf("輸入學號:");
scanf("%d",&num);
printf("choice the course:1.english2.computer3.math4.chinese:");
scanf("%d",&course);
printf("輸入新成績:");
scanf("%d",&score);
for (i=0;i
if(p->num==num)
break;
if(i
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;
}
return;
}
int main (void){
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("%d",&c);
if (c>5||c<0)
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;
}
出現三個問題,第一,input 函數裡yes or no 的選擇不出現,第二,總分排須有問題,第三,我自定義函數裡計算的值無法輸出,不知道該怎麼調用定義的cal,max,min.
求大神門幫助阿,用的是dev c++寫的。
看看我的代碼:
#if 1
#include
#include
#include
#define maxsize 5
struct student {
int num;
char name[30];
int english, computer, math, chinese;
int rank;
};
static float aen, aco, ama, ach; static int maxch, maxma, maxen, maxco, minch, minma, minen, minco, sum[5];
void menu(struct student students[]);
//void input_student(struct student students[]);
int 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 mnue();
int input_student(struct student students[])
{
int i; char ch;
for (i = 0; i<5; i++){
printf("*******************************\n");
printf("請輸入:\n學號:");
scanf("%d", &students[i].num);
printf("姓名:");
scanf("%s", students[i].name);
printf("英語:");
scanf("%d", &students[i].english);
printf("計算機:");
scanf("%d", &students[i].computer);
printf("語文:");
scanf("%d", &students[i].math);
printf("數學:");
scanf("%d", &students[i].chinese);
printf("rank:");
scanf("%d", &students[i].rank);
printf("是否繼續?(Y/N)________________:\n");
fflush(stdin);
scanf("%c", &ch);
//if (ch == ('N' || 'n'))
if (ch == 'N' || ch == 'n')
break;
//if (ch == ('Y' || 'y'))
if (ch == 'Y' || ch == 'y')
break;
}
return i+1;
}
void output_student(struct student students[], int n)
{
int i, a, b, j; int sum[5];
printf("---------------------------------------------------------------------------\n");
printf("num\tname\tenglish\tcomputer\tmath\tchinese\trank\n");
//此處必須得改變接口,因為數組名退化為指針,所以以下語句不能用。
//int aq = sizeof(students) / sizeof(students[0]);
for (i = 0; i
printf(" %d\t", students[i].num);
printf(" %s\t", students[i].name);
printf(" %d\t", students[i].english);
printf(" %d\t\t", students[i].computer);
printf(" %d\t", students[i].math);
printf(" %d\t", students[i].chinese);
printf(" %d\t\n", students[i].rank);
}
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("---------------------------------------------------------------------------");
//此處不要加return, 加了return函數立即返回,並且你有沒有要用返回值。並且記得清空緩存。
fflush(stdin);
getchar();
//return;
}
void search_student(struct student students[])
{
int i, cour, way, j;
char a[20];
printf("choice the way:1.姓名2.課程3.總分名次");
scanf("%d", &way);
switch (way){
case 1:
printf("姓名:");
scanf("%s", &a);
for (i = 0; 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("%c", &cour);
if (cour == 'a') {
printf(" 英語:");
for (i = 0; i
if (i == 4) {
//此處的輸出有問題,怎麼能給整型變量加&符呢?這樣的話你輸出的是地址。
//printf("平均分: %f", &aen);
printf(" 最高分:%d", &maxen);
printf("最低分: %d", &minen);
}
}
if (cour == 'b') {
printf(" 計算機:");
for (i = 0; i
if (i == 4) {
printf("平均分: %f", &aco);
printf(" 最高分:%d", &maxco);
printf("最低分: %d", &minco);
}
}
if (cour == 'c') {
printf(" 數學:");
for (i = 0; i
if (i == 4) {
printf("平均分: %f", &ama);
printf(" 最高分:%d", &maxma);
printf("最低分: %d", &minma);
}
}
if (cour == 'd') {
printf("語文:");
for (i = 0; i
if (i == 4) {
printf("平均分: %f", &ach);
printf(" 最高分:%d", &maxch);
printf("最低分: %d", &minch);
}
}
case 3:for (j = 1; j
for (i = 0; i
if (students[i].rank == j) printf(" name:%s", students[i].name);
return;
}
}
void sort(struct student students[]){
int i, j, x; x = 1;
for (i = 0; i
for (j = 1; i
if (sum[i]
{
x = x + 1;
students[i].rank = x;
}
return;
}
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 = 0; i
if (students[i].chinese>students[i + 1].chinese)
{
maxch = students[i + 1].chinese;
}
if (students[i].math>students[i + 1].math)
{
maxma = students[i + 1].math;
}
if (students[i].english>students[i + 1].english)
{
maxen = students[i + 1].english;
}
if (students[i].computer>students[i + 1].computer)
{
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 = 0; i
if (students[i].chinese
maxch = students[i + 1].chinese;
if (students[i].math
maxma = students[i + 1].math;
if (students[i].english
maxen = students[i + 1].english;
if (students[i].computer
maxco = students[i + 1].computer;
}
return;
}
void change_student(struct student *p)
{
int num, course, score, i;
printf("輸入學號:");
scanf("%d", &num);
printf("choice the course:1.english2.computer3.math4.chinese:");
scanf("%d", &course);
printf("輸入新成績:");
scanf("%d", &score);
for (i = 0; i
if (p->num == num)
break;
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;
}
}
void mnue()
{
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");
}
int main(void){
struct student students[maxsize];
int c, a;
while (1){
system("cls");
mnue();
scanf("%d", &c);
if (c>5 || c<0)
printf("error\n");
else
switch (c)
{
case 1:a = input_student(students); break;
case 2:search_student(students); break;
case 3:change_student(students); break;
case 4:output_student(students, a); break;
case 5:break;
}
}
return 0;
}
#endif
代碼邏輯錯誤很多,但是我改了一部分。