使用结构型建立一个朋友的通讯录,包括他们的姓名、生日、电话号码。输入n(n<10)个朋友的信息,按照他们的年龄从大到小的顺序依次显示他们的信息。用C语言编写相应程序。
时间: 2023-09-03 19:04:39 浏览: 40
### 回答1:
```c
#include <stdio.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_PHONE_LEN 20
#define MAX_FRIENDS 10
typedef struct {
char name[MAX_NAME_LEN];
char birthday[11];
char phone[MAX_PHONE_LEN];
int age;
} Friend;
void sort_friends_by_age(Friend friends[], int count);
int main() {
int i, n;
Friend friends[MAX_FRIENDS];
printf("请输入朋友的数量:");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("请输入第%d个朋友的信息(姓名、生日、电话号码):", i + 1);
scanf("%s%s%s", friends[i].name, friends[i].birthday, friends[i].phone);
// 计算年龄
int year, month, day;
sscanf(friends[i].birthday, "%d-%d-%d", &year, &month, &day);
friends[i].age = 2020 - year;
}
// 按年龄排序
sort_friends_by_age(friends, n);
// 显示结果
printf("按年龄从大到小的顺序依次显示他们的信息:\n");
for (i = 0; i < n; i++) {
printf("%s %s %s %d岁\n", friends[i].name, friends[i].birthday, friends[i].phone, friends[i].age);
}
return 0;
}
void sort_friends_by_age(Friend friends[], int count) {
int i, j;
for (i = 0; i < count - 1; i++) {
for (j = 0; j < count - 1 - i; j++) {
if (friends[j].age < friends[j + 1].age) {
// 交换位置
Friend temp = friends[j];
friends[j] = friends[j + 1];
friends[j + 1] = temp;
}
}
}
}
```
### 回答2:
这里是一个使用结构型建立朋友的通讯录,并按照年龄排序的C语言程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义朋友结构体
typedef struct {
char name[20];
char birthday[10];
char phone[15];
} Friend;
int compare(const void *a, const void *b) {
Friend *friend1 = (Friend *)a;
Friend *friend2 = (Friend *)b;
// 按照生日的年份进行比较
int year1 = atoi(strtok(friend1->birthday, "/"));
int year2 = atoi(strtok(friend2->birthday, "/"));
return year2 - year1;
}
int main() {
int n; // 朋友数量
printf("请输入朋友的数量:");
scanf("%d", &n);
getchar(); // 清空缓冲区
Friend friends[n]; // 定义朋友数组
// 输入朋友信息
for (int i = 0; i < n; i++) {
printf("请输入朋友%d的姓名:", i+1);
fgets(friends[i].name, 20, stdin);
friends[i].name[strcspn(friends[i].name, "\n")] = '\0'; // 去掉换行符
printf("请输入朋友%d的生日(xx/xx/xxxx):", i+1);
fgets(friends[i].birthday, 10, stdin);
friends[i].birthday[strcspn(friends[i].birthday, "\n")] = '\0';
printf("请输入朋友%d的电话号码:", i+1);
fgets(friends[i].phone, 15, stdin);
friends[i].phone[strcspn(friends[i].phone, "\n")] = '\0';
printf("\n");
}
// 按照年龄从大到小排序
qsort(friends, n, sizeof(Friend), compare);
// 显示排序后的信息
printf("按照年龄从大到小排序的朋友信息如下:\n");
for (int i = 0; i < n; i++) {
printf("姓名:%s\n", friends[i].name);
printf("生日:%s\n", friends[i].birthday);
printf("电话号码:%s\n", friends[i].phone);
printf("\n");
}
return 0;
}
```
以上是一个简单的例子,可以根据输入的朋友数量和相应的信息排序朋友通讯录并将结果显示出来。使用qsort函数以及自定义的比较函数进行年龄的排序。
### 回答3:
#include<stdio.h>
#include<string.h>
// 定义朋友结构体
struct friend{
char name[20]; // 姓名
char birthday[15]; // 生日
char phone[15]; // 电话号码
};
int main(){
int n;
printf("请输入朋友的个数:");
scanf("%d", &n);
getchar(); // 消耗输入缓冲区的换行符
struct friend friends[n];
struct friend temp;
// 输入朋友信息
for(int i=0; i<n; i++){
printf("请输入第%d个朋友的姓名:", i+1);
fgets(friends[i].name, sizeof(friends[i].name), stdin);
friends[i].name[strlen(friends[i].name)-1] = '\0'; // 去掉姓名最后的换行符
printf("请输入第%d个朋友的生日:", i+1);
fgets(friends[i].birthday, sizeof(friends[i].birthday), stdin);
friends[i].birthday[strlen(friends[i].birthday)-1] = '\0'; // 去掉生日最后的换行符
printf("请输入第%d个朋友的电话号码:", i+1);
fgets(friends[i].phone, sizeof(friends[i].phone), stdin);
friends[i].phone[strlen(friends[i].phone)-1] = '\0'; // 去掉电话号码最后的换行符
}
// 按年龄从大到小排序
for(int i=0; i<n-1; i++){
for(int j=0; j<n-1-i; j++){
if(strcmp(friends[j].birthday, friends[j+1].birthday) < 0){
temp = friends[j];
friends[j] = friends[j+1];
friends[j+1] = temp;
}
}
}
// 输出排序后的朋友信息
printf("按照年龄从大到小的顺序依次显示朋友的信息:\n");
for(int i=0; i<n; i++){
printf("姓名:%s\n", friends[i].name);
printf("生日:%s\n", friends[i].birthday);
printf("电话号码:%s\n", friends[i].phone);
}
return 0;
}