ADD file via upload

main
p62jhspgx 1 year ago
parent 10a821f129
commit eaf8a0de3e

74
t4.c

@ -0,0 +1,74 @@
#include <stdio.h>
int num[4], Class[4];
float Maths[4], Physics[4], English[4], sum[4];
void Swap(int k);
void insert_array();
int main()
{
insert_array();
}
void insert_array()
{
for (int i = 0; i < 3; i++)
{
scanf("%d%d%f%f%f\n", &num[i], &Class[i], &Maths[i], &Physics[i], &English[i]);
sum[i] = Maths[i] + Physics[i] + English[i];
}
scanf("%d%d%f%f%f", &num[3], &Class[3], &Maths[3], &Physics[3], &English[3]);
for (int i = 0; i < 3; i++)
{
if (num[i] == num[3])
{
printf("The student's info has already existed!\n");
return;
}
}
int inserted_num = num[3];
sum[3] = Maths[3] + Physics[3] + English[3];
for (int i = 0; i < 4; i++)
{
for (int j = 3; j > i; j--)
{
if (Class[j] < Class[j - 1])
{
Swap(j);
}
else if (Class[j] == Class[j - 1])
{
if (sum[j] > sum[j - 1])
{
Swap(j);
}
}
}
}
for (int i = 0; i < 4; i++)
{
if (num[i] == inserted_num)
{
printf("%d %d %.1f %.1f %.1f", num[i], Class[i], Maths[i], Physics[i], English[i]);
printf(" inserted\n");
}
else
{
printf("%d %d %.1f %.1f %.1f\n", num[i], Class[i], Maths[i], Physics[i], English[i]);
}
}
}
void Swap(int k)
{
int num0 = num[k], Class0 = Class[k];
float Maths0 = Maths[k], Physics0 = Physics[k], English0 = English[k], sum0 = sum[k];
num[k] = num[k - 1];
Class[k] = Class[k - 1];
Maths[k] = Maths[k - 1];
Physics[k] = Physics[k - 1];
English[k] = English[k - 1];
sum[k] = sum[k - 1];
num[k - 1] = num0;
Class[k - 1] = Class0;
Maths[k - 1] = Maths0;
Physics[k - 1] = Physics0;
English[k - 1] = English0;
sum[k - 1] = sum0;
}
Loading…
Cancel
Save