以下是我个人的代码:
#include<stdio.h>
using namespace std;
struct contacts
{
char name[10];
int telephone;
int age;
}contacts[10];
void swap(char* x, char* y)
{
char* p;
p = x;
x = y;
y = p;
}
void paixu(struct contacts *p,int m)
{
int k ,h;
struct contacts temp;
for (k = 0; k < m - 1; k++)
{
for (h = k + 1; k < m; k++)
{
if (p[h].age < p[k].age)
{
temp = p[h];
p[h]= p[k];
p[k]= temp;
}
}
}
}
int main()
{
int n, i;
printf(“Input n:”);
scanf_s("%d", &n);
for (i = 0; i < n; i++)
{
printf("\nInput the name,age,telephone of the %d friend:", i+1);
scanf_s("%s",&contacts[i].name);
scanf_s("%d%d", &contacts[i].age, &contacts[i].telephone);
}
paixu(contacts,n);
printf(“after sorted:\n”);
for (i = 0; i < n; i++)
{
printf("%d%d", contacts[i].age, contacts[i].telephone);
printf("%s", contacts[i].name);
}
return 0;
}