gpt4 book ai didi

c - 查找最高总分并显示具有该总分的学生信息

转载 作者:行者123 更新时间:2023-11-30 19:36:21 25 4
gpt4 key购买 nike

我在计算学生人数 (n) 的最高总分并显示该学生的信息时遇到了问题。请不要介意无用的信息,很抱歉代码太长,但我将不胜感激。谢谢!!

#include<stdio.h> 
#include<conio.h>
#include<stdlib.h>
#include<string.h>
//Function Prototype
void add( struct student s[] );
void deleting( struct student s[] );
void update( struct student s[] );
void view( struct student s[] );
void calculateAvg( struct student s[] );
void max( struct student s[] );
/*void min( struct student s[] );
void find( struct student s[] );
void sort( struct student s[] );*/
//Size
int size = 0;
//Structure
struct student{
int id;
char name[25];
char sex; // M(Male) || F(Female)
int q1,q2,mid,final,total;

}s[20];

int main()
{

int choice;

system("CLS");

printf("\n==========================");
printf("\n MENU");
printf("\n==========================");

printf("\n1. Add student records");
printf("\n2. Delete student records");
printf("\n3. Update student records");
printf("\n4. View all student records");
printf("\n5. Calculate an average of a selected student’s scores");
printf("\n6. Show student who gets the max total score");
printf("\n7. Show student who gets the min total score");
printf("\n8. Find student by ID");
printf("\n9. Sort records by total scores");
printf("\n10. EXIT!");

printf("\n\n Please enter your choice: ");
scanf("%d",&choice);

switch(choice)
{
case 1: add(s);break;
case 2: deleting(s);break;
case 3: update(s);break;
case 4: view(s);break;
case 5: calculateAvg(s);break;
case 6: max(s);break;
/*case 7: min(s);break;
case 8: find(s);break;
case 9: sort(s);break;
case 10: exit(1);break;*/
}

getch();

}

void add( struct student s[] )
{
char choice;
int j;

system("CLS");

printf("\nPlease enter student no.%d information below:\n",size+1);
printf("ID : ");
scanf("%d",&s[size].id);
printf("Name : ");
scanf("%s",&s[size].name);
printf("Sex [M=Male||F=Female] : ");
scanf(" %c",&s[size].sex);
printf("Quiz1: ");
scanf("%d",&s[size].q1);
printf("Quiz2: ");
scanf("%d",&s[size].q2);
printf("Mid-term: ");
scanf("%d",&s[size].mid);
printf("Final: ");
scanf("%d",&s[size].final);

++size;

printf("ADD PROCESS COMPLETE!");
printf(" SIZE=%d",size);
printf("\nPRESS (R) TO RETURN, PRESS (A) TO ADD: ");
scanf(" %c",&choice);

if ( choice == 'R' || choice == 'r')
main();
else if ( choice == 'a' || choice == 'A')
add(s);

getch();

}


void deleting( struct student s[] )
{
int num,i,j,flag=0;
char choice;

system("CLS");

printf("Please enter student ID to delete: ");
scanf("%d",&num);

for ( i=0; i<size; ++i)
{
if ( s[i].id == num )
{
for (j=i; j<size; ++j)
{
s[j] = s[j+1];

}
--size;
flag=1;
break;
}
}



if (flag==1)
printf("DELETE PROCESS IS DONE!");
else if (flag==0)
printf("ERROR!!");

printf("\nPRESS (R) TO RETURN, PRESS (D) TO DELETE: ");
scanf(" %c",&choice);

if ( choice == 'R' || choice == 'r')
main();
else if ( choice == 'D' || choice == 'd')
deleting(s);



getch();
}


