#include <stdio.h>
#include <string.h>
void bubble_sort_grades(char [], int);
int main(void)
{
int menuswitch=1;
int amountofstudents;
int i;
int z;
char studentinfo[100];
char fname[50][100];
char lname[50][100];
char grade[50][100];
printf("Enter Amount of Students: ");
scanf("%d ", &amountofstudents);
for (i=0;i<amountofstudents;i++)
{
fgets(studentinfo, sizeof(studentinfo), stdin);
strcpy(fname[i], strtok(studentinfo, " "));
strcpy(lname[i], strtok(NULL, " "));
strcpy(grade[i], strtok(NULL, " "));
}
while (menuswitch==1)
{
int answer;
printf("Enter 1 for Alphabetical Sort (First Name) \n");
printf("Enter 2 for Alphabetical Sort (Last Name) \n");
printf("Enter 3 for Score Sort \n");
printf("Enter 0 to exit \n");
printf("Enter choice now: ");
scanf("%d", &answer);
if (answer==1)
{
bubble_sort_grades(grade,amountofstudents);
printf("%s\n", grade[0]);
printf("%s\n", grade[1]);
}
if (answer==2)
{
printf("In 2 \n");
}
if (answer==3)
{
printf("In 3 \n");
}
if (answer==0)
{
printf("Ending Program \n");
menuswitch=0;
}
}
}
void bubble_sort_grades(char grades2[], int amount)
{
int c, d , t;
for (c=0; c<(amount); c++)
{
for (d=0; d<amount-1; d++)
{
if (grades2[c]>grades2[d+1])
{
t=grades2[d+1];
grades2[d+1]=grades2[d];
grades2[d]=t;
}
}
}
}
很抱歉问了其他问题,但我需要关于冒泡排序的帮助。我创建了一个函数来对输入的学生成绩进行冒泡排序。然而,当我这样做时,我只得到排序的一年级而不是数组。
Input:
John Smith 86
Victor Jones 76
输出:68 76