通信录包含 “姓名”(最多20个字符)、 “生日”(包括“年”、“月”、“日”)、 “电话号码”、 “家庭地址”(最多50个字符)。 定义一个嵌套的结构类型,输入n(n<10)个学生信息,再按照他们的年龄从小到大的顺序输出
时间: 2023-09-10 07:04:59 浏览: 88
### 回答1:
以下是一个可能的解法,使用了结构体和动态内存分配:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_ADDR_LEN 50
#define MAX_STUDENTS 10
typedef struct {
char name[MAX_NAME_LEN + 1];
int year;
int month;
int day;
char phone[12];
char addr[MAX_ADDR_LEN + 1];
} Student;
int compare_students(const void *a, const void *b) {
const Student *s1 = (const Student *)a;
const Student *s2 = (const Student *)b;
if (s1->year != s2->year) {
return s1->year - s2->year;
}
if (s1->month != s2->month) {
return s1->month - s2->month;
}
return s1->day - s2->day;
}
int main() {
int n;
Student *students;
printf("Enter the number of students (up to %d): ", MAX_STUDENTS);
scanf("%d", &n);
if (n > MAX_STUDENTS) {
printf("Too many students!\n");
return 1;
}
students = malloc(n * sizeof(Student));
if (students == NULL) {
printf("Out of memory!\n");
return 1;
}
printf("Enter student information:\n");
for (int i = 0; i < n; i++) {
printf("Student %d:\n", i + 1);
printf("Name: ");
scanf("%s", students[i].name);
printf("Birth year: ");
scanf("%d", &students[i].year);
printf("Birth month: ");
scanf("%d", &students[i].month);
printf("Birth day: ");
scanf("%d", &students[i].day);
printf("Phone: ");
scanf("%s", students[i].phone);
printf("Address: ");
scanf("%s", students[i].addr);
}
qsort(students, n, sizeof(Student), compare_students);
printf("Students sorted by age:\n");
for (int i = 0; i < n; i++) {
printf("%s (%d-%02d-%02d) %s %s\n",
students[i].name,
students[i].year,
students[i].month,
students[i].day,
students[i].phone,
students[i].addr);
}
free(students);
return 0;
}
```
这个程序首先读入学生的信息,然后使用 `qsort` 函数对学生结构体数组进行排序,排序使用了 `compare_students` 函数,该函数按照年龄从小到大的顺序比较两个学生的生日。最后程序输出排序后的学生信息。
需要注意的是,此程序没有进行输入数据的格式检查和错误处理,实际应用中需要加强输入验证。
### 回答2:
要实现上述功能,可以定义一个嵌套的结构类型,其中的成员包括姓名、生日、电话号码和家庭地址。下面是代码示例:
```cpp
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct Student {
string name;
struct Date {
int year;
int month;
int day;
} birthday;
string phoneNumber;
string address;
};
bool compareAge(const Student& s1, const Student& s2) {
if (s1.birthday.year < s2.birthday.year) {
return true;
}
else if (s1.birthday.year > s2.birthday.year) {
return false;
}
else {
if (s1.birthday.month < s2.birthday.month) {
return true;
}
else if (s1.birthday.month > s2.birthday.month) {
return false;
}
else {
return s1.birthday.day < s2.birthday.day;
}
}
}
int main() {
int n;
cout << "请输入学生人数n:";
cin >> n;
Student* students = new Student[n];
for (int i = 0; i < n; i++) {
cout << "请输入第" << i + 1 << "位学生的姓名:";
cin >> students[i].name;
cout << "请输入第" << i + 1 << "位学生的生日(年 月 日):";
cin >> students[i].birthday.year >> students[i].birthday.month >> students[i].birthday.day;
cout << "请输入第" << i + 1 << "位学生的电话号码:";
cin >> students[i].phoneNumber;
cout << "请输入第" << i + 1 << "位学生的家庭地址:";
cin >> students[i].address;
}
sort(students, students + n, compareAge);
cout << "按年龄从小到大的顺序输出学生信息:" << endl;
for (int i = 0; i < n; i++) {
cout << "姓名:" << students[i].name << endl;
cout << "生日:" << students[i].birthday.year << "年" << students[i].birthday.month << "月" << students[i].birthday.day << "日" << endl;
cout << "电话号码:" << students[i].phoneNumber << endl;
cout << "家庭地址:" << students[i].address << endl;
}
delete[] students;
return 0;
}
```
这段代码中,首先定义了一个嵌套的结构类型`Student`,其中包含姓名、生日、电话号码和家庭地址四个成员。在`main()`函数中,首先通过用户输入确定学生人数`n`,然后通过动态数组分配内存空间创建了`n`个学生的结构体,并在循环中依次为每个学生输入姓名、生日、电话号码和家庭地址信息。
接下来,使用`std::sort`函数对学生结构体数组按照年龄从小到大进行排序,排序的依据是自定义的`compareAge`函数,该函数比较了学生的生日,从而实现按年龄排序。
最后,按照排序后的顺序输出学生信息。
希望以上内容能够解答你的问题,如有任何疑问,请继续追问。
### 回答3:
嵌套的结构类型可定义如下:
```cpp
// 定义生日结构类型
struct Birthday {
int year;
int month;
int day;
};
// 定义学生信息结构类型
struct Student {
char name[21];
Birthday birthday;
char phoneNumber[12];
char address[51];
};
```
然后,我们可以输入n个学生信息,并按年龄从小到大的顺序输出:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
bool compareBirthday(const Student &s1, const Student &s2) {
// 比较生日的年份,若年份相同则比较月份,若月份相同则比较日
if (s1.birthday.year != s2.birthday.year) {
return s1.birthday.year < s2.birthday.year;
} else if (s1.birthday.month != s2.birthday.month) {
return s1.birthday.month < s2.birthday.month;
} else {
return s1.birthday.day < s2.birthday.day;
}
}
int main() {
int n;
std::cin >> n; // 输入学生个数
std::vector<Student> students(n);
// 输入学生信息
for (int i = 0; i < n; i++) {
std::cin >> students[i].name;
std::cin >> students[i].birthday.year >> students[i].birthday.month >> students[i].birthday.day;
std::cin >> students[i].phoneNumber;
std::cin.ignore(); // 忽略之前输入留下的换行符
std::cin.getline(students[i].address, 51);
}
// 按年龄从小到大排序学生信息
std::sort(students.begin(), students.end(), compareBirthday);
// 输出排序后的学生信息
for (int i = 0; i < n; i++) {
std::cout << students[i].name << " ";
std::cout << students[i].birthday.year << " " << students[i].birthday.month << " " << students[i].birthday.day << " ";
std::cout << students[i].phoneNumber << " ";
std::cout << students[i].address << std::endl;
}
return 0;
}
```
以上程序会按照输入的学生生日从小到大的顺序输出学生信息。
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)