void update( struct student s[] )
{
int i,num,flag=0;
char choice;

system("CLS");

printf("Please enter student ID to update: ");
scanf("%d",&num);

for ( i=0; i<size; ++i)
{
if ( s[i].id == num )
{
flag=1;
printf("\nStudent no.%d information:\n",i+1);
printf("ID:%d |",s[i].id);
printf("Name:%s |",s[i].name);
printf("Sex:%c |",s[i].sex);
printf("Quiz1:%d |Quiz2:%d |Mid-term:%d |Final:%d |",s[i].q1,s[i].q2,s[i].mid,s[i].final);

printf("\n\nPlease enter new inforamtion to update: \n\n");

printf("ID : ");
scanf("%d",&s[i].id);
printf("Name : ");
scanf("%s",&s[i].name);
printf("Sex [M=Male||F=Female] : ");
scanf(" %c",&s[i].sex);
printf("Quiz1: ");
scanf("%d",&s[i].q1);
printf("Quiz2: ");
scanf("%d",&s[i].q2);
printf("Mid-term: ");
scanf("%d",&s[i].mid);
printf("Final: ");
scanf("%d",&s[i].final);

break;

}

}


if (flag==1)
printf("UPDATE PROCESS IS DONE!");
else if (flag==0)
printf("ERROR!!");

printf("\nPRESS (R) TO RETURN, PRESS (U) TO UPDATE: ");
scanf(" %c",&choice);

if ( choice == 'R' || choice == 'r')
main();
else if ( choice == 'U' || choice == 'u')
update(s);

getch();
}


void view( struct student s[] )
{
char choice;
//system("CLS"); may be added if wanted.
for (int j=0; j<size; ++j)
{
printf("\nStudent no.%d information:\n",j+1);
printf("ID:%d |",s[j].id);
printf("Name:%s |",s[j].name);
printf("Sex:%c |",s[j].sex);
printf("Quiz1:%d |Quiz2:%d |Mid-term:%d |Final:%d |",s[j].q1,s[j].q2,s[j].mid,s[j].final);

}

printf("\n\nSIZE=%d",size);

printf("\nPRESS (R) TO RETURN :");
scanf(" %c",&choice);

if ( choice == 'R' || choice == 'r')
main();

getch();

}


void calculateAvg( struct student s[] )
{
int i,total,flag=0,num;
char choice;

system("CLS");

printf("Please enter student ID to calculate average of marks: ");
scanf("%d",&num);

for ( i=0; i<size; ++i)
{
if ( s[i].id == num )
{
s[i].total = s[i].q1 + s[i].q2 + s[i].mid + s[i].final;

printf("\n\n Student no.%d information:\n",i+1);
printf("ID:%d |",s[i].id);
printf("Name:%s |",s[i].name);
printf("Sex:%c |",s[i].sex);
printf("Quiz1:%d |Quiz2:%d |Mid-term:%d |Final:%d |",s[i].q1,s[i].q2,s[i].mid,s[i].final);

printf("\n\n TOTAL = %d",s[i].total);
printf("\n AVERAGE = %d",s[i].total/4);

flag=1;
}
break;
}

if (flag==1)
printf("\n\nAVERAGE CALCULATED!");
else if (flag==0)
printf("ERROR!!");

printf("\nPRESS (R) TO RETURN :");
scanf(" %c",&choice);

if ( choice == 'R' || choice == 'r')
main();


getch();

}


void max( struct student s[] )
{
int i,max;
s[i].total = s[i].q1 + s[i].q2 +s[i].mid + s[i].final
max = s[0].total;

for (i=0; i<size; ++i)
{
if ( s[i].total > max )
{
max = s[i].total;
}
}

printf("\nHighest total score is %d belongs to student ID (%d)",max,)//\\problem
}

最佳答案

所以我不确定你想用这一行做什么:s[i].total = s[i].q1 + s[i].q2 + s[i].mid + s [i].final。但我稍微修改了你的方法。

void max( struct student s[] )
{
int i, max, id;
s[0].total = s[0].q1 + s[0].q2 + s[0].mid + s[0].final
max = s[0].total;
id = s[0].id;

for (i = 1; i < size; i++)
{
s[i].total = s[i].q1 + s[i].q2 + s[i].mid + s[i].final
if ( s[i].total > max )
{
max = s[i].total;
id = s[i].id;
}
}

printf("\nHighest total score is %d belongs to student ID (%d)", max, id)//\\problem
}

因此,在此解决方案中,我添加了一个 ID,该 ID 是根据您的学生 ID 分配的。当发现新的最大值时,id 就会更新。 Max 用第一个学生总数进行初始化。您可以尝试使用类似的东西吗?

关于c - 查找最高总分并显示具有该总分的学生信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41001865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com