何钦铭《C语言程序设计(第四版)》 P235第五题,执行swap()后结构变量只有name发生了交换,其他结构体成员没有交换
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct bd{
char year[5];
char month[3];
char day[3];
char* hisbirthday;
};
struct message{
char name[10];
struct bd birthday;
char phoneNumber[12];
};
void bubble(struct message* messages,int n);
void swap(struct message a , struct message b);
int main()
{
int n,i;
printf("输入人数:");
scanf("%d",&n);
struct message messages=(struct message)malloc(n*sizeof(struct message));
for(i=0;i<n;i++){
printf("输入第%d个人的姓名:",i+1);
scanf("%s",&messages[i].name);
printf("输入第%d个人的出生年份:",i+1);
scanf("%s",&messages[i].birthday.year);
printf("输入第%d个人的出生月份:",i+1);
scanf("%s",&messages[i].birthday.month);
printf("输入第%d个人的出生日:",i+1);
scanf("%s",&messages[i].birthday.day);
printf("输入第%d个人的手机号码:",i+1);
scanf("%s",&messages[i].phoneNumber);
char temp=strcat(messages[i].birthday.year,messages[i].birthday.month);
messages[i].birthday.hisbirthday=strcat(temp,messages[i].birthday.day);
// printf("here:%s\n",messages[i].birthday.hisbirthday);
}
bubble(messages,n);
for(i=0;i<n;i++){
printf("第%d个人的姓名:%s\n",i+1,messages[i].name);
printf("第%d个人的出生日期:%s\n",i+1,messages[i].birthday.hisbirthday);
}
free (messages);
return 0;
}
void bubble(struct message messages,int n)
{
int i,j,t;
for(i=1;i<n;i++){
for(j=0;j<n-i;j++){
if(strcmp(messages[j].birthday.hisbirthday,messages[j+1].birthday.hisbirthday)){
swap( &messages[j],&messages[j+1] );
}
}
}
}
void swap(struct message *a , struct message *b)
{
struct message temp ;
temp = *a ;
*a = *b ;
*b = temp ;
}
通过对messages[i].birthday.hisbirthday大小的比较,实现对messages数组的冒泡排序,使数组元素按年龄从大到小的顺序排列
数组元素按年龄从大到小的顺序排列
复制了你的代码,有报错,我进行了一些修改,修改之后代码参考如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct bd{
char year[5];
char month[3];
char day[3];
char* hisbirthday;
};
struct message{
char name[10];
struct bd birthday;
char phoneNumber[12];
};
void bubble(struct message* messages,int n);
void swap(struct message *a , struct message *b);
char *appendBirthday(char *year, char *month, char *day);
int main(int argc, const char * argv[]) {
int n,i;
printf("输入人数:");
scanf("%d",&n);
struct message *messages=(struct message*)malloc(n*sizeof(struct message));
for(i=0;i<n;i++){
printf("输入第%d个人的姓名:",i+1);
scanf("%s",messages[i].name);
printf("输入第%d个人的出生年份:",i+1);
scanf("%s",messages[i].birthday.year);
printf("输入第%d个人的出生月份:",i+1);
scanf("%s",messages[i].birthday.month);
printf("输入第%d个人的出生日:",i+1);
scanf("%s",messages[i].birthday.day);
printf("输入第%d个人的手机号码:",i+1);
scanf("%s",messages[i].phoneNumber);
messages[i].birthday.hisbirthday= appendBirthday(messages[i].birthday.year,messages[i].birthday.month,messages[i].birthday.day);
printf("here:%s\n",messages[i].birthday.hisbirthday);
}
bubble(messages,n);
for(i=0;i<n;i++){
printf("第%d个人的姓名:%s\n",i+1,messages[i].name);
printf("第%d个人的出生日期:%s\n",i+1,messages[i].birthday.hisbirthday);
}
free (messages);
return 0;
}
char *appendBirthday(char *year, char *month, char *day) {
char *bir =(char *)malloc(sizeof(char));
int i = 0;
int j = 0;
while (year[i] != '\0') {
bir[j] = year[i];
i++;
j++;
}
i = 0;
while (month[i] != '\0') {
bir[j] = month[i];
i++;
j++;
}
i = 0;
while (day[i] != '\0') {
bir[j] = day[i];
i++;
j++;
}
bir[j] = '\0';
return bir;
}
void bubble(struct message *messages,int n) {
int i,j;
for(i=1;i<n;i++){
for(j=0;j<n-i;j++){
if(strcmp(messages[j].birthday.hisbirthday,messages[j+1].birthday.hisbirthday)){
swap(&messages[j], &messages[j+1]);
}
}
}
}
void swap(struct message *a , struct message *b) {
struct message temp ;
temp = *a ;
*a = *b ;
*b = temp ;
}